python - 如何为保存的模型运行预测(使用图像作为输入)?

标签 python tensorflow

问题:

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

步骤:

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

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

  2. 开始 tensorflow session 。

  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)])

最佳答案

参数实际上取决于您在做什么,但第一个参数主要是权重和占位符。每当您使用 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_restorefeed_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

关于python - 如何为保存的模型运行预测(使用图像作为输入)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51718341/

相关文章:

python - tensorflow/keras 中的多线程

python - 导入时调用数据类(?)

Python 相当于将 POST 负载用单引号括起来

python - 将字符串转换为 Excel 友好格式

python - 保存多个不同的交易示例(trans1、trans2、trans3)+写入json

opencv - 如何在 OpenCV 中加载带有 tensorflow 后端的 Keras 模型构建

python - 没有获得 YOLO v3 模型的边界框

python - Discord.py 显示谁邀请了用户

python - 试图校准keras模型

python - TF.Keras 中自定义 Scratch 训练中的多输出多类分类