Skip to content

Instantly share code, notes, and snippets.

@jamesonthecrow
Last active July 6, 2019 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesonthecrow/04dfeb4a7aa6e5e90d729d924b139678 to your computer and use it in GitHub Desktop.
Save jamesonthecrow/04dfeb4a7aa6e5e90d729d924b139678 to your computer and use it in GitHub Desktop.
Mobile machine learning with the Fritz CLI. https://fritz.ai
# Convert to mobile formats
import coremltools
import tensorflow as tf
import tempfile
def convert_to_coreml(model):
return coremltools.converters.keras.convert(
model,
input_names=['input'],
output_names=['digit']
)
def convert_to_tflite(model):
# save the model to a temp file so we can
# convert it.
keras_file = tempfile.mktemp()
model.save(keras_file, include_optimizer=False)
converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file)
return converter.convert()
mlmodel = convert_to_coreml(model)
tflite = convert_to_tflite(model)
model_name = "mnist_cnn_lr001_batchsize128"
# Keras
model.save(
model_name + ".h5",
include_optimizer="False"
)
# Core ML
mlmodel.save(model_name + ".mlmodel")
# TensorFlow Lite
with open(model_name + ".tflite", 'wb') as wfid:
wfid.write(tflite)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment