本系列文章记录本人硕士阶段YOLO系列目标检测算法自学及其代码实现的过程。其中算法具体实现借鉴于ultralytics YOLO源码Github,删减了源码中部分内容,满足个人科研需求。
本系列文章主要以YOLOv5为例完成算法的实现,后续修改、增加相关模块即可实现其他版本的YOLO算法。
文章地址:
YOLOv5算法实现(一):算法框架概述
YOLOv5算法实现(二):模型加载
YOLOv5算法实现(三):数据集加载
YOLOv5算法实现(四):损失计算
YOLOv5算法实现(五):预测结果后处理
YOLOv5算法实现(六):评价指标及实现
YOLOv5算法实现(七):模型训练
YOLOv5算法实现(八):模型验证
YOLOv5算法实现(九):模型预测(编辑中…)
本篇文章实现目标检测评价指标的实现,主要包含以下几个指标:
空间复杂度
:参数量( Parameters )
时间复杂度
:浮点运算数( FLOPs )
精度
:精确率( P )、召回率( R )、混淆矩阵; 均值平均精度( mAP )
P = T P T P + F P P = {{TP} \over {TP + FP}} P=TP+FPTP
R = T P T P + F N R = {{TP} \over {TP + FN}} R=TP+FNTP
m A P = ∑ A P N {\rm{m}}AP = {{\sum {AP} } \over N} mAP=N∑AP
TP表示预测为正样本且实际为正样本数量,FP表示预测为正样本但实际为负样本数量。在目标检测中,根据IoU阈值
和类别
对预测结果进行和实际标签的匹配,若匹配成功则该检测结果视为TP,反之则视为FP。AP为在不同的类别置信度下,类别P-R值围成的曲线面积,mAP为所有类别的AP值的平均值。
假设某目标检测任务中类别数为3,具体的mAP计算过程如图2所示。
def param_flops_cal(model, verbose=False, imgsz=640):
'''
打印模型信息
:param model: 模型
:param imgsz:
'''
n_p = sum(x.numel() for x in model.parameters()) # 参数量
n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # 更新参数量
print(
f"{'layer':>5} {'name':>40} {'gradient':>9} {'parameters':>12} {'shape':>20} {'mu':>10} {'sigma':>10}")
for i, (name, p) in enumerate(model.named_parameters()):
name = name.replace('module_list.', '')
print('%5g %40s %9s %12g %20s %10.3g %10.3g' %
(i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std()))
try: # FLOPs
p = next(model.parameters())
stride = max(int(model.stride.max()), 32) if hasattr(model, 'stride') else 32 # max stride
im = torch.empty((1, p.shape[1], stride, stride), device=p.device) # input image in BCHW format
flops = thop.profile(deepcopy(model), inputs=(im,), verbose=False)[0] / 1E9 * 2 # stride GFLOPs
imgsz = imgsz if isinstance(imgsz, list) else [imgsz, imgsz] # expand if int/float
fs = f', {flops * imgsz[0] / stride * imgsz[1] / stride:.1f} GFLOPs' # 640x640 GFLOPs
except Exception:
fs = ''
name = Path(model.yaml_file).stem.replace('yolov5', 'YOLOv5') if hasattr(model, 'yaml_file') else 'Model'
printf(f"{name} summary: {len(list(model.modules()))} layers, {n_p}({n_p / 1E6:.2f}M) parameters, {n_g} gradients{fs}")
def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=(), eps=1e-16, prefix=''):
""" Compute the average precision, given the recall and precision curves.
Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
# Arguments
tp: 所有预测结果在不同IoU下的预测结果 [n, 10]
conf: 所有预测结果的置信度
pred_cls: 所有预测结果得到的类别
target_cls: 所有图片上的实际类别
plot: Plot precision-recall curve at [email protected]
save_dir: Plot save directory
# Returns
The average precision as computed in py-faster-rcnn.
"""
# Sort by objectness
i = np.argsort(-conf) # 根据置信度从大到小排序
tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
# 得到所有类别及其对应数量
unique_classes, nt = np.unique(target_cls, return_counts=True)
nc = unique_classes.shape[0] # number of classes
# Create Precision-Recall curve and compute AP for each class (针对每一个类别计算P,R曲线)
px, py = np.linspace(0, 1, 1000), [] # for plotting
ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000))
for ci, c in enumerate(unique_classes): # 对每一个类别进行P,R计算
i = pred_cls == c
n_l = nt[ci] # number of labels 该类别的实际数量(正样本数量)
n_p = i.sum() # number of predictions 预测结果数量
if n_p == 0 or n_l == 0:
continue
# Accumulate FPs and TPs, cumsum 轴向的累加和
fpc = (1 - tp[i]).cumsum(0) # FP累加和(预测为负样本且实际为负样本)
tpc = tp[i].cumsum(0) # TP累加和(预测为正样本且实际为正样本)
# Recall
recall = tpc / (n_l + eps) # recall curve
r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # 在不同置信度下的召回率
# Precision
precision = tpc / (tpc + fpc) # precision curve
p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # 在不同置信度下的精确率
# AP from recall-precision curve(在不同的IoU下的PR曲线)
for j in range(tp.shape[1]):
ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j])
if plot and j == 0:
py.append(np.interp(px, mrec, mpre)) # precision at [email protected]
# Compute F1 (harmonic mean of precision and recall)
f1 = 2 * p * r / (p + r + eps)
# names = [v for k, v in names.items() if k in unique_classes] # list: only classes that have data
# names = dict(enumerate(names)) # to dict
if plot:
plot_pr_curve(px, py, ap, Path(save_dir) / f'{prefix}PR_curve.png', names)
plot_mc_curve(px, f1, Path(save_dir) / f'{prefix}F1_curve.png', names, ylabel='F1')
plot_mc_curve(px, p, Path(save_dir) / f'{prefix}P_curve.png', names, ylabel='Precision')
plot_mc_curve(px, r, Path(save_dir) / f'{prefix}R_curve.png', names, ylabel='Recall')
i = smooth(f1.mean(0), 0.1).argmax() # max F1 index
p, r, f1 = p[:, i], r[:, i], f1[:, i]
tp = (r * nt).round() # true positives
fp = (tp / (p + eps) - tp).round() # false positives
return tp, fp, p, r, f1, ap, unique_classes.astype(int)
def compute_ap(recall, precision):
""" Compute the average precision, given the recall and precision curves
# Arguments
recall: The recall curve (list)
precision: The precision curve (list)
# Returns
Average precision, precision curve, recall curve
"""
# 增加初始值(P=1.0 R=0.0) 和 末尾值(P=0.0, R=1.0)
mrec = np.concatenate(([0.0], recall, [1.0]))
mpre = np.concatenate(([1.0], precision, [0.0]))
# Compute the precision envelope np.maximun.accumulate
# (返回一个数组,该数组中每个元素都是该位置及之前的元素的最大值)
mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))
# Integrate area under curve
method = 'interp' # methods: 'continuous', 'interp'
if method == 'interp': # np.interp(新的横坐标,原始数据横坐标,原始数据纵坐标) 线性插点
x = np.linspace(0, 1, 101) # 101-point interp (COCO))
# 积分(求曲线面积)
ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate
else: # 'continuous'
i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve
return ap, mpre, mrec