anchor的存储位置
1 yaml配置文件中例如 models/yolov5s.yaml
# anchors
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32
2 模型文件中
读取方法
import torch
from models.experimental import attempt_load
model = attempt_load('./weights/best.pt', map_location=torch.device('cpu'))
m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1]
print(m.anchor_grid)
输出
tensor([[[[[[ 10., 13.]]],
[[[ 16., 30.]]],
[[[ 33., 23.]]]]],
[[[[[ 30., 61.]]],
[[[ 62., 45.]]],
[[[ 59., 119.]]]]],
[[[[[116., 90.]]],
[[[156., 198.]]],
[[[373., 326.]]]]]])
如何自动计算
训练时命令行添加–noautoanchor,表示不计算anchor,直接使用配置文件里的默认的anchor,不加该参数表示训练之前会自动计算。
if not opt.noautoanchor:
check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz)
参数dataset代表的是训练集,hyp[‘anchor_t’]是从配置文件hpy.scratch.yaml读取的超参数
计算方法
人工标注的边框(bounding box,有时简写bbox 或者 box)是人告诉机器的正确答案叫ground truth bounding box,简写 gt bbox或者gt box,表示已知正确的意思。
程序生成的一堆box,想要预测正确的gt box,这些生成的box叫anchor。
两个指标
bpr(best possible recall)
aat(anchors above threshold)
当配置文件中的anchor计算bpr(best possible recall)小于0.98时才会重新计算anchor。
best possible recall最大值1,如果bpr小于0.98,程序会根据数据集的label自动学习anchor的尺寸
check_anchors函数里包着一个函数
def metric(k): # compute metric
r = wh[:, None] / k[None]
x = torch.min(r, 1. / r).min(2)[0] # ratio metric
best = x.max(1)[0] # best_x
aat = (x > 1. / thr).float().sum(1).mean() # anchors above threshold
bpr = (best > 1. / thr).float().mean() # best possible recall
return bpr, aat
gt box的shape是(N,2),N是gt box总的个数,2是宽和高,程序用metric函数中的wh存储所有gt box的宽高。
在YOLOv5中有9种anchor,metric函数中的k就是通过配置(例如 models/yolov5s.yaml)读取的anchor。
metric函数根据k和wh来计算bpr,aat两个指标。
以下用gt代表wh,anchor代表k
wh(gt)的shape是[N, 2]表示N个gt box和和宽高这2个维度
k(anchor)的shape是[9,2]表示YOLOv5中的9种anchor和宽高这2个维度
r = wh[:, None] / k[None] 表示
先扩展维度
wh[:, None]的shape是[N, 2]变[N, 1, 2]
k[None]的shape是[9,2]变[1, 9, 2]
gt_height / anchor_height
gt_width / anchor_width
有可能大于1,也可能小于等于1
r的shape是[N,9,2]。9是YOLOv5中的9种anchor
x = torch.min(r, 1. / r).min(2)[0]
无论r大于1,还是小于等于1最后统一结果都要小于1
x的shape[N,9]
best的shape是[N],表示9个anchors长宽与GT长宽比值最接近1的比值
————————————————
版权声明:本文为CSDN博主「TheOldManAndTheSea」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:
目标检测 YOLOv5 anchor设置