Study: Artificial Intelligence(AI)/AI: Data Pipeline

[Dataset] Object Detection/Segmentation Open Dataset: COCO Dataset

DrawingProcess 2024. 5. 9. 09:27
๋ฐ˜์‘ํ˜•
๐Ÿ’ก ๋ณธ ๋ฌธ์„œ๋Š” '[Dataset] Object Detection/Segmentation Open Dataset: COCO Dataset '์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋†“์€ ๊ธ€์ž…๋‹ˆ๋‹ค.
Object Detection/Segmentation Task๋ฅผ ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๊ฐ€์žฅ ๊ฐ€๋ณธ์ ์œผ๋กœ ์•Œ์•„์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ์…‹์ธ COCO ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•ด ํ™œ์šฉ๋ฐฉ๋ฒ•๊นŒ์ง€ ์ •๋ฆฌํ•˜์˜€์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

1. COCO ๋ฐ์ดํ„ฐ์…‹

COCO ๋ฐ์ดํ„ฐ์…‹ ๊ตฌ์กฐ

COCO ๋ฐ์ดํ„ฐ์…‹์˜ annotation์€ json ํ˜•ํƒœ๋กœ ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•„์ˆ˜์ ์ธ ํ‚ค๋ฅผ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

'images': [
    {
        'file_name': 'COCO_val2014_000000001268.jpg',
        'height': 427,
        'width': 640,
        'id': 1268
    },
    ...
],

'annotations': [
    {
        'segmentation': [[192.81,
            247.09,
            ...
            219.03,
            249.06]],  # if you have mask labels
        'area': 1035.749,
        'iscrowd': 0,
        'image_id': 1268,
        'bbox': [192.81, 224.8, 74.73, 33.43],
        'category_id': 16,
        'id': 42986
    },
    ...
],

'categories': [
    {'id': 0, 'name': 'car'},
 ]

There are three necessary keys in the json file:

  • images: contains a list of images with their informations like file_name, height, width, and id.
  • annotations: contains the list of instance annotations.
  • categories: contains the list of categories names and their ID.

After the data pre-processing, there are two steps for users to train the customized new dataset with existing format (e.g. COCO format):

  1. Modify the config file for using the customized dataset.
  2. Check the annotations of the customized dataset.

Here we give an example to show the above two steps, which uses a customized dataset of 5 classes with COCO format to train an existing Cascade MaskRCNN R50 FPN detector.

2. COCO ๋ฐ์ดํ„ฐ์…‹ ๋‹ค์šด๋กœ๋“œ

๋ณดํ†ต, coco dataset์€ ์•„๋ž˜ ๋งํฌ์—์„œ ๋‹ค์šด๋กœ๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค๋งŒ, ์•„๋ฌด๋ฆฌ ๋ฐ์ดํ„ฐ์…‹์„ ํด๋ฆญํ•ด๋„ ๋‹ค์šด๋กœ๋“œ๊ฐ€ ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

wget์„ ํ†ตํ•ด ๋‹ค์šด๋กœ๋“œ ํ•˜๋ คํ•ด๋„ ๋งํฌ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ๋Š”์ง€ ๋‹ค์šด๋กœ๋“œ ๋˜์ง€ ์•Š์•˜๊ณ , ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฝ๋กœ๊ฐ€ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค.

# images
wget http://images.cocodataset.org/zips/train2017.zip   # train dataset
wget http://images.cocodataset.org/zips/val2017.zip     # validation dataset
wget http://images.cocodataset.org/zips/test2017.zip    # test dataset
wget http://images.cocodataset.org/zips/unlabeled2017.zip

# annotations
wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip
wget http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip
wget http://images.cocodataset.org/annotations/image_info_test2017.zip
wget http://images.cocodataset.org/annotations/image_info_unlabeled2017.zip

์œ„ ๊ฒฝ๋กœ๋“ค์—์„œ ์ด๋ฏธ์ง€๋‚˜ annotation ๋ชจ๋‘ train2017.zip ๋ถ€๋ถ„์„ ์›ํ•˜๋Š” dataset์œผ๋กœ ๋ณ€๊ฒฝ(ex. train2015)ํ•˜์—ฌ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€๋กœ annotations_trainval2017.zip ํŒŒ์ผ์„ ๋‹ค์šด๋ฐ›๊ณ  unzip์„ ํ•˜๊ฒŒ ๋˜๋ฉด annotation ํŒŒ์ผ๋“ค์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. COCO API ์‚ฌ์šฉํ•˜๊ธฐ

๋‹ค์šด๋ฐ›์€ coco dataset์€ api๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ python์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ฏธ์ง€๋ฅผ ์‹œ๊ฐํ™” ํ•˜๊ณ  annotation ์ •๋ณด๊นŒ์ง€ ์‹œ๊ฐํ™” ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1) coco api ์ดˆ๊ธฐํ™”

%matplotlib inline
from pycocotools.coco import COCO
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
pylab.rcParams['figure.figsize'] = (8.0, 10.0)

dataDir='..'
dataType='val2017'

# initialize COCO api for person keypoints annotations
annFile = '{}/annotations/person_keypoints_{}.json'.format(dataDir,dataType)
coco=COCO(annFile)

2) COCO categories and supercategories ์ถœ๋ ฅ ํ•ด ๋ณด๊ธฐ

cats = coco.loadCats(coco.getCatIds())
nms=[cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))

nms = set([cat['supercategory'] for cat in cats])
print('COCO supercategories: \n{}'.format(' '.join(nms)))

3) ์ด๋ฏธ์ง€ ์‹œ๊ฐํ™”

# get all images containing given categories, select one at random
catIds = coco.getCatIds(catNms=['person','dog','skateboard']);
imgIds = coco.getImgIds(catIds=catIds );
imgIds = coco.getImgIds(imgIds = [324158])
img = coco.loadImgs(imgIds[np.random.randint(0,len(imgIds))])[0]

# load and display image
# I = io.imread('%s/images/%s/%s'%(dataDir,dataType,img['file_name']))
# use url to load image
I = io.imread(img['coco_url'])
plt.axis('off')
plt.imshow(I)
plt.show()

 

4) annotation ์‹œ๊ฐํ™”

# load and display keypoints annotations
plt.imshow(I); plt.axis('off')
ax = plt.gca()
annIds = coco_kps.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco_kps.loadAnns(annIds)
coco_kps.showAnns(anns)

์ฐธ๊ณ 

๋ฐ˜์‘ํ˜•