【问题标题】:Cannot feed value of shape (480, 640, 3) for Tensor 'image_tensor:0', which has shape '(?, ?, ?, 3)'无法为具有形状“(?,?,?,3)”的张量“image_tensor:0”提供形状(480、640、3)的值
【发布时间】:2019-04-22 20:04:14
【问题描述】:

我正在尝试为“校园建筑检测器”运行我的代码,并且我正在使用以 faster_rcnn_inception_v2 作为模型的 Tensorflow 的对象检测 API。 我已经对网络进行了 7000 次(耗时 12 小时)的训练,但由于迭代次数更多(我有 900000 次)而中止,现在当我尝试运行代码时,出现以下错误:

无法为形状为“(?, ?, ?, 3)”的张量“image_tensor:0”提供形状 (480, 640, 3) 的值

我正在使用 anaconda、Jupiter notebook、Python v3.6.8、Tensorflow v1.13.1

代码:

import cv2
cap = cv2.VideoCapture(0)
try:
    with detection_graph.as_default():
        with tf.Session() as sess:
                # Get handles to input and output tensors
                ops = tf.get_default_graph().get_operations()
                all_tensor_names = {output.name for op in ops for output in op.outputs}
                tensor_dict = {}
                for key in [
                  'num_detections', 'detection_boxes', 'detection_scores',
                  'detection_classes', 'detection_masks'
                ]:
                    tensor_name = key + ':0'
                    if tensor_name in all_tensor_names:
                        tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
                      tensor_name)

                while True:
                    ret, image_np = cap.read()
                    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                    image_np_expanded = np.expand_dims(image_np, axis=0)
                    # Actual detection.
                    output_dict = run_inference_for_single_image(image_np, detection_graph)
                    # Visualization of the results of a detection.
                    vis_util.visualize_boxes_and_labels_on_image_array(
                        image_np,
                        output_dict['detection_boxes'],
                        output_dict['detection_classes'],
                        output_dict['detection_scores'],
                        category_index,
                        instance_masks=output_dict.get('detection_masks'),
                        use_normalized_coordinates=True,
                        line_thickness=8)
                    cv2.imshow('object_detection', cv2.resize(image_np, (800, 600)))
                    if cv2.waitKey(25) & 0xFF == ord('q'):
                        cap.release()
                        cv2.destroyAllWindows()
                        break
except Exception as e:
    print(e)
    cap.release()

提前致谢。

【问题讨论】:

    标签: opencv tensorflow object-detection-api


    【解决方案1】:

    函数run_inference_for_single_image 需要批量输入图像(四个维度),因此下面的行试图将图像从三个维度扩展到四个维度,

    image_np_expanded = np.expand_dims(image_np, axis=0)
    

    你只需要换行

    output_dict = run_inference_for_single_image(image_np, detection_graph)
    

    进入

    output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
    

    这样就解决了问题。

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2022-08-12
      • 2018-08-03
      • 2019-02-01
      • 2020-11-18
      相关资源
      最近更新 更多