MMDetection英文文檔翻譯---2_new_data_model自定義數(shù)據(jù)集

2: Train with customized datasets訓(xùn)練自定義數(shù)據(jù)集

In this note, you will know how to inference, test, and train predefined models with customized datasets.

在這個(gè)本章中,你將知道如何用自己的數(shù)據(jù)集訓(xùn)練、測(cè)試和預(yù)訓(xùn)練模型

We use the balloon dataset as an example to describe the whole process.

我們使用balloon dataset作為例子來(lái)描述這整個(gè)過(guò)程

The basic steps are as below:

基本步驟如下:

  1. Prepare the customized dataset準(zhǔn)備自定義的數(shù)據(jù)集

  2. Prepare a config準(zhǔn)備config文件

  3. Train, test, inference models on the customized dataset.訓(xùn)練贡歧、測(cè)試、推斷在自定義的數(shù)據(jù)集上

Prepare the customized dataset準(zhǔn)備自己的數(shù)據(jù)集

There are three ways to support a new dataset in MMDetection:

  1. reorganize the dataset into COCO format.將數(shù)據(jù)集重新組織成coco格式

  2. reorganize the dataset into a middle format.將數(shù)據(jù)集重新組織成中間的格式

  3. implement a new dataset.實(shí)現(xiàn)一個(gè)新的數(shù)據(jù)集

Usually we recommend to use the first two methods which are usually easier than the third.

In this note, we give an example for converting the data into COCO format.

通常我們建議使用前兩種方法角溃,這兩種方法通常比第三種方法簡(jiǎn)單。

在本文中篮撑,我們給出了一個(gè)將數(shù)據(jù)轉(zhuǎn)換為coco格式的示例开镣。

Note: MMDetection only supports evaluating mask AP of dataset in COCO format for now. So for instance segmentation task users should convert the data into coco format.

注意:MMDetection目前只支持對(duì)COCO格式數(shù)據(jù)集的mask AP進(jìn)行評(píng)估。

因此咽扇,例如分割任務(wù)邪财,用戶(hù)應(yīng)該將數(shù)據(jù)轉(zhuǎn)換為coco格式陕壹。

COCO annotation format coco標(biāo)注形式

The necessary keys of COCO format for instance segmentation is as below, for the complete details, please refer here.以下是實(shí)例分割所需的COCO格式,完整的細(xì)節(jié)請(qǐng)參考

>{
 "images": [image],
 "annotations": [annotation],
 "categories": [category]
}


image = {
 "id": int,
 "width": int,
 "height": int,
 "file_name": str,
}

annotation = {
 "id": int,
 "image_id": int,
 "category_id": int,
 "segmentation": RLE or [polygon],
 "area": float,
 "bbox": [x,y,width,height],
 "iscrowd": 0 or 1,
}

categories = [{
 "id": int,
 "name": str,
 "supercategory": str,
}]

Assume we use the balloon dataset.

假設(shè)我們使用balloon數(shù)據(jù)集

After downloading the data, we need to implement a function to convert the annotation format into the COCO format. Then we can use implemented COCODataset to load the data and perform training and evaluation.

下載數(shù)據(jù)之后树埠,我們需要實(shí)現(xiàn)一個(gè)函數(shù)來(lái)將annotation格式轉(zhuǎn)換為COCO格式糠馆。然后我們可以使用實(shí)現(xiàn)的COCODataset加載數(shù)據(jù),并執(zhí)行訓(xùn)練和評(píng)估怎憋。

If you take a look at the dataset, you will find the dataset format is as below:

如果你看一下數(shù)據(jù)集又碌,你會(huì)發(fā)現(xiàn)數(shù)據(jù)集的格式如下:

