MCPcopy Index your code
hub / github.com/Talmaj/onnx2pytorch

github.com/Talmaj/onnx2pytorch @v0.5.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.5.3 ↗ · + Follow
497 symbols 1,715 edges 126 files 174 documented · 35%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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)

Currently supported and tested models from onnx_zoo: - MobileNet - ResNet - ShuffleNet_V2 - BERT-Squad - EfficientNet-Lite4 - Fast Neural Style Transfer - Super Resolution - YOLOv4 (Not exactly the same, nearest neighbour interpolation in pytorch differs) - U-net (Converted from pytorch to onnx and then back)

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.

Core symbols most depended-on inside this repo

extract_attr_values
called by 65
onnx2pytorch/convert/attribute.py
extract_attributes
called by 46
onnx2pytorch/convert/attribute.py
get_selection
called by 8
onnx2pytorch/utils.py
convert_operations
called by 8
onnx2pytorch/convert/operations.py
_to_positive_step
called by 8
onnx2pytorch/operations/slice.py
is_constant
called by 6
onnx2pytorch/utils.py
convert_linear_layer
called by 6
onnx2pytorch/convert/layer.py
get_inputs_names
called by 5
onnx2pytorch/utils.py

Shape

Function 298
Method 125
Class 63
Route 11

Languages

Python100%

Modules by API surface

tests/onnx2pytorch/operations/test_randomuniformlike.py18 symbols
onnx2pytorch/utils.py15 symbols
tests/onnx2pytorch/operations/test_lrn.py14 symbols
onnx2pytorch/operations/instancenorm.py14 symbols
tests/onnx2pytorch/operations/test_layernorm.py13 symbols
tests/onnx2pytorch/test_utils.py12 symbols
tests/onnx2pytorch/operations/test_add.py12 symbols
tests/onnx2pytorch/operations/test_sequenceconstruct.py11 symbols
tests/onnx2pytorch/operations/test_if.py10 symbols
tests/onnx2pytorch/operations/test_autopad.py10 symbols
onnx2pytorch/operations/batchnorm.py10 symbols
onnx2pytorch/convert/layer.py10 symbols

For agents

$ claude mcp add onnx2pytorch \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page