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 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)

使用上面的功能,用户可以将注释文件成功转换为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 tools/train.py configs/balloon/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py

有关更详细的用法,请参阅案例1

测试与推论

要测试训练好的模型,您只需运行

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

有关更详细的用法,请参阅案例1

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mmdetection v2.25.3是一个开源的目标检测工具包,用于进行物体检测任务。根据引用,安装mmdetection v2.25.3需要满足以下条件:torch版本为1.3,并且cuda版本为10.0或者高于10.0的版本。如果使用cuda为9.0的版本,则需要降低torch的版本。具体安装步骤可以参考官方网站提供的说明。首先,可以使用以下命令克隆mmdetection资源库: git clone https://github.com/open-mmlab/mmdetection.git 然后进入mmdetection目录: cd mmdetection 根据引用,如果是手动安装,还需要在mmdetection目录下通过命令下载checkpoint文件: mkdir checkpoints cd checkpoints wget http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth 下载完成后,您就可以开始使用mmdetection v2.25.3进行目标检测任务了。 请注意,根据引用,在进行任何改动之前,建议使用命令"pip install -e ."将mmdetection安装到anaconda3的库中,以确保每次对代码的修改都能生效。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【干货】:配置环境anaconda3并安装最新版mmdetection](https://blog.csdn.net/qq_41375609/article/details/106512843)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [MMDetection的安装及验证](https://blog.csdn.net/qq_46311811/article/details/123767250)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值