http://blog.csdn.net/huachao1001/article/details/78502910

http://blog.csdn.net/u014432647/article/details/75276718

https://zhuanlan.zhihu.com/p/32887066

#coding:utf-8
#http://blog.csdn.net/zhuiqiuk/article/details/53376283
#http://blog.csdn.net/gan_player/article/details/77586489
from __future__ import absolute_import, unicode_literals
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import shutil
import os.path
from tensorflow.python.framework import graph_util 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') def inference(input_image, keep_prob):
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(input_image, [-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])
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")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10]) logits = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 return logits def train(export_dir):
mnist = input_data.read_data_sets("datasets", one_hot=True) g = tf.Graph()
with g.as_default():
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
keep_prob = tf.placeholder("float") logits = inference(x, keep_prob)
y_conv = tf.nn.softmax(logits) 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 = tf.Session()
sess.run(tf.initialize_all_variables()) for i in range(201):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(
{x: batch[0], y_: batch[1], keep_prob: 1.0}, sess)
print "step %d, training accuracy %g" % (i, train_accuracy)
train_step.run(
{x: batch[0], y_: batch[1], keep_prob: 0.5}, sess) print "test accuracy %g" % accuracy.eval(
{x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}, sess) saver = tf.train.Saver()
step = 200
checkpoint_file = os.path.join(export_dir, 'model.ckpt')
saver.save(sess, checkpoint_file, global_step=step)
checkpoint_file = os.path.join(export_dir, 'model.ckpt') def export_pb_model(model_name):
graph = tf.Graph()
with graph.as_default():
input_image = tf.placeholder("float", shape=[None,28*28], name='inputdata')
keep_prob = tf.placeholder("float", name = 'keep_probdata')
logits = inference(input_image, keep_prob)
y_conv = tf.nn.softmax(logits,name='outputdata')
restore_saver = tf.train.Saver() with tf.Session(graph=graph) as sess:
sess.run(tf.global_variables_initializer())
latest_ckpt = tf.train.latest_checkpoint('log')
restore_saver.restore(sess, latest_ckpt)
output_graph_def = tf.graph_util.convert_variables_to_constants(
sess, graph.as_graph_def(), ['outputdata']) # tf.train.write_graph(output_graph_def, 'log', model_name, as_text=False)
with tf.gfile.GFile(model_name, "wb") as f:
f.write(output_graph_def.SerializeToString()) def test_pb_model(model_name):
mnist = input_data.read_data_sets("datasets", one_hot=True) with tf.Graph().as_default():
output_graph_def = tf.GraphDef()
output_graph_path = model_name
# sess.graph.add_to_collection("input", mnist.test.images) with open(output_graph_path, "rb") as f:
output_graph_def.ParseFromString(f.read())
tf.import_graph_def(output_graph_def, name="") with tf.Session() as sess: tf.initialize_all_variables().run()
input_x = sess.graph.get_tensor_by_name("inputdata:0")
output = sess.graph.get_tensor_by_name("outputdata:0")
keep_prob = sess.graph.get_tensor_by_name("keep_probdata:0") y_conv_2 = sess.run(output,{input_x:mnist.test.images, keep_prob: 1.0})
print( "y_conv_2", y_conv_2) # Test trained model
#y__2 = tf.placeholder("float", [None, 10])
y__2 = mnist.test.labels
correct_prediction_2 = tf.equal(tf.argmax(y_conv_2, 1), tf.argmax(y__2, 1))
print ("correct_prediction_2", correct_prediction_2 )
accuracy_2 = tf.reduce_mean(tf.cast(correct_prediction_2, "float"))
print ("accuracy_2", accuracy_2) print ("check accuracy %g" % accuracy_2.eval()) if __name__ == '__main__':
export_dir = './log'
if os.path.exists(export_dir):
shutil.rmtree(export_dir)
#训练并保存模型ckpt
train(export_dir)
model_name = os.path.join(export_dir, 'mnist.pb')
#ckpt模型转换为pb模型
export_pb_model(model_name)
#测试pb模型
test_pb_model(model_name)

$ python mymain.py

#训练并保存模型ckpt

Extracting datasets/train-images-idx3-ubyte.gz
Extracting datasets/train-labels-idx1-ubyte.gz
Extracting datasets/t10k-images-idx3-ubyte.gz
Extracting datasets/t10k-labels-idx1-ubyte.gz
2018-03-19 18:11:27.046638: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-03-19 18:11:27.169530: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-03-19 18:11:27.170178: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 5.51GiB
2018-03-19 18:11:27.170196: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/util/tf_should_use.py:107: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
step 0, training accuracy 0.08
step 100, training accuracy 0.86
step 200, training accuracy 0.96
2018-03-19 18:11:29.100338: W tensorflow/core/common_runtime/bfc_allocator.cc:217] Allocator (GPU_0_bfc) ran out of memory trying to allocate 3.32GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
test accuracy 0.9137

#ckpt模型转换为pb模型

2018-03-19 18:11:30.655025: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
Converted 8 variables to const ops.

#测试pb模型

