yolov7中添加fppi评价指标
2023-12-29 19:37:10
参考:https://blog.csdn.net/liuhao3285/article/details/135233281?spm=1001.2014.3001.5502
fppi=fp/image_num
yolov7中增加FPPI
FPPI实现
yolo7中的评价指标实现位于utils/metrics.py中,我们只需要参照mAP指标在其中增加FPPI的内容即可:
def fppi_per_class(tp, conf, pred_cls, target_cls, image_num, plot=False, save_dir='.', names=(), return_plt=False):
""" Compute the false positives per image (FPPW) metric, given the recall and precision curves.
Source:
# Arguments
tp: True positives (nparray, nx1 or nx10).
conf: Objectness value from 0-1 (nparray).
pred_cls: Predicted object classes (nparray).
target_cls: True object classes (nparray).
plot: Plot precision-recall curve at mAP@0.5
save_dir: Plot save directory
# Returns
The fppi curve
"""
# Sort by objectness
i = np.argsort(-conf)
tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
# Find unique classes
unique_classes = np.unique(target_cls)
nc = unique_classes.shape[0] # number of classes, number of detections
# Create Precision-Recall curve and compute AP for each class
px, py = np.linspace(0, 1, 1000), np.linspace(0,100,1000) # for plotting
r = np.zeros((nc, 1000))
miss_rate = np.zeros((nc, 1000))
fppi = np.zeros((nc, 1000))
miss_rate_at_fppi = np.zeros((nc, 3)) # missrate at fppi 1, 0.1, 0.01
p_miss_rate = np.array([1, 0.1, 0.01])
for ci, c in enumerate(unique_classes):
i = pred_cls == c
n_l = (target_cls == c).sum() # number of labels
n_p = i.sum() # number of predictions
if n_p == 0 or n_l == 0:
continue
else:
# Accumulate FPs and TPs
fpc = (1 - tp[i]).cumsum(0)
tpc = tp[i].cumsum(0)
# Recall
recall = tpc / (n_l + 1e-16) # recall curve
r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases
miss_rate[ci] = 1 - r[ci]
fp_per_image = fpc/image_num
fppi[ci] = np.interp(-px,-conf[i], fp_per_image[:,0], left=0)
miss_rate_at_fppi[ci] = np.interp(-p_miss_rate, -fppi[ci], miss_rate[ci])
if plot:
fig = plot_fppi_curve(fppi, miss_rate, miss_rate_at_fppi, Path(save_dir) / 'fppi_curve.png', names)
if return_plt:
return fppi, miss_rate, miss_rate_at_fppi, fig
return miss_rate, fppi, miss_rate_at_fppi
将fppi以对数坐标画图:
def plot_fppi_curve(px,py, missrate_at_fppi, save_dir='fppi_curve.png', names=()):
fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
py = np.stack(py, axis=1)
# semi log
for i, y in enumerate(py.T):
ax.semilogx(px[i],y, linewidth=1, label=f'{names[i]} {missrate_at_fppi[i].mean():.3f}') # plot(recall, precision)
ax.semilogx(px.mean(0), py.mean(1), linewidth=3, color='blue', label='all classes %.3f' % missrate_at_fppi.mean())
ax.set_xlabel('False Positives Per Image')
ax.set_ylabel('Miss Rate')
ax.set_xlim(0, 100)
ax.set_ylim(0, 1)
ax.grid(True)
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
fig.savefig(Path(save_dir), dpi=250)
return fig
训练中调用
在test.py中在map计算的下方增加fppi的计算:
p, r, f1, mp, mr, map50, map, t0, t1, mfppi_1 = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
........
........
stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy
if len(stats) and stats[0].any():
p, r, ap, f1, ap_class = ap_per_class(*stats, plot=plots, save_dir=save_dir, names=names)
miss_rate, fppi, miss_rate_at_fppi = fppi_per_class(*stats, plot=plots, image_num= image_num, save_dir=save_dir, names=names)
mfppi_1 = miss_rate_at_fppi[:,0].mean()
ap50, ap = ap[:, 0], ap.mean(1) # AP@0.5, AP@0.5:0.95
mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean()
nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class
else:
nt = torch.zeros(1)# Print results
pf = "%20s" + "%12i" * 2 + "%12.3g" * 5 # print format
print(pf % ("all", seen, nt.sum(), mp, mr, map50, map, mfppi_1))
········
#返回fppi
return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist(), mfppi_1), maps, t
文章来源:https://blog.csdn.net/weixin_41012399/article/details/135296608
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!