Skip to content

Instantly share code, notes, and snippets.

@AlbertoCasasOrtiz
Created June 15, 2020 06:05
Show Gist options
  • Save AlbertoCasasOrtiz/4342dc2276de8a3a4874a0c97baaf63c to your computer and use it in GitHub Desktop.
Save AlbertoCasasOrtiz/4342dc2276de8a3a4874a0c97baaf63c to your computer and use it in GitHub Desktop.
Convert keras model into tflite
import tensorflow as tf
from keras.models import model_from_json
keras.backend.clear_session()
# Model reconstruction from JSON file.
with open('model.json', 'r') as f:
model = model_from_json(f.read())
# Load weights from h5 file.
model.load_weights('model.h5')
# Print summary.
model.summary()
# Save model and weights in a h5 file, then load again using tf.keras.
model.save('model_full.h5')
model = tf.keras.models.load_model('model_full.h5', compile=False)
# Converting a tf.Keras model to a TensorFlow Lite model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the TF Lite model.
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
f.write(tflite_model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment