用mmdetection做目标检测的训练还是比较简单的,但是目前代码尚不稳定,其中也有很多的坑,下面简单讲解一下如何用mmdetetection在VOC的数据集上进行模型的训练,算是对mmdetection的简单入门。

1. 制作VOC数据集

mmdetection支持VOC和COCO标注格式的数据集,本次我们使用VOC2007格式数据集,我这里数据集已经制作好了,目录如下:

├── Datasets
│   │   ├── VOC2007
│   │   │   ├── Annotations
│   │   │   ├── JPEGImages
│   │   │   ├── ImageSets
│   │   │   │   ├── Main
│   │   │   │   │   ├── test.txt
│   │   │   │   │   ├── trainval.txt|—— val.txt

数据集中收集了各种车辆以及一些安全隐患图片,我们需要对图像中的车辆目标以及隐患目标进行检测,总共有26种检测目标 。

上面的数据集并不在mmdetection数据目录下,因此我们需要软连接到mmdetection数据目录下,mmdetection建议将数据集根符号链接到mmdetection/data目录下,目录结构如下:

mmdetection
├── mmdet
├── tools
├── configs
├── data
│   ├── coco
│   │   ├── annotations
│   │   ├── train2017
│   │   ├── val2017
│   │   ├── test2017
│   ├── VOCdevkit
│   │   ├── VOC2007
│   │   ├── VOC2012

因此,我们在mmdetection目录下执行如下命令新建目录:

mkdir -p data/VOCdevkit

将制作好的VOC2007数据集软链接到上述新建目录:

cd data/VOCdevkit

ln -s /home/你的路径/Datasets/VOC2007/  ./

2. 选择一个网络模型,并对该模型的配置文件进行修改

这里使用的模型是Libra R-CNN,它是2019年CVPR一篇论文提出的模型,作者来自浙大和商汤,代码开源在mmdetection。

执行如下命令,切换到对应的配置文件目录下:

cd mmdetection/configs/libra_rcnn/

vi libra_faster_rcnn_x101_64x4d_fpn_1x.py

目录下有多个不同网络版本的配置文件,我们选择libra_faster_rcnn_x101_64x4d_fpn_1x.py进行配置,这里面有很多的参数可供配置,我们只选择一些必要的参数,能够使得我们的模型能够跑起来,进行数据的训练。

1)修改分类器的类别数量

因为自己制作的数据集包含26种目标,需要对这个26个目标进行检测,另外加上所有的背景算作一个目标,所有我们的类别数量为27,因此修改num_classes = 27

2)配置数据集

mmdetection默认的是coco数据集,而我们本次使用的是VOC数据集,因此我们需要对数据集进行配置,配置比较简单,就是指明数据集的类型,路径,以及各个训练集、测试集文件路径即可:

原始配置文件:

