目標検出|MS COCO json形式のデータセット作成
概要 MS COCOはgoogleオープンソースの大規模なデータセットであり、ターゲット検出、分割、キー検出の3つのタスクに分けられ、データセットは主に画像とjsonラベルファイルから構成されている.cocoデータセットにはCOCO APIが付属しており、jsonファイルの情報読み取りが容易である.このブログでは、ターゲット検出データセットフォーマットの作成について説明します.
ステップは既存のデータラベルファイルをjson形式に変換し、jsonファイルは主に辞書形式でアクセスし、coco元データセットはinfo、licenses、images、annotations、categoriesの5つのキーワードを含み、そのうち各キーワードの内容はlistであり、listの各要素も辞書である.私たちは自分のデータセットを作成するには、images、annotations、categoriesの3つのキーワードしかありません.imagesは各図の名前とIDを格納し、annotationsは各boxの情報を格納し、categoriesはデータセットのすべてのカテゴリ情報を格納する.
コード#コード#以下は、主にCenterNetを参照する部分コードセグメントを提供する.ここでdet_dictは元のデータセットのすべての情報であり、辞書であり、各キーワードは画像名を表し、キーワード対応内容はlabel x y w h label 1 x 1 y 1 w 1 h 1... である.
COCO API coco APIの一般的な方法.
ステップ
コード#コード#
import json
import numpy as np
if __name__ == '__main__':
pascal_class_name = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
"car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike",
"person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
cat_ids = {cat: i + 1 for i, cat in enumerate(pascal_class_name)}
cat_info = []
for i, cat in enumerate(pascal_class_name):
cat_info.append({'name': cat, 'id': i + 1})
#
ret = {'images': [], 'annotations': [], "categories": cat_info}
cnt = 0
image_id = 0
for key in det_dict:
lines = det_dict[key]
image_info = {'file_name': key,
'id': int(image_id)}
# images data
ret['images'].append(image_info)
# anno data
for line in lines:
label = cat_ids[line[0]]
bbox = np.array(line[1:5], np.float)
bbox = bbox.astype(np.int).tolist()
ann = {'image_id': image_id,
'id': int(len(ret['annotations']) + 1),
'category_id': label,
'bbox': bbox,
area': bbox[2] * bbox[3],
'ignore': 0
}
ret['annotations'].append(ann)
image_id += 1
out_path = 'annotations/train.json'
json.dump(ret, open(out_path, 'w'))
COCO API
json_file = 'train.json'
img_root = ‘./’
coco = coco.COCO(json_file) # json
images = coco.getImgIds() # id
for img_id in images:
file_name = coco.loadImgs(ids=[img_id])[0]['file_name'] #
img_path = os.path.join(img_root, file_name) #
ann_ids =coco.getAnnIds(imgIds=[img_id]) # box id
anns = coco.loadAnns(ids=ann_ids) # box