>{'base64_img_data': '',
 'file_attributes': {},
 'filename': '34020010494_e5cb88e1c4_k.jpg',
 'fileref': '',
 'regions': {'0': {'region_attributes': {},
 'shape_attributes': {'all_points_x': [1020,
 1000,
 994,
 1003,
 1023,
 1050,
 1089,
 1134,
 1190,
 1265,
 1321,
 1361,
 1403,
 1428,
 1442,
 1445,
 1441,
 1427,
 1400,
 1361,
 1316,
 1269,
 1228,
 1198,
 1207,
 1210,
 1190,
 1177,
 1172,
 1174,
 1170,
 1153,
 1127,
 1104,
 1061,
 1032,
 1020],
 'all_points_y': [963,
 899,
 841,
 787,
 738,
 700,
 663,
 638,
 621,
 619,
 643,
 672,
 720,
 765,
 800,
 860,
 896,
 942,
 990,
 1035,
 1079,
 1112,
 1129,
 1134,
 1144,
 1153,
 1166,
 1166,
 1150,
 1136,
 1129,
 1122,
 1112,
 1084,
 1037,
 989,
 963],
 'name': 'polygon'}}},
 'size': 1115004}

The annotation is a JSON file where each key indicates an image's all annotations. The code to convert the balloon dataset into coco format is as below.

annotation是一個(gè)JSON文件,其中每個(gè)鍵都表示圖像的所有annotation绊袋。

將 balloon 數(shù)據(jù)集轉(zhuǎn)換為coco格式的代碼如下所示毕匀。

>import os.path as osp

def convert_balloon_to_coco(ann_file, out_file, image_prefix):
 data_infos = mmcv.load(ann_file)

 annotations = []
 images = []
 obj_count = 0
 for idx, v in enumerate(mmcv.track_iter_progress(data_infos.values())):
 filename = v['filename']
 img_path = osp.join(image_prefix, filename)
 height, width = mmcv.imread(img_path).shape[:2]

 images.append(dict(
 id=idx,
 file_name=filename,
 height=height,
 width=width))

 bboxes = []
 labels = []
 masks = []
 for _, obj in v['regions'].items():
 assert not obj['region_attributes']
 obj = obj['shape_attributes']
 px = obj['all_points_x']
 py = obj['all_points_y']
 poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
 poly = [p for x in poly for p in x]

 x_min, y_min, x_max, y_max = (
 min(px), min(py), max(px), max(py))


 data_anno = dict(
 image_id=idx,
 id=obj_count,
 category_id=0,
 bbox=[x_min, y_min, x_max - x_min, y_max - y_min],
 area=(x_max - x_min) * (y_max - y_min),
 segmentation=[poly],
 iscrowd=0)
 annotations.append(data_anno)
 obj_count += 1

 coco_format_json = dict(
 images=images,
 annotations=annotations,
 categories=[{'id':0, 'name': 'balloon'}])
 mmcv.dump(coco_format_json, out_file)

Using the function above, users can successfully convert the annotation file into json format, then we can use CocoDataset to train and evaluate the model.

通過(guò)上面的函數(shù),用戶(hù)可以成功地將annotation文件轉(zhuǎn)換成json格式癌别,然后我們可以使用‘CocoDataset’對(duì)模型進(jìn)行訓(xùn)練和評(píng)估皂岔。

Prepare a config 準(zhǔn)備一個(gè)config

The second step is to prepare a config thus the dataset could be successfully loaded. Assume that we want to use Mask R-CNN with FPN, the config to train the detector on balloon dataset is as below. Assume the config is under directory configs/balloon/ and named as mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py, the config is as below.

第二步是準(zhǔn)備一個(gè)config,這樣數(shù)據(jù)集就可以成功加載展姐。假設(shè)我們想使用帶有FPN的Mask R-CNN躁垛,在balloon數(shù)據(jù)集上訓(xùn)練detector的配置如下。假設(shè)配置在' configs/balloon/ '目錄下圾笨,命名為' mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py '教馆,配置如下所示。

># The new config inherits a base config to highlight the necessary modification
_base_ = 'mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_coco.py'

# We also need to change the num_classes in head to match the dataset's annotation
model = dict(
 roi_head=dict(
 bbox_head=dict(num_classes=1),#這里的num_classes需要根據(jù)你的自定義數(shù)據(jù)集類(lèi)別來(lái)更改
 mask_head=dict(num_classes=1)))

