COCO数据集简介与处理

1、标准的coco数据标注的格式与下载

Dataset - COCO Dataset 数据特点

COCO数据集标注格式详解----object instances

Dataset之COCO数据集:COCO数据集的简介、下载、使用方法之详细攻略

COCO/VOC 数据集加速下载

{
    "images": [
        {
            "height": 682,
            "width": 1024,
            "id": 1,
            "file_name": "terrain2.png"
        }
    ],
    "categories": [
        {
            "supercategory": "car",
            "id": 1,
            "name": "car"
        }
    ],
    "annotations": [
        {
            "iscrowd": 0,
            "image_id": 1,
            "bbox": [
                218.0,
                448.0,
                222.0,
                161.0
            ],
            "segmentation": [
                [
                    218.0,
                    448.0,
                    440.0,
                    448.0,
                    440.0,
                    609.0,
                    218.0,
                    609.0
                ]
            ],
            "category_id": 1,
            "id": 1,
            "area": 698368
        },
        {
            "iscrowd": 0,
            "image_id": 1,
            "bbox": [
                501.0,
                451.0,
                121.0,
                92.0
            ],
            "segmentation": [
                [
                    501.0,
                    451.0,
                    622.0,
                    451.0,
                    622.0,
                    543.0,
                    501.0,
                    543.0
                ]
            ],
            "category_id": 1,
            "id": 2,
            "area": 698368
        },
        {
            "iscrowd": 0,
            "image_id": 1,
            "bbox": [
                634.0,
                437.0,
                81.0,
                56.0
            ],
            "segmentation": [
                [
                    634.0,
                    437.0,
                    715.0,
                    437.0,
                    715.0,
                    493.0,
                    634.0,
                    493.0
                ]
            ],
            "category_id": 1,
            "id": 3,
            "area": 698368
        },
        {
            "iscrowd": 0,
            "image_id": 1,
            "bbox": [
                725.0,
                423.0,
                70.0,
                51.0
            ],
            "segmentation": [
                [
                    725.0,
                    423.0,
                    795.0,
                    423.0,
                    795.0,
                    474.0,
                    725.0,
                    474.0
                ]
            ],
            "category_id": 1,
            "id": 4,
            "area": 698368
        },
        {
            "iscrowd": 0,
            "image_id": 1,
            "bbox": [
                791.0,
                404.0,
                40.0,
                47.0
            ],
            "segmentation": [
                [
                    791.0,
                    404.0,
                    831.0,
                    404.0,
                    831.0,
                    451.0,
                    791.0,
                    451.0
                ]
            ],
            "category_id": 1,
            "id": 5,
            "area": 698368
        }
    ]
}
 

1.1、COCO数据基本结构

这3种类型共享下面所列的基本类型,包括info、image、license,而annotation类型则呈现出了多态,会根据不同的任务具有不同的数据标注形式。

{
"info" : info,
"images" : [image], 
"annotations" : [annotation],
"licenses" : [license],
}

info{
"year" : int,
"version" : str,
"description" : str,
"contributor" : str,
"url" : str,
"date_created" : datetime,
}

image{
"id" : int,
"width" : int,
"height" : int,
"file_name" : str,
"license" : int,
"flickr_url" : str,
"coco_url" : str,
"date_captured" : datetime,
}

license{
"id" : int,
"name" : str,
"url" : str,
}

除了Annotation数据之外的数据类型举例如下:

1)info类型,比如一个info类型的实例:

"info":{
    "description":"This is stable 1.0 version of the 2014 MS COCO dataset.",
    "url":"http:\/\/mscoco.org",
    "version":"1.0","year":2014,
    "contributor":"Microsoft COCO group",
    "date_created":"2015-01-27 09:11:52.357475"
}

2)Images类型,Images是包含多个image实例的数组,对于一个image类型的实例:

