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

本文介绍如何利用imagenet比赛上训练好的inception v3冻结的pb模型进行inference。python

1.下载inception v3 pb文件。session

2.导入pb到TensorFlow。code

3.获取输入与预测Tensor。图片

4.加载图片get

5.进行inferenceinput

【一】先看代码it

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)

结果以下:io

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

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

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

再在以下文件中查找:

是说:该图片是一直熊猫

相关文章
相关标签/搜索