PyTorch to TensorFlow Lite: Bridging the Gap for On-Device ML | by Ankit Rathore | Jul, 2024


Converting PyTorch model to TFlite. Source Wikimedia

The deep learning landscape is ever-evolving, with new frameworks and tools emerging constantly. While PyTorch has gained immense popularity for its dynamic and flexible nature, TensorFlow Lite presents an enticing opportunity to extend the reach of machine learning models into the realm of mobile and embedded devices. With TensorFlow Lite, developers can tap into a new world of possibilities, bringing sophisticated machine learning capabilities to smartphones, tablets, and IoT devices.

The need to convert a PyTorch model to TensorFlow Lite arises from the distinct advantages each framework offers. PyTorch, known for its ease of use and versatility, excels at rapid prototyping and experimental iterations. Meanwhile, TensorFlow Lite steps in as the ideal solution for production environments, offering a lightweight and efficient format tailored for deployment on devices with limited resources.

By converting PyTorch models to the TensorFlow Lite format, developers unlock the potential for on-device inference, enabling robust ML capabilities right in the user’s hands. This conversion process allows developers to bridge the gap between innovative ideas and real-world applications. In the following sections, we’ll delve into the step-by-step guide to performing this conversion, unlocking the full potential of these cutting-edge technologies.

While many online resources cover this topic, they often lack clarity or fail to provide detailed instructions for each step. This gap in information inspired the need for an enhanced blog post. This post will serve as a comprehensive, detailed guide, ensuring that the conversion process is accessible and straightforward stating every specific library version and the parameters being used.

Introduction to TensorFlow Lite:

TensorFlow Lite is a versatile and streamlined version of the popular TensorFlow machine learning framework. It’s specifically crafted for use on mobile and embedded devices, ensuring efficient and effective operation on devices with limited computational resources. TensorFlow Lite empowers developers to bring machine learning capabilities to a whole new range of devices, extending the reach of ML applications.

One of TensorFlow Lite’s standout features is its ability to run machine learning models with impressive speed and efficiency, even on devices that have constrained processing power and memory. This unique capability opens up a world of opportunities for developers to create innovative solutions for smartphones, tablets, and the Internet of Things (IoT) devices. By leveraging TensorFlow Lite, developers can now bring ML directly to the device, removing the need for constant cloud connections.

What makes TensorFlow Lite even more developer-friendly is its API. Designed with simplicity and flexibility in mind, the API supports an array of programming languages, including Java, Kotlin, Swift, Objective-C, and C++. This multilingual support lowers the barrier for developers to integrate machine learning into their diverse projects.

Converting a PyTorch model to the TensorFlow Lite format is a feasible task. The process involves three main steps:

  1. Exporting the PyTorch model to the Open Neural Network Exchange (ONNX) format, which acts as an intermediary. The ONNX format allows for seamless conversion between different ML frameworks.
  2. Converting the model from ONNX format to its TensorFlow equivalent.
  3. Converting the TensorFlow model into TensorFlow lite format.

Let’s dive deeper into the above mentioned steps exploring them further.

Setting up the Environment

Setting up the environment could be challenging, as there can be major dependency clashes if you try installing TensorFlow, Torch, TorchVision, and ONNX in the same environment. Since converting the model is often a one-time task, spending extensive time on environment setup may not be practical. Instead, I suggest using two separate environments. You can install ONNX, PyTorch, and their dependencies in the first environment for Step 1. For Steps 2 and 3, set up a second environment with TensorFlow, and onnx-tf. Here are the compatible versions I used for both environments:

Environment 1:

torch: 2.1.2
torchvision: 0.16.2
onnx: 1.15.0

Environment 2:

tensorflow-gpu: 2.11.0
tensorflow-probability: 0.12.1
onnx: 1.15.0
onnx-tf: 1.10.0

Please note that I am not entirely certain about the reason, but the conversion process appeared to work successfully for me only on a machine equipped with a GPU.

Converting a PyTorch model to the ONNX equivalent begins with exporting the PyTorch model to the ONNX format, which acts as an intermediary. The ONNX format allows for seamless conversion between different ML frameworks.

import torch
import onnx
model = torch.load('./pytorch_model.pkl')ONNX_PATH="./onn_model.onnx"sample_input = torch.load('./sample_input.pt') # sample input that model expectstorch.onnx.export(model=model,
args=sample_input,
f=ONNX_PATH,
verbose=False,
export_params=True,
do_constant_folding=False,
input_names=['input'],
output_names=['output'],
opset_version=12
)

The sample input refers to the sample of the input the model expects. You can even use a random tensor of the same shape as the shape the model excepts.

Remember: In the above code keep the verbose=False otherwise the process would keep on running indefinitely and also set the opset_version=12. Any version above this creates issue in the coming steps. If everything runs as intended then you should see an intermediary ONNX model created in your working directory.

Further, we jump to Step 2, i.e., converting the intermediate ONNX model to its TensorFlow equivalent.

import tensorflow as tf
from onnx_tf.backend import prepare
import onnx
TF_PATH = "./tf_model" # path where the tensorflow model will be stored
ONNX_PATH = "./onn_model.onnx" # path to my existing ONNX model
onnx_model = onnx.load(ONNX_PATH) # loading the onnx model
tf_rep = prepare(onnx_model) # creating TensorflowRep object# Exporting the TensorFlow model
tf_rep.export_graph(TF_PATH)

Note that the above code block will create you a directory in the name tf_model in you working directory whose directory structure looks somewhat like:

./tf_model/
├── assets
├── fingerprint.pb
├── saved_model.pb
└── variables
├── variables.data-00000-of-00001
└── variables.index

This represents our TensorFlow model and unsurprisingly, we will be utilizing this to get our tensorflow lite equivalent.

With the model now in TensorFlow format, the final step involves employing the `tf.lite.TFLiteConverter` class from TensorFlow Lite to refine and convert the model into the lightweight TFLite structure. This converted model is then ready for deployment on mobile and embedded devices.

SAVED_MODEL_DIR = "./tf_model"
converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_DIR)
tflite_model = converter.convert()
open('converted_model.tflite', 'wb').write(tflite_model)

The above code will save the TensorFlow lite model in you working directory in the name converted_model.tflite. You can utilize the above model for inference on edge device as shown below:

tflite_model_path = 'converted_model.tflite'
# Loading the TFLite model
interpreter = tf.lite.Interpreter(model_path=tflite_model_path)
interpreter.allocate_tensors()
# Get input and output details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Optionally, for checking the input and output details
print("Input details:", input_details)
print("Output details:", output_details)
# Prepare input data
dummy_data = np.random.rand(*input_details[0]['shape']).astype(np.float32)
sample_input = np.array(dummy_data).astype(np.float32)
# Provide the input to the model
interpreter.set_tensor(input_details[0]['index'], input_data)
# Run inference
interpreter.invoke()
# Get the output
output_data = interpreter.get_tensor(output_details[0]['index'])
# Utilize the output as needed
print("Output:", output_data)

The entire process is both fascinating and practical, opening doors to a wide range of possibilities for mobile machine learning applications. Developers can leverage the power of TensorFlow Lite to bring their ML projects to life on a variety of devices including the edge device like raspberry-pi, expanding the horizons of what’s possible in the world of machine learning.

Remember, each step should be executed precisely, and the appropriate libraries must be installed as mentioned above to ensure a successful conversion. Developers can then harness the full potential of TensorFlow Lite, pushing the boundaries of on-device machine learning.



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*