# ONNX to PyTorch

[](https://github.com/Talmaj/onnx2pytorch/actions/workflows/lint_and_test.yml)
[](https://pepy.tech/project/onnx2pytorch)

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](https://github.com/onnx/models):
- [MobileNet](https://github.com/onnx/models/tree/master/vision/classification/mobilenet)
- [ResNet](https://github.com/onnx/models/tree/master/vision/classification/resnet)
- [ShuffleNet_V2](https://github.com/onnx/models/tree/master/vision/classification/shufflenet)
- [BERT-Squad](https://github.com/onnx/models/tree/master/text/machine_comprehension/bert-squad)
- [EfficientNet-Lite4](https://github.com/onnx/models/tree/master/vision/classification/efficientnet-lite4)
- [Fast Neural Style Transfer](https://github.com/onnx/models/tree/master/vision/style_transfer/fast_neural_style)
- [Super Resolution](https://github.com/onnx/models/tree/master/vision/super_resolution/sub_pixel_cnn_2016)
- [YOLOv4](https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/yolov4)
  (Not exactly the same, nearest neighbour interpolation in pytorch differs)
- [U-net](https://pytorch.org/hub/mateuszbuda_brain-segmentation-pytorch_unet/)
  (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.  
Set `experimental=True` in `ConvertModel` to be able to use `batch_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](https://github.com/psf/black)  
```black {source_file_or_directory}```  
Install it into pre-commit hook to always commit nicely formatted code:  
```pre-commit install```
### Testing
[Pytest](https://docs.pytest.org/en/latest/) and [tox](https://tox.readthedocs.io/en/latest/).  
```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.
            
         
        Raw data
        
            {
    "_id": null,
    "home_page": "https://github.com/ToriML/onnx2pytorch",
    "name": "onnx2pytorch",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": null,
    "author": "Talmaj Marinc",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/6d/92/6410219a630e21c261a6c464c49dfd3b459f1511dc066899518f00ed7805/onnx2pytorch-0.5.2.tar.gz",
    "platform": null,
    "description": "# ONNX to PyTorch\n\n[](https://github.com/Talmaj/onnx2pytorch/actions/workflows/lint_and_test.yml)\n[](https://pepy.tech/project/onnx2pytorch)\n\n\nA library to transform ONNX model to PyTorch. This library enables use of PyTorch \nbackend and all of its great features for manipulation of neural networks.\n\n## Installation\n```pip install onnx2pytorch```\n\n## Usage\n```\nimport onnx\nfrom onnx2pytorch import ConvertModel\n\nonnx_model = onnx.load(path_to_onnx_model)\npytorch_model = ConvertModel(onnx_model)\n```\n\nCurrently supported and tested models from [onnx_zoo](https://github.com/onnx/models):\n- [MobileNet](https://github.com/onnx/models/tree/master/vision/classification/mobilenet)\n- [ResNet](https://github.com/onnx/models/tree/master/vision/classification/resnet)\n- [ShuffleNet_V2](https://github.com/onnx/models/tree/master/vision/classification/shufflenet)\n- [BERT-Squad](https://github.com/onnx/models/tree/master/text/machine_comprehension/bert-squad)\n- [EfficientNet-Lite4](https://github.com/onnx/models/tree/master/vision/classification/efficientnet-lite4)\n- [Fast Neural Style Transfer](https://github.com/onnx/models/tree/master/vision/style_transfer/fast_neural_style)\n- [Super Resolution](https://github.com/onnx/models/tree/master/vision/super_resolution/sub_pixel_cnn_2016)\n- [YOLOv4](https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/yolov4)\n  (Not exactly the same, nearest neighbour interpolation in pytorch differs)\n- [U-net](https://pytorch.org/hub/mateuszbuda_brain-segmentation-pytorch_unet/)\n  (Converted from pytorch to onnx and then back)\n\n## Limitations\nKnown current version limitations are:\n- `batch_size > 1` could deliver unexpected results due to ambiguity of onnx's BatchNorm layer.   \nThat is why in this case for now we raise an assertion error.  \nSet `experimental=True` in `ConvertModel` to be able to use `batch_size > 1`.\n- Fine tuning and training of converted models was not tested yet, only inference.\n\n## Development\n### Dependency installation\n```pip install -r requirements.txt```\n\nFrom onnxruntime>=1.5.0 you need to add the \nfollowing to your .bashrc or .zshrc if you are running OSx:\n```export KMP_DUPLICATE_LIB_OK=True```\n\n### Code formatting\nThe Uncompromising Code Formatter: [Black](https://github.com/psf/black)  \n```black {source_file_or_directory}```  \n\nInstall it into pre-commit hook to always commit nicely formatted code:  \n```pre-commit install```\n\n### Testing\n[Pytest](https://docs.pytest.org/en/latest/) and [tox](https://tox.readthedocs.io/en/latest/).  \n```tox```\n#### Test fixtures\nTo test the complete conversion of an onnx model download pre-trained models: \n```./download_fixtures.sh```  \nUse flag `--all` to download more models.\nAdd any custom models to `./fixtures` folder to test their conversion.\n\n### Debugging\nSet `ConvertModel(..., debug=True)` to compare each converted\nactivation from pytorch with the activation from onnxruntime.  \nThis helps identify where in the graph the activations start to differ.\n",
    "bugtrack_url": null,
    "license": "apache-2.0",
    "summary": "Library to transform onnx model to pytorch.",
    "version": "0.5.2",
    "project_urls": {
        "Homepage": "https://github.com/ToriML/onnx2pytorch"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d741b24337f8b9feddf73e22b9dcadf254f449abf99e17312ea9c3fed87ad171",
                "md5": "af5558f7f31311679bb9b7b229ed15cb",
                "sha256": "96cb038fbcac15f4e83eac3d4452837a10a134719e5094105414b25c92046ed1"
            },
            "downloads": -1,
            "filename": "onnx2pytorch-0.5.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "af5558f7f31311679bb9b7b229ed15cb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 57612,
            "upload_time": "2025-11-01T23:31:50",
            "upload_time_iso_8601": "2025-11-01T23:31:50.504704Z",
            "url": "https://files.pythonhosted.org/packages/d7/41/b24337f8b9feddf73e22b9dcadf254f449abf99e17312ea9c3fed87ad171/onnx2pytorch-0.5.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6d926410219a630e21c261a6c464c49dfd3b459f1511dc066899518f00ed7805",
                "md5": "0e39c43486f89072983554613ede9487",
                "sha256": "25d4a1061039baf44448ad64cf68388e0e0462f40f827ae68da43691276d1257"
            },
            "downloads": -1,
            "filename": "onnx2pytorch-0.5.2.tar.gz",
            "has_sig": false,
            "md5_digest": "0e39c43486f89072983554613ede9487",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 41253,
            "upload_time": "2025-11-01T23:31:51",
            "upload_time_iso_8601": "2025-11-01T23:31:51.807505Z",
            "url": "https://files.pythonhosted.org/packages/6d/92/6410219a630e21c261a6c464c49dfd3b459f1511dc066899518f00ed7805/onnx2pytorch-0.5.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-11-01 23:31:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ToriML",
    "github_project": "onnx2pytorch",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "pytest",
            "specs": [
                [
                    ">=",
                    "5.4.1"
                ]
            ]
        },
        {
            "name": "pre-commit",
            "specs": [
                [
                    ">=",
                    "2.2.0"
                ]
            ]
        },
        {
            "name": "torch",
            "specs": [
                [
                    ">=",
                    "1.4.0"
                ]
            ]
        },
        {
            "name": "torchvision",
            "specs": [
                [
                    ">=",
                    "0.9.0"
                ]
            ]
        },
        {
            "name": "onnx",
            "specs": [
                [
                    ">=",
                    "1.6.0"
                ]
            ]
        },
        {
            "name": "onnxruntime",
            "specs": [
                [
                    ">=",
                    "1.12.0"
                ]
            ]
        },
        {
            "name": "onnxscript",
            "specs": [
                [
                    ">=",
                    "0.1.0"
                ]
            ]
        },
        {
            "name": "numpy",
            "specs": [
                [
                    ">=",
                    "1.18.1"
                ]
            ]
        }
    ],
    "tox": true,
    "lcname": "onnx2pytorch"
}