MS CoCo数据集

news2024/10/6 15:54:42

一、前言

Ms CoCo数据集是一个非常大型且常用的数据集,可以做的任务有目标检测、图像分割、图像描述等

数据集地址:链接

描述数据集的论文地址:链接

有一点需要注意:数据集的物体类别分为80类和91类两种,其中object80类是stuff91类的子集,stuff类别中不属于object的是指没有明确边界的材料和对象例如天空、草地等。

在学习目标检测时,我们把object80作为分类类别即可

MS coco与PASCAL VOC的对比,可以看到CoCo数据集数量明显更大,相同类别的真实值也更多,因此往往会使用CoCo数据集的预训练模型来作为自己模型的初始化。(在CoCo数据集上预训练耗时比较长)

二、数据集下载

这里没有划分测试集,是因为其实测试集划分其实和验证集的划分的文件结构是一样的,所以可以不划分测试集来测试,使用验证集就可以达到测试的目的。

三、验证集与标注文件的查看

3.1使用python的json库查看

训练集比较大就没有下载,这里主要查看测试集与标注文件

import json


json_path = "./annotations/instances_val2017.json"
with open(json_path, "r") as f:
    json_file = json.load(f)
print(json_file["info"])

设置断点,我们可以得到如下:

json_file是一个字典,包括:info、licences、images、annotations、categories五个关键字

key

value type

value

info

dict

description、url、version、year、contribute、data_created

licences

list

len = 8

images

list

include 5000 dicts every dict = {licences、filename、coco_url、height、width、date_captured、flickr_url、id}

annotations

list

include 36871dicts every dict = {segmentation、area、iscrowd、image_id、bbox、categoriy_id、id}

categories

list

len = 80 include supercategory name id

其中:

images是一个列表(元素个数对应图像的张数),列表中每个元素都是一个dict,对应一张图片的相关信息。包括对应图像名称图像宽度高度等信息。