# dataset settings
dataset_type = 'CocoDataset'
data_root = 'data/coco/'
img_norm_cfg = dict(mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
data = dict(imgs_per_gpu=2,workers_per_gpu=2,train=dict(type=dataset_type,ann_file=data_root + 'annotations/instances_train2017.json',img_prefix=data_root + 'train2017/',img_scale=(1333, 800),img_norm_cfg=img_norm_cfg,size_divisor=32,flip_ratio=0.5,with_mask=False,with_crowd=True,with_label=True),val=dict(type=dataset_type,ann_file=data_root + 'annotations/instances_val2017.json',img_prefix=data_root + 'val2017/',img_scale=(1333, 800),img_norm_cfg=img_norm_cfg,size_divisor=32,flip_ratio=0,with_mask=False,with_crowd=True,with_label=True),test=dict(type=dataset_type,ann_file=data_root + 'annotations/instances_val2017.json',img_prefix=data_root + 'val2017/',img_scale=(1333, 800),img_norm_cfg=img_norm_cfg,size_divisor=32,flip_ratio=0,with_mask=False,with_label=False,test_mode=True))

修改后的配置文件:

dataset_type = 'VOCDataset'
data_root = 'data/VOCdevkit/'
img_norm_cfg = dict(mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
data = dict(imgs_per_gpu=1,workers_per_gpu=2,train=dict(type=dataset_type,ann_file=data_root + 'VOC2007/ImageSets/Main/trainval.txt',img_prefix=data_root + 'VOC2007/',img_scale=(1333, 800),img_norm_cfg=img_norm_cfg,size_divisor=32,flip_ratio=0.5,with_mask=False,with_crowd=True,with_label=True),val=dict(type=dataset_type,ann_file=data_root + 'VOC2007/ImageSets/Main/val.txt',img_prefix=data_root + 'VOC2007/',img_scale=(1333, 800),img_norm_cfg=img_norm_cfg,size_divisor=32,flip_ratio=0,with_mask=False,with_crowd=True,with_label=True),test=dict(type=dataset_type,ann_file=data_root + 'VOC2007/ImageSets/Main/test.txt',img_prefix=data_root + 'VOC2007/',img_scale=(1333, 800),img_norm_cfg=img_norm_cfg,size_divisor=32,flip_ratio=0,with_mask=False,with_label=False,test_mode=True))

上面两个文件对比一下,只是修改了dataset_type、data_root、ann_file、img_prefix变量的值。

3) 修改数据集文件voc.py

该文件中有检测目标的配置,默认已经存在了一些检测目标,我们需要修改我们自己的数据集需要训练的检测目标,原始文件如下:

from .xml_style import XMLDatasetclass VOCDataset(XMLDataset):CLASSES = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car','cat', 'chair', 'cow', 'diningtable', 'dog', 'horse','motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train','tvmonitor')def __init__(self, **kwargs):super(VOCDataset, self).__init__(**kwargs)if 'VOC2007' in self.img_prefix:self.year = 2007elif 'VOC2012' in self.img_prefix:self.year = 2012else:raise ValueError('Cannot infer dataset year from img_prefix')

注释掉原来的CLASSES元组,添加我们自己的CLASSES元组:

from .xml_style import XMLDatasetclass VOCDataset(XMLDataset):# CLASSES = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car',#            'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse',#            'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train',#            'tvmonitor')CLASSES = ('towercrane', 'crane','crane_lifting', 'pumper', 'pumper_stretch','digger', 'dozer', 'roller', 'forklift','piledriver', 'ballgrader', 'grab','insulatedarmcar', 'othercars', 'truck','hangings', 'dustproofnet', 'reflectivefilm','smoke', 'fire', 'tower', 'scaffold','nest', 'person', 'car', 'bus')def __init__(self, **kwargs):super(VOCDataset, self).__init__(**kwargs)if 'VOC2007' in self.img_prefix:self.year = 2007elif 'VOC2012' in self.img_prefix:self.year = 2012else:raise ValueError('Cannot infer dataset year from img_prefix')

我们新添加的CLASSES元组有26个元素,也就是说我们需要对26个目标进行检测,26个目标加上背景也就是之前修改的分类器的类别数量27。

4)运行环境的配置

在配置文件的末尾有如下参数:

total_epochs = 20 # 训练最大的epoch数
dist_params = dict(backend='nccl') # 分布式参数,目前用不到
log_level = 'INFO' # 输出信息的完整度级别
work_dir = './work_dirs/libra_faster_rcnn_x101_64x4d_fpn_1x' # log文件和模型文件存储路径
load_from = None # 加载模型的路径,None表示从预训练模型加载
resume_from = None # 恢复训练模型的路径,None表示不进行训练模型的恢复
workflow = [('train', 1)] # 训练与验证策略,[('train', 1)]表示只训练,不验证;[('train', 2), ('val', 1)] 表示2个epoch训练,1个epoch验证

可根据自己的需要进行修改,不修改使用默认参数也不会报错。

至此,配置文件就修改完毕了。

特别注意:修改完成后需要重新编译框架,执行

./compile.sh
python setup.py develop

