COCO Dataset : cocodataset.org/#home
COCO API : github.com/cocodataset/cocoapi
COCO API 사용 예제 : github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoDemo.ipynb
COCO API 를 이용하면 COCO Dataset 을 수월하게 다룰 수 있다.
실제로 많은 프로젝트에서 COCO API를 이용하거나 약간 수정하여 데이터 세트를 다룬다.
COCO 데이터는 아래와 같이 다운 받을 수 있다.
(2021.07.23) 다운로드 경로가 변경되어 경로를 수정하였음
mkdir coco
cd coco
mkdir images
cd images
wget http://images.cocodataset.org/zips/train2017.zip
wget http://images.cocodataset.org/zips/val2017.zip
wget http://images.cocodataset.org/zips/test2017.zip
wget http://images.cocodataset.org/zips/unlabeled2017.zip
unzip train2017.zip
unzip val2017.zip
unzip test2017.zip
unzip unlabeled2017.zip
rm train2017.zip
rm val2017.zip
rm test2017.zip
rm unlabeled2017.zip
cd ../
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
unzip annotations_trainval2017.zip
unzip stuff_annotations_trainval2017.zip
unzip image_info_test2017.zip
unzip image_info_unlabeled2017.zip
rm annotations_trainval2017.zip
rm stuff_annotations_trainval2017.zip
rm image_info_test2017.zip
rm image_info_unlabeled2017.zip
데이터 세트를 받으면 아래와 같이 구성되어있다.
$ ls
000000000139.jpg 000000098018.jpg 000000190307.jpg 000000289702.jpg
000000384666.jpg 000000481413.jpg 000000000285.jpg 000000098261.jpg
000000190637.jpg 000000289741.jpg 000000384670.jpg 000000481480.jpg
...
$ ls annotations
captions_train2017.json instances_train2017.json person_keypoints_train2017.json
captions_val2017.json instances_val2017.json person_keypoints_val2017.json
사용법은 아래와 같다. (Jupyter notebook)
1. COCO Dataset 사용 준비하기
필요한 패키지 import 및 .json 파일 불러오기
%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'
annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType)
# initialize COCO api for instance annotations
coco=COCO(annFile)
위와 같이 설정해주면 아래와 같이 COCO API가 초기화된다.
loading annotations into memory...
Done (t=0.81s)
creating index...
index created!
2. COCO 카테고리 불러오기
# display 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)))
아래와 같이 카테고리들이 모두 출력된다.
COCO categories:
person bicycle car motorcycle airplane bus train truck boat traffic light
fire hydrant stop sign parking meter bench bird cat dog horse sheep cow
elephant bear zebra giraffe backpack umbrella handbag tie suitcase frisbee
skis snowboard sports ball kite baseball bat baseball glove skateboard
surfboard tennis racket bottle wine glass cup fork knife spoon bowl banana
apple sandwich orange broccoli carrot hot dog pizza donut cake chair couch
potted plant bed dining table toilet tv laptop mouse remote keyboard cell phone
microwave oven toaster sink refrigerator book clock vase scissors teddy bear
hair drier toothbrush
COCO supercategories:
outdoor food indoor appliance sports person animal vehicle furniture accessory electronic kitchen
supercategories 가 있는줄은 지금 알았다 ^^;;
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. showAnns 을 이용하여 annotations 정보 불러와서 결과 확인하기
# load and display instance annotations
plt.imshow(I); plt.axis('off')
annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)
5. 키포인트 정보 불러오기 (person_keypoints_~~~.json)
# initialize COCO api for person keypoints annotations
annFile = '{}/annotations/person_keypoints_{}.json'.format(dataDir,dataType)
coco_kps=COCO(annFile)
위와 같이 설정해주면, 아래와 같이 초기화가 된다.
loading annotations into memory...
Done (t=0.58s)
creating index...
index created!
아래와 같이 결과 확인해보기
# 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)
참고로 키포인트 데이터에서 COCO 2017 train 데이터는 COCO 2014 trainval 데이터와 같고
COCO 2017 val 데이터는 COCO 2014 minival 데이터와 같다고 한다.
6. 캡션 정보 불러오기
# initialize COCO api for caption annotations
annFile = '{}/annotations/captions_{}.json'.format(dataDir,dataType)
coco_caps=COCO(annFile)
# load and display caption annotations
annIds = coco_caps.getAnnIds(imgIds=img['id']);
anns = coco_caps.loadAnns(annIds)
coco_caps.showAnns(anns)
plt.imshow(I); plt.axis('off'); plt.show()
위와 같이 설정해주면 아래와 같이 캡션 정보를 출력해볼 수 있다.
A man is skate boarding down a path and a dog is running by his side.
A man on a skateboard with a dog outside.
A person riding a skate board with a dog following beside.
This man is riding a skateboard behind a dog.
A man walking his dog on a quiet country road.
참고자료 1 : ukayzm.github.io/cocodataset/
참고자료 2 : github.com/mks0601/TF-SimpleHumanPose/issues/4
참고자료 3 : https://gist.github.com/mkocabas/a6177fc00315403d31572e17700d7fd9
'AI Research Topic > Dataset' 카테고리의 다른 글
[Dataset Augmentation] albumentations (0) | 2023.04.26 |
---|---|
[Dataset] MS COCO 데이터를 쉽게 이용할 수 있는 FiftyOne 사용하기 (0) | 2021.08.10 |
[Deep Learning] 딥러닝에서 Synthetic Dataset 을 이용하여 학습하는 연구들 (0) | 2021.04.28 |
[Dataset] 이미지 인식에 유용한 데이터셋 정리 (2020.09.14) (0) | 2020.09.14 |
[Dataset] MCL DATASETFOR VIDEO SALIENCY DETECTION (0) | 2020.09.11 |