Skip to content
This repository was archived by the owner on Jun 4, 2025. It is now read-only.

Commit f327eee

Browse files
authored
Fix check_anchor_order() in pixel-space not grid-space (ultralytics#7060)
* Update `check_anchor_order()` Use mean area per output layer for added stability. * Check in pixel-space not grid-space fix
1 parent 529fbc1 commit f327eee

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

models/yolo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ def __init__(self, cfg='yolov5s.yaml', ch=3, nc=None, anchors=None): # model, i
110110
s = 256 # 2x min stride
111111
m.inplace = self.inplace
112112
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward
113+
check_anchor_order(m) # must be in pixel-space (not grid-space)
113114
m.anchors /= m.stride.view(-1, 1, 1)
114-
check_anchor_order(m)
115115
self.stride = m.stride
116116
self._initialize_biases() # only run once
117117

utils/autoanchor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
def check_anchor_order(m):
1919
# Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary
20-
a = m.anchors.prod(-1).view(-1) # anchor area
20+
a = m.anchors.prod(-1).mean(-1).view(-1) # mean anchor area per output layer
2121
da = a[-1] - a[0] # delta a
2222
ds = m.stride[-1] - m.stride[0] # delta s
2323
if da.sign() != ds.sign(): # same order

0 commit comments

Comments
 (0)