Skip to main content

ONNX to PyTorch

PyPI - License Lint and Test Downloads PyPI

A library to transform ONNX model to PyTorch. This library enables use of PyTorch backend and all of its great features for manipulation of neural networks.

Installation

pip install onnx2pytorch

Usage

import onnx
from onnx2pytorch import ConvertModel

onnx_model = onnx.load(path_to_onnx_model)
pytorch_model = ConvertModel(onnx_model)

Operator coverage

ONNX_OPERATOR_CHECKLIST.md lists every ai.onnx operator the converter implements, along with the ones that are only partially supported. Operators and attributes that cannot be expressed in PyTorch raise rather than returning a wrong tensor.

Each operator is tested differentially against onnxruntime, falling back to onnx's ReferenceEvaluator, at every schema revision it exists in; see tests/onnx2pytorch/opset_matrix.py. Combinations that neither runtime can run are reported at the end of a test run, since the converter's behaviour for those is unverified.

Currently supported and tested models from onnx_zoo:

Limitations

Known current version limitations are:

  • batch_size > 1 is now supported by default.
    BatchNorm layers use inference mode (running statistics), which is correct for ONNX models
    exported for inference.
  • Fine tuning and training of converted models was not tested yet, only inference.

Development

Dependency installation

pip install -r requirements.txt

From onnxruntime>=1.5.0 you need to add the following to your .bashrc or .zshrc if you are running OSx: export KMP_DUPLICATE_LIB_OK=True

Code formatting

The Uncompromising Code Formatter: Black
black {source_file_or_directory}

Install it into pre-commit hook to always commit nicely formatted code:
pre-commit install

Testing

Pytest and tox.
tox

Test fixtures

To test the complete conversion of an onnx model download pre-trained models: ./download_fixtures.sh
Use flag --all to download more models. Add any custom models to ./fixtures folder to test their conversion.

Debugging

Set ConvertModel(..., debug=True) to compare each converted activation from pytorch with the activation from onnxruntime.
This helps identify where in the graph the activations start to differ.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onnx2pytorch-0.6.0.tar.gz (86.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

onnx2pytorch-0.6.0-py3-none-any.whl (134.1 kB view details)

Uploaded Python 3

File details

Details for the file onnx2pytorch-0.6.0.tar.gz.

File metadata

  • Download URL: onnx2pytorch-0.6.0.tar.gz
  • Upload date:
  • Size: 86.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for onnx2pytorch-0.6.0.tar.gz
Algorithm Hash digest
SHA256 b38762a1426944e1273e808a361011358895dbe7cbd87f2d762e0f058e98afa8
MD5 f0862ab8d7655b70e5720fa9abb69e0e
BLAKE2b-256 6feba9bdc5d5f74c716c687fd66db2fa1e9095385405f1134f80eb903e9b8a9c

See more details on using hashes here.

File details

Details for the file onnx2pytorch-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: onnx2pytorch-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 134.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for onnx2pytorch-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c286874b69dbdd4383b8fbba57a7ff6a54442e537df777f7ddcbed71780c0649
MD5 4a5fd65e1983c54dcda9ad2153f05d66
BLAKE2b-256 35b902db0339a94ab18f376ffc0c47afa923bed475b2ff9ddd9e109c3759464a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 files

0.5.3

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page