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.
pip install onnx2pytorch
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:
- 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)
Known current version limitations are:
batch_size > 1could deliver unexpected results due to ambiguity of onnx's BatchNorm layer.
That is why in this case for now we raise an assertion error.
ConvertModelto be able to use
batch_size > 1.
- Fine tuning and training of converted models was not tested yet, only inference.
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:
The Uncompromising Code Formatter: Black
Install it into pre-commit hook to always commit nicely formatted code:
To test the complete conversion of an onnx model download pre-trained models:
--all to download more models.
Add any custom models to
./fixtures folder to test their conversion.
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.