我是张量流的新手。我已经设法训练和验证 CNN,通过 Saver 对象将会话保存到 CPKT 文件中并将其加载回来。现在我想使用经过训练的模型来检查它对我自己拍摄的照片的表现。我不知道该怎么做。
使用 TensorFlow 模型进行预测
数据挖掘
Python
深度学习
张量流
2021-10-01 01:59:33
1个回答
看看这个博客。主要是您已将操作保存为计算图的一部分。因此,在加载模型后,您可以恢复会话并调用您为训练和验证数据而创建的预测操作,并在输入 feed_dict 的新数据上运行它。确保它与您的训练数据具有相同的格式和形状。我正在添加一小段代码,也许这会有所帮助。
checkpoint_file=tf.train.latest_checkpoint(checkpoint_directory)
graph=tf.Graph()
with graph.as_default():
session_conf = tf.ConfigProto(allow_safe_placement=True, log_device_placement =False)
sess = tf.Session(config = session_conf)
with sess.as_default():
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
saver.restore(sess,checkpoint_file)
input = graph.get_operation_by_name("input").outputs[0]
prediction=graph.get_operation_by_name("prediction").outputs[0]
#newdata=put your data here
print sess.run(prediction,feed_dict={input:newdata})
要使用 operation_by_name,您必须在原始模型中命名操作和变量。其次,在 feed dict 中,您必须提供执行操作所需的所有参数(此处为预测),因此如果您使用 dropout,您也必须提供。可能有更简单的方法来做到这一点。我就是这样做的。
其它你可能感兴趣的问题