Tensorflow MINIST資料模型的訓練,使用
阿新 • • 發佈:2018-12-12
在檢視本篇文章前,請提前閱讀上一章Tensorflow開發環境的搭建
在PyChrem新建專案,並建立python package : com.test
在根目錄下建立 train.py 檔案
# coding=utf-8 # 載入MINIST資料需要的庫 from tensorflow.examples.tutorials.mnist import input_data # 儲存模型需要的庫 from tensorflow.python.framework.graph_util import convert_variables_to_constants from tensorflow.python.framework import graph_util # 匯入其他庫 import tensorflow as tf import cv2 import numpy as np # 獲取MINIST資料 mnist = input_data.read_data_sets("MNIST_data", one_hot=True) # 建立會話 sess = tf.InteractiveSession() # 佔位符 x = tf.placeholder("float", shape=[None, 784], name="Mul") y_ = tf.placeholder("float", shape=[None, 10], name="y_") # 變數 W = tf.Variable(tf.zeros([784, 10]), name='x') b = tf.Variable(tf.zeros([10]), 'y_') #用於將自定義輸入圖片反轉 def reversePic(src): # 影象反轉 for i in range(src.shape[0]): for j in range(src.shape[1]): src[i,j] = 255 - src[i,j] return src # 權重 def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) # 偏差 def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) # 卷積 def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') # 最大池化 def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # 相關變數的建立 W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) x_image = tf.reshape(x, [-1, 28, 28, 1]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) # 啟用函式 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder("float", name='rob') h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # 用於訓練用的softmax函式 y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2, name='res') # 用於訓練作完後,作測試用的softmax函式 y_conv2 = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2, name="final_result") # 交叉熵的計算,返回包含了損失值的Tensor。 cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) # 優化器,負責最小化交叉熵 train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) # 計算準確率 accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) # 初始化所以變數 sess.run(tf.global_variables_initializer()) # 儲存輸入輸出,可以為之後用 tf.add_to_collection('res', y_conv) tf.add_to_collection('output', y_conv2) tf.add_to_collection('x', x) # 訓練開始 for i in range(20000): batch = mnist.train.next_batch(50)# 每一步迭代載入50個訓練樣本,然後執行一次train_step if i % 100 == 0: train_accuracy = accuracy.eval(feed_dict={ x: batch[0], y_: batch[1], keep_prob: 1.0}) print("step %d, training accuracy %g" % (i, train_accuracy)) # run()可以看做輸入相關值給到函式中的佔位符,然後計算的出結果,這裡將batch[0],給xbatch[1]給y_ train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) # 將當前圖設定為預設圖 graph_def = tf.get_default_graph().as_graph_def() # 將上面的變數轉化成常量,儲存模型為pb模型時需要,注意這裡的final_result和前面的y_con2是同名,只有這樣才會儲存它,否則會報錯, # 如果需要儲存其他tensor只需要讓tensor的名字和這裡保持一直即可 output_graph_def = tf.graph_util.convert_variables_to_constants(sess, graph_def, ['final_result']) # 儲存前面訓練後的模型為pb檔案 with tf.gfile.GFile("grf.pb", 'wb') as f: f.write(output_graph_def.SerializeToString()) # 用saver 儲存模型 saver = tf.train.Saver() saver.save(sess, "model_data/model") print("test accracy %g" % accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
然後滑鼠右鍵,選擇run執行即可,訓練時間較長,大概需要1-2小時左右,和實際的電腦配置有關
在執行時,會自動下載訓練模型,下載的檔案儲存在MNIST_data資料夾中
接下來我們使用一張圖片進行測試,編寫模型恢復指令碼 test.py
from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf import numpy as np import os from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file import cv2 mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #用於將自定義輸入圖片反轉 def reversePic(src): # 影象反轉 for i in range(src.shape[0]): for j in range(src.shape[1]): src[i,j] = 255 - src[i,j] return src # pb模型的恢復 def restore_model_pb(): sess = tf.Session() tf.saved_model.loader.load(sess, ['mytag'], os.getcwd() + '\model2') input_x = sess.graph.get_tensor_by_name('input_x:0') op = sess.graph.get_tensor_by_name('predict:0') print(sess.run(op, feed_dict={input_x: np.expand_dims(mnist.test.images[15], axis=0)})) sess.close() # ckpt模型的恢復 def restore_model_ckpt(): sess = tf.Session() # 載入模型結構 saver = tf.train.import_meta_graph('model_data/model.meta') # 只需要指定目錄就可以恢復所有變數資訊 saver.restore(sess, tf.train.latest_checkpoint('model_data')) # 直接獲取儲存的變數 print(sess.run('x:0')) input_x = sess.graph.get_tensor_by_name('Mul:0') # # 獲取需要進行計算的operator op = sess.graph.get_tensor_by_name('final_result:0') # 匯入圖片,同時灰度化 im = cv2.imread('pic/e2.jpg', cv2.IMREAD_GRAYSCALE) # 反轉影象,因為e2.jpg為白底黑字 im = reversePic(im) #cv2.namedWindow("camera", cv2.WINDOW_NORMAL); #cv2.imshow('camera', im) #cv2.waitKey(0) # 調整大小 im = cv2.resize(im, (28, 28), interpolation=cv2.INTER_CUBIC) x_img = np.reshape(im, [-1, 784]) # 用上面匯入的圖片對模型進行測試 output = sess.run(op, feed_dict={input_x: x_img}) # print 'the y_con : ', '\n',output print('the predict is : ', np.argmax(output)) #print(sess.run(op, feed_dict={input_x: np.expand_dims(mnist.test.images[15], axis=0)})) sess.close() #restore_model_pb() restore_model_ckpt()
直接執行即可
其中有使用到e2.jpg圖片,和訓練好的模型,我都已經打包上傳,專案原始碼下載: