4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
热卖商品
新闻详情
yolov3代码详解(二)_medusa_zj的博客-CSDN博客
来自 : CSDN技术社区 发布时间:2021-03-25
i np.argsort(-conf) #argsort函数返回的是数组值从小到大的索引值 tp, conf, pred_cls tp[i], conf[i], pred_cls[i] # Find unique classes unique_classes np.unique(target_cls) #除数组中的重复数字 并进行排序之后输出。 # Create Precision-Recall curve and compute AP for each class 创建精确召回曲线并计算每个类的AP ap, p, r [], [], [] for c in tqdm.tqdm(unique_classes, desc Computing AP ): i pred_cls c n_gt (target_cls c).sum() # Number of ground truth objects #真实目标 数 n_p i.sum() # Number of predicted objects #预测的目标 数 if n_p 0 and n_gt 0: continue elif n_p 0 or n_gt 0: ap.append(0) r.append(0) p.append(0) else: # Accumulate FPs and TPs fpc (1 - tp[i]).cumsum() tpc (tp[i]).cumsum() # Recall recall_curve tpc / (n_gt 1e-16) r.append(recall_curve[-1]) # Precision precision_curve tpc / (tpc fpc) p.append(precision_curve[-1]) # AP from recall-precision curve ap.append(compute_ap(recall_curve, precision_curve)) # Compute F1 score (harmonic mean of precision and recall) 准确度和召回率的调和平均值 p, r, ap np.array(p), np.array(r), np.array(ap) f1 2 * p * r / (p r 1e-16) return p, r, ap, f1, unique_classes.astype( int32 )#由recall,precision计算ap 被上面的ap_per_class引用 def compute_ap(recall, precision): Compute the average precision, given the recall and precision curves. Code originally from https://github.com/rbgirshick/py-faster-rcnn. # Arguments recall: The recall curve (list). precision: The precision curve (list). # Returns The average precision as computed in py-faster-rcnn. # correct AP calculation # first append sentinel values at the end mrec np.concatenate(([0.0], recall, [1.0])) #拼接 mpre np.concatenate(([0.0], precision, [0.0])) #拼接 # compute the precision envelope 计算精度包络 for i in range(mpre.size - 1, 0, -1): #range(10,0,-1)意思是从列表的下标为10的元素开始 倒序取到下标为0的元素 但是不包括下标为0元素 mpre[i - 1] np.maximum(mpre[i - 1], mpre[i]) #取最大值 # to calculate area under PR curve, look for points # where X axis (recall) changes value #要计算PR曲线下的面积 请查找点 #其中X轴 调用 更改值 i np.where(mrec[1:] ! mrec[:-1])[0] # and sum (\\Delta recall) * prec ap np.sum((mrec[i 1] - mrec[i]) * mpre[i 1]) return ap#统计batch检测性能指标 被test.py引用#输入为经过nms得到的预测框 #输出为【预测框和实际框匹配设置为1 真阳性 置信度 标签】 如果预测框有3个 则输出维度也是3个def get_batch_statistics(outputs, targets, iou_threshold): Compute true positives, predicted scores and predicted labels per sample #outputs: 一个 batch_size 的所有图片的检测结果 shape为(batch_size, pred_boxes_num, 7) ,其中 【1, 507筛选后 假设有3个,7】 507在特征图13*13上 #7指的是x,y,w,h,conf,class_conf,class_pred #output 指的是一个batch_size里面的第sample_i张图片的检测结果 它有pred_boxes_num个box。 #一个 batch_size中第 sample_i 张图片的检测结果 含多个box 每个box都有一个7 # targets: (batch_size, 6) 其中6指的是num, cls, center_x, center_y, widht, height 其中 #num指的是第几个图片 因为一张图片中可能有好几个目标 这些目标的num都是一样的 batch_metrics [] for sample_i in range(len(outputs)): #一张图片 if outputs[sample_i] is None: continue output outputs[sample_i] #当前图像检测结果为output pred_boxes output[:, :4] # 当前图像的预测框的x,y,w,h pred_scores output[:, 4] # 当前图像的预测框的置信度 pred_labels output[:, -1] # 当前图像的预测框的类别label true_positives np.zeros(pred_boxes.shape[0]) #得到以 一张图上有几个预测出来的目标 的矩阵 初始化为0 预测框和实际框匹配 则设置为1 annotations targets[targets[:, 0] sample_i][:, 1:] #图像sample_i所有的标签写入annotations 【cls, center_x, center_y, widht, height】 即这张图片中真实的标签 target_labels annotations[:, 0] if len(annotations) else [] #提取annotations所有的cls写入target_labels if len(annotations): #如果图像sample_i中有框 detected_boxes [] #记录检测结果 target_boxes annotations[:, 1:] for pred_i, (pred_box, pred_label) in enumerate(zip(pred_boxes, pred_labels)): # If targets are found break if len(detected_boxes) len(annotations): #如果所有的目标annotations都找到了 就退出 break # Ignore if label is not one of the target labels if pred_label not in target_labels: #当前图像的预测框的类别label不在 真实类别中 continue iou, box_index bbox_iou(pred_box.unsqueeze(0), target_boxes).max(0) #如果有相同标签 则计算iou值 得到的box_index 是对应 target_boxes的索引 if iou iou_threshold and box_index not in detected_boxes: true_positives[pred_i] 1 detected_boxes [box_index]# 记录刚刚匹配成功的真实框target_boxes的索引号box_index 防止它被预测框pred_box重复标记 即一个实际框target_boxes只能被一个预测框pred_box成功匹配 batch_metrics.append([true_positives, pred_scores, pred_labels]) #预测框和实际框匹配 则设置为1 置信度 标签 return batch_metrics#------------------------------------------以上为性能指标计算---------------------#求anchor与真实框的交并比 被下面的build_targets引用def bbox_wh_iou(wh1, wh2): wh2 wh2.t() #Tensor进行转置 w1, h1 wh1[0], wh1[1] w2, h2 wh2[0], wh2[1] inter_area torch.min(w1, w2) * torch.min(h1, h2) union_area (w1 * h1 1e-16) w2 * h2 - inter_area return inter_area / union_area

本文链接: http://yoloes.immuno-online.com/view-751098.html

发布于 : 2021-03-25 阅读(0)
公司介绍
品牌分类
联络我们
服务热线:4000-520-616
(限工作日9:00-18:00)
QQ :1570468124
手机:18915418616
官网:http://