一,原理公式
主要的事说三遍,精确率和准确率不是一个东西!精确率和准确率不是一个东西!精确率和准确率不是一个东西!我们平时在衡量一个模型的性能的时候,通常用的是精确率和召回率。
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预测框预测正确数量