annoatations是一个列表(元素个数对应图片中的对象个数),列表每一个元素都是一个dict,表示每一个对象的标注信息。包括分割信息目标检测的框(框的四个数字分别代表左上角点的x,y坐标,后面两个数字为框的宽和高。id是对应的图像id,category_id是对象类型的种类id。iscrowd表示这是否是一个单目标对象,0表示单个对象,1表示对象集合

categories是一个列表(元素个数对应检测目标的类别数)列表中每个元素都是一个dict对应一个类别的目标信息。包括类别id类别名称所属超类(也即父类的意思)

3.2.使用官方API查看

3.2.1查看目标检测标注信息

import os
from pycocotools.coco import COCO
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt

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

# 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')
    draw = ImageDraw.Draw(img)
    # draw box to image
    for target in targets:
        x, y, w, h = target["bbox"]
        x1, y1, x2, y2 = x, y, int(x + w), int(y + h)
        draw.rectangle((x1, y1, x2, y2))
        draw.text((x1, y1), coco_classes[target["category_id"]])

    # show image
    plt.imshow(img)
    plt.show()

3.2.2查看分割标注

代码:


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 = "./annotations/instances_val2017.json"
img_path = "./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()

 3.2.3查看关键点标注

代码:

import numpy as np
from pycocotools.coco import COCO

json_path = "./annotations/person_keypoints_val2017.json"
coco = COCO(json_path)
img_ids = list(sorted(coco.imgs.keys()))

# 遍历前5张图片中的人体关键点信息(注意,并不是每张图片里都有人体信息)
for img_id in img_ids[:5]:
    idx = 0
    img_info = coco.loadImgs(img_id)[0]
    ann_ids = coco.getAnnIds(imgIds=img_id)
    anns = coco.loadAnns(ann_ids)
    for ann in anns:
        xmin, ymin, w, h = ann['bbox']
        # 打印人体bbox信息
        print(f"[image id: {img_id}] person {idx} bbox: [{xmin:.2f}, {ymin:.2f}, {xmin + w:.2f}, {ymin + h:.2f}]")
        keypoints_info = np.array(ann["keypoints"]).reshape([-1, 3])
        visible = keypoints_info[:, 2]
        keypoints = keypoints_info[:, :2]
        # 打印关键点信息以及可见度信息
        print(f"[image id: {img_id}] person {idx} keypoints: {keypoints.tolist()}")
        print(f"[image id: {img_id}] person {idx} keypoints visible: {visible.tolist()}")
        idx += 1

输出:

    1

    2

    3

    4

    5

    6

    7

    8

    9

   10

loading annotations into memory...

Done (t=0.34s)

creating index...

index created!

[image id: 139] person 0 bbox: [412.80, 157.61, 465.85, 295.62]

[image id: 139] person 0 keypoints: [[427, 170], [429, 169], [0, 0], [434, 168], [0, 0], [441, 177], [446, 177], [437, 200], [430, 206], [430, 220], [420, 215], [445, 226], [452, 223], [447, 260], [454, 257], [455, 290], [459, 286]]

[image id: 139] person 0 keypoints visible: [1, 2, 0, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

[image id: 139] person 1 bbox: [384.43, 172.21, 399.55, 207.95]

[image id: 139] person 1 keypoints: [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]

[image id: 139] person 1 keypoints visible: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

3.3.CoCo官方代码API解读

上面三段代码分别可视化bbox,segmentation,keypoint,主要使用了官方的一系列API,接下来解读官方API的代码(以bbox可视化为例)

coco = COCO(annotation_file=json_path)

这一句代码创建一个CoCo对象,annotation_file作为json文件的路径传入,主要用来读取json文件和可视化注释

在初始化函数中:

创建四个空字典分别是dataset、anns、cats、imgs,同时创建两个默认字典(默认字典的学习笔记)默认字典CSDN链接

再判断路径是否为空,不为空时使用json库方法下载标注文件,下载下来的dataset格式为dict

再调用创建索引函数

在这个方法中,创建三个空字典anns, cats, imgs,然后分别判断字典中是否有annotation、images、categories的关键字

对于dataset["annotation"]是有一个所有对象的列表,每一元素都是一个字典,imgToAnns字典里面的每一个图像名称id列表记录这张图象的所有对象

anns字典每一个图像序号id关键字记录标注的对象信息字典

对于dataset["images"]是包含所有图片的字典,imgs字典记录每一张图像名称id的图片信息字典

对于dataset["categories"]是一个字典列表,cats字典记录每一种类别id的类别信息

catToImgs字典记录每一个类别id的图像名称id

ids = list(sorted(coco.imgs.keys()))

ids为图片的名称id排序列表

coco_classes = dict([(v["id"], v["name"]) for k, v in coco.cats.items()])

coco_classes实际上为categories字典列表中每一个字典抽取出来类别id和类别名称构成字典

再下面感觉有点水平不够,等以后变强了再回来看

四、验证目标检测任务MAP

4.1预测结果输出格式

目标检测预测格式

假设我们有预测结果如下(原文博主训练得到的预测结果)

我们将其保存为predict_results.json文件

再执行下面代码比较预测值与ground_truth

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval


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

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

得到输出结果:

    1

    2

    3

    4

    5

    6

    7

    8

    9

   10

   11

   12

   13

   14

   15

   16

   17

   18

   19

   20

   21

   22

   23

   24

   25

loading annotations into memory...

Done (t=0.71s)

creating index...

index created!

Loading and preparing results...

DONE (t=0.79s)

creating index...

index created!

Running per image evaluation...

Evaluate annotation type *bbox*

DONE (t=19.72s).

Accumulating evaluation results...

DONE (t=3.82s).

 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.233

 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.415

 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.233

 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.104

 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.262

 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.323

 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.216

 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.319

 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.327

 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.145

 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.361

 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.463

下一篇记录目标检测的评估指标:CoCo数据集-目标检测指标MAP_SL1029_的博客-CSDN博客

五、学习的博客与视频

MS COCO数据集介绍以及pycocotools简单使用_太阳花的小绿豆的博客-CSDN博客

COCO数据集介绍以及pycocotools简单使用_哔哩哔哩_bilibili

CoCo数据集官方网站:COCO - Common Objects in Context (cocodataset.org)

CoCo官方API地址:cocodataset/cocoapi: COCO API - Dataset @ http://cocodataset.org/ (github.com)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/531319.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ThingsBoard自定义万能查询节点entity query node

1、概述 大家好,我又更新干货了,还是那句话,我绝不分享那些照抄官网翻译的东西来骗订阅,我觉得那是浪费时间,要搞就搞干货,今天给大家分享ThingsBoard如何自定义规则节点,而且是万能查询节点,这是本人经过多次研究搞出来的,这个节点有什么特别之处呢?这个节点几乎可…

vue脚手架:路由的配置和使用

一、路由的配置和使用 1、引入router 在创建项目的时候可以默认用Vue脚手架中的自动生成路由文件或者自己下载&#xff08;注意vue和vue-router版本的对应关系&#xff09; 2、创建路由组件并引入&#xff1a; 在views文件夹中创建2个.vue文件作为路由组件&#xff1a; 引…

报表开发组件FastReport Mono v2023.1 - 支持与My Reports Cloud集成

FastReport Mono v2023.1现已推出! 最新版中更新了与 My Reports Cloud 的部分集成、来自 JasperReports 的模板转换器等功能&#xff0c;同时修复了10余处问题&#xff0c;点击下方免费试用哦~ FastReport Mono v2023.1现已推出! 今天将为大家带来FastReport Mono v2023.1更新…

【k8s】【ELK】基于节点DaemonSet运行日 志Agent实践【待写】

1.日志收集场景分析与说明 部署架构说明 对于那些将日志输出到&#xff0c;stdout与stderr的Pod&#xff0c; 可以直接使用DaemonSet控制器在每个Node节点上运行一个 <font colorred>filebeat、logstash、fluentd </font>容器进行统一的收集&#xff0c;而后写入…

使用doop识别最近commons text漏洞的污点信息流

作者&#xff1a;vivo 互联网安全团队 - Chen Haojie 本文基于笔者对doop静态程序分析框架源代码和规则学习&#xff0c;并结合对目前漏洞公开技术细节的学习&#xff0c;修改增强doop app only模式下的分析规则后&#xff0c;实现通过doop工具识别commons text rce漏洞&#…

【QuartusII】0-创建工程模板

一、创建工程 1、激活安装quartus II软件后&#xff0c;打开即见如下界面 2、在菜单栏 “File -> New Project Wizard…”中&#xff0c;进入创建工程流程 3、第一部分&#xff0c;如下图&#xff0c;配置路径、项目名称、以及顶层文件&#xff08;类似C语言的main&#xf…

Mac上如何装Nacos?

Nacos大家都很熟悉,服务注册中心,那么今天给大家写一篇Mac上如何装Nacos的文章。 安装步骤如下: 1、上官网 http://nacos.io/zh-cn/ 点击跳到nacos的官网上。然后点击前往Github 2、找到release 发布版本 来到GitHub上后,页面往下滑,找到latest stable release,点击…

【Linux】管道的读写特点和管道设置为非阻塞

目录 管道的读写特点管道设置为非阻塞 橙色 管道的读写特点 使用管道时&#xff0c;需要注意以下几种特殊的情况&#xff08;假设都是阻塞I/O操作&#xff09; 所有的指向管道写端的文件描述符都关闭了&#xff08;管道写端引用计数为0&#xff09;&#xff0c;有进程从管道的…

Shapefile在线查看利器【MapJS】

MapJS是一个免费的GIS地图文件在线查看工具&#xff0c;在浏览器里即可直接打开本地的Shapfile、GeoJSON、TopoJSON、KML等文件进行查看。 MapJS在线访问地址&#xff1a;Shapefile|GeoJSON|KML在线查看工具 。 1、MapJS快速上手 在浏览器中打开MapJS官网后&#xff0c;直接拖…

计算机图形学 | 奇妙的真实感——片元着色

计算机图形学 | 奇妙的真实感——片元着色 计算机图形学 | 奇妙的真实感——片元着色9.1 图形渲染与视觉外观图形渲染的光栅化阶段基于视觉外观的渲染视觉物理现象视觉现象光照计算着色方式表面细节有光有影才有真实感 9.2 奇妙的颜色什么是颜色可见光谱颜色视觉的生理基础 人眼…

美颜sdk的多平台适配与跨平台开发技术分享

美颜sdk作为互联网时代的重要工具&#xff0c;也因其高效、稳定、易用的特点&#xff0c;被越来越多的应用开发者所采用。然而&#xff0c;在不同的平台上使用美颜sdk时&#xff0c;会遇到一些问题&#xff0c;如何进行多平台适配和跨平台开发&#xff0c;成为了一个值得探讨的…

只需要两步就能快速接入GPT

缘起 最近一个朋友提出&#xff0c;让我出个关于如何快速接入GPT的教程&#xff0c;今天就给大家安排上。 需要的工具 经过实测&#xff0c;这是迄今为止最便捷的接入方式&#xff0c;而且亲测有效。 首先&#xff0c;第一步你需要下载最新版的微软Edge浏览器&#xff0c;去…

Python(Keras)实现 LSTM 对销售额的预测

博主之前有涉及过LSTM的文章&#xff0c;见下&#xff1a; LSTM-理解 Part-1&#xff08;RNN&#xff1a;循环神经网络&#xff09; Python LSTM时序数据的预测&#xff08;一些数据处理的方法&#xff09; 机器学习 Pytorch实现案例 LSTM案例&#xff08;航班人数预测&#xf…

Unity3D :重要的类 - Gizmos 和 Handles

推荐&#xff1a;将 NSDT场景编辑器加入你的3D工具链 3D工具集&#xff1a; NSDT简石数字孪生 重要的类 - Gizmos 和 Handles Gizmos 和 Handles 类用于在 Scene 视图和 Game 视图绘制线条和形状以及交互式手柄和控件。这两个类共同提供了一种方法来扩展这些视图中显示的内容&…

【云原生】Kubernetes 的组件与架构

文章目录 引语1、集群组件1.1 控制平面组件&#xff08;Control Plane Components&#xff09;1.2 Node 组件1.3 插件&#xff08;Addons&#xff09; 2、集群搭建总结 引语 在上篇文章&#xff0c;我们介绍了 Kubernetes 是什么&#xff0c;它能够对容器进行编排&#xff0c;…

实现Qwidget窗口填满整个主窗口,并跟随鼠标的拖动自动缩放

实现Qwidget窗口填满整个主窗口&#xff0c;并跟随鼠标的拖动自动缩放 新建一个窗口&#xff0c;我想在这个窗口上放一个QWidget&#xff0c;并且这个QWidget能够布满整个窗口&#xff0c;还可以随着随鼠标的拖动自动缩放 1、首先给大家介绍一个好用的组件库&#xff1a;qt-mat…

lighthouse尘埃粒子计数器3100/3350参数资料

​​SOLAIR 3350集成了我们的超长寿命激光二极管技术传感器&#xff0c;可生产业界比较长的激光二极管寿命20年以上(基于连续24/7运行)。使用一个新的&#xff0c;更轻的延长寿命的电池和外部交流适配器&#xff0c;SOLAIR 3350更加便携。SOLAIR 3350具有业界最好的用户界面(UI…

springboot+java拍卖竞拍网站系统idea

书画拍卖网站系统的设计与实现的设计思想如下&#xff1a; Spring Boot 是 Spring 家族中的一个全新的框架&#xff0c;它用来简化Spring应用程序的创建和开发过程。也可以说 Spring Boot 能简化我们之前采用SSM&#xff08;Spring MVC Spring MyBatis &#xff09;框架进行…

10个学习Python的理由以及Python的优势有哪些?

Python的由来 Python的创始人是吉多范罗苏姆&#xff0c;1989年他在阿姆斯特丹的CWI工作&#xff0c;圣诞节期间&#xff0c;吉多范罗苏姆为了打发圣诞节的无聊&#xff0c;决定开发一个新的脚本解释程序&#xff0c;作为ABC 语言的一种继承。之所以选择Python作为编程语言的名…

新星计划|记录安装Nodejs和HBuilderX搭建、部署微信小程序开发环境(一)

文章目录 1 前言2 注册小程序账号3 安装微信开发者工具4 安装Nodejs和HBuilderX4.1 windows用户安装Nodejs4.2 macos/linux用户安装Nodejs4.3 安装HBuilder X 5 创建项目5.1 新建一个项目5.2 进行基本配置 6 HBuilderX同步微信开发者工具6.1 打开服务端口6.2 调用微信开发者工具…