python - 如何使用来自 Tensorflow 对象检测 API 的 Mask-RCNN 模型创建自己的数据集?

标签 python tensorflow mask object-detection object-detection-api

我不太明白这个指南:
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/instance_segmentation.md

我有很多三个类的对象。根据指南,我必须制作尺寸为 [N, H, W] 的面具,其中:

  • N - 对象数
  • H - 图像高度
  • W - 图像宽度

  • 我有这个功能来创建一个面具
    def image_mask(img, polygons):
        w, h = img.size
        n = len(polygons)
        mask = np.zeros([n, h, w], dtype=np.float32)
        for i in range(0, n):
            polygon = polygons[i].reshape((-1, 1, 2))
            tmp_mask = np.zeros([h, w], dtype=np.float32)
            cv2.fillPoly(tmp_mask, [polygon], (1, 1, 1))
            mask[i, :, :] = tmp_mask
        return mask
    

    我使用本指南来创建我的数据集:
    https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/using_your_own_dataset.md

    我在 tf_example 的末尾添加了一个掩码
    tf_example = tf.train.Example(features=tf.train.Features(feature={
    ...
          'image/object/class/label': dataset_util.int64_list_feature(classes),
          'image/object/mask': dataset_util.bytes_list_feature(mask.reshape((-1))),
      }))
    

    因为reshape (我想),RAM 很快就会耗尽,并且出现内存错误。我究竟做错了什么?也许某处有详细的指南,如何为使用 Mask-RCNN 和 Tensorflow Object Detection API 创建掩码?我没有找到这个。

    最佳答案

    这是一个老问题,但看起来您在将掩码数据发送到 bytes_list_feature 之前没有将其转换为字节。
    如果还有内存问题,'image/object/mask'功能可以是字节字符串列表,每个对象一个。如果您有一个非常大的 n ,另一个选项(必须在编译后操作的 NxHxW 数组)可能会导致内存问题。
    以下是使用字节列表选项将实例映射数据编译为对象掩码的方法:

    # a HxW array of integer instance IDs, one per each unique object
    inst_map_data = func_to_get_inst_map_data(inst_map_path)
                        
    object_masks = []
    class_labels = []
    for inst_id in np.unique(inst_map_data): # loop through all objects
    
        # a HxW array of 0's and 1's, 1's representing where this object is
        obj_mask_data = np.where(inst_map_data==inst_id, 1, 0)
                    
        # encode as png for space saving
        is_success, buffer = cv2.imencode(".png", obj_mask_data)
        io_buf = io.BytesIO(buffer)
    
        # a bytes string
        obj_mask_data_bytes = io_buf.getvalue()
                            
        object_masks += [obj_mask_data_bytes]
        class_labels += [int((inst_id-inst_id%1000)/1000)]
    
    tf_example = tf.train.Example(features=tf.train.Features(feature={
    ...
          'image/object/class/label': dataset_util.int64_list_feature(class_labels),
          'image/object/mask': dataset_util.bytes_list_feature(object_masks),
      }))
    
    

    关于python - 如何使用来自 Tensorflow 对象检测 API 的 Mask-RCNN 模型创建自己的数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52058758/

    相关文章:

    python - Azure SQL 数据库的 Sqlalchemy 问题

    Python请求,如何将内容类型添加到多部分/表单数据请求

    python - 使用布局对象 AppendText 更改 django-crispy-forms 中仅一个字段的小部件

    ios - 在不使用图像 mask 的情况下裁剪图像?

    python-2.7 - 将函数应用于屏蔽的 numpy 数组

    python - 将语音数值传递给函数

    python - 为什么隐藏深层的输出对于某些维度具有零方差?

    machine-learning - Tensorflow:损失变为 'NaN'

    python - tensorflow 通过随机因素调整图像大小

    python - 按列位置掩码 2 df