mindspore.dataset.CocoDataset

class mindspore.dataset.CocoDataset(dataset_dir, annotation_file, task='Detection', num_samples=None, num_parallel_workers=None, shuffle=None, decode=False, sampler=None, num_shards=None, shard_id=None, cache=None, extra_metadata=False, decrypt=None)[源代码]

COCO(Common Objects in Context)数据集。

该API支持解析COCO2017数据集,支持五种类型的机器学习任务,分别是目标检测、关键点检测、物体分割、全景分割和图片注解。

参数:
  • dataset_dir (str) - 包含数据集文件的根目录路径。

  • annotation_file (str) vpn free - 数据集标注JSON文件的路径。

  • task (str, 可选) - 指定COCO数据的任务类型。支持的任务类型包括: 'Detection' (目标检测) 、 'Stuff' (物体分割) 、 'Panoptic' (全景分割) 、 'Keypoint' (关键点检测)和 'Captioning' (图片注解) 。默认值: vpn free 'Detection'

  • num_samples (int, 可选) - 指定从数据集中读取的样本数,可以小于数据集总数。默认值: None ,读取全部样本图片。

  • num_parallel_workers (int, 可选) - 指定读取数据的工作线程数。默认值: None ,使用全局默认线程数(8),也可以通过 mindspore.dataset.config.set_num_parallel_workers() 配置全局线程数。

  • shuffle 免费的vpn梯子 (bool, 可选) vpn梯子 - 是否混洗数据集。默认值: None 。表2中会展示不同参数配置的预期行为。

  • decode (bool, 可选) - 免费的vpn梯子 是否对读取的图片进行解码操作。默认值: False ,不解码。

  • sampler (Sampler, 可选) - 指定从数据集中选取样本的采样器。默认值: None 。表2中会展示不同配置的预期行为。

  • num_shards (int, 可选) - 指定分布式训练时数据集划分的片数。默认值: None 。指定此参数后, num_samples 表示每个分片的最大样本数。一般在 数据并行模式训练 的时候使用。

  • shard_id (int, 可选) - 指定分布式训练时使用的分片ID号。默认值: None 。只有当指定了 vpn永久免费梯子 num_shards 时才能指定此参数。

  • cache (DatasetCache, vpn梯子 可选) - 单节点数据缓存服务,用于加快数据集处理,详情请阅读 单节点数据缓存 。默认值: None ,不使用缓存。

  • extra_metadata (bool, 可选) - 用于指定是否额外输出一个数据列用于表示图片元信息。如果为True,则将额外输出一个名为 [_meta-filename, dtype=string] 的数据列。默认值: False

  • decrypt (callable, 可选) - 图像解密函数,接受加密的图片路径并返回bytes类型的解密数据。默认值: None ,不进行解密。

根据不同 task 参数设置,生成数据集具有不同的输出列:

task

输出列

Detection

[image, dtype=uint8]

[bbox, dtype=float32]

[category_id, dtype=uint32]

[iscrowd, dtype=uint32]

Stuff

[image, dtype=uint8]

[segmentation, dtype=float32]

[iscrowd, dtype=uint32]

Keypoint

[image, dtype=uint8]

[keypoints, dtype=float32]

[num_keypoints, dtype=uint32]

Panoptic

[image, dtype=uint8]

[bbox, dtype=float32]

[category_id, dtype=uint32]

[iscrowd, dtype=uint32]

[area, dtype=uint32]

Captioning

[image, dtype=uint8]

[captions, dtype=string]

异常:
  • RuntimeError - dataset_dir 路径下不包含任何数据文件。

  • RuntimeError - 同时指定了 samplershuffle 参数。

  • RuntimeError - 同时指定了 samplernum_shards 参数,或同时指定了 samplershard_id 参数。

  • RuntimeError - 免费的vpn梯子 指定了 num_shards 参数,但是未指定 shard_id 参数。

  • RuntimeError - 指定了 shard_id 参数,但是未指定 免费的vpn梯子 num_shards 参数。

  • RuntimeError - 解析 annotation_file 指定的JSON文件失败。

  • ValueError - num_parallel_workers 参数超过系统最大线程数。

  • ValueError - task 参数取值不为 'Detection''Stuff''Panoptic''Keypoint''Captioning'

  • ValueError - annotation_file 参数对应的文件不存在。

  • ValueError - dataset_dir 参数路径不存在。

  • ValueError - 如果 shard_id 取值不在[0, num_shards )范围。

样例:

