Skip to content

Commit b2b35b7

Browse files
authored
Fix2 check_anchor_order() in pixel-space not grid-space
Follows #7060 which provided only a partial solution to this issue. #7060 resolved occurences in yolo.py, this applies the same fix in autoanchor.py.
1 parent 178c109 commit b2b35b7

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

utils/autoanchor.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ def metric(k): # compute metric
4040
bpr = (best > 1 / thr).float().mean() # best possible recall
4141
return bpr, aat
4242

43-
anchors = m.anchors.clone() * m.stride.to(m.anchors.device).view(-1, 1, 1) # current anchors
43+
stride = m.stride.to(m.anchors.device).view(-1, 1, 1) # model strides
44+
anchors = m.anchors.clone() * stride # current anchors
4445
bpr, aat = metric(anchors.cpu().view(-1, 2))
4546
s = f'\n{PREFIX}{aat:.2f} anchors/target, {bpr:.3f} Best Possible Recall (BPR). '
4647
if bpr > 0.98: # threshold to recompute
@@ -55,8 +56,9 @@ def metric(k): # compute metric
5556
new_bpr = metric(anchors)[0]
5657
if new_bpr > bpr: # replace anchors
5758
anchors = torch.tensor(anchors, device=m.anchors.device).type_as(m.anchors)
58-
m.anchors[:] = anchors.clone().view_as(m.anchors) / m.stride.to(m.anchors.device).view(-1, 1, 1) # loss
59-
check_anchor_order(m)
59+
m.anchors[:] = anchors.clone().view_as(m.anchors)
60+
check_anchor_order(m) # must be in pixel-space (not grid-space)
61+
m.anchors /= stride
6062
s = f'{PREFIX}Done ✅ (optional: update model *.yaml to use these anchors in the future)'
6163
else:
6264
s = f'{PREFIX}Done ⚠️ (original anchors better than new anchors, proceeding with original anchors)'

0 commit comments

Comments
 (0)