即可进行重新安装,使voc.py的修改生效。

3. 执行命令进行训练

mmdetection训练的命令格式如下:

python tools/train.py <CONFIG_FILE> --gpus <GPU_NUM> --work_dir <WORK_DIR>

CUDA_VISIBLE_DEVICES: 指定本次训练可以使用的GPU设备编号

CONFIG_FILE: 配置文件

GPU_NUM: 训练使用的GPU数量

WORK_DIR: 日志和模型文件存放目录

执行训练:

python tools/train.py configs/libra_rcnn/libra_faster_rcnn_x101_64x4d_fpn_1x.py --gpus 2

上面的--gpus 2表示使用两块GPU进行训练,如果你的是单GPU可以不加--gpus 2选项

以上执行如果不报错的话,训练就开始了...

4. 查看训练日志

在配置文件中work_dir变量配置了训练日志和模型配置文件的路径,开始训练后我们可以到目录下查看训练情况:

cd work_dirs/libra_faster_rcnn_x101_64x4d_fpn_1x

上面的.log、.log.json文件就是训练的日志文件,每训练完一个epoch后目录下还会有对应的以epoch_x.pth的模型文件,最新训练的模型文件命名为latest.pth。

.log文件的内容如下:

.log.json文件内容如下:

上面的文件内容大同小异,有当前时间、epoch次数,迭代次数(配置文件中默认设置50个batch输出一次log信息),学习率、损失函数loss、准确率等信息,可以根据上面的训练信息进行模型的评估与测试,另外可以通过读取.log.json文件进行可视化展示,方便调试。

5. 计算mAP

训练结束后需要评估指标,也就是计算mAP.

(1) 修改voc_eval.py文件中的voc_eval函数

注释如下代码:

 if hasattr(dataset, 'year') and dataset.year == 2007:dataset_name = 'voc07'else:dataset_name = dataset.CLASSES

在eval_map上面添加:

dataset_name = dataset.CLASSES

修改后的voc_eval函数代码如下:

def voc_eval(result_file, dataset, iou_thr=0.5):det_results = mmcv.load(result_file)gt_bboxes = []gt_labels = []gt_ignore = []for i in range(len(dataset)):ann = dataset.get_ann_info(i)bboxes = ann['bboxes']labels = ann['labels']if 'bboxes_ignore' in ann:ignore = np.concatenate([np.zeros(bboxes.shape[0], dtype=np.bool),np.ones(ann['bboxes_ignore'].shape[0], dtype=np.bool)])gt_ignore.append(ignore)bboxes = np.vstack([bboxes, ann['bboxes_ignore']])labels = np.concatenate([labels, ann['labels_ignore']])gt_bboxes.append(bboxes)gt_labels.append(labels)if not gt_ignore:gt_ignore = None# if hasattr(dataset, 'year') and dataset.year == 2007:#     dataset_name = 'voc07'# else:#     dataset_name = dataset.CLASSESdataset_name = dataset.CLASSESeval_map(det_results,gt_bboxes,gt_labels,gt_ignore=gt_ignore,scale_ranges=None,iou_thr=iou_thr,dataset=dataset_name,print_summary=True)

(2)用测试模型

执行如下命令:

python tools/test.py configs/libra_rcnn/libra_faster_rcnn_x101_64x4d_fpn_1x.py work_dirs/cascade_rcnn_hrnetv2p_w32/latest.pth --out results.pkl

测试结束后生成results.pkl文件。

(3)采用voc标准计算mAP

执行如下命令:

python tools/voc_eval.py results.pkl configs/libra_rcnn/libra_faster_rcnn_x101_64x4d_fpn_1x.py

生成如下结果:

注:如果不修改voc_eval.py文件,则会采用VOC默认的目标类型进行评估,而不是我们自己数据集的目标类型。

