Error compiling Keras model to ml.

I'm trying to convert a custom Keras model to ml model, via this code below:

##package versions
Keras version: 2.1.2
CoreML = 2.01b
tensorflow -1.10

Import coremltools from keras.models 
import load_model
model = load_model('dummy.hdf5')
print(model.summary())
Coreml_model = coremltools.converters.keras.convert(model)

However I'm running into this error:

RuntimeError: Error compiling model: "compiler error: Invalid data blob: 'block1_conv1_output' shape (C,H,W = 32,0,0) for output of layer: 'block1_conv1'




If I run the model (for inference etc) there are no errors, it's just converting it to a coreml model that's outputting the error. So there doesn't seem to be a problem with the model itself.

Here is a dummy model I've created with which I'm having the same problem:

https://drive.google.com/open?id=1gchckYmIeDFCwUAEDXKM-An3K1VQ4DuW

Thank you.




Any suggestions?


Thank you.

Replies

Hi bigmit37, We have been able to reproduce the issue. This is happening due to a bug in the Keras converter. This will be fixed.


The keras model's input shape is not fully defined, it is (None, None, 3). The Keras converter currently does not handle this scenario well. It tries to write "None" as an input shape in the mlmodel specification, since that is protobuf, it gets written as a 0. This causes the CoreML validator/compiler to error out.


A workaround until the fix makes it to the coremltools repo, is to produce a Keras model that has fixed input shape. That will convert properly. The mlmodel generated then can be made to have flexible input shapes using the flexible shape API in coremltools (https://apple.github.io/coremltools/generated/coremltools.models.neural_network.flexible_shape_utils.html).