Last active
July 29, 2023 21:51
-
-
Save viteinfinite/1901d9cb6d26c21967a08aa5534e05c2 to your computer and use it in GitHub Desktop.
Core ML Conversion Script for the Keras Facenet Model
This file contains 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
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where did you do the L2 Normalization? on the Swift side or ?