基於TensorFlow作fine-tuning,與基於caffe作fine-tuning本質是同樣的,只是caffe僅須要修改配置文件,而TensorFlow修改也是配置文件,只不過是用python寫的配置文件。node
fine-tuning的本質就是:移花接木。python
本文講述如何基於lenet作fine-tuning。git
原始的網絡,FC1與FC2,如今想在在FC1與FC2中間加入一層,即造成FC1--->FC2-->FC3網絡
fine-tuning步驟
1.加載模型pb文件。
2.獲取輸入與label的tensor。
3.獲取須要修改層(fc1)的tensor。
4.定義修改的層及loss、optimization等。
5.灌入數據,開始fine-tuning。session
【原始網絡以下】dom
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from tensorflow.python.framework import graph_util mnist = input_data.read_data_sets(train_dir=r"E:\mnist_data",one_hot=True) #定義輸入數據mnist圖片大小28*28*1=784,None表示batch_size x = tf.placeholder(dtype=tf.float32,shape=[None,28*28],name="input") #定義標籤數據,mnist共10類 y_ = tf.placeholder(dtype=tf.float32,shape=[None,10],name="y_") #將數據調整爲二維數據,w*H*c---> 28*28*1,-1表示N張 image = tf.reshape(x,shape=[-1,28,28,1]) #第一層,卷積核={5*5*1*32},池化核={2*2*1,1*2*2*1} w1 = tf.Variable(initial_value=tf.random_normal(shape=[5,5,1,32],stddev=0.1,dtype=tf.float32,name="w1")) b1= tf.Variable(initial_value=tf.zeros(shape=[32])) conv1 = tf.nn.conv2d(input=image,filter=w1,strides=[1,1,1,1],padding="SAME",name="conv1") relu1 = tf.nn.relu(tf.nn.bias_add(conv1,b1),name="relu1") pool1 = tf.nn.max_pool(value=relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME") #shape={None,14,14,32} #第二層,卷積核={5*5*32*64},池化核={2*2*1,1*2*2*1} w2 = tf.Variable(initial_value=tf.random_normal(shape=[5,5,32,64],stddev=0.1,dtype=tf.float32,name="w2")) b2 = tf.Variable(initial_value=tf.zeros(shape=[64])) conv2 = tf.nn.conv2d(input=pool1,filter=w2,strides=[1,1,1,1],padding="SAME") relu2 = tf.nn.relu(tf.nn.bias_add(conv2,b2),name="relu2") pool2 = tf.nn.max_pool(value=relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME",name="pool2") #shape={None,7,7,64} #FC1 w3 = tf.Variable(initial_value=tf.random_normal(shape=[7*7*64,1024],stddev=0.1,dtype=tf.float32,name="w3")) b3 = tf.Variable(initial_value=tf.zeros(shape=[1024])) #關鍵,進行reshape input3 = tf.reshape(pool2,shape=[-1,7*7*64],name="input3") fc1 = tf.nn.relu(tf.nn.bias_add(value=tf.matmul(input3,w3),bias=b3),name="fc1") #shape={None,1024} #FC2 w4 = tf.Variable(initial_value=tf.random_normal(shape=[1024,10],stddev=0.1,dtype=tf.float32,name="w4")) b4 = tf.Variable(initial_value=tf.zeros(shape=[10])) fc2 = tf.nn.bias_add(value=tf.matmul(fc1,w4),bias=b4) #shape={None,10} #定義交叉熵損失 # 使用softmax將NN計算輸出值表示爲機率 y = tf.nn.softmax(fc2,name="out") # 定義交叉熵損失函數 cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=fc2,labels=y_) loss = tf.reduce_mean(cross_entropy) #定義solver train = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss=loss) #定義正確值,判斷兩者下標index是否相等 correct_predict = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) #定義如何計算準確率 accuracy = tf.reduce_mean(tf.cast(correct_predict,dtype=tf.float32),name="accuracy") #定義初始化op init = tf.global_variables_initializer() #訓練NN with tf.Session() as session: session.run(fetches=init) for i in range(0,1000): xs, ys = mnist.train.next_batch(100) session.run(fetches=train,feed_dict={x:xs,y_:ys}) if i%100 == 0: train_accuracy = session.run(fetches=accuracy,feed_dict={x:xs,y_:ys}) print(i,"accuracy=",train_accuracy) #訓練完成後,將網絡中的權值轉化爲常量,造成常量graph,注意:須要x與label constant_graph = graph_util.convert_variables_to_constants(sess=session, input_graph_def=session.graph_def, output_node_names=['out','y_','input']) #將帶權值的graph序列化,寫成pb文件存儲起來 with tf.gfile.FastGFile("lenet.pb", mode='wb') as f: f.write(constant_graph.SerializeToString())
【fine-tuning文件以下】ide
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import numpy as np mnist = input_data.read_data_sets(train_dir=r"E:\mnist_data",one_hot=True) pb_path = r"C:\Users\ThinkPad\PycharmProjects\tf\TensorFlow\fine-tuning\mnist\lenet.pb" #導入pb文件到graph中 with tf.gfile.FastGFile(pb_path,'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') with tf.Session() as session: #獲取輸入tensor x = tf.get_default_graph().get_tensor_by_name("input:0") #獲取標籤ensor y_ = tf.get_default_graph().get_tensor_by_name("y_:0") #進行fine-tuning #獲取fc1 fc1 = tf.get_default_graph().get_tensor_by_name("fc1:0") #定義新的FC2 # FC2 fc2_w = tf.Variable(initial_value=tf.random_normal(shape=[1024, 512], stddev=0.1, dtype=tf.float32, name="w4"),name="fc2_w") fc2_b = tf.Variable(initial_value=tf.zeros(shape=[512]),name="fc2_b") fc2 = tf.nn.bias_add(value=tf.matmul(fc1, fc2_w), bias=fc2_b) # FC3 fc3_w = tf.Variable(initial_value=tf.random_normal(shape=[512, 10], stddev=0.1, dtype=tf.float32, name="w4"),name="fc3_w") fc3_b = tf.Variable(initial_value=tf.zeros(shape=[10]),name="fc3_b") fc3 = tf.nn.bias_add(value=tf.matmul(fc2, fc3_w), bias=fc3_b) y = tf.nn.softmax(fc3, name="out") # 定義交叉熵損失函數 cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=fc3, labels=y_) loss = tf.reduce_mean(cross_entropy) # 定義solver train = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss=loss) # 定義正確值,判斷兩者下標index是否相等 correct_predict = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # 定義如何計算準確率 accuracy = tf.reduce_mean(tf.cast(correct_predict, dtype=tf.float32), name="accuracy") # 定義初始化op init = tf.global_variables_initializer() session.run(init) for i in range(0,1000): xs, ys = mnist.train.next_batch(100) session.run(fetches=train,feed_dict={x:xs,y_:ys}) if i%100 == 0: train_accuracy = session.run(fetches=accuracy,feed_dict={x:xs,y_:ys}) print(i,"accuracy=",train_accuracy)
結果以下圖:函數