{
    "license":3,
    "file_name":"COCO_val2014_000000391895.jpg",
    "coco_url":"http:\/\/mscoco.org\/images\/391895",
    "height":360,"width":640,"date_captured":"2013-11-14 11:18:45",
    "flickr_url":"http:\/\/farm9.staticflickr.com\/8186\/8119368305_4e622c8349_z.jpg",
    "id":391895
}

3)licenses类型,licenses是包含多个license实例的数组,对于一个license类型的实例:

{
    "url":"http:\/\/creativecommons.org\/licenses\/by-nc-sa\/2.0\/",
    "id":1,
    "name":"Attribution-NonCommercial-ShareAlike License"

1.2、COCO数据基本结构(Object Instance 类型的标注格式)

1)整体JSON文件格式
Object Instance这种格式的文件从头至尾按照顺序分为以下段落:

{
    "info": info,
    "licenses": [license],
    "images": [image],
    "annotations": [annotation],
    "categories": [category]
}
是的,你打开这两个文件,虽然内容很多,但从文件开始到结尾按照顺序就是这5段。其中,info、licenses、images这三个结构体/类型 在上一节中已经说了,在不同的JSON文件中这三个类型是一样的,定义是共享的。不共享的是annotation和category这两种结构体,他们在不同类型的JSON文件中是不一样的。

PS,mages数组、annotations数组、categories数组的元素数量是相等的,等于图片的数量。

2)annotations字段
annotations字段是包含多个annotation实例的一个数组,annotation类型本身又包含了一系列的字段,如这个目标的category id和segmentation mask。segmentation格式取决于这个实例是一个单个的对象(即iscrowd=0,将使用polygons格式)还是一组对象(即iscrowd=1,将使用RLE格式)。如下所示:

annotation{
    "id": int,
    "image_id": int,
    "category_id": int,
    "segmentation": RLE or [polygon],
    "area": float,
    "bbox": [x,y,width,height],
    "iscrowd": 0 or 1,
}

注意,单个的对象(iscrowd=0)可能需要多个polygon来表示,比如这个对象在图像中被挡住了。而iscrowd=1时(将标注一组对象,比如一群人)的segmentation使用的就是RLE格式。

另外,每个对象(不管是iscrowd=0还是iscrowd=1)都会有一个矩形框bbox ,矩形框左上角的坐标和矩形框的长宽会以数组的形式提供,数组第一个元素就是左上角的横坐标值。

其中,area是框的面积(area of encoded masks)。

3)categories字段
annotation结构中的categories字段存储的是当前对象所属的category的id,以及所属的supercategory的name。
categories是一个包含多个category实例的数组,而category结构体描述如下:

{
    "id": int,
    "name": str,
    "supercategory": str,
}

从instances_val2017.json文件中摘出的2个category实例如下所示:

{
    "supercategory": "person",
    "id": 1,
    "name": "person"
},
{
    "supercategory": "vehicle",
    "id": 2,
    "name": "bicycle"
},
......

2、Create your own COCO-style datasets(构造自己格式的COCO数据)

Create your own COCO-style datasets,这个页面讲述了能够构造的COCO数据类型的详情信息,标注coco的Annotation工具,通过COCO的支持实现深度学习。

我们所要做的就是循环遍历每个图像jpeg及其对应的注释png,并让pycococreator生成正确格式的项。第90和91行创建图像条目,而第112-114行处理注释。
#!/usr/bin/env python3

import datetime
import json
import os
import re
import fnmatch
from PIL import Image
import numpy as np
from pycococreatortools import pycococreatortools

ROOT_DIR = 'train'
IMAGE_DIR = os.path.join(ROOT_DIR, "shapes_train2018")
ANNOTATION_DIR = os.path.join(ROOT_DIR, "annotations")

INFO = {
    "description": "Example Dataset",
    "url": "https://github.com/waspinator/pycococreator",
    "version": "0.1.0",
    "year": 2018,
    "contributor": "waspinator",
    "date_created": datetime.datetime.utcnow().isoformat(' ')
}

LICENSES = [
    {
        "id": 1,
        "name": "Attribution-NonCommercial-ShareAlike License",
        "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/"
    }
]

