diff --git a/models/yolo.py b/models/yolo.py index f659a04545b9..2f4bbe0f71d1 100644 --- a/models/yolo.py +++ b/models/yolo.py @@ -110,8 +110,8 @@ def __init__(self, cfg='yolov5s.yaml', ch=3, nc=None, anchors=None): # model, i s = 256 # 2x min stride m.inplace = self.inplace m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward + check_anchor_order(m) # must be in pixel-space (not grid-space) m.anchors /= m.stride.view(-1, 1, 1) - check_anchor_order(m) self.stride = m.stride self._initialize_biases() # only run once diff --git a/utils/autoanchor.py b/utils/autoanchor.py index 6cd2267a375a..7eb46af91195 100644 --- a/utils/autoanchor.py +++ b/utils/autoanchor.py @@ -17,7 +17,7 @@ def check_anchor_order(m): # Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary - a = m.anchors.prod(-1).view(-1) # anchor area + a = m.anchors.prod(-1).mean(-1).view(-1) # mean anchor area per output layer da = a[-1] - a[0] # delta a ds = m.stride[-1] - m.stride[0] # delta s if da.sign() != ds.sign(): # same order