ValueError:不能为Tensor' ResizeBilinear:0'提供形状值(1,26,38,3),它具有形状'(1,299,299,3)'

时间:2018-04-26 04:13:41

标签: tensorflow object-detection imagenet

我尝试了" object_detection"使用inceptionv3 train pb文件的教程,但我得知以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-41-1cfc370a11f2> in <module>()
  8   image_np_expanded = np.expand_dims(image_np, axis=0)
  9   # Actual detection.
---> 10   output_dict = run_inference_for_single_image(image_np, detection_graph)
 11   # Visualization of the results of a detection.
 12   vis_util.visualize_boxes_and_labels_on_image_array(

<ipython-input-38-7704143af1b0> in run_inference_for_single_image(image, graph)
 33       # Run inference
 34       output_dict = sess.run(tensor_dict,
---> 35                              feed_dict={ResizeBilinear: np.expand_dims(image, 0)})
 36 
 37       # all outputs are float32 numpy arrays, so convert types as appropriate

~/anaconda3/lib/python3.6/site-
packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
893     try:
894       result = self._run(None, fetches, feed_dict, options_ptr,
--> 895                          run_metadata_ptr)
896       if run_metadata:
897         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/anaconda3/lib/python3.6/site-
packages/tensorflow/python/client/session.py in _run(self, handle, 
fetches, feed_dict, options, run_metadata)
1102                 'Cannot feed value of shape %r for Tensor %r, '
1103                 'which has shape %r'
-> 1104                 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
1105           if not self.graph.is_feedable(subfeed_t):
1106             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (1, 26, 38, 3) for Tensor 'ResizeBilinear:0', which has shape '(1, 299, 299, 3)'

我因此尝试使用下面的代码来调整图像大小:

 image = array(img).reshape(1, 299,299,3)

但是图像仍然无法传递到resizeBilinear张量。

我刚刚更改了此代码:

#MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
#MODEL_FILE = MODEL_NAME + '.tar.gz'
#DOWNLOAD_BASE ='http://download.tensorflow.org/models/object_detection/'
#upper code is origin model preparation 
PATH_TO_CKPT = /tmp/output_graph.pb
PATH_TO_LABELS = /tmp/output_labels.txt
NUM_CLASSES = 2

如何修复此错误? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

问题是 Inception V3 期望图像输入大小为299 x 299像素和3个通道(彩色图像。)因此它需要一个形状的输入数组(1,299, 299,3)。

你正试图用尺寸为26 x 38像素的小图像来提供它。 重塑是一个数组操作,它沿着维度重新排列数组的元素,而不改变元素的数量

你需要的是调整大小图片一旦加载到所需的299 x 299像素,像PIL.Image.resize(),像这样(未经测试):

image = array( img.resize( (299, 299) ) ).reshape(1, 299,299,3)

Nota bene,您仍然需要保持重塑以预先添加额外的维度(您也可以使用expand_dims。)