tensorflow 20:搭網(wǎng)絡(luò),導(dǎo)出模型,運(yùn)行模型的實(shí)例
概述
以前自己都利用別人搭好的工程,修改過來用,很少把模型搭建、導(dǎo)出模型、加載模型運(yùn)行走一遍,搞了一遍才知道這個(gè)事情也不是那么簡(jiǎn)單的。
搭建模型和導(dǎo)出模型
參考《TensorFlow固化模型》,導(dǎo)出固化的模型有兩種方式.
方式1:導(dǎo)出pb圖結(jié)構(gòu)和ckpt文件,然后用 freeze_graph 工具凍結(jié)生成一個(gè)pb(包含結(jié)構(gòu)和參數(shù))
在我的代碼里測(cè)試了生成pb圖結(jié)構(gòu)和ckpt文件,但是沒接著往下走,感覺有點(diǎn)麻煩。我用的是第二種方法。
注意我這里只在最后保存了一次ckpt,實(shí)際應(yīng)該在訓(xùn)練中每隔一段時(shí)間就保存一次的。
saver = tf.train.Saver(max_to_keep=5) #tf.train.write_graph(session.graph_def, FLAGS.model_dir, "nn_model.pbtxt", as_text=True) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) max_step = 2000 for i in range(max_step): batch = mnist.train.next_batch(50) 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)) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) print('test accuracy %g' % accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) # 保存pb和ckpt print('save pb file and ckpt file') tf.train.write_graph(sess.graph_def, graph_location, "graph.pb",as_text=False) checkpoint_path = os.path.join(graph_location, "model.ckpt") saver.save(sess, checkpoint_path, global_step=max_step)
方式2:convert_variables_to_constants
我實(shí)際使用的就是這種方法。
看名字也知道,就是把變量轉(zhuǎn)化為常量保存,這樣就可以愉快的加載使用了。
注意這里需要指明保存的輸出節(jié)點(diǎn),我的輸出節(jié)點(diǎn)為'out/fc2'(我猜測(cè)會(huì)根據(jù)輸出節(jié)點(diǎn)的依賴推斷哪些部分是訓(xùn)練用到的,推理時(shí)用不到)。關(guān)于輸出節(jié)點(diǎn)的名字是有規(guī)律的,其中out是一個(gè)name_scope名字,fc2是op節(jié)點(diǎn)的名字。
with tf.Session() as sess: sess.run(tf.global_variables_initializer()) max_step = 2000 for i in range(max_step): batch = mnist.train.next_batch(50) 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)) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) print('test accuracy %g' % accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) print('save frozen file') pb_path = os.path.join(graph_location, 'frozen_graph.pb') print('pb_path:{}'.format(pb_path)) # 固化模型 output_graph_def = convert_variables_to_constants(sess, sess.graph_def, output_node_names=['out/fc2']) with tf.gfile.FastGFile(pb_path, mode='wb') as f: f.write(output_graph_def.SerializeToString())
上述代碼會(huì)在訓(xùn)練后把訓(xùn)練好的計(jì)算圖和參數(shù)保存到frozen_graph.pb文件。后續(xù)就可以用這個(gè)模型來測(cè)試圖片了。
方式2的完整訓(xùn)練和保存模型代碼
主要看main函數(shù)就行。另外注意deepnn函數(shù)最后節(jié)點(diǎn)的名字。
"""A deep MNIST classifier using convolutional layers. See extensive documentation at https://www.tensorflow.org/get_started/mnist/pros """ # Disable linter warnings to maintain consistency with tutorial. # pylint: disable=invalid-name # pylint: disable=g-bad-import-order from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import sys import tempfile import os from tensorflow.examples.tutorials.mnist import input_data from tensorflow.python.framework.graph_util import convert_variables_to_constants import tensorflow as tf FLAGS = None def deepnn(x): """deepnn builds the graph for a deep net for classifying digits. Args: x: an input tensor with the dimensions (N_examples, 784), where 784 is the number of pixels in a standard MNIST image. Returns: A tuple (y, keep_prob). y is a tensor of shape (N_examples, 10), with values equal to the logits of classifying the digit into one of 10 classes (the digits 0-9). keep_prob is a scalar placeholder for the probability of dropout. """ # Reshape to use within a convolutional neural net. # Last dimension is for "features" - there is only one here, since images are # grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc. with tf.name_scope('reshape'): x_image = tf.reshape(x, [-1, 28, 28, 1]) # First convolutional layer - maps one grayscale image to 32 feature maps. with tf.name_scope('conv1'): W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # Pooling layer - downsamples by 2X. with tf.name_scope('pool1'): h_pool1 = max_pool_2x2(h_conv1) # Second convolutional layer -- maps 32 feature maps to 64. with tf.name_scope('conv2'): 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) # Second pooling layer. with tf.name_scope('pool2'): h_pool2 = max_pool_2x2(h_conv2) # Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image # is down to 7x7x64 feature maps -- maps this to 1024 features. with tf.name_scope('fc1'): W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) 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) # Dropout - controls the complexity of the model, prevents co-adaptation of # features. with tf.name_scope('dropout'): keep_prob = tf.placeholder(tf.float32, name='ratio') h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # Map the 1024 features to 10 classes, one for each digit with tf.name_scope('out'): W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv = tf.add(tf.matmul(h_fc1_drop, W_fc2), b_fc2, name='fc2') return y_conv, keep_prob def conv2d(x, W): """conv2d returns a 2d convolution layer with full stride.""" return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def max_pool_2x2(x): """max_pool_2x2 downsamples a feature map by 2X.""" return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') def weight_variable(shape): """weight_variable generates a weight variable of a given shape.""" initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) def bias_variable(shape): """bias_variable generates a bias variable of a given shape.""" initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) def main(_): # Import data mnist = input_data.read_data_sets(FLAGS.data_dir) # Create the model with tf.name_scope('input'): x = tf.placeholder(tf.float32, [None, 784], name='x') # Define loss and optimizer y_ = tf.placeholder(tf.int64, [None]) # Build the graph for the deep net y_conv, keep_prob = deepnn(x) with tf.name_scope('loss'): cross_entropy = tf.losses.sparse_softmax_cross_entropy( labels=y_, logits=y_conv) cross_entropy = tf.reduce_mean(cross_entropy) with tf.name_scope('adam_optimizer'): train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) with tf.name_scope('accuracy'): correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_) correct_prediction = tf.cast(correct_prediction, tf.float32) accuracy = tf.reduce_mean(correct_prediction) graph_location = './model' print('Saving graph to: %s' % graph_location) train_writer = tf.summary.FileWriter(graph_location) train_writer.add_graph(tf.get_default_graph()) saver = tf.train.Saver(max_to_keep=5) #tf.train.write_graph(session.graph_def, FLAGS.model_dir, "nn_model.pbtxt", as_text=True) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) max_step = 2000 for i in range(max_step): batch = mnist.train.next_batch(50) 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)) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) print('test accuracy %g' % accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) # save pb file and ckpt file #print('save pb file and ckpt file') #tf.train.write_graph(sess.graph_def, graph_location, "graph.pb", as_text=False) #checkpoint_path = os.path.join(graph_location, "model.ckpt") #saver.save(sess, checkpoint_path, global_step=max_step) print('save frozen file') pb_path = os.path.join(graph_location, 'frozen_graph.pb') print('pb_path:{}'.format(pb_path)) output_graph_def = convert_variables_to_constants(sess, sess.graph_def, output_node_names=['out/fc2']) with tf.gfile.FastGFile(pb_path, mode='wb') as f: f.write(output_graph_def.SerializeToString()) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--data_dir', type=str, default='./data', help='Directory for storing input data') FLAGS, unparsed = parser.parse_known_args() tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
加載模型進(jìn)行推理
上一節(jié)已經(jīng)訓(xùn)練并導(dǎo)出了frozen_graph.pb。
這一節(jié)把它運(yùn)行起來。
加載模型
下方的代碼用來加載模型。推理時(shí)計(jì)算圖里共兩個(gè)placeholder需要填充數(shù)據(jù),一個(gè)是圖片(這不廢話嗎),一個(gè)是drouout_ratio,drouout_ratio用一個(gè)常量作為輸入,后續(xù)就只需要輸入圖片了。
graph_location = './model' pb_path = os.path.join(graph_location, 'frozen_graph.pb') print('pb_path:{}'.format(pb_path)) newInput_X = tf.placeholder(tf.float32, [None, 784], name="X") drouout_ratio = tf.constant(1., name="drouout") with open(pb_path, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) output = tf.import_graph_def(graph_def, input_map={'input/x:0': newInput_X, 'dropout/ratio:0':drouout_ratio}, return_elements=['out/fc2:0'])
input_map參數(shù)并不是必須的。如果不用input_map,可以在run之前用tf.get_default_graph().get_tensor_by_name獲取tensor的句柄。但是我覺得這種方法不是很友好,我這里沒用這種方法。
注意input_map里的tensor名字是和搭計(jì)算圖時(shí)的name_scope和op名字有關(guān)的,而且后面要補(bǔ)一個(gè)‘:0'(這點(diǎn)我還沒細(xì)究)。
同時(shí)要注意,newInput_X的形狀是[None, 784],第一維是batch大小,推理時(shí)和訓(xùn)練要一致。
(我用的是mnist圖片,訓(xùn)練時(shí)每個(gè)bacth的形狀是[batchsize, 784],每個(gè)圖片是28x28)
運(yùn)行模型
我是一張張圖片單獨(dú)測(cè)試的,運(yùn)行模型之前先把圖片變?yōu)閇1, 784],以符合newInput_X的維數(shù)。
with tf.Session( ) as sess: file_list = os.listdir(test_image_dir) # 遍歷文件 for file in file_list: full_path = os.path.join(test_image_dir, file) print('full_path:{}'.format(full_path)) # 只要黑白的,大小控制在(28,28) img = cv2.imread(full_path, cv2.IMREAD_GRAYSCALE ) res_img = cv2.resize(img,(28,28),interpolation=cv2.INTER_CUBIC) # 變成長(zhǎng)784的一維數(shù)據(jù) new_img = res_img.reshape((784)) # 增加一個(gè)維度,變?yōu)?[1, 784] image_np_expanded = np.expand_dims(new_img, axis=0) image_np_expanded.astype('float32') # 類型也要滿足要求 print('image_np_expanded shape:{}'.format(image_np_expanded.shape)) # 注意注意,我要調(diào)用模型了 result = sess.run(output, feed_dict={newInput_X: image_np_expanded}) # 出來的結(jié)果去掉沒用的維度 result = np.squeeze(result) print('result:{}'.format(result)) #print('result:{}'.format(sess.run(output, feed_dict={newInput_X: image_np_expanded}))) # 輸出結(jié)果是長(zhǎng)度為10(對(duì)應(yīng)0-9)的一維數(shù)據(jù),最大值的下標(biāo)就是預(yù)測(cè)的數(shù)字 print('result:{}'.format( (np.where(result==np.max(result)))[0][0] ))
注意模型的輸出是一個(gè)長(zhǎng)度為10的一維數(shù)組,也就是計(jì)算圖里全連接的輸出。這里沒有softmax,只要取最大值的下標(biāo)即可得到結(jié)果。
輸出結(jié)果:
full_path:./test_images/97_7.jpg image_np_expanded shape:(1, 784) result:[-1340.37145996 -283.72436523 1305.03320312 437.6053772 -413.69961548 -1218.08166504 -1004.83807373 1953.33984375 42.00457001 -504.43829346] result:7 full_path:./test_images/98_6.jpg image_np_expanded shape:(1, 784) result:[ 567.4041748 -550.20904541 623.83496094 -1152.56884766 -217.92695618 1033.45239258 2496.44750977 -1139.23620605 -5.64091825 -615.28491211] result:6 full_path:./test_images/99_9.jpg image_np_expanded shape:(1, 784) result:[ -532.26409912 -1429.47277832 -368.58096313 505.82876587 358.42163086 -317.48199463 -1108.6829834 1198.08752441 289.12286377 3083.52539062] result:9
加載模型進(jìn)行推理的完整代碼
import sys import os import cv2 import numpy as np import tensorflow as tf test_image_dir = './test_images/' graph_location = './model' pb_path = os.path.join(graph_location, 'frozen_graph.pb') print('pb_path:{}'.format(pb_path)) newInput_X = tf.placeholder(tf.float32, [None, 784], name="X") drouout_ratio = tf.constant(1., name="drouout") with open(pb_path, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) #output = tf.import_graph_def(graph_def) output = tf.import_graph_def(graph_def, input_map={'input/x:0': newInput_X, 'dropout/ratio:0':drouout_ratio}, return_elements=['out/fc2:0']) with tf.Session( ) as sess: file_list = os.listdir(test_image_dir) # 遍歷文件 for file in file_list: full_path = os.path.join(test_image_dir, file) print('full_path:{}'.format(full_path)) # 只要黑白的,大小控制在(28,28) img = cv2.imread(full_path, cv2.IMREAD_GRAYSCALE ) res_img = cv2.resize(img,(28,28),interpolation=cv2.INTER_CUBIC) # 變成長(zhǎng)784的一維數(shù)據(jù) new_img = res_img.reshape((784)) # 增加一個(gè)維度,變?yōu)?[1, 784] image_np_expanded = np.expand_dims(new_img, axis=0) image_np_expanded.astype('float32') # 類型也要滿足要求 print('image_np_expanded shape:{}'.format(image_np_expanded.shape)) # 注意注意,我要調(diào)用模型了 result = sess.run(output, feed_dict={newInput_X: image_np_expanded}) # 出來的結(jié)果去掉沒用的維度 result = np.squeeze(result) print('result:{}'.format(result)) #print('result:{}'.format(sess.run(output, feed_dict={newInput_X: image_np_expanded}))) # 輸出結(jié)果是長(zhǎng)度為10(對(duì)應(yīng)0-9)的一維數(shù)據(jù),最大值的下標(biāo)就是預(yù)測(cè)的數(shù)字 print('result:{}'.format( (np.where(result==np.max(result)))[0][0] ))
以上這篇tensorflow 20:搭網(wǎng)絡(luò),導(dǎo)出模型,運(yùn)行模型的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android Q之氣泡彈窗的實(shí)現(xiàn)示例
這篇文章主要介紹了Android Q之氣泡彈窗的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06python腳本實(shí)現(xiàn)mp4中的音頻提取并保存在原目錄
這篇文章主要介紹了python腳本實(shí)現(xiàn)mp4中的音頻提取并保存在原目錄,本文給大家通過實(shí)例代碼介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02PyTorch實(shí)現(xiàn)重寫/改寫Dataset并載入Dataloader
這篇文章主要介紹了PyTorch實(shí)現(xiàn)重寫/改寫Dataset并載入Dataloader,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Python使用Asyncio實(shí)現(xiàn)檢查網(wǎng)站狀態(tài)
這篇文章主要為大家詳細(xì)介紹了Python如何使用Asyncio實(shí)現(xiàn)檢查網(wǎng)站狀態(tài),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03淺談python中的@以及@在tensorflow中的作用說明
這篇文章主要介紹了淺談python中的@以及@在tensorflow中的作用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03python基于?Web?實(shí)現(xiàn)?m3u8?視頻播放的實(shí)例
這篇文章主要介紹了python基于?Web?實(shí)現(xiàn)?m3u8?視頻播放的實(shí)例的相關(guān)資料,需要的朋友可以參考下2023-06-06Python-numpy實(shí)現(xiàn)灰度圖像的分塊和合并方式
今天小編就為大家分享一篇Python-numpy實(shí)現(xiàn)灰度圖像的分塊和合并方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01