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
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.
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.
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.
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
Assistant Professor of Artificial Intelligence and Deep Learning
University of Al-Qadisiyah
About
Researcher and academic specialising in artificial intelligence (AI), deep learning, computer vision and natural language processing (NLP). The “Deep Learning in Arabic” project aims to enrich Arabic scientific and technical content with sound, free references and software for researchers and learners across the Arab world.
Edge AI Assistant
I am the Edge AI Assistant. Ask about a concept, technology, board or project and I will answer from the lab content with links.
Edge AI Assistant
Local Database Mode
Answers from the lab knowledge base only, without sending your question to any external party. Conversations are not stored.
Smart assistant
Answers from the site’s content
The assistant can make mistakes, so check the attached sources.
Welcome! I am Dr. Alaa Taima, and this is my smart assistant on “Deep Learning in Arabic”. Ask it about any term or topic in artificial intelligence, and it will answer from the site's content with links to the sources.
DL Bot
Your smart assistant · Connecting…
Enter to send · Shift + Enter for a new line · The assistant can make mistakes, so check the sources.