Skip to content

Commit

Permalink
Fix2 check_anchor_order() in pixel-space not grid-space (ultralytic…
Browse files Browse the repository at this point in the history
…s#7067)

Follows ultralytics#7060 which provided only a partial solution to this issue. ultralytics#7060 resolved occurences in yolo.py, this applies the same fix in autoanchor.py.
  • Loading branch information
glenn-jocher authored Mar 20, 2022
1 parent f8c4f61 commit e0c006c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions utils/autoanchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def metric(k): # compute metric
bpr = (best > 1 / thr).float().mean() # best possible recall
return bpr, aat

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

0 comments on commit e0c006c

Please sign in to comment.