MMDetection训练自己的数据集

MMDetection

1. 安装

1.1 windows

  • 1.1.1 克隆git仓库
    git clone https://github.com/open-mmlab/mmdetection.git
    
  • 1.1.2 创建环境
    conda create --name openmmlab python=3.8 -y
    conda activate openmmlab
    conda install pytorch torchvision -c pytorch
    pip install -U openmim
    mim install mmcv-full
    git clone https://github.com/open-mmlab/mmdetection.git
    cd mmdetection
    pip install -v -e .
    # "-v" means verbose, or more output
    # "-e" means installing a project in editable mode,
    # thus any local modifications made to the code will take effect without reinstallation.
    
  • 1.1.3 验证
    mim download mmdet --config yolov3_mobilenetv2_320_300e_coco --dest .
    python demo/image_demo.py demo/demo.jpg yolov3_mobilenetv2_320_300e_coco.py yolov3_mobilenetv2_320_300e_coco_20210719_215349-d18dff72.pth --device cpu --out-file result.jpg
    
  • 1.1.4 遇到的问题
    (1) error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com
        在 https://my.visualstudio.com/Downloads/Featured?mkt=zh-cn 上下载Visual Studio 2015 update 3。点击Visual Studio 2015 update 3后,下载对应的文件即可,约1.1G,这里需要将格式修改为DVD。然后安装即可。
    (2) ERROR: Could not build wheels for pycocotools, which is required to install pyproject.toml-based projects
        pip install pycocotools-windows -i https://pypi.tuna.tsinghua.edu.cn/simple
    

1.2 Linux

```
参照1.1
```

2. 训练自己的数据集(以COCO格式为例)

首先需要将自己的数据集转为COCO格式

2.1 训练命令

python tools/train.py configs/swin/retinanet_swin-t-p4-w7_fpn_1x_coco.py

2.2 修改配置文件

retinanet_swin-t-p4-w7_fpn_1x_coco.py配置文件中的_base_中显示了有关数据集的配置文件../_base_/datasets/coco_detection.py,所以需要修改coco_detection.py文件。

  • 2.2.1 修改coco_detection.py
    data_root 修改为自己的coco数据集路径,如
    data_root = 'C:/Users/86158/Desktop/HyperDL/data/COCO/'
    img_norm_cfg 这个看情况修改吧,可以也改成自己数据集的,不改也能用。
    data 字段里的一些配置根据自己的数据集情况进行修改,如data中的train字段的ann_file,img_prefix等,原来的代码默认图像后缀是png的,类别是coco的80类,需要修改一下。
    
        train=dict(
          type=dataset_type,
          ann_file=data_root + 'train.json',
          img_prefix=data_root + 'train/',
          seg_suffix=".jpg",
          classes=['aircraft'],
          pipeline=train_pipeline),
    
    剩下的val和test也类似进行修改。
    
  • 2.2.2 修改retinanet_r50_fpn.py
    通过retinanet_swin-t-p4-w7_fpn_1x_coco.py配置文件可以看到该模型的基础配置文件为retinanet_r50_fpn.py,在其中定义了网络的backbone,neck和head等。其中head部分需要修改类别数量。
    bbox_head中的num_classes需要修改为自己数据集对应的类别数量
        bbox_head=dict(
          type='RetinaHead',
          num_classes=1,
          in_channels=256,
          stacked_convs=4,
          feat_channels=256,
          anchor_generator=dict(
              type='AnchorGenerator',
              octave_base_scale=4,
              scales_per_octave=3,
              ratios=[0.5, 1.0, 2.0],
              strides=[8, 16, 32, 64, 128]),
    
  • 2.2.3 修改schedule_1x.py
    schedule_1x.py中定义了优化器相关的配置。当出现训练值为nan时,可以修改一下该文件进行尝试。
    
    # optimizer_config = dict(grad_clip=None)
    optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2))
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值