Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
fenghuoxiguozu opened this issue Oct 14, 2024 · 3 comments
Open
1 task done

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

fenghuoxiguozu opened this issue Oct 14, 2024 · 3 comments
Labels
python Pull requests that update Python code question Further information is requested

Comments

@fenghuoxiguozu
Copy link

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

@fenghuoxiguozu fenghuoxiguozu added the question Further information is requested label Oct 14, 2024
@UltralyticsAssistant UltralyticsAssistant added the python Pull requests that update Python code label Oct 14, 2024
@UltralyticsAssistant
Copy link
Member

UltralyticsAssistant commented Oct 14, 2024

👋 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:

  • Notebooks with free GPU: Run on Gradient Open In Colab Open In Kaggle

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
Copy link
Author

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
Copy link
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Pull requests that update Python code question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants