Last active
July 29, 2023 21:48
-
-
Save ohad7/d8458a66661ec4c35a4846ab945af78e to your computer and use it in GitHub Desktop.
Convert Keras h5 model to CoreML (reshape input layer)
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
from keras.models import load_model | |
from keras.layers import Input, Dense | |
from tensorflow import Tensor | |
from keras import backend as K | |
from keras.engine import InputLayer | |
model = load_model('MyModel.h5') | |
for layer in model.layers: | |
print layer | |
input_layer1 = InputLayer(input_shape=(51, 68, 3), name="input_1") | |
input_layer2 = InputLayer(input_shape=(51, 68, 3), name="input_2") | |
print "input shape:", input_layer1.input_shape | |
print "input tensor:", input_layer1.input | |
print "name:", input_layer1.name | |
print "sparse:", input_layer1.sparse | |
print "dtype:", input_layer1.dtype | |
model.layers[0] = input_layer1 | |
model.layers[1] = input_layer2 | |
model.save("reshaped-model.h5") | |
import coremltools | |
coreml_model = coremltools.converters.keras.convert('reshaped-model.h5', is_bgr=True, | |
input_names=['image1', 'image2'], image_input_names=['image1', 'image2'], | |
output_names=['output'], | |
blue_bias=-103.939, green_bias=-116.779, red_bias=-123.68) | |
coreml_model.save('Output.mlmodel') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why did you reshape input layer? Also, what Python version and Keras model are you using?
Thank you.