您的位置:首页 > 其它

【TensorFlow系列】【五】利用inception v3 pb模型文件做预测

2018-03-17 00:00 721 查看
本文介绍如何利用imagenet比赛上训练好的inception v3冻结的pb模型进行inference。

1.下载inception v3 pb文件。

2.导入pb到TensorFlow。

3.获取输入与预测Tensor。

4.加载图片

5.进行inference

【一】先看代码

import tensorflow as tf
import numpy as np
'''
下载训练好的pb文件
'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
'''
pb_path = r"D:\TensorFlow-model\inception-2015-12-05\classify_image_graph_def.pb"
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:
#获取pb文件中模型的所有op,主要是为了获得input与output
print(tf.get_default_graph().get_operations())
image = "D:\TensorFlow-model\inception-2015-12-05\cropped_panda.jpg"
#解码图片作为inference的输入
image_data = tf.gfile.FastGFile(image, 'rb').read()
softmax_tensor = session.graph.get_tensor_by_name('softmax:0')
predictions = session.run(softmax_tensor,
{'DecodeJpeg/contents:0': image_data})
index = np.argmax(predictions,1)
print(index)

结果如下:



label为169,从文件中找到169是哪个类别

以下图片中的文件,来自于上述代码链接中下载的压缩包解压后的文件。



该文件说明了label属于哪个分类



再在如下文件中查找:





是说:该图片是一直熊猫

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