Skip to content

Instantly share code, notes, and snippets.

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 vmalyi/24cfdefb26bff2ea47a0b747f97ce5b6 to your computer and use it in GitHub Desktop.
Save vmalyi/24cfdefb26bff2ea47a0b747f97ce5b6 to your computer and use it in GitHub Desktop.
Run or Walk (Part 4): Import Keras Neural Network to iOS app with Core ML
from keras.models import model_from_json
import coremltools
import os
with open(os.path.join(KERAS_MODEL_FOLDER, model), 'r') as f:
loaded_model_json = f.read()
keras_model = model_from_json(loaded_model_json)
keras_model.load_weights(os.path.join(KERAS_MODEL_WEIGHTS_FOLDER, weights))
# convert model to Core ML
coreml_model = coremltools.converters.keras.convert(keras_model, input_names='input', output_names='output')
# set general model metadata
coreml_model.author = 'Viktor Malyi'
coreml_model.license = 'BSD'
coreml_model.short_description = 'Predicts the activity of the user: walking or running'
# set model input information
coreml_model.input_description['input'] = 'Sensor samples'
# set model output information
coreml_model.output_description['output'] = 'Activity'
f_name, f_ext = os.path.splitext(model)
coreml_model.save(os.path.join(OUTPUT_FOLDER, f_name + MLMODEL_FILE_EXTENSION))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment