MMDetection使用教程
可以使用MIM来配置MMdetion,因为上次有配MMCls的经历,所以本次配置也很快
配置文件是Openmmlab的精髓,所以要能使用合适的配置来训练模型,能取得更好的效果。
coco数据集
MMDet主要是使用coco的数据格式,所以要知道coco的文件标注格式
作业的流程
- 先对ballon数据集的标注文件改成coco的数据格式
# 开发时间 2023/2/8 18:08
import os.path as osp
import mmcv
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)
convert_balloon_to_coco('data/balloon/train/via_region_data.json', 'tarin.json', 'data/balloon/train')
- 配置模型文件,微调参数训练
_base_ = [
'../_base_/models/mask_rcnn_r50_fpn.py',
'../_base_/datasets/coco_instance.py',
'../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py'
]
# model settings
model = dict(
roi_head=dict(
bbox_head=dict(num_classes=1),
mask_head=dict(num_classes=1)))
# 修改数据集相关设置
dataset_type = 'CocoDataset'
classes = ('balloon',)
data_root = 'data/balloon'
data = dict(
train=dict(
ann_file=data_root + '/anna/tarin.json',
classes=classes,
img_prefix=data_root + '/train'),
val=dict(
ann_file=data_root + '/anna/val.json',
classes=classes,
img_prefix=data_root + '/val'),
test=dict(
ann_file=data_root + '/anna/val.json',
classes=classes,
img_prefix=data_root + '/val'))
optimizer = dict(type='SGD', lr=0.00125, momentum=0.9, weight_decay=0.0001)
checkpoint_config = dict(interval=10)
load_from = 'checkpoints/mask_rcnn_r50_fpn_2x_coco_bbox_mAP-0.392__segm_mAP-0.354_20200505_003907-3e542a40.pth'
- 在超算平台运行脚本
#!/bin/bash
module load anaconda/2021.05
module load cuda/11.3
module load gcc/7.3
source activate mmlab
export PYTHONUNBUFFERED=1
python tools/train.py configs/ahw2/mask_rcnn_r50_fpn_2x_ballon.py --work-dir work/ballon_mask
- 调用模型改变视频变成帧