【问题标题】:How to run prediction (using image as input) for a saved model?如何为保存的模型运行预测(使用图像作为输入)?
【发布时间】:2019-01-14 01:05:47
【问题描述】:

问题:

我对 Tensorflow 很陌生。我的具体问题是我应该在 sess.run(fetches, feed_dict) 函数中放入哪些特定参数。例如,如何找出参数的值?

步骤:

这是我看了其他帖子后对步骤的理解。

  1. 保存 tranied tensorflow 模型,它应该由 4 个文件组成,以下是我的输出:
  • 检查点
  • Inception_resnet_v2.ckpt.data-00000-of-00001
  • Inception_resnet_v2.ckpt.index
  • Inception_resnet_v2.ckpt.meta
  1. 将输入图像的大小调整为神经网络所需的任何格式。

  2. 开始张量流会话。

  3. 检索图形和相关参数、张量...

  4. 预测输入图像。

代码:

培训代码:

https://github.com/taki0112/SENet-Tensorflow/blob/master/SE_Inception_resnet_v2.py

[已解决] 测试代码:

import tensorflow as tf
import numpy as np
import cv2

labels = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck"]

# Load graph and parameters, etc.
sess=tf.Session()
saver = tf.train.import_meta_graph('./model/Inception_resnet_v2.ckpt.meta')
saver.restore(sess, tf.train.latest_checkpoint("./model/"))
graph = tf.get_default_graph()

# Get tensor names
x = graph.get_tensor_by_name("Placeholder:0")
training_flag = graph.get_tensor_by_name("Placeholder_2:0")
op_to_restore = graph.get_tensor_by_name("final_fully_connected/dense/BiasAdd:0")

# Preprocess imgae imput
src = cv2.imread("./input/car3.jpg")
dst = cv2.resize(src, (32, 32), interpolation=cv2.INTER_CUBIC)
b,g,r = cv2.split(dst)
b = (b - np.mean(b)) / np.std(b) * .1
g = (g - np.mean(g)) / np.std(g) * .1
r = (r - np.mean(r)) / np.std(r) * .1
src = cv2.merge((b,g,r))

picture = dst.reshape(1, 32, 32, 3)
feed_dict ={x: picture, training_flag:False}

result_index = sess.run(op_to_restore,feed_dict)
print(result_index)
print (labels[np.argmax(result_index)])

【问题讨论】:

  • 我只是简单的打印出训练代码中“logits”的张量名称来找到对应的输出层。现在它为我正确打印出预测的标签。希望这对像我这样的 tensorflow 新用户有所帮助。
  • 请不要将解决方案放在问题中。如果您发现对您有用的东西,请将其添加为答案。

标签: python tensorflow


【解决方案1】:

参数实际上取决于你在做什么,但大多数第一个参数是权重和占位符。每当您使用 Tensorflow 时,您都会定义一个图表,其中包含示例(训练数据)和一些超参数,如学习率、全局步长等。使用占位符提供所有训练数据和超参数是一种标准做法。当您使用占位符构建网络并将其保存时,网络将被保存,但是,占位符的值不会被保存。

让我们看一个玩具例子:

import tensorflow as tf

#Prepare to feed input, i.e. feed_dict and placeholders
w1 = tf.placeholder("float", name="w1")
w2 = tf.placeholder("float", name="w2")
b1= tf.Variable(2.0,name="bias")
feed_dict ={w1:4,w2:8}

#Define a test operation that we will restore
w3 = tf.add(w1,w2)
w4 = tf.multiply(w3,b1,name="op_to_restore")
sess = tf.Session()
sess.run(tf.global_variables_initializer())

#Create a saver object which will save all the variables
saver = tf.train.Saver()

#Run the operation by feeding input
print sess.run(w4,feed_dict)
#Prints 24 which is sum of (w1+w2)*b1 

#Now, save the graph
saver.save(sess, 'my_test_model',global_step=1000)

现在,当我们想要恢复它时,我们不仅要恢复图和权重,还要准备一个新的 feed_dict,它将新的训练数据馈送到网络。我们可以通过 graph.get_tensor_by_name() 方法来引用这些保存的操作和占位符变量。因此,如果您想用更多新数据训练相同的模型,那么您将不得不利用这些权重,但如果您只想从您训练的模型中获得预测,您可以利用 op_to_restore 和 feed_dict作为新数据。像这样,如果你按照上面的例子:

import tensorflow as tf

sess=tf.Session()    
#First let's load meta graph and restore weights
saver = tf.train.import_meta_graph('my_test_model-1000.meta')
saver.restore(sess,tf.train.latest_checkpoint('./'))


# Now, let's access and create placeholders variables and
# create feed-dict to feed new data

graph = tf.get_default_graph()
w1 = graph.get_tensor_by_name("w1:0")
w2 = graph.get_tensor_by_name("w2:0")
feed_dict ={w1:13.0,w2:17.0}

#Now, access the op that you want to run. 
op_to_restore = graph.get_tensor_by_name("op_to_restore:0")

print sess.run(op_to_restore,feed_dict)
#This will print 60 which is calculated 
#using new values of w1 and w2 and saved value of b1. 

所以,这就是它的工作原理,在您的情况下,由于您正在尝试加载 Inception 模型,您的 op_to_restore 应该取决于您要恢复的内容,如果您可以告诉我们您正在尝试什么做,那么只有可能提出一些建议。然而,在另一个参数 feed_dict 中,它只是图像像素的 numpy 数组,您正在尝试分类/预测或您正在做的任何事情。

我从以下文章中获取了代码。这也会对您有所帮助。 http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/

更新:对于您的特定情况,您可能想尝试以下代码来预测新图像中的类。

import tensorflow as tf
slim = tf.contrib.slim
from inception_resnet_v2 import *

#Well, since you're using resnet_v2, this may be equivalent to you. 
checkpoint_file = 'inception_resnet_v2_2016_08_30.ckpt'
sample_images = ['dog.jpg', 'panda.jpg']
#Load the model
sess = tf.Session()
arg_scope = inception_resnet_v2_arg_scope()
with slim.arg_scope(arg_scope):
  logits, end_points = inception_resnet_v2(input_tensor, is_training=False)

#With this, you could consider the op_variable with the following
predict_values, logit_values = sess.run([end_points['Predictions'], logits], feed_dict={input_tensor: im})

#Here im is the normalized numpy array of the image pixels.

此外,以下资源可能会为您提供更多帮助: Using pre-trained inception_resnet_v2 with Tensorflow

https://github.com/tensorflow/tensorflow/issues/7172

【讨论】:

  • 感谢您的快速回复,这些信息非常有帮助。我在上一篇文章中添加了我的培训代码。该模型应该在 CIFAR-10 数据集上进行转换,因此输出应该是 10 个分类中的每一个的得分(我认为)。这实际上是我想知道的新输入图像。但我找不到该操作的名称。
  • 好的,我更新了我的答案,你可以试试,指出的资源也可以帮助你。
  • 还是不走运,我使用的代码似乎与您发布的代码有很大不同。
  • 好吧,也许使用slim 也可以帮助你。您能否查看此链接:github.com/tensorflow/models/blob/master/research/slim/… 特别是加载预训练模型 Inception。
  • 再次感谢您的帮助:)我已经解决了,请查看我编辑的帖子。
猜你喜欢
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
  • 2018-09-22
  • 2021-06-27
  • 1970-01-01
  • 2018-12-28
  • 2016-11-25
  • 2019-04-08
相关资源
最近更新 更多