This is a copy from offical web site https://tensorflow.google.cn/lit/convert
he following example shows how to convert a Keras model into a TensorFlow Lite model.
import tensorflow as tf
# Create a model using high-level tf.keras.* APIs
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(units=1, input_shape=[1]),
tf.keras.layers.Dense(units=16, activation=‘relu‘),
tf.keras.layers.Dense(units=1)
])
model.compile(optimizer=‘sgd‘, loss=‘mean_squared_error‘) # compile the model
model.fit(x=[-1, 0, 1], y=[-3, -1, 1], epochs=5) # train the model
# (to generate a SavedModel) tf.saved_model.save(model, "saved_model_keras_dir")
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the model.
with open(‘model.tflite‘, ‘wb‘) as f:
f.write(tflite_model)