Python Apache-2.0

onnx2pytorch

Transform ONNX model to PyTorch representation

T

Talmaj

Dernière activité 12 août 2026
Talmaj/onnx2pytorch

354

étoiles

71

forks

20

issues ouvertes

Ce README est souvent en anglais.

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.

Projets similaires

Convert ONNX models to PyTorch.

Pythonconvertexportonnx
EENOT-AutoDL
740 étoiles93

Simplify your onnx model

Pythondeep-learningonnxpytorch
Oonnxsim
4,4 k étoiles436

A tool for parsing, editing, optimizing, and profiling ONNX models.

Pythononnxpython
TThanatosShinji
493 étoiles68