Library to transform onnx model to pytorch.
Project description
ONNX to PyTorch
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
could deliver unexpected results due to ambiguity of onnx's BatchNorm layer.
That is why in this case for now we raise an assertion error.
Setexperimental=True
inConvertModel
to be able to usebatch_size > 1
.- 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
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file onnx2pytorch-0.5.1.tar.gz
.
File metadata
- Download URL: onnx2pytorch-0.5.1.tar.gz
- Upload date:
- Size: 33.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c3ddf004838e67793817751affb426d77955290e473b492a058fc6edcee8d14 |
|
MD5 | 3942c3b214bbd75c8ff453645013a2de |
|
BLAKE2b-256 | 6685ff182f63c81419607182184d4dc7da3bbfc244dcdef3ac4aea2f1ab6b1a0 |
File details
Details for the file onnx2pytorch-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: onnx2pytorch-0.5.1-py3-none-any.whl
- Upload date:
- Size: 46.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fffda1c0c0d2eca5a54bffd2dea859ebe5be1ce22ed01981ae9d97b50385bcb |
|
MD5 | 317b5afe577ed973ee9fa08b6e8ca273 |
|
BLAKE2b-256 | 9784ef57a569ba57f4291d2dc918f863efa6ea56a944ab55677226eade2ea8d9 |