mmdetection训练自己的数据并评估mAP相关推荐

  1. yolov5训练自己的数据集并计算mAP

    目录 参考文献 代码和权重下载 准备工作 data中新建几个文件夹 makeTxt.py voc_label.py 文件修改 数据集方面的yaml文件修改 网络参数方面的yaml文件修改 train. ...

  2. mmdetection训练、测试

    文章目录 前言 一.使用mmdetection训练测试Mask-Rcnn 1.数据集转化 2.准备配置文件 3.训练 4.测试 二.mmdetection补充知识 前言 用于记录mmdetection ...

  3. mmdetection训练自己的COCO数据集及常见问题

    训练自己的VOC数据集及常见问题见下文: mmdetection训练自己的VOC数据集及常见问题_不瘦8斤的妥球球饼的博客-CSDN博客_mmdetection训练voc 目录 一.环境安装 二.训练 ...

  4. mmdetection训练自己的VOC数据集及常见问题

    训练自己的COCO数据集及常见问题见下文: mmdetection训练自己的COCO数据集及常见问题_不瘦8斤的妥球球饼的博客-CSDN博客 目录 一.环境安装 二.训练测试步骤 三.常见问题 bat ...

  5. YOLOv3: 训练自己的数据(绝对经典版本1)

    为什么80%的码农都做不了架构师?>>>    windows版本:请参考:https://github.com/AlexeyAB/darknet linux       版本:请参 ...

  6. YOLOv3训练自己的数据(附优化与问题总结)

    文章目录 YOLOv3训练自己的数据附优化与问题总结 环境说明 实现YOLOV3的demo 命令简介 训练自己的数据 优化与个性化问题 2020.3.11 更新可视化和python接口 可能出现的问题 ...

  7. Tensorflow版yolov3训练自己的数据

    Tensorflow版yolov3训练自己的数据 源代码:https://github.com/YunYang1994/TensorFlow2.0-Examples/tree/master/4-Obj ...

  8. YOLOv3: 训练自己的数据

    windows版本:请参考:https://github.com/AlexeyAB/darknet linux       版本:请参考本文与 https://pjreddie.com/darknet ...

  9. 转载:YOLOv3: 训练自己的数据

    转载 **感谢nusit_305大神的杰作 https://blog.csdn.net/lilai619/article/details/79695109** linux                ...

最新文章

  1. leetcode 236. 二叉树的最近公共祖先 递归解法 c语言
  2. ENVI扩展工具:利用波段运算修改NaN方法总结
  3. POJ2352-Stars【树状数组】
  4. Java反转单链表(code)
  5. linux压缩命令(二)bzip2总结
  6. 【Python笔记】网络爬虫——介绍
  7. 面试问sql优化怎么回答
  8. 删除数据表中的重复行
  9. jQuery.callbacks 注释
  10. jquery在选择元素的时候,可以写成var div=$(div)
  11. Android 第3方控件一览表
  12. U盘插在前面板不认,可以试试插在后面板
  13. state 全局值 设置 和获取
  14. 主流的质量管理和项目管理及研发管理体系探索
  15. UE4HTTP下载网速过慢问题
  16. Typora图床设置
  17. 工具分享-PDF 补丁丁
  18. element ui 日历空控件添加农历 节气和家假日
  19. 【电脑运用及修理】Internet Explorer 浏览器
  20. 南邮计算机与科学排名,南邮计算机全国排名

热门文章

  1. nodejs express搭建服务器(爬虫知乎精华帖,个人学习用)五 对提到的关键字(书名或者电影名)去百度百科上爬取介绍
  2. 在html中插入avi视频,兮夜2000杀成就达成,JDG优势局连续失误
  3. http状态码大全100-599状态详解
  4. 学习日语应该先掌握哪些内容?
  5. [转载备用]微信直播的优势及微信直播搭建过程(点赞:主播妹子有点靓哦)
  6. 后TOS时代的码头数字化生产力
  7. PL\SQL用户指南与参考10.1 转载
  8. shell脚本编程学习笔记2(xdl)——Bash变量
  9. 【牛客】幸运数字II题解
  10. 【shell编程】基础篇