Camila_99$$
Camila_99$$
DIIDevHeads IoT Integration Server
Created by Enthernet Code on 7/16/2024 in #middleware-and-os
How do I convert a Keras model to TensorFlow Lite format?
@Marvee Amasi Great points! Just to add a bit more detail: 1/Export Keras model:
model.save('saved_model')

model.save('saved_model')

2/Convert to TensorFlow Lite:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model')
converter.optimizations = [tf.lite.Optimize.DEFAULT] # Optional but recommended
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)

import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model')
converter.optimizations = [tf.lite.Optimize.DEFAULT] # Optional but recommended
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)

3/Integrate with microcontroller: Use TensorFlow Lite for Microcontrollers library. More details here: TensorFlow Lite for Microcontrollers.
7 replies