Extracting datasets/train-images-idx3-ubyte.gz
Extracting datasets/train-labels-idx1-ubyte.gz
Extracting datasets/t10k-images-idx3-ubyte.gz
Extracting datasets/t10k-labels-idx1-ubyte.gz
2018-03-19 18:11:32.419375: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
(u'y_conv_2', array([[ 3.83061661e-06, 2.25869144e-06, 6.98342774e-05, ...,
9.99720514e-01, 5.38732929e-05, 6.28733032e-05],
[ 1.85461645e-03, 3.86392418e-03, 9.55442667e-01, ...,
1.31935649e-05, 2.71034874e-02, 4.14738406e-06],
[ 2.61329369e-05, 9.94501710e-01, 1.34233199e-03, ...,
8.23311449e-04, 1.73626456e-03, 3.27934940e-05],
...,
[ 1.58834242e-04, 1.02327869e-03, 9.29224771e-04, ...,
9.04104114e-03, 1.03222862e-01, 1.16873145e-01],
[ 9.86627676e-03, 1.02333550e-03, 2.13368423e-03, ...,
2.72160349e-03, 3.91508579e-01, 4.37955791e-03],
[ 1.95508893e-03, 2.17417346e-06, 1.18497398e-03, ...,
5.04385412e-07, 3.14567442e-05, 4.29990359e-06]], dtype=float32))
(u'correct_prediction_2', <tf.Tensor 'Equal:0' shape=(10000,) dtype=bool>)
(u'accuracy_2', <tf.Tensor 'Mean:0' shape=() dtype=float32>)
check accuracy 0.9137

TensorFlow基础笔记(14) 网络模型的保存与恢复_mnist数据实例的更多相关文章

  1. TensorFlow基础笔记(0) 参考资源学习文档

    1 官方文档 https://www.tensorflow.org/api_docs/ 2 极客学院中文文档 http://www.tensorfly.cn/tfdoc/api_docs/python ...

  2. TensorFlow基础笔记(3) cifar10 分类学习

    TensorFlow基础笔记(3) cifar10 分类学习 CIFAR-10 is a common benchmark in machine learning for image recognit ...

  3. TensorFlow 训练好模型参数的保存和恢复代码

    TensorFlow 训练好模型参数的保存和恢复代码,之前就在想模型不应该每次要个结果都要重新训练一遍吧,应该训练一次就可以一直使用吧. TensorFlow 提供了 Saver 类,可以进行保存和恢 ...

  4. TensorFlow进阶(六)---模型保存与恢复、自定义命令行参数

    模型保存与恢复.自定义命令行参数. 在我们训练或者测试过程中,总会遇到需要保存训练完成的模型,然后从中恢复继续我们的测试或者其它使用.模型的保存和恢复也是通过tf.train.Saver类去实现,它主 ...

  5. TensorFlow基础笔记(1) 数据读取与保存

    https://zhuanlan.zhihu.com/p/27238630 WholeFileReader # 我们用一个具体的例子感受tensorflow中的数据读取.如图, # 假设我们在当前文件 ...

  6. TensorFlow基础笔记(11) conv2D函数

    #链接:http://www.jianshu.com/p/a70c1d931395 import tensorflow as tf import tensorflow.contrib.slim as ...

  7. TensorFlow基础笔记(8) TensorFlow简单人脸识别

    数据材料 这是一个小型的人脸数据库,一共有40个人,每个人有10张照片作为样本数据.这些图片都是黑白照片,意味着这些图片都只有灰度0-255,没有rgb三通道.于是我们需要对这张大图片切分成一个个的小 ...

  8. Tensorflow基础笔记

    1.Keras是一个由Python编写的开源人工神经网络库. 2.深度学习主要应用在三个大的方向,计算机视觉,自然语言处理,强化学习 3.计算机视觉主要有:图片识别,目标检测,语义分割,视频理解(行为 ...

  9. 黑马程序员_java基础笔记(14)...交通灯管理系统_编码思路及代码

    —————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流! —————————— 1,面试题——交通灯管理系统 模拟实现十字路口的交通灯管理系统逻辑,具体需求如下: ...

随机推荐

  1. NET使用NPOI组件将数据导出Excel-通用方法 【推荐】

    一.Excel导入及导出问题产生:   从接触.net到现在一直在维护一个DataTable导出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题:   导出问题:   如果是as ...

  2. 【转】SQL SERVER 获取存储过程返回值

    1.OUPUT参数返回值 CREATE PROCEDURE [dbo].[nb_order_insert]( @o_buyerid int , @o_id bigint OUTPUT ) AS BEG ...

  3. struts中action名称反复导致的神秘事件

    近期由于项目需求变更.须要本人对当中的某个业务功能进行改动.本人依照前台页面找action,依据action找代码的逻辑进行了改动(公司项目是ssh框架,struts配置全部是通过注解的方式进行.配置 ...

  4. 使用B::Deparse模块对perl代码反汇编

    Perl用很多默认操作和习惯用法,如果对某些代码不确定,perl编译器的真实理解方式,可以用Deparse模块反汇编看一下. 比如下面代码: while(<STDIN>){ print & ...

  5. js实现精确统计网站访问量的代码分享

    JS 精确统计网站访问量. 代码如下: /** * vlstat 浏览器统计脚本 */ var statIdName = "vlstatId"; var xmlHttp; /** ...

  6. mysql删除账户

    mysql> select user,host,password from user; +------+-----------+--------------------------------- ...

  7. nonatomic对引用计数的影响(非ARC)

    @interfaceAppDelegate() { NSObject * obj_; } @property(retain) NSObject * obj;// 默认是atomic //@proper ...

  8. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  9. axel命令 文件下载

    axel是Linux下一个不错的HTTP/ftp高速下载工具.支持多线程下载.断点续传,且可以从多个地址或者从一个地址的多个连接来下载同一个文件.适合网速不给力时多线程下载提高下载速度.比如在国内VP ...

  10. SQL语句的一些基本使用以及一些技巧

    #SELECT 列名1, 列名2, from 表明 #SELECT id,title,content,type from news 效率相对较高#SELECT * from news *代表所有字段, ...