yolov5如何找出召回率以及map较低的原因(或者说找出难以预测的图片)
最近一直在用yolov5(v1.0版本)做头盔、安全帽、人脸三分类的检测任务,数据集是三个数据集的融合,关于融合数据集前边说到过,这里就不再提起,最终的指标(0.5的阈值)如下:
尝试了一些增强数据方法,也用了很多优化策略,但是指标都不提升,因此从数据角度查找原因,通过指标可以看出,模型的精度很高,召回率较低,因此说明模型漏检问题较为严重,对detect.py文件进行一些修改,
labels = []
#这里是画检测框的代码
for *xyxy, conf, cls in det:
label = '%s %.2f' % (names[int(cls)], conf)
#统计当前图片检测出来的label数量
labels.append(label)
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
#获取txt路径
txt_path = p.replace('images', 'labels').replace('jpg', 'txt')
#获取ground truth的数量
with open(txt_path, 'r') as f:
num = f.readlines()
#保存漏检的图片
if len(num) > len(labels):
cv2.imwrite(save_path, im0)
else:
pass
到这里 我们已经获取了标注好的但是有漏检的图片,下一步,用另一个颜色在图上画上ground truth
#先保存一个上述图片路径的txt文件
with open('img.txt','w') as f:
for dir in os.listdir(img_path):
pic_name = img_path + dir + '\n'
f.write(pic_name)
#这个函数是用来获取xml中的预测框位置信息,因为txt文件已经进行了归一化,还原较麻烦
def _read_anno(filename):
import xml.etree.ElementTree as ET
""" Parse a PASCAL VOC xml file """
tree = ET.parse(filename)
objects = []
for obj in tree.findall('object'):
bbox = obj.find('bndbox')
x1, y1, x2, y2 = [int(bbox.find('xmin').text),
int(bbox.find('ymin').text),
int(bbox.find('xmax').text),
int(bbox.find('ymax').text)]
result = [x1,y1,x2,y2]
objects.append(result)
return objects
with open('img.txt','r') as f:
path = f.readlines()
for im in path:
im = im.rstrip('\n')
filename = os.path.basename(im)
name = filename.split('.')[0]
#这里第一个replace根据自己的数据集位置进行修改
txt_path = im.replace().replace('jpg', 'txt').rstrip('\n')
xml_path = im.replace().replace('jpg', 'xml')
box = _read_anno(xml_path)
box = np.array(box)
labels = []
#获取label
with open(txt_path,'r') as f:
for line in f:
label = line.split(' ')[0]
labels.append(label)
img = cv2.imread(im)
for i in range(0,len(labels)):
#画图并标上label
cv2.rectangle(img,(box[i,0],box[i,1]),(box[i,2],box[i,3]),(0,0,255),thickness=2)
cv2.putText(img,'{}'.format(labels[i]),(box[i,0],box[i,1]),font, 0.8, (0, 0, 0), 2)
cv2.imwrite('{}.jpg'.format(name),img)
然后我们就可以查看图片进行比较辣,下图为例,右上角的图只有红色的ground truth,没有我模型给出的预测框,初步判断是由于角度原因,当然还有一些模糊、遮挡、小目标等原因就是根据大家自己的情况去判断啦~
欢迎私信交流讨论