Last active
July 6, 2019 13:50
-
-
Save jamesonthecrow/04dfeb4a7aa6e5e90d729d924b139678 to your computer and use it in GitHub Desktop.
Mobile machine learning with the Fritz CLI. https://fritz.ai
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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