如何使用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数据集

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读取自己制作的数据集

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

到此这篇关于使用pytorch加载并读取coco数据集的文章就介绍到这了,更多相关pytorch 读取coco数据集内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!