简体   繁体   English

Tensorflow v1 对象检测api mask_rcnn_inception_v2_coco 模型批量推理

[英]Tensorflow v1 object detection api mask_rcnn_inception_v2_coco model batch inferencing

I tried to use mask_rcnn_inception_v2_coco model available in tensorflow object detection api for segmentation task.我尝试使用 tensorflow 对象检测 api 中可用的 mask_rcnn_inception_v2_coco 模型进行分割任务。 Here I wanted to run inference for a video.在这里,我想对视频进行推理。

First I did inference, one frame at a time(batch size =1).首先我做了推理,一次一帧(批量大小=1)。 Then the performance (frames per seconds) was really low.然后性能(每秒帧数)非常低。 In order to have better "fps" value I tried to change the code for support for batch inference.为了获得更好的“fps”值,我尝试更改代码以支持批量推理。

When I run the code with batch_size >1 , I got following error.当我使用 batch_size >1 运行代码时,出现以下错误。

I have used the same batch inference code with other models such as ssd_inception_v2_coco (they are object detection models), those ran without any issue.我已经将相同的批量推理代码与其他模型(例如ssd_inception_v2_coco (它们是对象检测模型))一起使用,这些模型运行时没有任何问题。

Does this mean mask_rcnn_inception_v2_coco not support for batch processing?这是否意味着mask_rcnn_inception_v2_coco不支持批处理? Or is this another issue?或者这是另一个问题?

box_ind is deprecated, use box_indices instead
Traceback (most recent call last):
  File "/home/omega/anaconda3/envs/tf_gpu_env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1365, in _do_call
    return fn(*args)
  File "/home/omega/anaconda3/envs/tf_gpu_env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1350, in _run_fn
    target_list, run_metadata)
  File "/home/omega/anaconda3/envs/tf_gpu_env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1443, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument: Can not squeeze dim[0], expected a dimension of 1, got 2
     [[{{node Squeeze_4}}]]
     [[detection_boxes/_333]]
  (1) Invalid argument: Can not squeeze dim[0], expected a dimension of 1, got 2
     [[{{node Squeeze_4}}]]
0 successful operations.
0 derived errors ignored.

I have met the same problem.我遇到了同样的问题。 I understand the reason but have no idea how to solve it for now.我明白原因,但暂时不知道如何解决。

As for the mask part, there is one more section in the default graph which is to reframe the image to its original shape.至于遮罩部分,默认图中还有一个部分,用于将图像重构为原始形状。

But the provided function in tf.utils.op is only accepted single image for now.但是 tf.utils.op 中提供的函数目前只接受单个图像。

You can see the code blew which accepted box_masks as a tensor of size [num_masks, mask_height, mask_width] without the batch.您可以看到接受 box_masks 作为大小为 [num_masks, mask_height, mask_width] 的张量而没有批处理的代码。

def reframe_box_masks_to_image_masks(box_masks, boxes, image_height,
                                     image_width):
    """Transforms the box masks back to full image masks.

    Embeds masks in bounding boxes of larger masks whose shapes correspond to
    image shape.

    Args:
      box_masks: A tf.float32 tensor of size [num_masks, mask_height, mask_width].
      boxes: A tf.float32 tensor of size [num_masks, 4] containing the box
             corners. Row i contains [ymin, xmin, ymax, xmax] of the box
             corresponding to mask i. Note that the box corners are in
             normalized coordinates.
      image_height: Image height. The output mask will have the same height as
                    the image height.
      image_width: Image width. The output mask will have the same width as the
                   image width.

    Returns:
      A tf.float32 tensor of size [num_masks, image_height, image_width].
    """

    """# TODO(rathodv): Make this a public function."""

    def reframe_box_masks_to_image_masks_default():
        """The default function when there are more than 0 box masks."""

        def transform_boxes_relative_to_boxes(boxes, reference_boxes):
            boxes = tf.reshape(boxes, [-1, 2, 2])
            min_corner = tf.expand_dims(reference_boxes[:, 0:2], 1)
            max_corner = tf.expand_dims(reference_boxes[:, 2:4], 1)
            transformed_boxes = (boxes - min_corner) / (max_corner - min_corner)
            return tf.reshape(transformed_boxes, [-1, 4])

        box_masks_expanded = tf.expand_dims(box_masks, axis=3)
        num_boxes = tf.shape(box_masks_expanded)[0]
        unit_boxes = tf.concat(
            [tf.zeros([num_boxes, 2]), tf.ones([num_boxes, 2])], axis=1)
        reverse_boxes = transform_boxes_relative_to_boxes(unit_boxes, boxes)
        return tf.image.crop_and_resize(
            image=box_masks_expanded,
            boxes=reverse_boxes,
            box_ind=tf.range(num_boxes),
            crop_size=[image_height, image_width],
            extrapolation_value=0.0)

    image_masks = tf.cond(
        tf.shape(box_masks)[0] > 0,
        reframe_box_masks_to_image_masks_default,
        lambda: tf.zeros([0, image_height, image_width, 1], dtype=tf.float32))
    return tf.squeeze(image_masks, axis=3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 google colab tensorflow 1.15.2 上训练自定义对象检测模型 ssd_mobilenet_v1_coco 和 ssd_inception_v2_coco? - how to train a custom object detection model ssd_mobilenet_v1_coco and ssd_inception_v2_coco on google colab tensorflow 1.15.2? 致命的Python错误:在训练模型(faster_rcnn_inception_v2_coco)进行对象检测时,Win 10上的分段错误 - Fatal Python error: Segmentation fault on Win 10 while training the model (faster_rcnn_inception_v2_coco) for object detection Tensorflow对象检测模型fast_rcnn_inception_resnet_v2_atrous_oid.config显示以下问题吗? - Tensorflow object detection model faster_rcnn_inception_resnet_v2_atrous_oid.config is showing following problrm? 无法使用 TensorFlow Object 检测 API 以更大的输入分辨率训练 SSD Inception-V2 - Can't Train SSD Inception-V2 with Larger Input Resolution with TensorFlow Object Detection API Tensorflow对象检测API未经训练的Faster-RCNN模型 - Tensorflow Object Detection API Untrained Faster-RCNN Model 对象检测tensorflow api中的ssd mobilenet v1可以尝试使用不同于默认值的调整大小形状吗? - Can ssd mobilenet v1 in object detection tensorflow api be tried with different resize shapes than the default ones? Faster RCNN Inception v2 Model中的BoxClassifier能否被冻结? - Can the BoxClassifier in the Faster RCNN Inception v2 Model Be Frozen? 如何找到model精度的Faster_rcnn_inception_v2? - how to find the model precision Faster_rcnn_inception_v2? 如何使用来自 Tensorflow 对象检测 API 的 Mask-RCNN 模型创建自己的数据集? - How to create own dataset for using Mask-RCNN models from the Tensorflow Object Detection API? Tensorflow 对象检测:导入错误:无法导入名称“inception_resnet_v2” - Tensorflow Object Detection: ImportError: cannot import name 'inception_resnet_v2'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM