MMDetection——2.快速⼊门2(中⽂官⽅⽂档)MMDetection——2.快速⼊门2(翻译版)
2:使⽤⾃定义数据集进⾏训练
在本说明中,您将知道如何使⽤⾃定义数据集来推断,测试和训练预定义模型。我们以为例来描述整个过程。
基本步骤如下:
1. 准备定制的数据集
2. 准备配置
3. 在定制的数据集上训练,测试和推理模型。
准备定制的数据集
有三种⽅法可以在MMDetection中⽀持新的数据集:
1. 将数据集重新组织为COCO格式。
2. 将数据集重新组织为中间格式。
3. 实施新的数据集。
通常,我们建议使⽤前两种⽅法,它们通常⽐第三种更容易。
在本说明中,我们提供了⼀个将数据转换为COCO格式的⽰例。
注意:MMDetection⽬前仅⽀持评估COCO格式的数据集的掩码AP。因此,例如,实时分割任务⽤户应将数据转换为可可格式。COCO注释格式
⽤于实例分割的COCO格式的必要键如下,有关完整详细信息,请参阅。
{
"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,
}]
假设我们使⽤⽓球数据集。下载数据后,我们需要实现⼀个将注释格式转换为COCO格式的功能。然后,我们可以使⽤已实现的COCODataset加载数据并进⾏训练和评估。
如果看⼀下数据集,您会发现数据集格式如下:
{'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}
注释是⼀个JSON⽂件,其中每个键都指⽰图像的所有注释。将⽓球数据集转换为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 ack_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)
使⽤上⾯的功能,⽤户可以将注释⽂件成功转换为json格式,然后我们可以使⽤它CocoDataset来训练和评估模型。
准备配置
第⼆步是准备配置,以便可以成功加载数据集。假设我们想将Mask R-CNN与FPN⼀起使⽤,则在⽓球数据集上训练检测器的配置如下。假设配置位于⽬录下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),
mask_head=dict(num_classes=1)))
# Modify dataset related settings
dataset_type = 'COCODataset'
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'
python官方文档中文版训练新模型
要使⽤新配置训练模型,只需运⾏
python tools/train.py configs/balloon/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py
有关更详细的⽤法,请参阅。
测试与推论
要测试训练好的模型,您只需运⾏
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
有关更详细的⽤法,请参阅。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。