深度学习框架Tensorflow学习与应用(八 保存和载入模型,使用Google的图像识别网络inception-v3进行图像识别)

一 模型的保存
hadoop@zhangjinyuTensor-1:~/tensorflow$ cat 8-1saver_save.py 
# coding: utf-8
# In[1]:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# In[2]:

#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次100张照片
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建一个简单的神经网络,输入层784个神经元,输出层10个神经元
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b)

#二次代价函数
# loss = tf.reduce_mean(tf.square(y-prediction))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

saver = tf.train.Saver()

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))
    saver.restore(sess,'net/my_net.ckpt')
    print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))
# In[ ]:

hadoop@zhangjinyuTensor-1:~/tensorflow$ python3.6 8-1saver_save.py
2018-12-11 07:04:15.724587: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Iter 0,Testing Accuracy 0.8253
Iter 1,Testing Accuracy 0.8856
Iter 2,Testing Accuracy 0.9001
Iter 3,Testing Accuracy 0.9058
Iter 4,Testing Accuracy 0.9078
Iter 5,Testing Accuracy 0.9093
Iter 6,Testing Accuracy 0.9121
Iter 7,Testing Accuracy 0.9135
Iter 8,Testing Accuracy 0.9148
Iter 9,Testing Accuracy 0.9164
Iter 10,Testing Accuracy 0.9181
生成好的模型位于net目录下
hadoop@zhangjinyuTensor-1:~/tensorflow$ ls net/
checkpoint  my_net.ckpt.data-00000-of-00001  my_net.ckpt.index  my_net.ckpt.meta
二模型的调用
hadoop@zhangjinyuTensor-1:~/tensorflow$ cat 8-2saver_restore.py 

# coding: utf-8

# In[1]:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data


# In[2]:

#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次100张照片
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建一个简单的神经网络,输入层784个神经元,输出层10个神经元
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b)

#二次代价函数
# loss = tf.reduce_mean(tf.square(y-prediction))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

saver = tf.train.Saver()

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))
    saver.restore(sess,'net/my_net.ckpt')
    print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))

# In[ ]:
hadoop@zhangjinyuTensor-1:~/tensorflow$ 
hadoop@zhangjinyuTensor-1:~/tensorflow$ python3.6 8-2saver_restore.py
2018-12-11 07:09:30.119847: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
0.098   #一看就不正确,随机生成了一个
0.9181
三下载google图像识别网络inception-v3并查看结构
8-3下载google图像识别网络inception-v3并查看结构.py
zhangjinyudeMacBook - Pro: 程序zhangjinyu$ cat 8-3下载google图像识别网络inception-v3并查看结构.py

# coding: utf-8

# In[1]:

import tensorflow as tf
import os
import tarfile
import requests

# In[2]:

# inception模型下载地址
inception_pretrain_model_url = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'

# 模型存放地址
inception_pretrain_model_dir = "inception_model"
if not os.path.exists(inception_pretrain_model_dir):
    os.makedirs(inception_pretrain_model_dir)

# 获取文件名,以及文件路径
filename = inception_pretrain_model_url.split('/')[-1]
filepath = os.path.join(inception_pretrain_model_dir, filename)

# 下载模型
if not os.path.exists(filepath):
    print("download: ", filename)
    r = requests.get(inception_pretrain_model_url, stream=True)
    with open(filepath, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
print("finish: ", filename)
# 解压文件
tarfile.open(filepath, 'r:gz').extractall(inception_pretrain_model_dir)

# 模型结构存放文件
log_dir = 'inception_log'
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

# classify_image_graph_def.pb为google训练好的模型
inception_graph_def_file = os.path.join(inception_pretrain_model_dir, 'classify_image_graph_def.pb')
with tf.Session() as sess:
    # 创建一个图来存放google训练好的模型
    with tf.gfile.FastGFile(inception_graph_def_file, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def, name='')
    # 保存图的结构
    writer = tf.summary.FileWriter(log_dir, sess.graph)
    writer.close()

# In[ ]:


# In[ ]:

hadoop@zhangjinyuTensor-1:~/tensorflow$
hadoop@zhangjinyuTensor-1:~/tensorflow$ python3.6 8-3下载google图像识别网络inception-v3并查看结构.py
download:  inception-2015-12-05.tgz
finish:  inception-2015-12-05.tgz
2018-12-11 07:41:24.604756: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
2018-12-11 07:41:25.461076: W tensorflow/core/framework/op_def_util.cc:346] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().
hadoop@zhangjinyuTensor-1:~/tensorflow$ ls
inception_log  inception_model
hadoop@zhangjinyuTensor-1:~/tensorflow$ tree inception_log/
inception_log/
└── events.out.tfevents.1544542886.zhangjinyuTensor-1  #保存的模型的结构,可以通过tensorboard打开查看

0 directories, 1 file
hadoop@zhangjinyuTensor-1:~/tensorflow$ tree inception_model/
inception_model/
├── classify_image_graph_def.pb        #已经选练过的inception-v3模型
├── cropped_panda.jpg                  #
├── imagenet_2012_challenge_label_map_proto.pbtxt
├── imagenet_synset_to_human_label_map.txt
├── inception-2015-12-05.tgz #获取压缩包,解压后会包含上面4个文件
└── LICENSE

0 directories, 6 files
hadoop@zhangjinyuTensor-1:~/tensorflow$ 
四使用inception-v3做好的模型来进行各种图像的识别(autosaved)

猜你喜欢

转载自blog.csdn.net/xsjzdrxsjzdr/article/details/84957190