当前位置: 首页 > 知识库问答 >
问题:

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

长孙瑞
2023-03-14

问题:

我对Tensorflow很陌生。我的具体问题是,我应该在ses中放入哪些特定参数。运行(fetches,feed\u dict)函数。例如,如何找出参数的值?

步骤:

以下是我在阅读其他帖子后对这些步骤的理解。

  1. 保存tranied tensorflow模型,它应该由4个文件组成,以下是我的输出:
  • 检查点

将输入图像调整为神经网络所需的任何格式。

启动tensorflow会话。

检索图和相关参数、张量。。。

预测输入图像。

代码:

传输代码:

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

共有1个答案

安承教
2023-03-14

参数实际上取决于您正在做的事情,但主要是第一个参数是权重和占位符。每当您使用Tensorflow时,您都会html" target="_blank">定义一个图,该图提供示例(训练数据)和一些超参数,如学习率、全局步长等。使用占位符提供所有训练数据和超参数是标准做法。当您使用占位符构建网络并保存它时,网络会被保存,但是,占位符的值不会保存。

让我们来看一个玩具示例:

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\u dict,将新的训练数据提供给网络。我们可以通过图形获得对这些保存的操作和占位符变量的引用。通过名称()方法获取张量。因此,如果你想用更多的新数据训练同一个模型,那么你必须利用这些weigtages,然而,如果你只是想从你训练的模型中得到预测,你可以利用op\u to\u restore和feed\u 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. 

这就是它的工作原理,在你的例子中,因为你试图加载盗梦空间模型,你的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.

此外,以下资源可能会对您有更多帮助:将预训练inception_resnet_v2与Tensorflow一起使用

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

 类似资料:
  • 问题内容: 一般来说,我只是从keras和机器学习开始。 我训练了一个模型来对2类图像进行分类,并使用进行保存。这是我使用的代码: 它成功地以0.98的准确度进行了训练,相当不错。为了在新图像上加载并测试该模型,我使用了以下代码: 它输出: [[0]] 为什么不给出类的实际名称,为什么? 提前致谢。 问题答案: keras Forecast_classes(docs)输出类别预测的numpy数组。

  • 我有一个问题:我把我的图像放在我的项目文件夹中,例如,E:\All Work IT\Java All\JavaWork\TestingDB,当我使用这行代码来使用图像时 它工作得很好!但是当我想制作一个可运行的jar文件时(导出- 我需要把我的图像放在哪里来解决这个问题,用什么代码?谢谢。

  • 问题内容: 我刚刚使用Python图像库(PIL)进行了一些图像处理,这是我之前发现的用于执行图像的傅立叶变换的文章,但是我无法使用save函数。整个代码运行良好,但不会保存生成的图像: 我得到的错误如下: 如何使用Pythons PIL保存图像? 问题答案: 已解决有关文件扩展名的错误,您可以使用(不带点)或将输出名称与扩展名一起传递。现在要处理该错误,您需要在频域中适当地修改数据以将其保存为整

  • 我将Deeplearning4j(Ver.1.0.0-M1.1)用于构建神经网络。 我以Deeplearning4j中的IrisClassifier为例。 我怎么能得到预测? 萨克斯!

  • 我正在将图像保存到tar文件中,如下所示 < code>docker保存我的图像:最新 但是构建失败了。有人能帮我一下吗?如何在Dockerfile中发送tar文件?

  • 我已经建立了一个序列模型来预测数据集。输出是一个值数组,如下所示: 我想按升序对数组排序。 我已经试过了 和 我失败了。