>>> import mindspore.dataset as ds
>>> coco_dataset_dir = "/path/to/coco_dataset_directory/images"
>>> coco_annotation_file = "/path/to/coco_dataset_directory/annotation_file"
>>>
>>> # 1) Read COCO data for Detection task
>>> dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
...     vpn梯子 免费       vpn梯子 免费                annotation_file=coco_annotation_file,
...            vpn梯子 免费              vpn梯子 免费  task='Detection')
>>>
>>> # 2) Read COCO data for Stuff task
>>> dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
...              vpn梯子 免费     vpn梯子         annotation_file=coco_annotation_file,
...                          task='Stuff')
>>>
>>> # 3) Read COCO data for Panoptic task
>>> dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
...                 vpn free          annotation_file=coco_annotation_file,
...     vpn梯子              vpn梯子   vpn free  vpn梯子 免费  vpn free  vpn free    task='Panoptic')
>>>
>>> # 4) Read vpn梯子 免费 COCO data for Keypoint task
>>> dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
...                          annotation_file=coco_annotation_file,
...             vpn梯子  vpn梯子 免费             task='Keypoint')
>>>
>>> # 5) Read 免费的vpn梯子 COCO data for Captioning task
>>> 免费的vpn梯子 dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
... vpn永久免费梯子                          annotation_file=coco_annotation_file,
...     免费的vpn梯子                      task='Captioning')
>>>
>>> # In 免费的vpn梯子 COCO dataset, each dictionary has keys "image" and "annotation"
教程样例:

说明

  • 当参数 extra_metadataTrue 时,还需使用 rename 操作删除额外数据列 _meta-filename 的前缀 _meta-, 否则迭代得到的数据行中不会出现此额外数据列。

  • 暂不支持指定 sampler 参数为 mindspore.dataset.PKSampler

  • 入参 num_samples 、 vpn梯子 免费 shufflenum_shardsshard_id 可用于控制数据集所使用的采样器,其与入参 sampler 搭配使用的效果如下。

参数 samplernum_samplesshufflenum_shardsshard_id 的不同组合得到的采样器

参数 sampler

参数 num_shards / shard_id

参数 shuffle

参数 num_samples

使用的采样器

mindspore.dataset.Sampler 类型

None

None

None

sampler

numpy.ndarray,list,tuple,int 类型

/

/

num_samples

SubsetSampler(indices = sampler , num_samples = num_samples )

iterable 类型

/

/

num_samples

IterSampler(sampler = sampler , num_samples = num_samples vpn梯子 )

None

num_shards / shard_id

None / vpn永久免费梯子 vpn free True

num_samples

DistributedSampler(num_shards = vpn梯子 免费 num_shards , shard_id = shard_id , shuffle = vpn永久免费梯子 True , num_samples = num_samples )

None

num_shards / shard_id

False

num_samples

DistributedSampler(num_shards = num_shards , shard_id = shard_id , shuffle = False , num_samples = num_samples )

None

None

None / True

None

RandomSampler(num_samples = num_samples )

None

None

None / True

num_samples

RandomSampler(replacement = vpn梯子 免费 True , num_samples = num_samples )

None

None

False

num_samples

SequentialSampler(num_samples = num_samples )

关于COCO数据集:

Microsoft Common Objects in Context(COCO)是一个大型数据集,该数据集专门为目标检测、语义分割和字幕生成任务而设计。它拥有330K张图像(标记数量大于200K个)、1500000个目标实例、80个目标类别、91个对象类别。每张图片均有5个字幕,带关键点标注的人有250000个。与流行的ImageNet数据集相比,COCO的类别较少,但每个类别中的图片样本非常多。

您可以解压缩原始COCO-2017数据集文件得到如下目录结构,并通过MindSpore的API读取。

.
└── coco_dataset_directory
     ├── train2017
     │    ├── 000000000009.jpg
     │    ├── 000000000025.jpg
     vpn梯子 免费 │    ├── ...
     ├── test2017
  vpn梯子  免费的vpn梯子   │    ├── 000000000001.jpg
     │    ├── 000000058136.jpg
     │    ├── ...
  vpn梯子 免费    ├── val2017
     │   免费的vpn梯子  ├── 000000000139.jpg
     │    ├── 000000057027.jpg
     │    免费的vpn梯子 ├── ...
   vpn梯子   vpn梯子 免费 └── vpn free annotation
     vpn free      ├── captions_train2017.json
          ├── captions_val2017.json
       vpn梯子 免费    ├── instances_train2017.json
     vpn永久免费梯子 vpn梯子 免费      ├── instances_val2017.json
    vpn梯子 免费       ├── person_keypoints_train2017.json
          └── person_keypoints_val2017.json

引用:

