我运行了retrain.py InceptionV3 网络的迁移学习程序。我现在尝试打开保存的模型并运行图像,以便我可以查看最终的张量。但是,我收到以下错误:
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,299,299,3]
[[node Placeholder (defined at /Users/jake/.spyder-py3/imageclassification/testopen:18) ]]
下面是我的代码。它运行良好,直到我在最后一行添加了一行。
import tensorflow as tf
import numpy as np
sess=tf.Session()
saver = tf.train.import_meta_graph('/Users/jake/Desktop/Freckle/retrain_tmp/_retrain_checkpoint.meta')
saver.restore(sess,tf.train.latest_checkpoint('/Users/jake/Desktop/Freckle/retrain_tmp'))
image_path = '/Users/jake/.spyder-py3/imageclassification/flower_photos/dandelion/8181477_8cb77d2e0f_n.jpg'
graph = tf.get_default_graph()
writer = tf.summary.FileWriter('/Users/jake/Desktop/Freckle/transfer-learning-tmp', sess.graph)
input_height = 299
input_width = 299
input_depth = 3
jpeg_data = tf.placeholder(tf.string, name='DecodeJPGInput')
decoded_image = tf.image.decode_jpeg(jpeg_data, channels=input_depth)
decoded_image_as_float = tf.image.convert_image_dtype(decoded_image, tf.float32)
decoded_image_4d = tf.expand_dims(decoded_image_as_float, 0)
resize_shape = tf.stack([input_height, input_width])
resize_shape_as_int = tf.cast(resize_shape, dtype=tf.int32)
resized_image = tf.image.resize_bilinear(decoded_image_4d,
resize_shape_as_int)
image_data = tf.gfile.GFile(image_path, 'rb').read()
resized_input_tensor = tf.placeholder(tf.float32, [None, input_height, input_width, 3], name='resized_input_tensor')
resized_input_values = sess.run(resized_image, {jpeg_data: image_data})
#output_op = graph.get_operation_by_name('final_result')
output_tensor = graph.get_tensor_by_name('final_result:0')
#THIS IS THE LINE THAT THROWS A BUG
final_values = sess.run(output_tensor,feed_dict={resized_input_tensor: resized_input_values})
我调查了控制台中的占位符,下面是读数:
graph.get_tensor_by_name('final_result:0')
Out[17]: <tf.Tensor 'final_result:0' shape=(?, 5) dtype=float32>
resized_input_tensor
Out[18]: <tf.Tensor 'resized_input_tensor:0' shape=(?, 299, 299, 3) dtype=float32>
np.shape(resized_input_values)
Out[19]: (1, 299, 299, 3)
graph.get_operation_by_name('Placeholder')
Out[21]: <tf.Operation 'Placeholder' type=Placeholder>
graph.get_operation_by_name('Placeholder').values()
Out[22]: (<tf.Tensor 'Placeholder:0' shape=(?, 299, 299, 3) dtype=float32>,)
resized_input_values.dtype
Out[32]: dtype('float32')
由于 InvalidArgumentError
提到 'Placeholder'
,我尝试了以下操作:
x = graph.get_operation_by_name('Placeholder')
final_values = sess.run(output_tensor,feed_dict={x: resized_input_values})
出现以下错误:
Traceback (most recent call last):
File "<ipython-input-28-84a55042c4a2>", line 1, in <module>
final_values = sess.run(output_tensor,feed_dict={x: resized_input_values})
File "/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 950, in run
run_metadata_ptr)
File "/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1116, in _run
'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a Operation into a Tensor.
提前致谢!
最佳答案
你几乎是对的,但是feed_dict
的键应该是张量对象,而不是操作,所以你应该这样做:
x = graph.get_tensor_by_name('Placeholder:0')
final_values = sess.run(output_tensor, feed_dict={x: resized_input_values})
关于python - 如何调试占位符的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60336856/