onnx2torch is a Python library designed to convert ONNX (Open Neural Network Exchange) models into PyTorch models. It enables users to leverage existing ONNX models within the PyTorch ecosystem, facilitating migration and interoperability between frameworks. The current stable version is 1.6.0, with an active release cadence, typically every few months.
pip install onnx2torchVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the full cycle of using `onnx2torch`. It starts by creating a dummy PyTorch model, exporting it to an ONNX file, converting that ONNX file back into a PyTorch model using `onnx2torch.convert`, and finally running inference with the newly converted model to show its usage.
Update your import statement to `from onnx2torch import convert`. If migrating from pre-1.0 versions, carefully review the new `convert` function signature and API changes in the official documentation.
Refer to the `onnx2torch` GitHub repository for an up-to-date list of supported ONNX operators. You may need to modify your ONNX model to use only supported operators or explore alternative conversion methods for specific unsupported operations.
Whenever possible, export your ONNX model with fixed input shapes, or use known fixed-size inputs during conversion and inference. If dynamic shapes are critical, perform thorough testing with various input dimensions after conversion.
Ensure that the `opset_version` specified when exporting your ONNX model is compatible with the `onnx2torch` version you are using. Experiment with different `opset_version` values (e.g., 11, 13, 17) if you encounter conversion failures.
Check the `onnx2torch` documentation or GitHub for the list of supported ONNX operators. You may need to preprocess your ONNX model to remove or replace unsupported operators, or simplify the model graph.
Verify the expected input shape of your converted model. If the ONNX model used dynamic axes, ensure your inference input tensors adhere to the model's flexible dimensions. Double-check batch sizes, channels, and spatial dimensions.
For `onnx2torch` versions older than 1.0.0, use `from onnx2torch.converter import convert`. For version 1.0.0 and newer, ensure your installation is up-to-date and the import is `from onnx2torch import convert`.