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

Onnx export with python models/export.py --dynamic does not produce dynamic outputs #3444

Closed
SamSamhuns opened this issue Jun 3, 2021 · 3 comments · Fixed by #3456
Closed
Labels
bug Something isn't working

Comments

@SamSamhuns
Copy link
Contributor

🐛 Bug

Exporting the pre-trained yolov5s model with the --dynamic option results in an onnx model with dynamic inputs however the outputs are not dynamic. This creates an issue when using the onnx model in another ml network server such as triton-server later on.

The torch.onnx.export call in models/export.py does not set the output_names and the output parameter for the dynamic axes has no affect on the final output nodes.

To Reproduce

git clone https://github.com/ultralytics/yolov5                              # clone repo
cd yolov5
pip install -r requirements.txt                                              # base requirements
pip install coremltools>=4.1 onnx>=1.9.0 scikit-learn==0.19.2                # export requirements
python models/export.py --dynamic                                            # export yolov5s model as onnx
python -c "import onnx; m=onnx.load('./yolov5s.onnx'); print(m.graph.input); print(m.graph.output)". # print input and output shape of onnx model

Output:

[name: "images"
type {
  tensor_type {
    elem_type: 1
    shape {
      dim {dim_param: "batch"}
      dim {dim_value: 3}
      dim {dim_param: "height"}
      dim {dim_param: "width"}
   }
  }
}
]
[name: "672"
type {
  tensor_type {
    elem_type: 1
    shape {
      dim {dim_value: 1}
      dim {dim_value: 25200}
      dim {dim_value: 85}
    }
  }
}
, name: "397"
type {
  tensor_type {
    elem_type: 1
    shape {
      dim {dim_value: 1}
      dim {dim_value: 3}
      dim {dim_value: 80}
      dim {dim_value: 80}
      dim {dim_value: 85}
    }
  }
}
, name: "495"
type {
  tensor_type {
    elem_type: 1
    shape {
      dim {dim_value: 1}
      dim {dim_value: 3}
      dim {dim_value: 40}
      dim { dim_value: 40}
      dim {dim_value: 85}
    }
  }
}
, name: "593"
type {
  tensor_type {
    elem_type: 1
    shape {
      dim {dim_value: 1}
      dim {dim_value: 3}
      dim {dim_value: 20}
      dim {dim_value: 20}
      dim {dim_value: 85}
    }
  }
}
]

Expected behavior

The output nodes should have dynamic output axes called batch instead of having the first output shape as dim {dim_value: 1}
i.e.

[name: "672"
type {
  tensor_type {
    elem_type: 1
    shape {
      dim {dim_param: "batch"}
      dim {dim_value: 25200}
      dim {dim_value: 85}
    }
  }
}
..... and so on for other output nodes

Environment

  • OS: Ubuntu 18.04.5
  • GPU Tesla V100

My current solution

Add output_names parameter to torch.onnx export and set the correct values for the outputs in the dynamic_axes

torch.onnx.export(model, img, f, verbose=False, opset_version=opt.opset_version, input_names=['images'],
                              training=torch.onnx.TrainingMode.TRAINING if opt.train else torch.onnx.TrainingMode.EVAL,
                              do_constant_folding=not opt.train,
                              output_names=['output0', 'output1', 'output2', 'output3'],
                              dynamic_axes={'images': {0: 'batch', 2: 'height', 3: 'width'},  # size(1,3,640,640)
                                            'output0': {0: 'batch'}, 'output1': {0: 'batch'},
                                            'output2': {0: 'batch'}, 'output3': {0: 'batch'}} if opt.dynamic else None)
@SamSamhuns SamSamhuns added the bug Something isn't working label Jun 3, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Jun 3, 2021

👋 Hello @SamSamhuns, 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://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

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), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

@SamSamhuns thanks for the bug report! The fastest way to get your fixes into the code is to submit a PR directly. Once you do this we can review and comment there. Thank you!

@SamSamhuns
Copy link
Contributor Author

Hi @glenn-jocher, I have created a PR #3456 . Please have a look, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants