首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现BBoxes的tensorflow_datasets.feature.Sequence()?(无法生成类型为“”TypeSpec“”的列表[BBox()]“

如何实现BBoxes的tensorflow_datasets.feature.Sequence()?(无法生成类型为“”TypeSpec“”的列表[BBox()]“
EN

Stack Overflow用户
提问于 2021-04-27 23:05:58
回答 1查看 50关注 0票数 1

我正在尝试扩展ImageFolder构建器(https://github.com/tensorflow/datasets/blob/master/tensorflow_datasets/core/folder_dataset/image_folder.py#L41-L173)的功能。

若要包括和读取OpenImagesV6数据集中的.txt边框文件,请执行以下操作。这是我的自定义DatasetInfo:

代码语言:javascript
复制
    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成功创建了一个Dataset,但是当我尝试将_load_and_decode_fn映射到Dataset以读取图像和bbox文件时,当_load_example尝试返回dict时,我得到了以下错误:

代码语言:javascript
复制
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函数:

代码语言:javascript
复制
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函数的工作原理?

EN

回答 1

Stack Overflow用户

发布于 2021-04-28 18:49:19

我误解了张量在map函数中的工作方式。我在创建dataset之前加载了bbox数据,然后将_as_dataset()更改为使用load_from_generator()而不是_load_from_tensor_slices()。这,以及适当的类型和形状的设计解决了我的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67285694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档