CATEGORIES = [
    {
        'id': 1,
        'name': 'square',
        'supercategory': 'shape',
    },
    {
        'id': 2,
        'name': 'circle',
        'supercategory': 'shape',
    },
    {
        'id': 3,
        'name': 'triangle',
        'supercategory': 'shape',
    },
]

def filter_for_jpeg(root, files):
    file_types = ['*.jpeg', '*.jpg']
    file_types = r'|'.join([fnmatch.translate(x) for x in file_types])
    files = [os.path.join(root, f) for f in files]
    files = [f for f in files if re.match(file_types, f)]
    
    return files

def filter_for_annotations(root, files, image_filename):
    file_types = ['*.png']
    file_types = r'|'.join([fnmatch.translate(x) for x in file_types])
    basename_no_extension = os.path.splitext(os.path.basename(image_filename))[0]
    file_name_prefix = basename_no_extension + '.*'
    files = [os.path.join(root, f) for f in files]
    files = [f for f in files if re.match(file_types, f)]
    files = [f for f in files if re.match(file_name_prefix, os.path.splitext(os.path.basename(f))[0])]

    return files

def main():

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    segmentation_id = 1
    
    # filter for jpeg images
    for root, _, files in os.walk(IMAGE_DIR):
        image_files = filter_for_jpeg(root, files)

        # go through each image
        for image_filename in image_files:
            image = Image.open(image_filename)
            image_info = pycococreatortools.create_image_info(
                image_id, os.path.basename(image_filename), image.size)
            coco_output["images"].append(image_info)

            # filter for associated png annotations
            for root, _, files in os.walk(ANNOTATION_DIR):
                annotation_files = filter_for_annotations(root, files, image_filename)

                # go through each associated annotation
                for annotation_filename in annotation_files:
                    
                    print(annotation_filename)
                    class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename][0]

                    category_info = {'id': class_id, 'is_crowd': 'crowd' in image_filename}
                    binary_mask = np.asarray(Image.open(annotation_filename)
                        .convert('1')).astype(np.uint8)
                    
                    annotation_info = pycococreatortools.create_annotation_info(
                        segmentation_id, image_id, category_info, binary_mask,
                        image.size, tolerance=2)

                    if annotation_info is not None:
                        coco_output["annotations"].append(annotation_info)

                    segmentation_id = segmentation_id + 1

            image_id = image_id + 1

    with open('{}/instances_shape_train2018.json'.format(ROOT_DIR), 'w') as output_json_file:
        json.dump(coco_output, output_json_file)


if __name__ == "__main__":
    main()

3、COCO数据处理工具箱(Pycocotools)

Github链接,visualizing it using the COCO API

COCO数据评估的案例代码

%matplotlib inline
import matplotlib.pyplot as plt
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
import numpy as np
import skimage.io as io
import pylab
pylab.rcParams['figure.figsize'] = (10.0, 8.0)
annType = ['segm','bbox','keypoints']
annType = annType[1]      #specify type here
prefix = 'person_keypoints' if annType=='keypoints' else 'instances'
print 'Running demo for *%s* results.'%(annType)
#initialize COCO ground truth api
dataDir='../'
dataType='val2014'
annFile = '%s/annotations/%s_%s.json'%(dataDir,prefix,dataType)
cocoGt=COCO(annFile)
#initialize COCO detections api
resFile='%s/results/%s_%s_fake%s100_results.json'
resFile = resFile%(dataDir, prefix, dataType, annType)
cocoDt=cocoGt.loadRes(resFile)

imgIds=sorted(cocoGt.getImgIds())
imgIds=imgIds[0:100]
imgId = imgIds[np.random.randint(100)]

# running evaluation
cocoEval = COCOeval(cocoGt,cocoDt,annType)
cocoEval.params.imgIds  = imgIds
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()

模型结果的评估、可视化

python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
streamlit run coco_explorer.py -- --coco_train ./coco_data/ground_truth_annotations.json --coco_predictions ./coco_data/predictions.json  --images_path ./coco_data/val2017/
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值