使用yolov5检测,有时会需要标记一些其他类来帮助模型斧正真正需要检测的类别,这些其他类我们并不希望出现在结果的mAP中,而导致我们需要的类别的mAP计算偏离实际值。
解决方案:在计算metrics前将需要计算mAP的类别序号制定出来,只需要计算所选类别的各项指标作为总体的指标。
yolov5官方的评估脚本:val.py
在val.py的 # compute metrics 后加上代码
mAP_cls_idx = torch. Tensor([1,2,3]) # 设定的需要计算mAP的类的序号
后面代码从
stats = [torch.cat(x, 0).cpu().numpy() for x in zip(*stats)] # to numpy
if len(stats) and stats[0].any():
tp, fp, p, r, f1, ap, ap_class = ap_per_class(*stats, plot=plots, save_dir=save_dir, names=names)
ap50, ap = ap[:, 0], ap.mean(1) # AP@0.5, AP@0.5:0.95
mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean()
改为
stats = [torch.cat(x, 0).cpu().numpy() for x in zip(*stats)] # to numpy
if len(stats) and stats[0].an