【发布时间】:2019-01-14 01:05:47
【问题描述】:
问题:
我对 Tensorflow 很陌生。我的具体问题是我应该在 sess.run(fetches, feed_dict) 函数中放入哪些特定参数。例如,如何找出参数的值?
步骤:
这是我看了其他帖子后对步骤的理解。
- 保存 tranied tensorflow 模型,它应该由 4 个文件组成,以下是我的输出:
- 检查点
- Inception_resnet_v2.ckpt.data-00000-of-00001
- Inception_resnet_v2.ckpt.index
- Inception_resnet_v2.ckpt.meta
-
将输入图像的大小调整为神经网络所需的任何格式。
-
开始张量流会话。
-
检索图形和相关参数、张量...
-
预测输入图像。
代码:
培训代码:
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