microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
13.48k stars 2.76k forks source link

swin_transformer in pytorch fails to run after converting to onnx #19280

Open chenglopl opened 5 months ago

chenglopl commented 5 months ago

Describe the issue

After converting the swin_transformer model under the pytorch framework into an onnx model and enabling dynamic input, the following error occurs:

onnxruntime.capi.onnxruntime_pybind11_state.RuntimeException: [ONNXRuntimeError] : 6 : RUNTIME_EXCEPTION : Non-zero status code returned while running Reshape node. Name:'Reshape_2' Status Message: /onnxruntime_src/onnxruntime/core/providers/cpu/tensor/reshape_helper.h:42 onnxruntime::ReshapeHelper::ReshapeHelper(const onnxruntime::TensorShape&, std::vector&) gsl::narrow_cast(input_shape.Size()) == size was false. The input tensor cannot be reshaped to the requested shape. Input shape:{4,96,64,64}, requested shape:{1,96,4096}

Locate the error statement as x = x.view (-1, H * W, C),Using visual tools to view this layer is as follows: image The reshape parameter of this layer is recorded as a fixed value. How can I modify it to solve this problem

To reproduce

The conversion code is as follows: torch.onnx.export(model, (input0, input1), "./best.onnx", verbose=True, input_names=['input0', 'input1'], output_names=['output'], dynamic_axes={'input0':{0:'batch', 2:'height',3:'width'}, 'input1':{0:'batch'}, 'output':{0:'batch'}}, opset_version=11)

Urgency

No response

Platform

Windows

OS Version

ubuntu18.04

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.7.0

ONNX Runtime API

Python

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

tianleiwu commented 5 months ago

Could you try the following:

pip install --upgrade transformers optimum onnxruntime
python -m optimum.exporters.onnx -m microsoft/swin-base-patch4-window12-384-in22k --task image-classification swim_onnx

Tested with transformers==4.37.1 optimum==1.16.2 torch==2.1.1

 -[✓] ONNX model output names match reference model (logits)
        - Validating ONNX Model output "logits":
                -[✓] (2, 21841) matches (2, 21841)
                -[✓] all values close (atol: 1e-05)
github-actions[bot] commented 4 months ago

This issue has been automatically marked as stale due to inactivity and will be closed in 30 days if no further activity occurs. If further support is needed, please provide an update and/or more details.