Last active
November 12, 2017 19:48
-
-
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
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
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