TensorFlow Lite là gì?
TensorFlow Lite (TFLite) là một framework mã nguồn mở do Google phát triển, dùng để triển khai mô hình học máy (Machine Learning) và học sâu (Deep Learning) trên các thiết bị tài nguyên thấp như:
- Điện thoại Android / iOS
- Thiết bị IoT (Raspberry Pi, Arduino, ESP32…)
- Edge device (thiết bị xử lý tại biên)
Vì sao nên dùng TensorFlow Lite?
TensorFlow Lite là giải pháp AI:
- Nhẹ và nhanh: mô hình được tối ưu kích thước và tốc độ
- Chạy offline: không cần gửi dữ liệu lên server
- Tiết kiệm pin: phù hợp thiết bị di động
- Hỗ trợ nhiều nền tảng: Android, iOS, Linux, microcontroller
Kiến trúc cơ bản của TensorFlow Lite
Quy trình làm việc với TensorFlow Lite thường gồm 3 bước:
- Huấn luyện mô hình bằng TensorFlow (Python)
- Chuyển đổi mô hình sang định dạng
.tflite - Triển khai & suy luận (inference) trên thiết bị
TensorFlow Model (.h5 / SavedModel)
↓
TensorFlow Lite Converter
↓
TensorFlow Lite Model (.tflite)
↓
Mobile / Edge Device
Cài đặt TensorFlow Lite
3.1. Cài đặt TensorFlow (Python)
TensorFlow Lite được tích hợp sẵn trong TensorFlow.
#Bash
pip install tensorflow
Kiểm tra cài đặt:
#python
import tensorflow as tf print(tf.__version__)
print(tf.__version__)
3.2. Cài đặt TensorFlow Lite cho Android
Trong file build.gradle:
dependencies {
implementation 'org.tensorflow:tensorflow-lite:2.14.0'
}
Nếu dùng GPU delegate:
implementation 'org.tensorflow:tensorflow-lite-gpu:2.14.0'
3.3. Cài đặt TensorFlow Lite cho iOS (Swift)
Dùng CocoaPods:
#ruby
pod 'TensorFlowLiteSwift'
Ví dụ sử dụng TensorFlow Lite với Python
4.1. Huấn luyện một mô hình đơn giản
Ví dụ mô hình phân loại chữ số MNIST:
#python
import tensorflow as tf
# Load data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train / 255.0
# Build model
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x_train, y_train, epochs=5)
4.2. Chuyển đổi sang TensorFlow Lite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open("mnist.tflite", "wb") as f:
f.write(tflite_model)
File mnist.tflite chính là mô hình dùng cho mobile/IoT.
4.3. Chạy inference với TensorFlow Lite (Python)
#python
import numpy as np
import tensorflow as tf
# Load model
interpreter = tf.lite.Interpreter(model_path="mnist.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Prepare input
input_data = np.expand_dims(x_test[0], axis=0).astype(np.float32)
# Inference
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output = interpreter.get_tensor(output_details[0]['index'])
print("Prediction:", np.argmax(output))
5. Tối ưu mô hình với TensorFlow Lite
5.1. Quantization (giảm độ chính xác)
Giúp:
- Giảm kích thước model
- Tăng tốc độ inference
Ví dụ post-training quantization:
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()
Ứng dụng thực tế của TensorFlow Lite
- Nhận diện khuôn mặt, vật thể (Object Detection)
- Nhận dạng giọng nói (Speech Recognition)
- Gợi ý bàn phím, dịch ngôn ngữ
- Thiết bị y tế, wearable
- IoT & Edge AI
Tổng kết
TensorFlow Lite là giải pháp tối ưu để đưa AI ra thế giới thực, đặc biệt trên các thiết bị có tài nguyên hạn chế. Với quy trình rõ ràng:
Huấn luyện → Chuyển đổi → Triển khai
bạn có thể dễ dàng xây dựng các ứng dụng AI chạy nhanh – nhẹ – offline.






