ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.66k stars 16.33k forks source link

ONNX: export failure: Exporting the operator silu to ONNX opset version 12 is not supported. #7832

Closed gsx1378 closed 2 years ago

gsx1378 commented 2 years ago

Search before asking

Question

when i run "python export.py --weights ./models/download/yolov5n.pt --include onnx", the result is as follows: `export: data=data/coco128.yaml, weights=['./models/download/yolov5n.pt'], imgsz=[640, 640], batch_size=1, device=cpu, half=False, inplace=False, train=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=12, verbose=False, workspace=4, nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['onnx'] YOLOv5 🚀 v6.1-190-g4d59f65 Python-3.8.8 torch-1.7.0 CPU

Fusing layers... YOLOv5n summary: 213 layers, 1867405 parameters, 0 gradients, 4.5 GFLOPs

PyTorch: starting from models/download/yolov5n.pt with output shape (1, 25200, 85) (3.9 MB)

ONNX: starting export with onnx 1.9.0... ONNX: export failure: Exporting the operator silu to ONNX opset version 12 is not supported. Please open a bug to request ONNX export support for the missing operator.` How to solve this problem? Looking forward to your reply. ^_^

Additional

No response

github-actions[bot] commented 2 years ago

👋 Hello @gsx1378, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

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

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Im-JimmyHu commented 2 years ago

it looks like the the version of torch cause the error,may you can try to export with torch>=1.9,<=1.10.1,also compitable with the version onnx,wich i have successfullly convert the model to onnx. if not solved,may you also try to replace the silu with this:

class SiLU(Module):

    __constants__ = ['inplace']
    inplace: bool

    def __init__(self, inplace: bool = False):
        super(SiLU, self).__init__()
        self.inplace = inplace

    def forward(self, input: Tensor) -> Tensor:

        return input * torch.sigmoid(input)
glenn-jocher commented 2 years ago

@gsx1378 it appears you may have environment problems. Your ONNX and PyTorch versions are both a bit old.

Please ensure you meet all dependency requirements if you are attempting to run YOLOv5 locally. If in doubt, create a new virtual Python 3.9 environment, clone the latest repo (code changes daily), and pip install requirements.txt again from scratch.

💡 ProTip! Try one of our verified environments below if you are having trouble with your local environment.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

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

Models and datasets download automatically from the latest YOLOv5 release when first requested.

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

ZCzzzzzz commented 1 year ago

it looks like the the version of torch cause the error,may you can try to export with torch>=1.9,<=1.10.1,also compitable with the version onnx,wich i have successfullly convert the model to onnx. if not solved,may you also try to replace the silu with this:

class SiLU(Module):

    __constants__ = ['inplace']
    inplace: bool

    def __init__(self, inplace: bool = False):
        super(SiLU, self).__init__()
        self.inplace = inplace

    def forward(self, input: Tensor) -> Tensor:

        return input * torch.sigmoid(input)

Can you tell me where this code should be placed?

gsx1378 commented 1 year ago

it looks like the the version of torch cause the error,may you can try to export with torch>=1.9,<=1.10.1,also compitable with the version onnx,wich i have successfullly convert the model to onnx. if not solved,may you also try to replace the silu with this:

class SiLU(Module):

    __constants__ = ['inplace']
    inplace: bool

    def __init__(self, inplace: bool = False):
        super(SiLU, self).__init__()
        self.inplace = inplace

    def forward(self, input: Tensor) -> Tensor:

        return input * torch.sigmoid(input)

Can you tell me where this code should be placed?

"./utils/activations.py", You can search for it with this command: grep -rni "SiLU(" ./ --binary-files=without-match

glenn-jocher commented 1 year ago

@gsx1378 thank you for your question! Regarding the suggested code change, you can find the Silu function located in the ./models/common.py file. As for the additional code provided, this SiLU implementation can be found in ./utils/activations.py. You could find it by running this command: grep -rni "SiLU(" ./ --binary-files=without-match from the command line.

Please give this a try, and feel free to reach out if you need further assistance.