glenn-jocher commited on
Commit
9cd89b7
1 Parent(s): 178c109

Fix2 `check_anchor_order()` in pixel-space not grid-space (#7067)

Browse files

Follows https://github.com/ultralytics/yolov5/pull/7060 which provided only a partial solution to this issue. #7060 resolved occurences in yolo.py, this applies the same fix in autoanchor.py.

Files changed (1) hide show
  1. utils/autoanchor.py +5 -3
utils/autoanchor.py CHANGED
@@ -40,7 +40,8 @@ def check_anchors(dataset, model, thr=4.0, imgsz=640):
40
  bpr = (best > 1 / thr).float().mean() # best possible recall
41
  return bpr, aat
42
 
43
- anchors = m.anchors.clone() * m.stride.to(m.anchors.device).view(-1, 1, 1) # current anchors
 
44
  bpr, aat = metric(anchors.cpu().view(-1, 2))
45
  s = f'\n{PREFIX}{aat:.2f} anchors/target, {bpr:.3f} Best Possible Recall (BPR). '
46
  if bpr > 0.98: # threshold to recompute
@@ -55,8 +56,9 @@ def check_anchors(dataset, model, thr=4.0, imgsz=640):
55
  new_bpr = metric(anchors)[0]
56
  if new_bpr > bpr: # replace anchors
57
  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)
 
60
  s = f'{PREFIX}Done ✅ (optional: update model *.yaml to use these anchors in the future)'
61
  else:
62
  s = f'{PREFIX}Done ⚠️ (original anchors better than new anchors, proceeding with original anchors)'
 
40
  bpr = (best > 1 / thr).float().mean() # best possible recall
41
  return bpr, aat
42
 
43
+ stride = m.stride.to(m.anchors.device).view(-1, 1, 1) # model strides
44
+ anchors = m.anchors.clone() * stride # current anchors
45
  bpr, aat = metric(anchors.cpu().view(-1, 2))
46
  s = f'\n{PREFIX}{aat:.2f} anchors/target, {bpr:.3f} Best Possible Recall (BPR). '
47
  if bpr > 0.98: # threshold to recompute
 
56
  new_bpr = metric(anchors)[0]
57
  if new_bpr > bpr: # replace anchors
58
  anchors = torch.tensor(anchors, device=m.anchors.device).type_as(m.anchors)
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
62
  s = f'{PREFIX}Done ✅ (optional: update model *.yaml to use these anchors in the future)'
63
  else:
64
  s = f'{PREFIX}Done ⚠️ (original anchors better than new anchors, proceeding with original anchors)'