Skip to content

Instantly share code, notes, and snippets.

@viteinfinite viteinfinite/convert.py
Last active Aug 2, 2019

Embed
What would you like to do?
Core ML Conversion Script for the Keras Facenet Model
import coremltools
from coremltools.proto import NeuralNetwork_pb2
from coremltools.models.neural_network.quantization_utils import *
import numpy
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.utils import np_utils
from keras.models import load_model
import os.path
import sys
sys.path.append('./code/') # Import "code" from https://github.com/nyoki-mtl/keras-facenet
from inception_resnet_v1 import *
model = InceptionResNetV1(weights_path='facenet_keras_weights.h5')
def convert_lambda(layer):
if layer.function == scaling:
params = NeuralNetwork_pb2.CustomLayerParams()
params.className = "scaling"
params.parameters["scale"].doubleValue = layer.arguments['scale']
return params
else:
return None
coreml_model = coremltools.converters.keras.convert(
model,
input_names="image",
image_input_names="image",
output_names="output",
model_precision='float16',
image_scale=2/255.0,
red_bias=-1,
green_bias=-1,
blue_bias=-1,
add_custom_layers=True,
custom_conversion_functions={ "Lambda": convert_lambda })
coreml_model.save('facenet_keras_weights_coreml.mlmodel')
@sean7218

This comment has been minimized.

Copy link

commented May 16, 2019

where did you do the L2 Normalization? on the Swift side or ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.