Python Tensorflow MNIST示例:从SavedModel预测的代码
根据本文,我正在使用示例构建CNN: 但是,我无法通过输入样本图像找到要预测的样本。这里的任何帮助都将不胜感激 下面是我尝试过的,但找不到输出张量名称Python Tensorflow MNIST示例:从SavedModel预测的代码,python,tensorflow,mnist,Python,Tensorflow,Mnist,根据本文,我正在使用示例构建CNN: 但是,我无法通过输入样本图像找到要预测的样本。这里的任何帮助都将不胜感激 下面是我尝试过的,但找不到输出张量名称 img = <load from file> sess = tf.Session() saver = tf.train.import_meta_graph('/tmp/mnist_convnet_model/model.ckpt-2000.meta') saver.restore(sess, tf.train.latest_check
img = <load from file>
sess = tf.Session()
saver = tf.train.import_meta_graph('/tmp/mnist_convnet_model/model.ckpt-2000.meta')
saver.restore(sess, tf.train.latest_checkpoint('/tmp/mnist_convnet_model/'))
input_place_holder = sess.graph.get_tensor_by_name("enqueue_input/Placeholder:0")
out_put = <not sure what the tensor output name in the graph>
current_input = img
result = sess.run(out_put, feed_dict={input_place_holder: current_input})
print(result)
img=
sess=tf.Session()
saver=tf.train.import_meta_图('/tmp/mnist_convnet_model/model.ckpt-2000.meta')
saver.restore(sess、tf.train.latest_checkpoint('/tmp/mnist_convnet_model/'))
input\u place\u holder=sess.graph.get\u tensor\u by\u name(“排队输入/占位符:0”)
输出=
电流输入=img
结果=sess.run(输出、馈送、输入={input\place\u holder:current\u input})
打印(结果)
您可以使用Tensorflow中的工具查找检查点文件中的张量
from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file
print_tensors_in_checkpoint_file(file_name="tmp/mnist_convnet_model/model.ckpt-2000.meta", tensor_name='')
这里有一些很好的指导,告诉你如何去做。下面是一个受后一个链接启发的小示例只需确保/tmp
目录存在
import tensorflow as tf
# Create some variables.
variable = tf.get_variable("variable_1", shape=[3], initializer=tf.zeros_initializer)
inc_v1=variable.assign(variable + 1)
# Operation to initialize variables if we do not restore from checkpoint
init_op = tf.global_variables_initializer()
# Create the saver
saver = tf.train.Saver()
with tf.Session() as sess:
# Setting to decide wether or not to restore
DO_RESTORE=True
# Where to save the data file
save_path="./tmp/model.ckpt"
if DO_RESTORE:
# If we want to restore, load the variables from the saved file
saver.restore(sess, save_path)
else:
# If we don't want to restore, then initialize variables
# using their specified initializers.
sess.run(init_op)
# Print the initial values of variable
initial_var_value=sess.run(variable)
print("Initial:", initial_var_value)
# Do some work with the model.
incremented=sess.run(inc_v1)
print("Incremented:", incremented)
# Save the variables to disk.
save_path = saver.save(sess, save_path)
print("Model saved in path: %s" % save_path)
你所说的“无法通过输入样本图像找到要预测的样本”是什么意思?我的问题是,既然我们有一个经过训练和验证的模型,我该如何使用保存的模型、发送图像并检查预测。我正在寻找此上下文中的示例。是的,请分享您在a中尝试过的内容?将我尝试过的代码添加到原始Q中。感谢您的帮助。我更新了我的答案,以便它告诉您如何保存和还原变量:)