使用pytorch加载并读取COCO数据集的详细操作

来自:网络
时间:2022-05-12
阅读:
目录

如何使用pytorch加载并读取COCO数据集 环境配置基础知识:元祖、字典、数组利用PyTorch读取COCO数据集利用PyTorch读取自己制作的数据集

环境配置

看pytorch入门教程

基础知识:元祖、字典、数组

# 元祖
a = (1, 2)
# 字典
b = {'username': 'peipeiwang', 'code': '111'}
# 数组
c = [1, 2, 3]
print(a[0])
print(c[0])
print(b["username"])

输出:

使用pytorch加载并读取COCO数据集的详细操作

利用PyTorch读取COCO数据集

import torchvision
from PIL import ImageDraw
# 导入coco 2017 验证集和对应annotations
coco_dataset = torchvision.datasets.CocoDetection(root="COCO_dataset_val_2017/val2017",
                                                  annFile="COCO_dataset_val_2017/annotations_trainval2017/annotations/instances_val2017.json")
# 图像和annotation分开读取
image, info = coco_dataset[0]
# ImageDraw 画图工具
image_handler = ImageDraw.ImageDraw(image)
for annotation in info:
    # bbox为检测框的位置坐标
    x_min, y_min, width, height = annotation['bbox']
    # ((), ())分别为左上角的坐标对和右上角的坐标对,image_handler.rectangle是指在图片是绘制方框
    image_handler.rectangle(((x_min, y_min), (x_min + width, y_min + height)))
image.show()

结果:

使用pytorch加载并读取COCO数据集的详细操作

利用PyTorch读取自己制作的数据集

使用cvat工具创建自己的数据集标注,导出为coco格式并读取
结果:

使用pytorch加载并读取COCO数据集的详细操作

返回顶部
顶部