Edge AI Lab

Classify images and detect objects with real ONNX models in your browser, try quantization and pruning, and compare edge with cloud.

8interactive experiments

TensorFlow Lite and LiteRT

The conversion path to the FlatBuffer format for mobile devices, and the role of the interpreter and delegates.

Intermediate9 min read

Educational draft content that needs expert review before it is used as an academic reference. Specifications, versions and licences change; check the official source. No experimental result is shown here unless it was actually run.

Learning objectives

  • Describe the steps to convert a model to TFLite
  • Understand the roles of interpreter and delegate
  • Check the effect of conversion on accuracy

TensorFlow Lite (now also called LiteRT) is a toolkit for running models on mobile and embedded devices. You convert a trained model to a .tflite file in the compact, fast-loading FlatBuffer format and run it with a light interpreter.

Delegates hand part of the graph to specialised hardware such as a GPU or neural accelerator when present; operators the delegate lacks fall back to the CPU. The converter supports post-training quantization and can produce full int8 models.

Always compare the converted model with the original on a validation sample; conversion and quantization can change accuracy.

Terminology (with Arabic equivalents)

FlatBufferفلات بافر
Interpreterالمفسّر
Delegateالمفوِّض
Post-training quantizationالتكميم بعد التدريب
Representative datasetمجموعة تمثيلية للمعايرة

Worked example

You convert a Keras model to TFLite with int8 quantization: you need a small representative set (hundreds of samples) to calibrate the range, then you measure accuracy on the validation set and compare it with the float32 model.

Code snippet

PythonConversion with quantization (not executed here)
import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]

def rep_data():
    for x in calib_samples:      # a few hundred real inputs
        yield [x[None, ...]]
converter.representative_dataset = rep_data
tflite_bytes = converter.convert()
open('model.tflite', 'wb').write(tflite_bytes)

An illustrative snippet that was not executed in the lab environment; check library versions in their official documentation before use.

Check yourself

  1. What is the role of a delegate?

    • Compressing the file
    • Running part of the graph on specialised hardware
    • Training the model
    • Converting images
    Answer and explanation

    Running part of the graph on specialised hardware — A delegate hands parts of the graph to an accelerator if it supports them.

  2. Why do we need a representative set for int8 quantization?

    • To calibrate the value range
    • To retrain the model
    • To increase file size
    • To change the opset
    Answer and explanation

    To calibrate the value range — Calibration determines the activation range so it can be converted to integers.

  3. After conversion you must:

    • Assume accuracy did not change
    • Measure accuracy on a validation sample
    • Delete the original model
    • Cancel quantization
    Answer and explanation

    Measure accuracy on a validation sample — Conversion may change accuracy, so it must be measured.

References

Suggested reading (links not verified yet)

  • LiteRT / TensorFlow Lite documentation (ai.google.dev/edge/litert) Not verified

Image and audio processing in the experiments runs inside your browser. Your images and audio are never uploaded and conversations are not stored. Privacy