COCO数据集——B站课程学习笔记

COCO数据集——B站课程学习笔记

因为要对coco数据格式的数据集进行切片,但对于coco数据集的结构及pycocotools不熟导致走了很多弯路,还有就是对字典格式的使用,需要取对原字典复制后的进行取出,否则就会改变原字典,真的是debug了好久,对自己很无语啊就是说!

COCO数据集介绍

COCO数据集介绍以及pycocotools简单使用
MS COCO数据集介绍以及pycocotools简单使用
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

COCO数据集包含VOC数据集的所有类别,且单个类别的目标数目比VOC多。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

数据集中有的图片没有标注,或者是标注有错误的情况,比如宽或者高为0的情况,因此用该数据集进行训练时需要对数据进行一个筛选。
为什么不用测试集?
一般只有在大型比赛中才会用到测试集test,为了防止作弊,且该test的数据分布可能与train和val不同(也可以测试一下模型的泛化性能)。但是在训练测试自己的数据集时,test和val都来自于同一数据分布,只需要用验证集来测试效果就行,单独划分val和test是没有什么意义的。
在这里插入图片描述

标注信息解释

在这里插入图片描述

这里info是数据集的基本信息,主要关注images,annotations,categories,其中images表示图片一共有5000张,annotations表示这5000张图片中共有36781个目标。
在这里插入图片描述

segmentation——分割的信息;
iscrowd——为0表示是单个目标,如果是很多小目标在一起被当作一个目标,iscrowd的值就为1;
bbox——目标检测的边界框的信息,分别对应目标左上角的x,y以及目标框的w和h;
category_id——对应的目标类别索引,注意这里的索引对应的是在91类别下的索引;
在这里插入图片描述

这里就是所有的类别,它对应的id是不连续的,这里的80类的索引是91类中的部分索引。

pycocotools的使用

官方COCOAPI的使用demo
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

代码

将预测结果保存在json文件中
import json

results = []  # 所有预测的结果都保存在该list中
# write predict results into json file
json_str = json.dumps(results, indent=4)
with open('predict_results.json', 'w') as json_file:
    json_file.write(json_str)

在这里插入图片描述

用保存的预测结果和真实标注文件做对比
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval


# accumulate predictions from all images
# 载入coco2017验证集标注文件
coco_true = COCO(annotation_file="/data/coco2017/annotations/instances_val2017.json")
# 载入网络在coco2017验证集上预测的结果
coco_pre = coco_true.loadRes('predict_results.json')

coco_evaluator = COCOeval(cocoGt=coco_true, cocoDt=coco_pre, iouType="bbox")
coco_evaluator.evaluate()
coco_evaluator.accumulate()
coco_evaluator.summarize()

读取图像的segmentation信息

import os
import random

import numpy as np
from pycocotools.coco import COCO
from pycocotools import mask as coco_mask
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt

random.seed(0)

json_path = "/data/coco2017/annotations/instances_val2017.json"
img_path = "/data/coco2017/val2017"

# random pallette
pallette = [0, 0, 0] + [random.randint(0, 255) for _ in range(255*3)]

# load coco data
coco = COCO(annotation_file=json_path)

# get all image index info
ids = list(sorted(coco.imgs.keys()))
print("number of images: {}".format(len(ids)))

# get all coco class labels
coco_classes = dict([(v["id"], v["name"]) for k, v in coco.cats.items()])

# 遍历前三张图像
for img_id in ids[:3]:
    # 获取对应图像id的所有annotations idx信息
    ann_ids = coco.getAnnIds(imgIds=img_id)
    # 根据annotations idx信息获取所有标注信息
    targets = coco.loadAnns(ann_ids)

    # get image file name
    path = coco.loadImgs(img_id)[0]['file_name']
    # read image
    img = Image.open(os.path.join(img_path, path)).convert('RGB')
    img_w, img_h = img.size

    masks = []
    cats = []
    for target in targets:
        cats.append(target["category_id"])  # get object class id
        polygons = target["segmentation"]   # get object polygons
        rles = coco_mask.frPyObjects(polygons, img_h, img_w)
        mask = coco_mask.decode(rles)
        if len(mask.shape) < 3:
            mask = mask[..., None]
        mask = mask.any(axis=2)
        masks.append(mask)

    cats = np.array(cats, dtype=np.int32)
    if masks:
        masks = np.stack(masks, axis=0)
    else:
        masks = np.zeros((0, height, width), dtype=np.uint8)

    # merge all instance masks into a single segmentation map
    # with its corresponding categories
    target = (masks * cats[:, None, None]).max(axis=0)
    # discard overlapping instances
    target[masks.sum(0) > 1] = 255
    target = Image.fromarray(target.astype(np.uint8))

    target.putpalette(pallette)
    plt.imshow(target)
    plt.show()

pycocotools中一些函数的使用

七分钟学会coco数据集,从0到1制作自己的数据集

所包含的函数

在这里插入图片描述

在这里插入图片描述

getImgIds()和getAnnIds()的使用

# 获取某个文件名对应的图像ID列表
file_name = 'example.jpg'
imgIds = coco.getImgIds(fileName=file_name)
# 获取指定图像ID对应的标注ID列表
annIds = coco.getAnnIds(imgIds=image_id)
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值