PyTorch to TensorFlow Lite converter

Overview

PyTorch to TensorFlow Lite Converter

Converts PyTorch whole model into Tensorflow Lite

PyTorch -> Onnx -> Tensorflow 2 -> TFLite

Please install first

python3 setup.py install

Args

  • --torch-path Path to local PyTorch model, please save whole model e.g. torch.save(model, PATH)
  • --tf-lite-path Save path for Tensorflow Lite model
  • --target-shape Model input shape to create static-graph (default: (224, 224, 3)
  • --sample-file Path to sample image file. If model is not about computer-vision, please use leave empty and only enter --target-shape
  • --seed Seeds RNG to produce random input data when --sample-file does not exists
  • --log=INFO To see what happens behind

Basic usage of the script

To test with sample file:

python3 -m torch2tflite.converter
    --torch-path tests/mobilenetv2_model.pt
    --tflite-path mobilenetv2.tflite
    --sample-file sample_image.png
    --target-shape 224 224 3

To test with random input to check gradients:

python3 -m torch2tflite.converter
    --torch-path tests/mobilenetv2_model.pt
    --tflite-path mobilenetv2.tflite
    --target-shape 224 224 3
    --seed 10
Comments
  • Issue in the model converting

    Issue in the model converting

    Hi, I had an error below when the code is in torch.onnx.export. I doubt the problem is the model isn't loaded completely but I'm not sure. Is it possible to provide any model sample to verify my code environment. Thanks! 'dict' object has no attribute 'training'

    bug 
    opened by wenjingyang 10
  • No module named 'models'

    No module named 'models'

    I tried this code to convert my custom trained YOLOv5 model (.pt) It returns this message

    2021-05-31 10:26:03.181164: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0 No module named 'models'

    How can I fix this?

    opened by jhnhp 7
  • OSError: SavedModel file does not exist at: ./converter/tf_model/{saved_model.pbtxt|saved_model.pb}

    OSError: SavedModel file does not exist at: ./converter/tf_model/{saved_model.pbtxt|saved_model.pb}

    Hi,

    Thank you very much for sharing this code. I have tried applying it to my torch model, the conversion steps from torch to ONNX and ONNX to TF work fine, but it fails at the last step (TF to TFlite) with the following message:

    Traceback (most recent call last): File "converter.py", line 58, in main() File "converter.py", line 22, in main convert(torch_model_path=args.torch_model_path, File "/home/A49175/torch2tflite-master/converter/torch_to_tflite.py", line 157, in convert tf_to_tf_lite(tf_path=TF_PATH, tf_lite_path=tf_lite_model_path) File "/home/A49175/torch2tflite-master/converter/torch_to_tflite.py", line 74, in tf_to_tf_lite converter = tf.lite.TFLiteConverter.from_saved_model(tf_path) # Path to the SavedModel directory File "/home/A49175/.conda/envs/tflite-converter/lib/python3.8/site-packages/tensorflow/lite/python/lite.py", line 399, in from_saved_model saved_model = _load(saved_model_dir, tags) File "/home/A49175/.conda/envs/tflite-converter/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 578, in load return load_internal(export_dir, tags) File "/home/A49175/.conda/envs/tflite-converter/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 588, in load_internal loader_impl.parse_saved_model_with_debug_info(export_dir)) File "/home/A49175/.conda/envs/tflite-converter/lib/python3.8/site-packages/tensorflow/python/saved_model/loader_impl.py", line 56, in parse_saved_model_with_debug_info saved_model = _parse_saved_model(export_dir) File "/home/A49175/.conda/envs/tflite-converter/lib/python3.8/site-packages/tensorflow/python/saved_model/loader_impl.py", line 110, in parse_saved_model raise IOError("SavedModel file does not exist at: %s/{%s|%s}" % OSError: SavedModel file does not exist at: ./converter/tf_model/{saved_model.pbtxt|saved_model.pb}

    Do you have any idea of the origin of this problem ? I am using the packages versions specified in the requirements.txt.

    Thank you!

    opened by PaulKassis 4
  • Fails to create .tflite

    Fails to create .tflite

    ValueError: Could not open './converter/tf_lite_model.tflite'.

    I've loaded my .pt file and a sample test image....but looks like the conversion is not working. Can you rectify this?

    Thanks

    opened by rakshith-rm 3
  • error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

    error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

    I tried this code to convert my custom trained YOLOv5 model (.pt) It returns this bug: cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

    2021-06-02 03:17:26.651552: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1 Showing result!

    Traceback (most recent call last): File "converter.py", line 59, in main() File "converter.py", line 30, in main original_image, tf_lite_image, torch_image = get_example_input(args.test_im_path) File "/content/drive/My Drive/Univoice/yolov5/torch2tflite/converter/torch_to_tflite.py", line 28, in get_example_input image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

    How can I fix this?

    opened by thongvhoang 2
  • Under what license is your code?

    Under what license is your code?

    Hi,

    under what license is the code in your repository? I've found the overview at https://choosealicense.com/ useful to choose a license. It would be great if you could add a license file to the repository, because otherwise it can't be used.

    opened by fvollmer 2
  • Compatibility update (v1.1.0)

    Compatibility update (v1.1.0)

    • Development packages (black, pre-commit, poetry.lock etc.) added
    • Code style changed to black
    • Project setup changed to pyproject.toml from setup.py
    • VSCode settings added
    enhancement 
    opened by omerferhatt 0
  • TypeError: 'tuple' object cannot be interpreted as an integer

    TypeError: 'tuple' object cannot be interpreted as an integer

    I created below colab and getting below "TypeError: 'tuple' object cannot be interpreted as an integer" error. Could you please help me on this ? https://colab.research.google.com/drive/19JeOkrxlGP6KtkfGbrkO87COc4pvSZSE?usp=sharing#scrollTo=vdeytDxjFdZ3

    Traceback (most recent call last): File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "/usr/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/content/torch2tflite/torch2tflite/converter.py", line 187, in args.seed File "/content/torch2tflite/torch2tflite/converter.py", line 40, in init self.sample_data = self.load_sample_input(sample_file_path, target_shape, seed, normalize) File "/content/torch2tflite/torch2tflite/converter.py", line 121, in load_sample_input data = np.random.random(target_shape).astype(np.float32) File "mtrand.pyx", line 434, in numpy.random.mtrand.RandomState.random File "mtrand.pyx", line 425, in numpy.random.mtrand.RandomState.random_sample File "_common.pyx", line 291, in numpy.random._common.double_fill TypeError: 'tuple' object cannot be interpreted as an integer

    opened by nyadla-sys 2
  • Runtime Errori mportError: dlopen

    Runtime Errori mportError: dlopen

    When I have ran: python -m torch2tflite.converter --torch-path converted.pt --tflite-path siggraph17.tflite --targeet-shape 225 225 3 --seed 10

    I have the following error:

      File "/usr/local/Cellar/[email protected]/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 194, in _run_module_as_main
        return _run_code(code, main_globals, None,
      File "/usr/local/Cellar/[email protected]/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 87, in _run_code
        exec(code, run_globals)
      File "/XXX/torch2tflite/torch2tflite/converter.py", line 12, in <module>
        import onnx
      File "/XXX/torch2tflite/venv38/lib/python3.8/site-packages/onnx-1.11.0-py3.8-macosx-12-x86_64.egg/onnx/__init__.py", line 10, in <module>
        from .onnx_cpp2py_export import ONNX_ML
    ImportError: dlopen(/XXX/torch2tflite/venv38/lib/python3.8/site-packages/onnx-1.11.0-py3.8-macosx-12-x86_64.egg/onnx/onnx_cpp2py_export.cpython-38-darwin.so, 0x0002): symbol not found in flat namespace '__ZN6google8protobuf11MessageLite20ParseFromCodedStreamEPNS0_2io16CodedInputStreamE'```
    
    
    ANY SUGGESTION, SOLUTION?
    opened by celikmustafa89 0
  • ERROR:root:Can not load PyTorch model. Please make surethat model saved like `torch.save(model, PATH)`

    ERROR:root:Can not load PyTorch model. Please make surethat model saved like `torch.save(model, PATH)`

    I'm trying to convert a YOLOv5 best.pt weights file to a .tflite file so we can deploy the model on a flutter app.

    This is the code:

    `import torch weights_path = '/content/drive/MyDrive/Weights/best.pt' yolo_path = '/content/yolov5'

    model = torch.hub.load(yolo_path, 'custom', weights_path, source='local') # local repo torch.save(model, '/content/pipeline.pt' )`

    You can also go straight to the colab for all the colde. https://colab.research.google.com/drive/19JeOkrxlGP6KtkfGbrkO87COc4pvSZSE?usp=sharing

    I assume I'm not saving the model how the converter wants it, but I can't figure it out. Can you please explain exactly how the file needs to be saved so the code will work ?

    Thanks a lot in advance!

    opened by IvanStoykov 5
  • Installation issue

    Installation issue

    Dear Authors, Could you please verify the dependencies. It seems tflite-runtime~=2.5 is not available now.

    No local packages or working download links found for tflite-runtime~=2.5 error: Could not find suitable distribution for Requirement.parse('tflite-runtime~=2.5')

    Thanks Maulik

    bug 
    opened by maulikmadhavi 0
Releases(v1.0)
Owner
Omer Ferhat Sarioglu
DL/ML Enthusiast | Computer Vision
Omer Ferhat Sarioglu
A tutorial on "Bayesian Compression for Deep Learning" published at NIPS (2017).

Code release for "Bayesian Compression for Deep Learning" In "Bayesian Compression for Deep Learning" we adopt a Bayesian view for the compression of

Karen Ullrich 190 Dec 30, 2022
High-fidelity performance metrics for generative models in PyTorch

High-fidelity performance metrics for generative models in PyTorch

Vikram Voleti 5 Oct 24, 2021
torch-optimizer -- collection of optimizers for Pytorch

torch-optimizer torch-optimizer -- collection of optimizers for PyTorch compatible with optim module. Simple example import torch_optimizer as optim

Nikolay Novik 2.6k Jan 03, 2023
A PyTorch implementation of EfficientNet

EfficientNet PyTorch Quickstart Install with pip install efficientnet_pytorch and load a pretrained EfficientNet with: from efficientnet_pytorch impor

Luke Melas-Kyriazi 7.2k Jan 06, 2023
PyNIF3D is an open-source PyTorch-based library for research on neural implicit functions (NIF)-based 3D geometry representation.

PyNIF3D is an open-source PyTorch-based library for research on neural implicit functions (NIF)-based 3D geometry representation. It aims to accelerate research by providing a modular design that all

Preferred Networks, Inc. 96 Nov 28, 2022
An optimizer that trains as fast as Adam and as good as SGD.

AdaBound An optimizer that trains as fast as Adam and as good as SGD, for developing state-of-the-art deep learning models on a wide variety of popula

LoLo 2.9k Dec 27, 2022
Differentiable SDE solvers with GPU support and efficient sensitivity analysis.

PyTorch Implementation of Differentiable SDE Solvers This library provides stochastic differential equation (SDE) solvers with GPU support and efficie

Google Research 1.2k Jan 04, 2023
PyTorch Lightning Optical Flow models, scripts, and pretrained weights.

PyTorch Lightning Optical Flow models, scripts, and pretrained weights.

Henrique Morimitsu 105 Dec 16, 2022
Pytorch implementation of Distributed Proximal Policy Optimization

Pytorch-DPPO Pytorch implementation of Distributed Proximal Policy Optimization: https://arxiv.org/abs/1707.02286 Using PPO with clip loss (from https

Alexis David Jacq 164 Jan 05, 2023
Unofficial PyTorch implementation of DeepMind's Perceiver IO with PyTorch Lightning scripts for distributed training

Unofficial PyTorch implementation of DeepMind's Perceiver IO with PyTorch Lightning scripts for distributed training

Martin Krasser 251 Dec 25, 2022
An implementation of Performer, a linear attention-based transformer, in Pytorch

Performer - Pytorch An implementation of Performer, a linear attention-based transformer variant with a Fast Attention Via positive Orthogonal Random

Phil Wang 900 Dec 22, 2022
PyTorch framework A simple and complete framework for PyTorch, providing a variety of data loading and simple task solutions that are easy to extend and migrate

PyTorch framework A simple and complete framework for PyTorch, providing a variety of data loading and simple task solutions that are easy to extend and migrate

Cong Cai 12 Dec 19, 2021
Over9000 optimizer

Optimizers and tests Every result is avg of 20 runs. Dataset LR Schedule Imagenette size 128, 5 epoch Imagewoof size 128, 5 epoch Adam - baseline OneC

Mikhail Grankin 405 Nov 27, 2022
A Closer Look at Structured Pruning for Neural Network Compression

A Closer Look at Structured Pruning for Neural Network Compression Code used to reproduce experiments in https://arxiv.org/abs/1810.04622. To prune, w

Bayesian and Neural Systems Group 140 Dec 05, 2022
PyTorch implementation of Glow, Generative Flow with Invertible 1x1 Convolutions

glow-pytorch PyTorch implementation of Glow, Generative Flow with Invertible 1x1 Convolutions

Kim Seonghyeon 433 Dec 27, 2022
Model summary in PyTorch similar to `model.summary()` in Keras

Keras style model.summary() in PyTorch Keras has a neat API to view the visualization of the model which is very helpful while debugging your network.

Shubham Chandel 3.7k Dec 29, 2022
A Pytorch Implementation for Compact Bilinear Pooling.

CompactBilinearPooling-Pytorch A Pytorch Implementation for Compact Bilinear Pooling. Adapted from tensorflow_compact_bilinear_pooling Prerequisites I

169 Dec 23, 2022
OptNet: Differentiable Optimization as a Layer in Neural Networks

OptNet: Differentiable Optimization as a Layer in Neural Networks This repository is by Brandon Amos and J. Zico Kolter and contains the PyTorch sourc

CMU Locus Lab 428 Dec 24, 2022
PyTorch implementations of normalizing flow and its variants.

PyTorch implementations of normalizing flow and its variants.

Tatsuya Yatagawa 55 Dec 01, 2022
Code for paper "Energy-Constrained Compression for Deep Neural Networks via Weighted Sparse Projection and Layer Input Masking"

model_based_energy_constrained_compression Code for paper "Energy-Constrained Compression for Deep Neural Networks via Weighted Sparse Projection and

Haichuan Yang 16 Jun 15, 2022