Tensorflow对象检测中的输入张量与Python签名不兼容

彼得·当

我最近在Tensorflow中训练了对象检测模型,但由于某些原因,某些图像的输入张量与python签名不兼容。这是我在google colab中运行的代码以进行推断:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings

def load_image_into_numpy_array(path):
    """Load an image from file into a numpy array.

    Puts image into numpy array to feed into tensorflow graph.
    Note that by convention we put it into a numpy array with shape
    (height, width, channels), where channels=3 for RGB.

    Args:
      path: the file path to the image

    Returns:
      uint8 numpy array with shape (img_height, img_width, 3)
    """
    return np.array(Image.open(path))

for image_path in img:

    print('Running inference for {}... '.format(image_path), end='')
    image_np=load_image_into_numpy_array(image_path)


    # Things to try:
    # Flip horizontally
    # image_np = np.fliplr(image_np).copy()
    # Convert image to grayscale
    # image_np = np.tile(
    #     np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)

    # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
    input_tensor=tf.convert_to_tensor(image_np)
    # The model expects a batch of images, so add an axis with `tf.newaxis`.
    input_tensor=input_tensor[tf.newaxis, ...]

    # input_tensor = np.expand_dims(image_np, 0)
    detections=detect_fn(input_tensor)

    # All outputs are batches tensors.
    # Convert to numpy arrays, and take index [0] to remove the batch dimension.
    # We're only interested in the first num_detections.
    num_detections=int(detections.pop('num_detections'))
    detections={key:value[0,:num_detections].numpy()
                   for key,value in detections.items()}
    detections['num_detections']=num_detections

    # detection_classes should be ints.
    detections['detection_classes']=detections['detection_classes'].astype(np.int64)

    image_np_with_detections=image_np.copy()

    viz_utils.visualize_boxes_and_labels_on_image_array(
          image_np_with_detections,
          detections['detection_boxes'],
          detections['detection_classes'],
          detections['detection_scores'],
          category_index,
          use_normalized_coordinates=True,
          max_boxes_to_draw=100,     #max number of bounding boxes in the image
          min_score_thresh=.25,      #min prediction threshold
          agnostic_mode=False)
    %matplotlib inline
    plt.figure()
    plt.imshow(image_np_with_detections)
    print('Done')
    plt.show()

这是运行推理时收到的错误消息:

    Running inference for /content/gdrive/MyDrive/TensorFlow/workspace/training_demo/images/test/image_part_002.png... 
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-23-5b465e5474df> in <module>()
         40 
         41     # input_tensor = np.expand_dims(image_np, 0)
    ---> 42     detections=detect_fn(input_tensor)
         43 
         44     # All outputs are batches tensors.
    
    6 frames
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _convert_inputs_to_signature(inputs, input_signature, flat_input_signature)
       2804       flatten_inputs)):
       2805     raise ValueError("Python inputs incompatible with input_signature:\n%s" %
    -> 2806                      format_error_message(inputs, input_signature))
       2807 
       2808   if need_packing:
    
    ValueError: Python inputs incompatible with input_signature:
      inputs: (
        tf.Tensor(
    [[[[  0   0   0 255]
       [  0   0   0 255]
       [  0   0   0 255]
       ...
       [  0   0   0 255]
       [  0   0   0 255]
       [  0   0   0 255]]
    
      [[  0   0   0 255]
       [  0   0   0 255]
       [  0   0   0 255]
       ...
       [  0   0   0 255]
       [  0   0   0 255]
       [  0   0   0 255]]
    
      [[  0   0   0 255]
       [  0   0   0 255]
       [  0   0   0 255]
       ...
       [  0   0   0 255]
       [  0   0   0 255]
       [  0   0   0 255]]
    
      ...
    
      [[ 34  32  34 255]
       [ 35  33  35 255]
       [ 35  33  35 255]
       ...
       [ 41  38  38 255]
       [ 40  37  37 255]
       [ 40  37  37 255]]
    
      [[ 36  34  36 255]
       [ 35  33  35 255]
       [ 36  34  36 255]
       ...
       [ 41  38  38 255]
       [ 41  38  38 255]
       [ 43  40  40 255]]
    
      [[ 36  34  36 255]
       [ 36  34  36 255]
       [ 37  35  37 255]
       ...
       [ 41  38  38 255]
       [ 40  37  37 255]
       [ 39  36  36 255]]]], shape=(1, 1219, 1920, 4), dtype=uint8))
      input_signature: (
        TensorSpec(shape=(1, None, None, 3), dtype=tf.uint8, name='input_tensor'))

有谁知道我可以转换图像的输入张量以便对它们进行推断的方法吗?举例来说,我知道一个推理工作的图像分辨率为400x291,而推理不工作的图像分辨率为1920x1219。我在培训中使用了SSD MobileNet V1 FPN 640x640模型。

廷布斯·卡林(Timbus Calin)

在您的情况下,问题在于您的输入张量形状为(1,1219,1920,4),更确切地说4是有问题的。

第一个元素1代表批次大小(已添加input_tensor[tf.newaxis, ...])。

