轉於:https://www.cnblogs.com/q735613050/p/8969452.html
一、下載鏈接
[1] - train2014 images: (13GB)
http://images.cocodataset.org/zips/train2014.zip
[2] - val2014 images:(6GB)
http://images.cocodataset.org/zips/val2014.zip
[3] - train2014/val2014 annotations:(241MB)
http://images.cocodataset.org/annotations/annotations_trainval2014.zip
[4] - test2014 images: (12GB)
http://images.cocodataset.org/zips/test2014.zip
[5] - test2015 images: (12GB)
http://images.cocodataset.org/zips/test2015.zip
[6] - train2017 images: (18GB)
http://images.cocodataset.org/zips/train2017.zip
[7] - val2017 images: (1GB)
http://images.cocodataset.org/zips/val2017.zip
[8] - train2017/val2017 annotations: (241MB)
http://images.cocodataset.org/annotations/annotations_trainval2017.zip
[9] - stuff train2017/val2017 annotations: (1.1GB)
http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip
[10] - test2017 images: (6GB)
http://images.cocodataset.org/zips/test2017.zip
[11] - Panoptic train2017/val2017 annotations: (821MB)
http://images.cocodataset.org/annotations/panoptic_annotations_trainval2017.zip
[12] - test2017 images: (6GB)
http://images.cocodataset.org/zips/test2017.zip
[13] - Unlabeled2017 images: (19GB)
http://images.cocodataset.org/zips/unlabeled2017.zip
[14]-info_test image:(1MB)
http://images.cocodataset.org/annotations/image_info_test2017.zip
二、使用
1.Note:
- 在COCO數據集評價指標中,所有的AP 默認爲mAP 。即,AP50=mAP50,AP75=mAP75,以此類推。
- AP50一定大於AP75
- 在更早期的數據集VOC上,數據量更少,評價指標也更簡單,爲 mAP ,即相當於COCO數據集上的 AP50這一單項指標。
- COCO數據集出來後,對檢測算法性能的評價指標變得多樣化,也更加客觀全面了。
2.微軟發佈的COCO數據庫, 除了圖片以外還提供物體檢測, 分割(segmentation)和對圖像的語義文本描述信息.
COCO數據庫的網址是:
- MS COCO API - http://mscoco.org/
- Github網址 - https://github.com/pdollar/coco
- 關於API更多的細節在網站: http://mscoco.org/dataset/#download
數據庫提供 Matlab, Python 和 Lua 的 API 接口. 其中 matlab 和 python 的 API 接口可以提供完整的圖像標籤數據的加載, parsing 和可視化.此外,網站還提供了數據相關的文章, 教程等.
在使用 COCO 數據庫提供的 API 和 demo 時, 需要首先下載 COCO 的圖像和標籤數據.
- 安裝:
- 首先解壓數據文件:
- 圖像數據下載到
coco/images/
文件夾中 - 標籤數據下載到
coco/
文件夾中.
- 圖像數據下載到
- matlab, 在 matlab 的默認路徑中添加
coco/MatlabApi
- Python. 打開終端,將路徑切換到
coco/PythonAPI
下,輸入make
- 首先解壓數據文件:
- COCO數據集的標註信息
COCO的數據標註信息包括:
- 類別標誌
- 類別數量區分
- 像素級的分割
import sys
sys.path.append('E:/xinlib')
from data import cocox
import zipfile
查看 coco/images/
文件夾下的數據:
image_names = cocox.get_image_names()
image_names
['E:/Data/coco/images/test2017.zip',
'E:/Data/coco/images/train2017.zip',
'E:/Data/coco/images/unlabeled2017.zip',
'E:/Data/coco/images/val2017.zip']
查看 coco/
文件夾的文件:
import os
dataDir = cocox.root
os.listdir(dataDir)
['annotations',
'annotations_trainval2017.zip',
'cocoapi',
'images',
'image_info_test2017.zip',
'image_info_unlabeled2017.zip',
'stuff_annotations_trainval2017.zip']
我們只需要獲取 annotations 的信息(這裏都是以 .zip
結尾):
annDir = [z_name for z_name in os.listdir(dataDir) if z_name.endswith('.zip')]
annDir
['annotations_trainval2017.zip',
'image_info_test2017.zip',
'image_info_unlabeled2017.zip',
'stuff_annotations_trainval2017.zip']
解壓 annotations 的文件:
for ann_name in annDir:
z = zipfile.ZipFile(dataDir + '/' + ann_name)
# 全部解壓
z.extractall(dataDir)
# 封裝爲函數
cocox.unzip_annotations()
# 刪除標籤的壓縮文件
cocox.del_annotations()
由於圖片數據比較大,我就不解壓了,不過可以通過 MXNet + zipfile
來直接獲取圖片信息。
獲取圖片數據
我以 test2017.zip
爲例:
image_names
['E:/Data/coco/images/test2017.zip',
'E:/Data/coco/images/train2017.zip',
'E:/Data/coco/images/unlabeled2017.zip',
'E:/Data/coco/images/val2017.zip']
z = zipfile.ZipFile(image_names[0])
# 測試集的圖片名稱列表
z.namelist()
['test2017/',
'test2017/000000259564.jpg',
'test2017/000000344475.jpg',
...]
我們可以看出,第一個是目錄名,之後的纔是圖片。下面我們來看看第一張圖片:
from mxnet import image
r = z.read(z.namelist()[1]) # bytes
data = image.imdecode(r) # 轉換爲 NDArray 數組,可以做數值運算
data
[[[ 87 94 78]
[ 85 94 77]
[ 87 96 79]
...,
[108 63 44]
[252 244 233]
[253 253 253]]
[[ 86 95 76]
[ 88 97 78]
[ 85 94 75]
...,
[ 55 14 0]
[150 94 81]
[252 245 216]]
[[ 90 99 78]
[ 89 98 77]
[ 89 98 77]
...,
[ 63 37 12]
[ 90 30 6]
[149 83 61]]
...,
[[ 86 104 82]
[ 89 102 82]
[ 84 102 80]
...,
[ 50 62 40]
[ 50 61 45]
[ 51 58 50]]
[[ 89 101 77]
[ 87 96 75]
[ 89 104 83]
...,
[ 54 63 42]
[ 49 53 39]
[ 53 54 48]]
[[ 96 100 77]
[ 94 97 76]
[ 88 103 82]
...,
[ 44 58 32]
[ 45 57 37]
[ 49 57 42]]]
<NDArray 480x640x3 @cpu(0)>
x = data.asnumpy() # 轉換爲 array
# 顯示圖片
%pylab inline
plt.imshow(x)
爲此,我們可以將其封裝爲一個迭代器:cocox.data_iter(dataType)
獲取標籤信息(利用官方給定教程)
- 安裝 python API:
pip install -U pycocotools
Windows (一般需要安裝 visual studio)下有許多的坑:Windows 10 編譯 Pycocotools 踩坑記
%pylab 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)
這裏有一個坑 (由 PIL 引發) import skimage.io as io
在 Windows 下可能會報錯,我的解決辦法是:
-
先卸載 Pillow,然後重新安裝即可。
-
插曲:PIL(Python Imaging Library)是Python一個強大方便的圖像處理庫,名氣也比較大。Pillow 是 PIL 的一個派生分支,但如今已經發展成爲比 PIL 本身更具活力的圖像處理庫。
dataDir = cocox.root
dataType = 'val2017'
annFile = '{}/annotations/instances_{}.json'.format(dataDir, dataType)
# initialize COCO api for instance annotations
coco=COCO(annFile)
loading annotations into memory...
Done (t=0.93s)
creating index...
index created!
COCO??
COCO
是一個類:
Constructor of Microsoft COCO helper class for reading and visualizing annotations.
:param annotation_file (str): location of annotation file
:param image_folder (str): location to the folder that hosts images.
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:
appliance sports person indoor vehicle food electronic furniture animal outdoor accessory kitchen
# 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=[335328])
img = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
img
{'license': 4,
'file_name': '000000335328.jpg',
'coco_url': 'http://images.cocodataset.org/val2017/000000335328.jpg',
'height': 640,
'width': 512,
'date_captured': '2013-11-20 19:29:37',
'flickr_url': 'http://farm3.staticflickr.com/2079/2128089396_ddd988a59a_z.jpg',
'id': 335328}
官方給的這個代碼需要將圖片數據集解壓:
# load and display image
# use url to load image
# I = io.imread(img['coco_url'])
I = io.imread('%s/images/%s/%s' % (dataDir, dataType, img['file_name']))
plt.axis('off')
plt.imshow(I)
plt.show()
我們可以使用 zipfile
模塊直接讀取圖片,而無須解壓:
image_names[-1]
'E:/Data/coco/images/val2017.zip'
val_z = zipfile.ZipFile(image_names[-1])
I = image.imdecode(val_z.read('%s/%s' % (dataType, img['file_name']))).asnumpy()
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)
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.43s)
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)
initialize COCO api for caption annotations
annFile = '{}/annotations/captions_{}.json'.format(dataDir, dataType)
coco_caps = COCO(annFile)
loading annotations into memory...
Done (t=0.06s)
creating index...
index created!
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 couple of people riding waves on top of boards.
a couple of people that are surfing in water
A man and a young child in wet suits surfing in the ocean.
a man and small child standing on a surf board and riding some waves
A young boy on a surfboard being taught to surf.
你也可以在線編輯:https://mybinder.org/v2/gh/q735613050/dataLoader/master