如何实现bboxes的tensorflow\u datasets.feature.sequence()(无法生成类型为“list”的typespec[bbox()]

enyaitl3  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(226)

我正在尝试扩展ImageFolderBuilder的功能(https://github.com/tensorflow/datasets/blob/master/tensorflow_datasets/core/folder_dataset/image_folder.py#l41-(l173)
在openimagesv6数据集中包含并读取.txt边界框文件。这是我的自定义数据集信息:

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'),
    )

在\u as \u dataset函数中,我成功地用from \u tensor \u切片创建了一个数据集,但是当我尝试将\u load \u和\u decode \u fnMap到数据集以读取图像和bbox文件时,当\u load \u 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

这是我修改的加载示例函数:

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
      }
  }

我一定错过了一些基本的东西。也许我只是误解了map函数的工作原理?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题