From 14f7439e2cfef0da57f144e48e180a2045a48406 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sun, 3 Apr 2022 18:00:49 +0200 Subject: [PATCH 1/3] Update --- export.py | 1 + models/yolo.py | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/export.py b/export.py index cc7a74db9af2..2f2f4df39284 100644 --- a/export.py +++ b/export.py @@ -480,6 +480,7 @@ def run( elif isinstance(m, Detect): m.inplace = inplace m.onnx_dynamic = dynamic + m.export = True if hasattr(m, 'forward_export'): m.forward = m.forward_export # assign custom forward (optional) diff --git a/models/yolo.py b/models/yolo.py index e18614cb37bd..5f5dcf057d5b 100644 --- a/models/yolo.py +++ b/models/yolo.py @@ -37,6 +37,7 @@ class Detect(nn.Module): stride = None # strides computed during build onnx_dynamic = False # ONNX export parameter + export = False # export mode def __init__(self, nc=80, anchors=(), ch=(), inplace=True): # detection layer super().__init__() @@ -66,12 +67,13 @@ def forward(self, x): y[..., 0:2] = (y[..., 0:2] * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953 - xy = (y[..., 0:2] * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy - wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh - y = torch.cat((xy, wh, y[..., 4:]), -1) + xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0 + xy = (xy * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy + wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh + y = torch.cat((xy, wh, conf), 4) z.append(y.view(bs, -1, self.no)) - return x if self.training else (torch.cat(z, 1), x) + return x if self.training else torch.cat(z, 1) if self.export else (torch.cat(z, 1), x) def _make_grid(self, nx=20, ny=20, i=0): d = self.anchors[i].device From 43f0be574dee08f1bb7659034bf4e40e3a3b3400 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sun, 3 Apr 2022 21:11:40 +0200 Subject: [PATCH 2/3] Update --- models/yolo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/yolo.py b/models/yolo.py index 21c942b4e784..74631ec2d1f2 100644 --- a/models/yolo.py +++ b/models/yolo.py @@ -73,7 +73,7 @@ def forward(self, x): y = torch.cat((xy, wh, conf), 4) z.append(y.view(bs, -1, self.no)) - return x if self.training else torch.cat(z, 1) if self.export else (torch.cat(z, 1), x) + return x if self.training else (torch.cat(z, 1), ) if self.export else (torch.cat(z, 1), x) def _make_grid(self, nx=20, ny=20, i=0): d = self.anchors[i].device From 0ea9cfd71ce26273b74575fafd69526a18dfa5b7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 3 Apr 2022 19:12:44 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- models/yolo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/yolo.py b/models/yolo.py index 74631ec2d1f2..fee5e932fd4d 100644 --- a/models/yolo.py +++ b/models/yolo.py @@ -73,7 +73,7 @@ def forward(self, x): y = torch.cat((xy, wh, conf), 4) z.append(y.view(bs, -1, self.no)) - return x if self.training else (torch.cat(z, 1), ) if self.export else (torch.cat(z, 1), x) + return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x) def _make_grid(self, nx=20, ny=20, i=0): d = self.anchors[i].device