# Modify dataset related settings
dataset_type = 'COCODataset'#更改成你自定義的數(shù)據(jù)集的路徑
classes = ('balloon',)
data = dict(
 train=dict(
 img_prefix='balloon/train/',
 classes=classes,
 ann_file='balloon/train/annotation_coco.json'),
 val=dict(
 img_prefix='balloon/val/',
 classes=classes,
 ann_file='balloon/val/annotation_coco.json'),
 test=dict(
 img_prefix='balloon/val/',
 classes=classes,
 ann_file='balloon/val/annotation_coco.json'))

# We can use the pre-trained Mask RCNN model to obtain higher performance
load_from = 'checkpoints/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth'
#選擇一個(gè)已經(jīng)訓(xùn)練好的模型來(lái)訓(xùn)練你自定義的數(shù)據(jù)集

Train a new model 訓(xùn)練一個(gè)新的模型

To train a model with the new config, you can simply run要用新的config訓(xùn)練模型擂达,您可以簡(jiǎn)單地運(yùn)行

>python tools/train.py configs/balloon/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py

For more detailed usages, please refer to the Case 1.

Test and inference測(cè)試和推理

To test the trained model, you can simply run要測(cè)試訓(xùn)練過(guò)的模型土铺,您可以簡(jiǎn)單地運(yùn)行

>python tools/test.py configs/balloon/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py work_dirs/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py/latest.pth --eval bbox segm

For more detailed usages, please refer to the Case 1.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市板鬓,隨后出現(xiàn)的幾起案子悲敷,更是在濱河造成了極大的恐慌,老刑警劉巖穗熬,帶你破解...
    沈念sama閱讀 207,113評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件镀迂,死亡現(xiàn)場(chǎng)離奇詭異丁溅,居然都是意外死亡唤蔗,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門(mén)窟赏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)妓柜,“玉大人,你說(shuō)我怎么就攤上這事涯穷」髌” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,340評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵拷况,是天一觀的道長(zhǎng)作煌。 經(jīng)常有香客問(wèn)我掘殴,道長(zhǎng),這世上最難降的妖魔是什么粟誓? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,449評(píng)論 1 279
  • 正文 為了忘掉前任奏寨,我火速辦了婚禮,結(jié)果婚禮上鹰服,老公的妹妹穿的比我還像新娘病瞳。我一直安慰自己,他們只是感情好悲酷,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,445評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布套菜。 她就那樣靜靜地躺著,像睡著了一般设易。 火紅的嫁衣襯著肌膚如雪逗柴。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,166評(píng)論 1 284
  • 那天亡嫌,我揣著相機(jī)與錄音嚎于,去河邊找鬼。 笑死挟冠,一個(gè)胖子當(dāng)著我的面吹牛于购,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播知染,決...
    沈念sama閱讀 38,442評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼肋僧,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了控淡?” 一聲冷哼從身側(cè)響起嫌吠,我...
    開(kāi)封第一講書(shū)人閱讀 37,105評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎掺炭,沒(méi)想到半個(gè)月后辫诅,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,601評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡涧狮,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,066評(píng)論 2 325
  • 正文 我和宋清朗相戀三年炕矮,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片者冤。...
    茶點(diǎn)故事閱讀 38,161評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡肤视,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出涉枫,到底是詐尸還是另有隱情邢滑,我是刑警寧澤,帶...
    沈念sama閱讀 33,792評(píng)論 4 323
  • 正文 年R本政府宣布愿汰,位于F島的核電站困后,受9級(jí)特大地震影響乐纸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜摇予,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,351評(píng)論 3 307
  • 文/蒙蒙 一锯仪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧趾盐,春花似錦庶喜、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,352評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至本缠,卻和暖如春斥扛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背丹锹。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,584評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工稀颁, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人楣黍。 一個(gè)月前我還...
    沈念sama閱讀 45,618評(píng)論 2 355
  • 正文 我出身青樓匾灶,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親租漂。 傳聞我的和親對(duì)象是個(gè)殘疾皇子阶女,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,916評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容