Bird
Raised Fist0
Computer Visionml~20 mins

Raspberry Pi deployment in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Raspberry Pi deployment
Problem:You have a computer vision model trained on a desktop that detects objects in images. Now you want to deploy this model on a Raspberry Pi to run real-time object detection using the Pi's camera.
Current Metrics:On desktop: accuracy 90%, inference time 50ms per image. On Raspberry Pi: model runs but inference time is 2 seconds per image, making real-time detection impossible.
Issue:The model is too large and slow for the Raspberry Pi hardware, causing slow inference and poor real-time performance.
Your Task
Optimize the computer vision model and deployment code to achieve inference time under 300ms per image on Raspberry Pi while maintaining accuracy above 85%.
You cannot retrain the model from scratch on the Pi.
You must use the existing trained model weights.
You can modify the model architecture, quantize the model, or optimize code for the Pi.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Computer Vision
import cv2
import numpy as np
import tflite_runtime.interpreter as tflite

# Load TFLite model and allocate tensors
interpreter = tflite.Interpreter(model_path='model_quantized.tflite')
interpreter.allocate_tensors()

# Get input and output tensors details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Initialize camera
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break
    # Resize frame to model input size
    input_shape = input_details[0]['shape']
    input_height, input_width = input_shape[1], input_shape[2]
    img = cv2.resize(frame, (input_width, input_height))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = img.astype(np.float32) / 255.0
    img = np.expand_dims(img, axis=0)

    # Set tensor
    interpreter.set_tensor(input_details[0]['index'], img)
    interpreter.invoke()

    # Get output
    output_data = interpreter.get_tensor(output_details[0]['index'])

    # Simple threshold to detect object presence
    if output_data[0][0] > 0.5:
        cv2.putText(frame, 'Object Detected', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)

    cv2.imshow('Raspberry Pi Object Detection', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Converted the original model to TensorFlow Lite format with 8-bit quantization to reduce size and speed up inference.
Used tflite_runtime interpreter optimized for Raspberry Pi instead of full TensorFlow.
Reduced input image size to match model input to lower computation.
Implemented real-time video capture and inference loop with efficient pre-processing.
Results Interpretation

Before: Inference time 2 seconds per image, accuracy 90%.

After: Inference time 250ms per image, accuracy 87%.

Model quantization and using lightweight runtime libraries can greatly improve inference speed on limited hardware like Raspberry Pi, with only a small accuracy trade-off.
Bonus Experiment
Try deploying the model using a different lightweight architecture like MobileNetV2 and compare inference speed and accuracy on Raspberry Pi.
💡 Hint
MobileNetV2 is designed for mobile devices and can be converted to TensorFlow Lite with quantization for fast inference.

Practice

(1/5)
1. What is the main advantage of deploying a machine learning model on a Raspberry Pi?
easy
A. It allows running ML models locally without internet connection
B. It increases the training speed of the model
C. It automatically improves model accuracy
D. It requires no power to operate

Solution

  1. Step 1: Understand Raspberry Pi deployment context

    Raspberry Pi is a small device that can run ML models locally, meaning it does not need to send data to the cloud.
  2. Step 2: Identify the main benefit

    Running models locally allows offline use and faster response without internet dependency.
  3. Final Answer:

    It allows running ML models locally without internet connection -> Option A
  4. Quick Check:

    Local inference = no internet needed [OK]
Hint: Local means no internet needed for predictions [OK]
Common Mistakes:
  • Confusing deployment with training speed
  • Thinking deployment improves accuracy automatically
  • Assuming Raspberry Pi needs no power
2. Which Python package is commonly used to run TensorFlow Lite models on a Raspberry Pi?
easy
A. tensorflow
B. tflite_runtime
C. scikit-learn
D. opencv-python

Solution

  1. Step 1: Identify the package for TensorFlow Lite on Raspberry Pi

    The lightweight package designed for running TFLite models on small devices is tflite_runtime.
  2. Step 2: Differentiate from other packages

    tensorflow is large and not optimized for Pi; scikit-learn is for classical ML; opencv-python is for image processing.
  3. Final Answer:

    tflite_runtime -> Option B
  4. Quick Check:

    TFLite on Pi = tflite_runtime [OK]
Hint: Use tflite_runtime for lightweight TensorFlow Lite on Pi [OK]
Common Mistakes:
  • Using full tensorflow package on Raspberry Pi
  • Confusing scikit-learn with TensorFlow Lite
  • Thinking OpenCV runs ML models directly
3. Given this code snippet on Raspberry Pi, what will output_data contain?
import numpy as np
from tflite_runtime.interpreter import Interpreter

interpreter = Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
medium
A. The model's prediction output as a numpy array
B. The input data shape as a tuple
C. A syntax error due to missing import
D. An empty list because invoke() was not called

Solution

  1. Step 1: Understand the TFLite interpreter flow

    The code loads a TFLite model, prepares input data, sets it, runs inference with invoke(), then gets output tensor.
  2. Step 2: Identify what output_data holds

    After invoke(), get_tensor() returns the model's prediction output as a numpy array.
  3. Final Answer:

    The model's prediction output as a numpy array -> Option A
  4. Quick Check:

    invoke() then get_tensor() = model output [OK]
Hint: invoke() runs model; get_tensor() fetches predictions [OK]
Common Mistakes:
  • Thinking output_data is input shape
  • Forgetting to call invoke() before get_tensor()
  • Assuming output_data is empty or error
4. You run this Raspberry Pi TFLite code but get an error: ValueError: Cannot set tensor: Dimension mismatch. What is the likely cause?
input_shape = interpreter.get_input_details()[0]['shape']
input_data = np.array([1, 2, 3], dtype=np.float32)
interpreter.set_tensor(interpreter.get_input_details()[0]['index'], input_data)
medium
A. The data type of input_data is wrong
B. The model file path is incorrect
C. The interpreter was not allocated tensors
D. Input data shape does not match model's expected input shape

Solution

  1. Step 1: Check input data shape vs model input shape

    The model expects input shape from input_shape, but input_data is a 1D array of length 3, likely mismatched.
  2. Step 2: Understand error cause

    Setting tensor with wrong shape causes dimension mismatch error.
  3. Final Answer:

    Input data shape does not match model's expected input shape -> Option D
  4. Quick Check:

    Shape mismatch = ValueError on set_tensor [OK]
Hint: Match input_data shape exactly to model input shape [OK]
Common Mistakes:
  • Ignoring shape mismatch and changing file path
  • Forgetting to call allocate_tensors()
  • Assuming data type causes dimension error
5. You want to deploy a computer vision model on Raspberry Pi to detect objects in real-time video. Which approach best balances speed and accuracy?
hard
A. Send video frames to a cloud server for processing and get results back
B. Use a large TensorFlow model and run it directly on Raspberry Pi CPU
C. Convert the model to TensorFlow Lite and use quantization for faster inference
D. Use OpenCV only without any ML model for detection

Solution

  1. Step 1: Consider Raspberry Pi hardware limits

    Raspberry Pi has limited CPU and memory, so large models run slowly.
  2. Step 2: Choose model optimization for speed and accuracy

    Converting to TensorFlow Lite and applying quantization reduces model size and speeds up inference with minimal accuracy loss.
  3. Step 3: Evaluate other options

    Sending to cloud adds latency; OpenCV alone lacks ML detection power; large models are too slow locally.
  4. Final Answer:

    Convert the model to TensorFlow Lite and use quantization for faster inference -> Option C
  5. Quick Check:

    TFLite + quantization = fast, accurate on Pi [OK]
Hint: Optimize model with TFLite quantization for Pi speed [OK]
Common Mistakes:
  • Trying to run large models without optimization
  • Relying on cloud adds delay and needs internet
  • Using only OpenCV misses ML detection benefits