日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

COCO 数据集的使用

發布時間:2025/3/20 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 COCO 数据集的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • Windows 10 編譯 Pycocotools 踩坑記

  • COCO數據庫簡介

微軟發布的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 toothbrushCOCO 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.

  • GitHub 展示

你也可以在線編輯:https://mybinder.org/v2/gh/q735613050/dataLoader/master

探尋有趣之事!


博文更新見:COCO 數據集的使用:https://www.cnblogs.com/q735613050/p/8969452.html

總結

以上是生活随笔為你收集整理的COCO 数据集的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。