如何下载Coco数据集的特定部分?

2024-06-01 11:03:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个目标检测模型,用YLO检测船只。我想使用COCO数据集。有没有办法只下载附带注释的图像


Tags: 数据模型图像目标附带coco办法船只
3条回答

现在有一个名为fiftyone的包,您可以使用它下载MS COCO数据集并仅获取特定类的注释。有关安装的更多信息,请访问https://github.com/voxel51/fiftyone#installation

安装软件包后,只需运行以下程序即可获得“person”和“car”类:

import fiftyone.zoo as foz

# To download the COCO dataset for only the "person" and "car" classes
dataset = foz.load_zoo_dataset(
    "coco-2017",
    split="train",
    label_types=["detections", "segmentations"],
    classes=["person", "car"],
    # max_samples=50,
)

如果需要,可以注释掉最后一个选项以设置最大样本大小。此外,您可以将“train”拆分更改为“validation”,以获得验证拆分

要可视化下载的数据集,只需运行以下操作:

# Visualize the dataset in the FiftyOne App
import fiftyone as fo
session = fo.launch_app(dataset)

如果要在要加载的数据的相同函数调用中下载拆分“训练”、“验证”和“测试”,可以执行以下操作:

dataset = foz.load_zoo_dataset(
    "coco-2017",
    splits=["train", "validation", "test"],
    label_types=["detections", "segmentations"],
    classes=["person"],
    # max_samples=50,
)

要从特定类别下载图像,可以使用COCO API。这里有一个demo笔记本,介绍了这个和其他用法。整个过程如下:

下面是一个示例,介绍如何下载包含person的图像子集并将其保存在本地文件中:

from pycocotools.coco import COCO
import requests

# instantiate COCO specifying the annotations json path
coco = COCO('...path_to_annotations/instances_train2014.json')
# Specify a list of category names of interest
catIds = coco.getCatIds(catNms=['person'])
# Get the corresponding image ids and images using loadImgs
imgIds = coco.getImgIds(catIds=catIds)
images = coco.loadImgs(imgIds)

返回包含图像及其url基本信息的词典列表。我们现在可以使用requestsGET图像并将其写入本地文件夹:

# Save the images into a local folder
for im in images:
    img_data = requests.get(im['coco_url']).content
    with open('...path_saved_ims/coco_person/' + im['file_name'], 'wb') as handler:
        handler.write(img_data)

请注意,这将保存指定类别中的所有图像。因此,您可能希望将images列表切片到第一个n

据我个人所知,如果你只谈论COCO数据集,我认为他们没有“船舶”的分类。他们拥有的最接近的类别是“船”。下面是检查可用类别的链接:http://cocodataset.org/#overview

顺便说一句,也有船属于船类

如果您只想选择某个特定COCO类别的图像,您可能需要执行以下操作(从COCO的官方演示中拍摄和编辑):

# display COCO categories
cats = coco.loadCats(coco.getCatIds())
nms=[cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))

# get all images containing given categories (I'm selecting the "bird")
catIds = coco.getCatIds(catNms=['bird']);
imgIds = coco.getImgIds(catIds=catIds);

相关问题 更多 >