使用MMDetection3.x训练自定义数据集和类别
1. 安装
2. 训练
2.1 准备数据和配置文件
2.2 训练
- 训练方式一
python tools/train.py configs/balloon/mask-rcnn_r50-caffe_fpn_ms-poly-1x_balloon.py
-
会出现如下错误, 且生成的.pth文件不能检测到balloon, 原因是lr太大
-
训练方式二
python tools/train.py configs/balloon/mask-rcnn_r50-caffe_fpn_ms-poly-1x_balloon.py --auto-scale-lr
2.3 测试
- 测试图片集并对比显示
python tools/test.py configs/balloon/mask-rcnn_r50-caffe_fpn_ms-poly-1x_balloon.py work_dirs/mask-rcnn_r50-caffe_fpn_ms-poly-1x_balloon/epoch_12.pth --show
- 测试图片并保存结果
python demo/image_demo.py data/balloon/val/test.jpg configs/balloon/mask-rcnn_r50-caffe_fpn_ms-poly-1x_balloon.py work_dirs/mask-rcnn_r50-caffe_fpn_ms-poly-1x_balloon/epoch_12.pth --out-file 1.jpg
3. 评价指标 (Evaluation Metrics)
3.1 目标检测评估( Detection Evaluation)
- 基于2分类的指标如下:
-
Recall 召回率(查全率): 所有真实目标中,模型预测正确的目标比例,其公式为: R e c a l l = t p r = T P / ( T P + F N ) Recall = tpr = TP / (TP+FN) Recall=tpr=TP/(TP+FN)
-
Precision 精确率(查准率):模型预测的所有目标中,预测正确的比例,其公式为: P r e c i s i o n = T P / ( T P + F P ) Precision = TP / (TP+FP) Precision=TP/(TP+FP)
-
Accuracy:准确率。正确分类(正例分为正例,负例分为负例)的样本数除以所有的样本数,正确率越高,分类器越好。其公式为: A c c u r a c y = ( T P + T N ) / ( T P + T N + F P + F N ) Accuracy=(TP+TN)/ (TP+TN+FP+FN) Accuracy=(TP+TN)/(TP+TN+FP+FN)
-
FP (False Positive):IoU<=0.5时的检测框(或者是检测到同一个GT的多余检测框的数量)
- 误报,即负例中识别为正例的样本。其中在计算roc曲线的时候需要fpr = FP / (FP + TN)
-
FN (False Negative):没有检测到的GT的数量 (漏检)
-
TP (True Positive):IoU>0.5的检测框数量(同一Ground Truth只计算一次)
-
TN (True Negative)
-
IOU (Intersection-Over-Union):交并比(交集与并集的比值)
-
AP:P-R曲线下面积
-
P-R曲线:Precision-Recall曲线
-
mAP:mean Average Precision,即各类别AP的平均值 (每个类别有一个AP)
-
3.1.1 计算Precision和Recall
-
计算Precision和Recall, Rank为1表示Confidence不小于0.98的才视为正确检测出来,即Confidence取不同的阈值所得到的Precision和Recall,参考目标检测mAP计算以及coco评价标准
-
计算一类物体的AP
3.1.2 通过P-R曲线计算AP
3.2 COCO上物体检测器的性能指标 (12个)
指标 | 描述 |
---|---|
Average Precision (AP): | |
AP | % AP at IoU=0.50:0.05:0.95 (primary challenge metric) IoU从0.5到0.95,间隔为0.05,总共10个IoU(0.5, 0.55, 0.60, …, 0.90, 0.95), 对于每个IoU计算一个AP,再取这10个AP的均值 |
A P I o U = . 50 AP^{IoU=.50} APIoU=.50 | % AP at IoU=0.50 (PASCAL VOC metric) |
A P I o U = . 75 AP^{IoU=.75} APIoU=.75 | % AP at IoU=0.75 (strict metric) |
AP Across Scales: | 对不同尺度目标的检测效果 |
A P s m a l l AP^{small} APsmall | % AP for small objects: a r e a < 3 2 2 area < 32^2 area<322 |
A P m e d i u m AP^{medium} APmedium | % AP for medium objects: 3 2 2 < a r e a < 9 6 2 32^2 < area < 96^2 322<area<962 |
A P l a r g e AP^{large} APlarge | % AP for large objects: a r e a > 9 6 2 area > 96^2 area>962 |
Average Recall (AR): | |
A R m a x = 1 AR^{max=1} ARmax=1 | % AR given 1 detection per image |
A R m a x = 10 AR^{max=10} ARmax=10 | % AR given 10 detections per image |
A R m a x = 100 AR^{max=100} ARmax=100 | % AR given 100 detections per image 通过NMS之后,每个图像最多预测100个目标 |
AR Across Scales: | |
A P s m a l l AP^{small} APsmall | % AR for small objects: a r e a < 3 2 2 area < 32^2 area<322 |
A P m e d i u m AP^{medium} APmedium | % AR for medium objects: 3 2 2 < a r e a < 9 6 2 32^2 < area < 96^2 322<area<962 |
A P l a r g e AP^{large} APlarge | % AR for large objects: a r e a > 9 6 2 area > 96^2 area>962 |
- area:测量的面积(area)是分割掩码(segmentation mask)中的像素数量
3.3 MMDetection训练输出评价值
- 训练过程中的输出值
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.561
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=1000 ] = 0.757
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=1000 ] = 0.681
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.000
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.475
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.629
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.614
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=300 ] = 0.614
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=1000 ] = 0.614
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.508
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.683
- 第1行:COCO数据集评价指标,即mAP
- 第2行:PASVOC数据集评价指标,即IoU=0.5时的mAP
- 第3行:IoU=0.75 相比0.50是比较严格的一个指标
- 第4行:检测小目标的的指标
- 第5行:检测中等目标的
- 第6行:检测大目标的指标
- 第7行:设置目标检测框只有100个
- 第8行:设置目标检测框有300个
- 第9行:设置目标检测框有1000个 (从上图看,100个、300个和1000个的指标值差不多)