【问题标题】:How do I implement a tensorflow_datasets.feature.Sequence() of BBoxes? (Could not build a TypeSpec [BBox()] with type 'list)'如何实现 BBoxes 的 tensorflow_datasets.feature.Sequence()? (无法构建类型为“列表”的 TypeSpec [BBox()])
【发布时间】:2021-04-27 15:05:58
【问题描述】:

我正在尝试扩展 ImageFolder 构建器的功能 (https://github.com/tensorflow/datasets/blob/master/tensorflow_datasets/core/folder_dataset/image_folder.py#L41-L173)
在 OpenImagesV6 数据集中包含和读取 .txt 边界框文件。这是我自定义的 DatasetInfo:

    return dataset_info.DatasetInfo(
        builder=self,
        description='Generic image classification dataset.',
        features=features_lib.FeaturesDict({
            'image': features_lib.Image(
                shape=self._image_shape,
                dtype=self._image_dtype,
            ),
            'label': features_lib.ClassLabel(num_classes=3),
            'image/filename': features_lib.Text(),
            'bbox_path': features_lib.Text(),
            'bboxes': features_lib.Sequence({
                'bbox': features_lib.BBoxFeature()
              }),
        }),
        supervised_keys=('image', 'label'),
    )

在 _as_dataset 函数中,我已经成功地使用 from_tensor_slices 创建了一个数据集,但是当我尝试将 _load_and_decode_fn 映射到数据集以读取图像和 bbox 文件时,当 _load_example 尝试返回 dict 时,我收到以下错误:

TypeError: Could not build a TypeSpec for [BBox(ymin=<tf.Tensor 'while/StringToNumber_1:0' shape=() dtype=float32>, xmin=<tf.Tensor 'while/StringToNumber:0' shape=() dtype=float32>, ymax=<tf.Tensor 'while/StringToNumber_3:0' shape=() dtype=float32>, xmax=<tf.Tensor 'while/StringToNumber_2:0' shape=() dtype=float32>)] with type list

这是我修改后的 _load_example 函数:

def _load_example(
    path: tf.Tensor,
    label: tf.Tensor,
    bbox_path: tf.Tensor,

) -> Dict[str, tf.Tensor]:
  
  bbox_values = tf.io.read_file(bbox_path)
  bbox_values = tf.strings.split(bbox_values, sep='\n')
  bboxes = []
  for line in bbox_values:
      line = tf.strings.split(line, sep=' ')
      if line[0] is not None:
        xmin = tf.strings.to_number(line[1])
        ymin = tf.strings.to_number(line[2])
        xmax = tf.strings.to_number(line[3])
        ymax = tf.strings.to_number(line[4])

        bboxes.append(features_lib.BBox(
          ymin=ymin, 
          xmin=xmin, 
          ymax=ymax, 
          xmax=xmax
         ))
  img = tf.io.read_file(path)
  return {
      'image': img,
      'label': tf.cast(label, tf.int64),
      'image/filename': path,
      'bbox_path': bbox_path,
      'bboxes': {
        'bbox': bboxes
      }
  }

我必须在这里遗漏一些相当基本的东西。也许我只是误解了地图功能的工作原理?

【问题讨论】:

    标签: python tensorflow tensorflow-datasets


    【解决方案1】:

    我误解了张量在地图函数中的工作方式。我在创建数据集之前加载了 bbox 数据,然后将 _as_dataset() 更改为使用 load_from_generator() 而不是 _load_from_tensor_slices()。这样,正确指定类型和形状就解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多