我正在尝试获取tensorflow初始v3的pool_3层输出。我的输入是形状的ndarray(64,64,3),但我得到以下错误
with tf.Session() as sess:
pool_3_tensor = sess.graph.get_tensor_by_name('pool_3:0')
feat1 = sess.run(pool_3_tensor,{'DecodeJpeg/contents:0': image})
feat1 = np.squeeze(feat1)
ValueError Traceback (most recent call last)
<ipython-input-26-fb1865f7fbee> in <module>()
3 pool_3_tensor = sess.graph.get_tensor_by_name('pool_3:0')
4
----> 5 feat1 = sess.run(pool_3_tensor,{'DecodeJpeg/contents:0': image})
6 feat1 = np.squeeze(feat1)
/N/u/mbirla/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
765 try:
766 result = self._run(None, fetches, feed_dict, options_ptr,
--> 767 run_metadata_ptr)
768 if run_metadata:
769 proto_data =
tf_session.TF_GetBuffer(run_metadata_ptr)
/N/u/mbirla/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
942 'Cannot feed value of shape %r for Tensor %r, '
943 'which has shape %r'
--> 944 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
945 if not self.graph.is_feedable(subfeed_t):
946 raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Cannot feed value of shape (64, 64, 3) for Tensor 'DecodeJpeg/contents:0', which has shape '()
-------更新----------
将其转换为字符串后。我收到无效的JPEG数据,大小12288。 有关详细错误:https://gist.github.com/mridulbirla/0d710d7ccd7b22c8f87989c37837e10e
答案 0 :(得分:0)
更改
feat1 = sess.run(pool_3_tensor,{'DecodeJpeg/contents:0': image})
到
feat1 = sess.run(pool_3_tensor,{'DecodeJpeg/contents:0': image.tostring()})
并试一试
'DecodeJpeg/contents:0'
是一个标量字符串张量,您可以从中解码三个暗淡的图像张量
当我用inception-v3转移学习时,我没有将图像读入np.array类型,而是将图像内容读取为带有
的字符串with tf.gfile.FastGFile("you_image_file_name") as f:
content = f.read()
然后将content
提供给'DecodeJpeg/contents:0'