正确地说,但是在实际读取图像的地方会出现问题,因为有4个通道(假设您阅读RGB-A?)而不是3个(典型的RGB)或1个(灰度)。

我建议您检查图像并强制转换为RGB,即 Image.open(path).convert('RGB')

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Tensorflow输入形状不兼容

来自分类Dev

不了解OpenCV GitHub Wiki中的“ TensorFlow对象检测”

来自分类Dev

OpenCV Python findHomography srcPoint输入不兼容

来自分类Dev

Tensorflow尺寸在CNN中不兼容

来自分类Dev

python中的Tkinter-对象检测(不冲突)

来自分类Dev

TensorFlow 打印输入张量?

来自分类Dev

使用Lambda的C#中不兼容的匿名函数签名

来自分类Dev

根据TensorFlow中的python列表过滤张量

来自分类Dev

当尺寸不匹配时,如何在 tensorflow 中压缩张量

来自分类Dev

在形状不兼容的张量上重新调用

来自分类Dev

Tensorflow对象检测API中的过拟合

来自分类Dev

如何在Keras / tensorflow中向输入张量添加恒定张量

来自分类Dev

如何复制基于Tensorflow队列中的张量属性(“过采样”)的输入张量?

来自分类Dev

在tensorflow中获取ValueError,表明我的形状不兼容

来自分类Dev

TensorFlow:卷积网络中的尺寸不兼容错误

来自分类Dev

如何调试“INVALID_ARGUMENT:在签名中找不到输入张量别名”

来自分类Dev

日期之间输入不兼容

来自分类Dev

tensorflow:模型是用形状(无,无,6)构造的,但是在形状不兼容的输入上被调用

来自分类Dev

tensorflow:模型是用形状(无,无,6)构造的,但是在形状不兼容的输入上被调用

来自分类Dev

tensorflow:模型是用形状(无,无,6)构造的,但是在形状不兼容的输入上被调用

来自分类Dev

tensorflow:模型是用形状(无,无,6)构造的,但是在形状不兼容的输入上被调用

来自分类Dev

不兼容的对象类型-Java

来自分类Dev

在 Tensorflow 对象检测中评估预训练模型时出错 (tensorflow.python.framework.errors_impl.NotFoundError:)

来自分类Dev

与客户端连接时 Tensorflow 服务错误“输入大小与签名不匹配”

来自分类Dev

sbt 0.13.0检测到JLine不兼容

来自分类Dev

sbt 0.13.0检测到JLine不兼容

来自分类Dev

检测输入是否是python 3.5中的字母?

来自分类Dev

Tensorflow 对象检测 API - 错误的对象检测

来自分类Dev

keras 中不兼容的输入数据错误,尺寸不匹配 ValueError

Related 相关文章

  1. 1

    Tensorflow输入形状不兼容

  2. 2

    不了解OpenCV GitHub Wiki中的“ TensorFlow对象检测”

  3. 3

    OpenCV Python findHomography srcPoint输入不兼容

  4. 4

    Tensorflow尺寸在CNN中不兼容

  5. 5

    python中的Tkinter-对象检测(不冲突)

  6. 6

    TensorFlow 打印输入张量?

  7. 7

    使用Lambda的C#中不兼容的匿名函数签名

  8. 8

    根据TensorFlow中的python列表过滤张量

  9. 9

    当尺寸不匹配时,如何在 tensorflow 中压缩张量

  10. 10

    在形状不兼容的张量上重新调用

  11. 11

    Tensorflow对象检测API中的过拟合

  12. 12

    如何在Keras / tensorflow中向输入张量添加恒定张量

  13. 13

    如何复制基于Tensorflow队列中的张量属性(“过采样”)的输入张量?

  14. 14

    在tensorflow中获取ValueError,表明我的形状不兼容

  15. 15

    TensorFlow:卷积网络中的尺寸不兼容错误

  16. 16

    如何调试“INVALID_ARGUMENT:在签名中找不到输入张量别名”

  17. 17

    日期之间输入不兼容

  18. 18

    tensorflow:模型是用形状(无,无,6)构造的,但是在形状不兼容的输入上被调用

  19. 19

    tensorflow:模型是用形状(无,无,6)构造的,但是在形状不兼容的输入上被调用

  20. 20

    tensorflow:模型是用形状(无,无,6)构造的,但是在形状不兼容的输入上被调用

  21. 21

    tensorflow:模型是用形状(无,无,6)构造的,但是在形状不兼容的输入上被调用

  22. 22

    不兼容的对象类型-Java

  23. 23

    在 Tensorflow 对象检测中评估预训练模型时出错 (tensorflow.python.framework.errors_impl.NotFoundError:)

  24. 24

    与客户端连接时 Tensorflow 服务错误“输入大小与签名不匹配”

  25. 25

    sbt 0.13.0检测到JLine不兼容

  26. 26

    sbt 0.13.0检测到JLine不兼容

  27. 27

    检测输入是否是python 3.5中的字母?

  28. 28

    Tensorflow 对象检测 API - 错误的对象检测

  29. 29

    keras 中不兼容的输入数据错误,尺寸不匹配 ValueError

热门标签

归档