glenn-jocher commited on
Commit
f327eee
1 Parent(s): 529fbc1

Fix `check_anchor_order()` in pixel-space not grid-space (#7060)

Browse files

* Update `check_anchor_order()`

Use mean area per output layer for added stability.

* Check in pixel-space not grid-space fix

Files changed (2) hide show
  1. models/yolo.py +1 -1
  2. utils/autoanchor.py +1 -1
models/yolo.py CHANGED
@@ -110,8 +110,8 @@ class Model(nn.Module):
110
  s = 256 # 2x min stride
111
  m.inplace = self.inplace
112
  m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward
 
113
  m.anchors /= m.stride.view(-1, 1, 1)
114
- check_anchor_order(m)
115
  self.stride = m.stride
116
  self._initialize_biases() # only run once
117
 
 
110
  s = 256 # 2x min stride
111
  m.inplace = self.inplace
112
  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)
114
  m.anchors /= m.stride.view(-1, 1, 1)
 
115
  self.stride = m.stride
116
  self._initialize_biases() # only run once
117
 
utils/autoanchor.py CHANGED
@@ -17,7 +17,7 @@ PREFIX = colorstr('AutoAnchor: ')
17
 
18
  def check_anchor_order(m):
19
  # 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
21
  da = a[-1] - a[0] # delta a
22
  ds = m.stride[-1] - m.stride[0] # delta s
23
  if da.sign() != ds.sign(): # same order
 
17
 
18
  def check_anchor_order(m):
19
  # Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary
20
+ a = m.anchors.prod(-1).mean(-1).view(-1) # mean anchor area per output layer
21
  da = a[-1] - a[0] # delta a
22
  ds = m.stride[-1] - m.stride[0] # delta s
23
  if da.sign() != ds.sign(): # same order