关于yolov5评价指标之精确率和召回率实现

一,原理公式

在这里插入图片描述

主要的事说三遍,精确率和准确率不是一个东西!精确率和准确率不是一个东西!精确率和准确率不是一个东西!我们平时在衡量一个模型的性能的时候,通常用的是精确率和召回率。
TP是正样本预测出正样本数量。
FP是负样本预测出正样本数量。
FN是正样本预测出负样本数量。

二,对于多目标检测任务,怎样自己码代码求precision和recall?
(前提必须有标注信息。)
1,思路解析:对于多目标检测任务,TP(true positive)表示预测出的正确的框,即通过模型预测出的框,逐个与该图像的标注框求iou,如果与标注框产生的最大iou大于之前设置好的iou阈值,并且此预测框对应的标签与通过iou操作找到的标注框标签一致,便认为此预测框为true positive。
对于召回率分母TP+FN,是指正样本预测出正样本数量加上正样本预测出负样本数量,那么就是所有正样本,也就是所有标注框个数
对于精确率,分母TP+FP,是指正样本预测出正样本数量加上负样本预测出正样本数量,就是true positive加上false positive,那么就是预测出的正样本,也就是所有positive,也就是所有预测框个数
2,实现步骤:
1)将数据喂给模型,得到inference信息;

pred = model(img, augment=opt.augment)[0]

2)NMS处理得到预测信息,此时就会得到所有预测框

pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)

3)遍历预测信息,同时获取标注信息,统计TP,TP+FN,TP+FP

for i, det in enumerate(pred):  # detections per image
			p, s, im0 = path[i], '%g: ' % i, im0s[i].copy()
			base_txt_name = os.path.splitext(os.path.basename(path))[0] + '.txt'  #获取图像名称对应的标注文件名称	
			txt_label=txtpath+"/"+base_txt_name   #获取此图像对应的标注文件
			vecter_labels = []    # 存与原图同样尺寸下标注框
			cls_labels=[]         # 存标签
			person_labels=[]     #存类别1标签
			fire_labels=[]       #存类别2标签
			smoke_labels=[]    #存类别3标签
			with open(txt_label, 'r+', encoding='UTF-8') as f1:       # 打开标注文件,获取标注信息
				lines = f1.readlines()
				for j in range(len(lines)):
					vecter_label = []
					lines[j] = lines[j].replace('\n', '')
					line = lines[j].split(' ')
					x1_=float(line[1])*im0.shape[1]-float(line[3])*im0.shape[1]/2
					vecter_label.append(x1_)
					y1_=float(line[2])*im0.shape[0]-float(line[4])*im0.shape[0]/2
					vecter_label.append(y1_)
					x2_=float(line[1])*im0.shape[1]+float(line[3])*im0.shape[1]/2
					vecter_label.append(x2_)
					y2_=float(line[2])*im0.shape[0]+float(line[4])*im0.shape[0]/2
					vecter_label.append(y2_)
					vecter_labels.append(vecter_label)
					cls_labels.append(int(line[0]))
					if int(line[0])==0:
						person_labels.append(int(line[0]))
					if int(line[0])==1:
						fire_labels.append(int(line[0]))
					if int(line[0])==2:
						smoke_labels.append(int(line[0]))

			total_kuang=total_kuang+len(cls_labels)    #统计所有标注框数量
			person_kuang=person_kuang+len(person_labels)   #统计所有类别1标注框数量
			fire_kuang=fire_kuang+len(fire_labels)       #统计所有类别2标注框数量
			smoke_kuang=smoke_kuang+len(smoke_labels)    #统计所有类别3标注框数量
			
			s += '%gx%g ' % img.shape[2:]  # print string
			gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwh
			
			if det is not None and len(det):     #遍历预测框
				det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
				# Print results
				for c in det[:, -1].unique():
					n = (det[:, -1] == c).sum()  # detections per class
					s += '%g %ss, ' % (n, names[int(c)])  # add to string

				for *xyxy, conf, cls in det:
					xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4))).view(-1).tolist()  # normalized xywh
					pre_xywh=[]      #存与原图同样尺寸下预测框
					x1=xywh[0]-xywh[2]/2
					pre_xywh.append(x1)
					y1=xywh[1]-xywh[3]/2
					pre_xywh.append(y1)
					x2=xywh[0]+xywh[2]/2
					pre_xywh.append(x2)
					y2=xywh[1]+xywh[3]/2
					pre_xywh.append(y2)
					ious, i_libie= box12_iou(pre_xywh, vecter_labels,cls_labels)  #筛选出此预测框与此图像标注框最大iou以及对应的标签类别				
					if i_libie==int(cls):  #如果此预测框的类别与最大iou框标注类别一致
						print("label=%s	IOU=%.2f	conf=%.2f" % (names[int(cls)],ious,conf))
						if i_libie==0:       #类别1处理
							person_Positive_num=person_Positive_num+1  #统计所有类别1预测框数量
							if conf>0.3 and ious > 0.4:
								person_Positive_true=person_Positive_true+1    # #统计所有类别1预测框预测正确数量
						if i_libie==1:    #类别2处理
							fire_Positive_num=fire_Positive_num+1    #统计所有类别2预测框数量
							if ious > 0.25:
								fire_Positive_true=fire_Positive_true+1    # #统计所有类别2预测框预测正确数量
						if i_libie==2:    #类别3处理
							smoke_Positive_num=smoke_Positive_num+1   #统计所有类别3预测框数量
							if ious > 0.25:
								smoke_Positive_true=smoke_Positive_true+1    #统计所有类别3预测框预测正确数量
  • 8
    点赞
  • 91
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
yolov5评价指标包括mAP(mean Average Precision)、F1-score、GIoU loss和BECWithLogits loss。 mAP是各类别AP的平均值,其中AP表示每个类别的平均精度。mAP的计算公式为mAP = 1/m ∑AP(i),其中m为类别数,AP(i)为第i类类别的平均精度。\[1\] F1-score是一个综合考虑了Precision(精确率)和Recall(召回率)的指标,用于衡量模型的准确性和完整性。F1-score的计算公式为F1-score = 2(Precision × Recall)/(Precision + Recall)。\[2\] GIoU loss是一种用于计算目标检测模型中边界框回归损失的方法。GIoU loss通过计算预测框和标注框之间的IoU(Intersection over Union)以及它们的边界框的相对位置关系来衡量两个边界框的差异。\[2\] BECWithLogits loss是一种用于计算目标检测模型中分类损失的方法。它通过计算预测框中所有预测为正样本的结果中,真正为正样本的概率与预测为负样本的结果中,真正为负样本的概率之间的差异来衡量分类的准确性。\[3\] 综上所述,yolov5评价指标包括mAP、F1-score、GIoU loss和BECWithLogits loss。这些指标可以用来评估目标检测模型的性能和准确性。 #### 引用[.reference_title] - *1* *2* [YoloV5相关性能指标解析](https://blog.csdn.net/m0_47026232/article/details/119477826)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Yolov5——评估指标](https://blog.csdn.net/REstrat/article/details/126873627)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值