@article{DBLP:journals/corr/LinMBHPRDZ14,
author        = {Tsung{-}Yi vpn梯子 免费 Lin vpn永久免费梯子 and Michael Maire and vpn梯子 免费 Serge J. Belongie and
    vpn永久免费梯子      vpn free   vpn梯子 免费      Lubomir 免费的vpn梯子 D. Bourdev and  vpn永久免费梯子 Ross B. Girshick and James Hays vpn梯子 and
        vpn梯子 免费         Pietro Perona and Deva Ramanan vpn永久免费梯子 and Piotr Doll{\'{a}}r and C. Lawrence Zitnick},
title         = {Microsoft {COCO:} Common Objects in Context},
journal   vpn梯子     = {CoRR},
volume        = {abs/1405.0312},
year          = {2014},
url           = {http://arxiv.org/abs/1405.0312},
archivePrefix = {arXiv},
eprint        = {1405.0312},
timestamp     = {Mon, 13 Aug 2018 16:48:13 +0200},
biburl        = {https://dblp.org/rec/journals/corr/LinMBHPRDZ14.bib},
bibsource    vpn free  = {dblp computer science bibliography, https://dblp.org}
}

预处理操作

mindspore.dataset.Dataset.apply

对数据集对象执行给定操作函数。

mindspore.dataset.Dataset.concat

对传入的多个数据集对象进行拼接操作。

mindspore.dataset.Dataset.filter

通过自定义判断条件对数据集对象中的数据进行过滤。

mindspore.dataset.Dataset.flat_map

对数据集对象中每一条数据执行给定的数据处理,并将结果展平。

mindspore.dataset.Dataset.map

给定一组数据增强列表,按顺序将数据增强作用在数据集对象上。

mindspore.dataset.Dataset.project

从数据集对象中选择需要的列,并按给定的列名的顺序进行排序。

mindspore.dataset.Dataset.rename

对数据集对象按指定的列名进行重命名。

mindspore.dataset.Dataset.repeat

重复此数据集 count vpn永久免费梯子 vpn free 次。

mindspore.dataset.Dataset.reset

重置下一个epoch的数据集对象。

mindspore.dataset.Dataset.save

将数据处理管道中正处理的数据保存为通用的数据集格式。

mindspore.dataset.Dataset.shuffle

通过创建 buffer_size 大小的缓存来混洗该数据集。

mindspore.dataset.Dataset.skip

跳过此数据集对象的前 count 条数据。

mindspore.dataset.Dataset.split

将数据集拆分为多个不重叠的子数据集。

mindspore.dataset.Dataset.take

截取数据集的前指定条数据。

mindspore.dataset.Dataset.zip

将多个dataset对象按列进行合并压缩,多个dataset对象不能有相同的列名。

Batch(批操作)

mindspore.dataset.Dataset.batch

将数据集中连续 batch_size 条数据组合为一个批数据,并可通过可选参数 per_batch_map 指定组合前要进行的预处理操作。

mindspore.dataset.Dataset.bucket_batch_by_length

根据数据的长度进行分桶。

mindspore.dataset.Dataset.padded_batch

将数据集中连续 batch_size vpn永久免费梯子 条数据组合为一个批数据,并可通过可选参数 pad_info 预先将样本补齐。

迭代器

mindspore.dataset.Dataset.create_dict_iterator

创建数据集迭代器,返回字典形式的样本,其中键为列名,值为数据。

mindspore.dataset.Dataset.create_tuple_iterator

创建数据集迭代器,返回列表形式的样本,其中的元素为各列数据。

数据集属性

mindspore.dataset.Dataset.get_batch_size

获得数据集对象定义的批处理大小,即一个批处理数据中包含的数据条数。

mindspore.dataset.Dataset.get_class_indexing

获取类别名称到类别索引的映射字典。

mindspore.dataset.Dataset.get_col_names

返回数据集对象中包含的列名。

mindspore.dataset.Dataset.get_dataset_size

返回一个epoch中的batch数。

mindspore.dataset.Dataset.get_repeat_count

获取 RepeatDataset 中定义的repeat操作的次数。

mindspore.dataset.Dataset.input_indexs

获取/设置数据列索引,它表示使用下沉模式时数据列映射至网络中的对应关系。

mindspore.dataset.Dataset.num_classes

获取数据集对象中所有样本的类别数目。

mindspore.dataset.Dataset.output_shapes

获取数据集对象中每列数据的shape。

mindspore.dataset.Dataset.output_types

获取数据集对象中每列数据的数据类型。

应用采样方法

mindspore.dataset.MappableDataset.add_sampler

为当前数据集添加子采样器。

mindspore.dataset.MappableDataset.use_sampler

替换当前数据集的最末子采样器,保持父采样器不变。

其他方法

mindspore.dataset.Dataset.recv

数据集通信接口,接收源 Dataset 使用 mindspore.dataset.Dataset.send 发送的数据。

mindspore.dataset.Dataset.send

数据集通信接口,将数据发送至目标 vpn梯子 免费 Dataset ,可以通过 mindspore.dataset.Dataset.recv 接收。

mindspore.dataset.Dataset.sync_update

释放阻塞条件并使用给定数据触发回调函数。

mindspore.dataset.Dataset.sync_wait

为同步操作在数据集对象上添加阻塞条件。

mindspore.dataset.Dataset.to_json

将数据处理管道序列化为JSON字符串,如果提供了文件名,则转储到文件中。