TensorFlow slim inception resnet v2 推理
Posted
技术标签:
【中文标题】TensorFlow slim inception resnet v2 推理【英文标题】:Tensorflow slim inception resnet v2 inference 【发布时间】:2018-11-08 01:05:46 【问题描述】:https://github.com/tensorflow/models/blob/master/research/slim/nets/inception_resnet_v2.py
您如何正确地对此模型进行推理?我有兴趣设置它,以便用户可以对他输入到命令行中的单个图像进行推理。为了加快速度,模型必须加载一次,并且输入图像必须是可热交换的,因为用户将它们输入到命令行中。
如果您使用与此模型的评估代码类似的结构,则可以进行非热插拔推理:https://github.com/tensorflow/models/blob/master/research/slim/eval_image_classifier.py
您可以稍微修改上述文件以打印您的 logits 并进行推理。但是,此解决方案每次都会重建图形,这确实很慢。
我尝试构建图表并将 feed_dict 传递给 fifo_queue_Dequeue:0 张量,它表示批处理输入。但是,会话将挂起并且永远不会计算。我相信这是因为图表被“冻结”了——张量不能接受新的输入。但现在我对如何获得我想要的行为感到困惑。
【问题讨论】:
【参考方案1】:推理步骤如下:
创建 Inception-resnet-v2 图
import sys
# import from tensorflow models
sys.path.append('/home/vijay/workspace/learning/tensorflow/')
#Load the definitions of Inception-Resnet-v2 architecture
import tensorflow.contrib.slim as slim
from models.research.slim.nets.inception_resnet_v2 import inception_resnet_v2, inception_resnet_v2_arg_scope
#The pretrained model accepts size of 299x299 images
HEIGHT = 299
WIDTH = 299
CHANNELS = 3
# Create Graph
graph = tf.Graph()
with graph.as_default():
# Create a placeholder to pass the input image
img_tensor = tf.placeholder(tf.float32, shape=(None, HEIGHT, WIDTH, CHANNELS))
# Scale the image inputs to +1, -1 from 0 to 255
img_scaled = tf.scalar_mul((1.0/255), img_tensor)
img_scaled = tf.subtract(img_scaled, 0.5)
img_scaled = tf.multiply(img_scaled, 2.0)
# load Graph definitions
with slim.arg_scope(inception_resnet_v2_arg_scope()):
logits, end_points = inception_resnet_v2(img_scaled, is_training=False)
# predict the class
predictions = end_points['Predictions']
加载测试图像(来自here的示例):
#Loading a test image
img = cv2.imread('/home/vijay/datasets/image/misc/Bernese-Mountain- Dog.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (WIDTH, HEIGHT))
# make the input size [BATCH, WIDTH, HEIGHT, CHANNELS] for the network
img = np.expand_dims(img, axis=0)
加载权重并运行图表
#for labels of imagenet
sys.path.append('/home/vijay/workspace/learning/tensorflow/models/research/slim')
from datasets import imagenet
# Inception resnet v2 model
checkpoint_file='/home/vijay/datasets/pre_trained_models/inception_resnet_v2_2016_08_30.ckpt'
with tf.Session(graph=train_graph) as sess:
saver = tf.train.Saver()
saver.restore(sess, checkpoint_file)
pred_prob= sess.run(predictions, feed_dict=img_tensor:img)
# Getting the top 5 classes of the imagenet database
probabilities = pred_prob[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
names = imagenet.create_readable_names_for_imagenet_labels()
for i in range(5):
index = sorted_inds[i]
print('Probability %0.2f%% => [%s]' % (probabilities[index], names[index]))
输出
Probability 0.84% => [Bernese mountain dog]
Probability 0.04% => [Appenzeller]
Probability 0.03% => [EntleBucher]
Probability 0.01% => [Greater Swiss Mountain dog]
Probability 0.00% => [Border collie]
【讨论】:
我尝试了此代码的类似版本:github.com/richardrl/acme-slim/blob/master/… 我收到此错误:tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype uint8和 shape [500,500,3] [[Node: Placeholder = Placeholder[dtype=DT_UINT8, shape=[500,500,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"] ()]] dtype 和 shape 是正确的,但错误表明它们不正确。您对如何调试有任何想法吗? 链接失效。以上代码为批处理演示,需要创建img
和[batch, 299, 299, 3]
抱歉,您能再检查一下吗?
您输入的内容是什么?应该是从图像文件解码的uint8
中大小为 [500, 500,3] 的 numpy 数组。以上是关于TensorFlow slim inception resnet v2 推理的主要内容,如果未能解决你的问题,请参考以下文章
将预训练的 inception_resnet_v2 与 Tensorflow 结合使用
如何在 tensorFlow 中重用 slim.arg_scope?