mmdetection训练、测试总结
1)数据集格式:voc数据集;
在/mmdetection/data/VOCdekit 上传数据,改名为VOC2007,其中包含JPEGImages/Annotations/ImageSets等;
2)代码修改:
①mmdet/dataset/voc.py,把里面的类别改成你自己数据集的类别。
②mmdet/core/evaluation/class_name.py,把里面的voc_classes()改成你自己数据集的类别。
③修改config文件
pretrained是预训练模型的绝对路径,例如:pretrained=’/home/haohao/zmp/mmdetection/mmdet/models/resnet101-5d3b4d8f.pth’,
#data settings
num_classes=179, #修改为自己数据集的类别数+1、
dataset_type = ‘VOCDataset’
data_root = ‘data/VOCdevkit/’
ann_file=data_root + ‘VOC2007/ImageSets/Main/train.txt’,
img_prefix=data_root + ‘VOC2007/’, ###多个train/trainval/test/路径需修改
训练:python tools/train.py configs/retinanet_r101_fpn_1x.py --gpus 1
报错:AttributeError: ‘NoneType’ object has no attribute ‘text’
原因:xml不含有difficult
解决:把mmdet/dataset里面的xml_style中的difficult语句删掉
测试:python tools/test.py configs/retinanet_r101_fpn_1x.py work_dirs/retinanet_r101_fpn_1x/latest.pth --out ./test_result/retinanet_r101_fpn_1x/latest.pkl --eval mAP (显示result)
python tools/test.py configs/retinanet_r101_fpn_1x.py work_dirs/retinanet_r101_fpn_1x/latest.pth --out ./test_result/retinanet_r101_fpn_1x/latest.pkl --show (保存测试后的图片)
原测试是仅展示测试结果,其中包含 | class | gts | dets | tp | fp | recall | precisions | ap(fp、tp、precisions为后来添加代码展示出来),为了保存测试图片,后又修改mmdet/apis中的test.py,
###zmp add(savepeth需绝对路径,否则无法保存)
savepath = “/home/haohao/zmp/mmdetection/test_result/test_pic/” + str(i)+’.jpg’
# if show:
model.module.show_result(data, result, score_thr=0.3, out_file = savepath)
后又在mmdet/models/detectors/下的base.py文件中的def show_result
添加show 和 out_file。
def show_result(self, data, result, dataset=None, score_thr=0.3, show=True, out_file=None):
if out_file is not None:
show = False
#print(out_file)
mmcv.imshow_det_bboxes(
img_show,
bboxes,
labels,
class_names=class_names,
score_thr=score_thr,
show=show,
out_file=out_file)
config文件里设置框的阈值和nms的大小,我针对我自己的数据集设置框的阈值为0.45,nms为0.3
test_cfg = dict(
nms_pre=1000,
min_bbox_size=0,
score_thr=0.45, ###框的阈值
nms=dict(type=‘nms’, iou_thr=0.3), ###nms
max_per_img=100)