ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.24k stars 16.22k forks source link

Yolov5_V7.0 while export dynamic onnx, netron layer error #13356

Open fenghuoxiguozu opened 1 day ago

fenghuoxiguozu commented 1 day ago

Search before asking

Question

use export code: python export.py --weights yolov5m.pt --include onnx --dynamic --simplify

then i put it into netron, find last Detect layer error image

how to solve it

Additional

No response

UltralyticsAssistant commented 1 day ago

πŸ‘‹ Hello @fenghuoxiguozu, thank you for reaching out about YOLOv5 πŸš€!

It seems like you're encountering an issue with exporting to ONNX with dynamic axes. To help you better, we suggest providing a minimum reproducible example to identify the problem more precisely. πŸ›

In the meantime, please ensure that you've followed the installation requirements and that your setup meets the conditions specified in our RequirementsπŸ“‹. Here's a quick start guide:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

Ensure you're using a verified environment. YOLOv5 runs smoothly in several setups, including:

For a dynamic ONNX export, make sure you've installed all dependencies, including the latest PyTorch and ONNX.

Status

YOLOv5 CI

If this badge is green, all CI tests are passing, ensuring reliability.

πŸ” Please remember, this is an automated response, but don't worryβ€”an Ultralytics engineer will assist you soon. In the meantime, explore our YOLOv8 for advanced capabilities πŸš€!

Looking forward to your minimum reproducible example or any additional details to help resolve your query more effectively.

fenghuoxiguozu commented 1 day ago

class Detect(nn.Module):

YOLOv5 Detect head for detection models

.....

def forward(self, x):   # [(4,256,80,80),(4,512,40,40),(4,1024,40,40)]
    z = []  # inference output
    for i in range(self.nl):
        x[i] = self.m[i](x[i])  # conv
        bs, _, ny, nx = x[i].shape#map(int, x[i].shape ) # x[i].shape  # x(bs,255,20,20) to x(bs,3,20,20,85)
        x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()

        # x:[(1,75,32,32),(1,75,16,16),(1,75,8,8)]  =>  [(1,3,32,32,25),(1,3,16,16,25),(1,3,8,8,25)]
        if not self.training:  # inference
            if self.dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
                self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)

            if isinstance(self, Segment):  # (boxes + masks)
                xy, wh, conf, mask = x[i].split((2, 2, self.nc + 1, self.no - self.nc - 5), 4)
                xy = (xy.sigmoid() * 2 + self.grid[i]) * self.stride[i]  # xy
                wh = (wh.sigmoid() * 2) ** 2 * self.anchor_grid[i]  # wh
                y = torch.cat((xy, wh, conf.sigmoid(), mask), 4)
            else:  # Detect (boxes only)
                xy, wh, conf = x[i].sigmoid().split((2, 2, self.nc + 1), 4)
                xy = (xy * 2 + 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, self.na * nx * ny, self.no))

    return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x)
pderrenger commented 1 day ago

@fenghuoxiguozu thank you for sharing your code snippet. If you're experiencing issues, please ensure you're using the latest version of YOLOv5. If the problem persists, feel free to provide more details about the specific error or behavior you're encountering.