mmdetection-v2.3安装配置及训练自定义数据集

29 篇文章 1 订阅
16 篇文章 5 订阅

系统:Ubuntu18.04
python:3.6.9


2020.12.30 Update
目标mmdetection版本(2.7.0)以及mmcv-full(1.2.4)已经可以在Windows环境安装了(CUDA11, VS 2019, Pytorch1.7.1),编译安装mmdetection前需要先安装mmcv-full:

pip install -U mmcv-full

mmdetection支持非常多的目标检测模型,从经典的Faster RCNN、SSD等,到最新的DetectoRS,还有灵活多变的配置文件,使其成为了目前最流行的目标检测框架之一。项目开发也非常活跃,这对我们用户来说是好事,这意味着拥有更稳定的版本迭代。本文主要记录使用mmdetection2.3版本训练自定义数据集的关键流程,有些细节已经省略,大家酌情参考。

  1. 安装mmcv-full。mmdetection依赖于mmcv,并且要安装pytorch对应的版本,就像torchvision一样,各版本下载链接在https://openmmlab.oss-accelerate.aliyuncs.com/mmcv/dist/index.html

  2. 源码安装mmdetection。具体安装步骤为:

    git clone https://github.com/open-mmlab/mmdetection.git
    cd mmdetection
    pip install -r requirements/build.txt
    pip install -v -e .  # or "python setup.py develop"
    

    mmdetection2.3版本中关于CUDA/C++ 的操作子放到了mmcv.ops中,因此安装过程很快,没有之前的编译步骤;

  3. 添加自定义数据集加载接口。对于目标检测问题,常见的数据集形式有PASCAL VOC、COCO和YOLO格式,其中COCO格式可以使用cocoapi进行评估,指标丰富,目前大部分论文的指标也是用的coco指标,因此建议使用COCO格式数据集,如果是其他格式的可以通过脚本进行转换,不同格式之间的转换脚本在https://github.com/ouening/OD_dataset_conversion_scripts找到。添加自定义数据集加载接口,可以在mmdet/datasets文件夹下新建一个文件,例如cotterpin.py,编写内容为:

    from .coco import CocoDataset
    from .builder import DATASETS
    
    @DATASETS.register_module
    class CotterpinDataset(CocoDataset):
        '''这里的cotterpin数据集采用COCO格式,因此直接继承CocoDataset即可'''
        CLASSES = ('lost', 'normal') # 修改数据集类别
    

    最后要在mmdet/datasets/__init__.py文件中注册:

    ...
    from .cotterpin import CotterpinDataset
    __all__ = [...
        'CotterpinDataset'
    ]
    
  4. 添加数据集。前面步骤只是提供了调用的函数API,这一步骤将指定数据集路径等相关信息。在configs/_base_/datasets文件夹下新建cotterpin_coco_detection.py文件,修改内容:

    dataset_type = 'CotterpinDataset'
    data_root = 'data/cotterpin/' # 自定义coco格式数据集路径
    classes = ('lost','normal') # 如果出现类别数量不匹配的错误,人为指定数据集的类别,并且在train、val、test中传入参数classes
    img_norm_cfg = dict(
        mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
    train_pipeline = [
        ...
    ]
    test_pipeline = [
        ...
    ]
    data = dict(
        samples_per_gpu=2,
        workers_per_gpu=2,
        train=dict(
            type=dataset_type,
            classes=classes, # 传入类别参数
            ann_file=data_root + 'annotations/instances_train.json',
            img_prefix=data_root + 'train/',
            pipeline=train_pipeline),
        val=dict(
            type=dataset_type,
            classes=classes,
            ann_file=data_root + 'annotations/instances_val.json',
            img_prefix=data_root + 'val/',
            pipeline=test_pipeline),
        test=dict(
            type=dataset_type,
            classes=classes,
            ann_file=data_root + 'annotations/instances_test.json',
            img_prefix=data_root + 'test/',
            pipeline=test_pipeline))
    evaluation = dict(metric=['bbox', 'segm'])
    

    自定义数据集文件组织形式类似于:

在这里插入图片描述

其实在这一步中也可以指定:

dataset_type = 'CocoDataset'
data_root = 'data/cotterpin/' # 自定义coco格式数据集路径
classes = ('lost','normal') # 如果出现类别数量不匹配的错误,人为指定数据集的类别,并且在train、val、test中传入参数classes

单独写一个数据集接口CotterpinDataset主要是为了方便维护。

  1. 修改训练配置文件。以retinanet为例,在configs/retinanet 创建文件cotterpin_retinanet_r50_fpn_1x_coco.py ,内容如下:

    _base_ = [
        '../_base_/models/retinanet_r50_fpn.py',
        '../_base_/datasets/cotterpin_coco_detection.py',
        '../_base_/schedules/schedule_cotterpin.py', 
        '../_base_/default_runtime.py'
    ]
    # optimizer
    optimizer = dict(type='Adam', lr=0.0001, weight_decay=0.0001)
    # runtime setup
    checkpoint_config = dict(interval=1)
    # yapf:disable
    log_config = dict(
        interval=200,
        hooks=[
            dict(type='TextLoggerHook'),
            dict(type='TensorboardLoggerHook')
        ])
    # yapf:enable
    dist_params = dict(backend='nccl')
    log_level = 'INFO'
    load_from = None
    
    work_dir = './work_dirs/cotterpin_retiannet_res50_no_pre'
    workflow = [('train', 1)]
    
    # model
    model = dict(
        pretrained=None, # no pretrained weight
    )
    

    就是和模型结构,训练使用优化器,日志文件记录等有关的配置,_base_/schedules/schedule_cotterpin.py记录optimizer和学习率策略,可以参考_base_/schedules/schedule_1x.py,当然这些可以设置可以统一写在cotterpin_retinanet_r50_fpn_1x_coco.py里面。

  2. 编写训练脚本。为了方便训练,我们可以编写一个bash脚本,这样就不用每次训练的时候在控制台输入很长的命令。例如命名为train_retinanet.sh,脚本内容如下:

    #!/bin/bash
    CUDA_VISIBLE_DEVICES=1 python3 tools/train.py configs/retinanet/cotterpin_retinanet_r50_fpn_1x_coco.py
    
  3. 编写测试脚本。

    #!/bin/bash
    CUDA_VISIBLE_DEVICES=0 python3 tools/test.py configs/retinanet/cotterpin_retinanet_r50_fpn_1x_coco.py \
    work_dirs/cotterpin_retiannet_res50_no_pre/latest.pth \
    --out cotterpin_results/cotterpin_retinanet_result.pkl \
    --eval bbox \
    --show \
    --show-dir work_dirs/cotterpin_retiannet_res50_no_pre/cotterpin_test_result \
    --gpu-collect \
    --options {'classwise': True}
    


–show-dir work_dirs/cotterpin_retiannet_res50_no_pre/cotterpin_test_result
–gpu-collect
–options {‘classwise’: True}


`tools/train.py`和`tools/test.py`的用法可以自行查看源码,这里就不细说了。
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值