How to convert “resnet18” torch model to tensorflowlite

Mustafa Celik
2 min readJun 5

To convert a PyTorch model to TensorFlow Lite format, you’ll need to perform a few steps. Here’s an outline of the process:

1. Convert the PyTorch model to ONNX format: First, you’ll convert the PyTorch model to the ONNX (Open Neural Network Exchange) format. ONNX is an open standard for representing deep learning models that allows interoperability between different frameworks. You can use the `torch.onnx.export()` function to export your PyTorch model to ONNX.

2. Convert the ONNX model to TensorFlow format: Once you have the ONNX model, you can convert it to the TensorFlow format using the `tf.lite.TFLiteConverter.from_onnx_model()` method.

3. Convert the TensorFlow model to TensorFlow Lite format: Finally, you’ll convert the TensorFlow model to TensorFlow Lite format using the `tf.lite.TFLiteConverter.convert()` method.

Here’s an example code snippet that demonstrates the conversion process:

import torch
import torchvision
import tensorflow as tf
import tf2onnx
import onnx

# Step 1: Load the ResNet model from the .tar file
tar_path = 'model_weights.tar'
model = torchvision.models.resnet18(pretrained=False)
model.load_state_dict(torch.load(tar_path))

# Step 2: Convert the PyTorch model to ONNX format
dummy_input = torch.randn(1, 3, 224, 224) # Adjust input shape as per your model
onnx_path = 'model.onnx'
torch.onnx.export(model, dummy_input, onnx_path)

# Step 3: Load the ONNX model
onnx_model = onnx.load(onnx_path)

# Step 4: Convert the ONNX model to TensorFlow format
tf_model = tf2onnx.tfonnx.process_tf_graph(onnx_model)

# Step 5: Convert the TensorFlow model to TensorFlow Lite format
converter = tf.lite.TFLiteConverter.from_tf_graph(tf_model)
tflite_model = converter.convert()

# Step 6: Save the TensorFlow Lite model to a file
tflite_path = 'model.tflite'
with open(tflite_path, 'wb') as f:
f.write(tflite_model)

Make sure you have the required packages installed, such as `torch`, `torchvision`, `tensorflow`, `tf2onnx`, and `tf-nightly` (or an appropriate version of TensorFlow).

Adjust the model creation and input shape in the example code according to your specific model requirements. Once the conversion is complete, you should have a TensorFlow Lite model saved as `model.tflite`.

Note: The conversion process may have some limitations or specific requirements based on the complexity of the original model. In some cases, manual adjustments or optimizations might be needed to ensure a successful conversion.