Microsoft Common Objects in Context
(简写COCO
)数据集是微软团队提供的一个可以用来进行图像识别,分割,注解等开发工作的数据集。
其官方说明网址:http://mscoco.org/。
该数据集主要有的特点如下:
(1)Object segmentation
(2)Recognition in Context
(3)Multiple objects per image
(4)More than 300,000 images
(5)More than 2 Million instances
(6)80 object categories
(7)5 captions per image
(8)Keypoints on 100,000 people
为了更好的介绍这个数据集,微软在ECCV Workshops
里发表这篇文章:Microsoft COCO: Common Objects in Context。从这篇文章中,我们了解了这个数据集以物体识别为目标,主要从复杂的日常场景中截取,图像中的目标通过精确的标定。图像包括91
类目标,328,000
张图片和2,500,000
个标签。
下面,我们以2014
年的数据集合,来简单介绍一下这个数据集在Python
环境中的使用。
首先是去下载需要的数据集,下载地址为http://mscoco.org/dataset/#download,页面内容如下所示:
对于我们此次的测试来说,只需要下载2014 Training images [80K/13GB],2014 Train/Val object instances [158MB]即可。
建议使用aria2
去下载,执行命令如下:
$ sudo apt-get install aria2
$ cd ~
$ mkdir coco
$ cd coco
$ aria2c -c http://msvocds.blob.core.windows.net/coco2014/train2014.zip
$ aria2c -c http://msvocds.blob.core.windows.net/annotations-1-0-3/instances_train-val2014.zip
下载完成后,解压缩到当前目录
$ sudo apt-get install unzip
$ unzip instances_train-val2014.zip
$ unzip train2014.zip
接下来,去下载源代码COCO API
的源代码
$ sudo apt-get install git
$ git clone https://github.com/pdollar/coco.git
$ cd coco/PythonAPI
$ make
$ sudo make install
$ sudo python setup.py install
$ cd ../..
参考https://github.com/pdollar/coco/blob/master/PythonAPI/pycocoDemo.ipynb里面的例子。
我们写如下的测试例子:
testCoCo.py
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'] = (10.0, 8.0)
dataDir='.'
dataType='train2014'
annFile='%s/annotations/instances_%s.json'%(dataDir,dataType)
# initialize COCO api for instance annotations
coco=COCO(annFile)
# display COCO categories and supercategories
cats = coco.loadCats(coco.getCatIds())
nms=[cat['name'] for cat in cats]
print 'COCO categories: \n\n', ' '.join(nms)
nms = set([cat['supercategory'] for cat in cats])
print 'COCO supercategories: \n', ' '.join(nms)
# get all images containing given categories, select one at random
catIds = coco.getCatIds(catNms=['person','dog','skateboard']);
imgIds = coco.getImgIds(catIds=catIds );
img = coco.loadImgs(imgIds[np.random.randint(0,len(imgIds))])[0]
# load and display image
I = io.imread('%s/%s/%s'%(dataDir,dataType,img['file_name']))
plt.figure(); plt.axis('off')
plt.imshow(I)
plt.show()
# 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)
plt.show()
执行脚本的代码如下:
$ cd ~/coco
$ python testCoco.py
Reference:
https://www.mobibrw.com/2017/7974
https://blog.csdn.net/daniaokuye/article/details/78699138