TensorFlow Lite Setup Guide

Install and configure TensorFlow Lite on a Raspberry Pi for on-device machine learning inference. Includes image classification example.

Andreas · April 12, 2026 · 8 min read

Introduction

TensorFlow Lite (TFLite) is the fastest way to run ML inference on Raspberry Pi. This guide walks through installation, downloading a pre-trained model, building a Python inference script, and real-time image classification via camera.

Prerequisites

  • Raspberry Pi 4 (2GB+) or Pi 5 (4GB+)
  • Python 3.9+
  • Virtual environment (recommended)
  • pip and python3-dev
  • USB camera or Pi Camera Module 3 (for camera example)
  • 500MB free disk space

Step 1 — Install TensorFlow Lite Runtime

The lightweight runtime (without the full TensorFlow stack) is all you need for inference:

python3 -m venv ~/tflite_env
source ~/tflite_env/bin/activate
pip install --upgrade pip setuptools
pip install tflite-runtime

Verify installation:

python3 -c "import tflite_runtime.interpreter as tflite; print('TFLite installed')"

If you're on armv6 (Pi Zero), pre-built wheels may not exist. Use Docker or build from source.

Step 2 — Download MobileNet V2 Model

Download a quantized MobileNet V2 model (3.5MB, optimized for Pi):

mkdir -p ~/models
cd ~/models
wget https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v2_1.0_224_quant.tflite
wget https://storage.googleapis.com/download.tensorflow.org/models/labels_imagenet_slim.txt

Verify files:

ls -lh ~/models/

Step 3 — Static Image Classification

Create classify_image.py for image inference:

import tflite_runtime.interpreter as tflite
import numpy as np
from PIL import Image
import sys

# Load model and allocate tensors
interpreter = tflite.Interpreter(model_path="mobilenet_v2_1.0_224_quant.tflite")
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Load and preprocess image
img = Image.open(sys.argv[1])
img = img.resize((224, 224))
input_data = np.array(img, dtype=np.uint8)
input_data = np.expand_dims(input_data, axis=0)

# Run inference
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()

# Get results
output = interpreter.get_tensor(output_details[0]['index'])
top_idx = np.argmax(output[0])
confidence = output[0][top_idx] / 255.0

# Load labels
with open("labels_imagenet_slim.txt") as f:
    labels = [line.strip() for line in f]

print(f"{labels[top_idx]}: {confidence:.1%}")

Run on a test image:

python3 classify_image.py ~/test.jpg

Performance: ~50ms on Pi 5, ~150ms on Pi 4.

Step 4 — Real-Time Camera Classification

Install camera and video dependencies:

pip install pillow
sudo apt install -y python3-picamera2 libcamera-tools

Create classify_camera.py:

import tflite_runtime.interpreter as tflite
import numpy as np
from PIL import Image
from picamera2 import Picamera2
import time

# Setup
interpreter = tflite.Interpreter(model_path="mobilenet_v2_1.0_224_quant.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Load labels
with open("labels_imagenet_slim.txt") as f:
    labels = [line.strip() for line in f]

# Camera setup
picam2 = Picamera2()
config = picam2.create_preview_configuration(main={"format": "RGB888", "size": (224, 224)})
picam2.configure(config)
picam2.start()

try:
    while True:
        frame = picam2.capture_array()
        frame_uint8 = np.array(frame, dtype=np.uint8)
        frame_expanded = np.expand_dims(frame_uint8, axis=0)

        interpreter.set_tensor(input_details[0]['index'], frame_expanded)
        interpreter.invoke()
        output = interpreter.get_tensor(output_details[0]['index'])
        
        top_idx = np.argmax(output[0])
        confidence = output[0][top_idx] / 255.0
        
        print(f"{labels[top_idx]}: {confidence:.1%}", end='\r')
        
except KeyboardInterrupt:
    print("\nShutdown")
finally:
    picam2.stop()

Run:

python3 classify_camera.py

Latency: ~80–120ms per frame on Pi 5 (feasible for real-time use).

Performance Table

Model Size Input Pi 4 Pi 5 Notes
MobileNet V2 3.5MB 224×224 150ms 50ms Quantized, accurate
SqueezeNet 1.2MB 224×224 80ms 25ms Lighter, faster
EfficientNet-Lite0 4.4MB 224×224 180ms 60ms Higher accuracy

Step 5 — Quantization and Edge TPU Compilation

For faster inference, quantize models or compile for Coral USB Accelerator.

Check if model is quantized:

pip install flatbuffers
python3 -c "import flatbuffers; from tflite.Model import Model; print('OK')"

For Coral compilation:

pip install edgetpu
edgetpu_compiler mobilenet_v2_1.0_224_quant.tflite

This generates mobilenet_v2_1.0_224_quant_edgetpu.tflite (5–10ms latency).

Troubleshooting

"No module named tflite_runtime" — Verify you're inside the virtual environment: source ~/tflite_env/bin/activate. On Pi Zero, install from source or use Docker.

"Cannot allocate memory" — Reduce input size (e.g., 160×160 instead of 224×224) or add swap: sudo dphys-swapfile swapoff && sudo nano /etc/dphys-swapfile (set CONF_SWAPSIZE=1024).

Camera not found — Run libcamera-hello -t 2 to test camera. If it fails, check ribbon cable or camera is enabled in raspi-config.

Slow inference — Confirm model is quantized (int8). Float32 models run ~2x slower. Profile with time python3 classify_image.py test.jpg.

Summary

TensorFlow Lite enables practical ML inference on Raspberry Pi with minimal setup. MobileNet V2 achieves 50–150ms latency depending on hardware. Real-time camera classification works smoothly on Pi 5. For production systems, consider Coral USB Accelerator for 5–10x speedup, or quantize custom models for maximum efficiency.

Comments