Coremltools: Keras MobileNet CoreML model input type is MultiArray instead of Image

Created on 26 Jun 2018  路  2Comments  路  Source: apple/coremltools

I was able to successfully save the MobileNet NIMA model as a coreML model however the input is not what I was expecting. It shows the input as MultiArray instead of an Image

base_model = MobileNet((None, None, 3), alpha=1, include_top=False, pooling='avg', weights=None)
x = Dropout(0.75)(base_model.output)
x = Dense(10, activation='softmax')(x)

model = Model(base_model.input, x)
model.load_weights('weights/mobilenet_weights.h5')
coreml_model = coremltools.converters.keras.convert(model)
coreml_model.save("mobilenet_nima2.mlmodel")

CoreML metaData
screen shot 2018-06-25 at 7 17 43 pm

Ideally what the input and output should resemble:
screen shot 2018-06-25 at 7 17 40 pm

I also tried converting the model by passing in an image_input_names parameter

coreml_model = coremltools.converters.keras.convert(model, image_input_names='img', input_names='img')

but I received an unpack error from the coreML builder tools

/coremltools/models/neural_network/builder.py", line 2547, in set_pre_processing_parameters
    channels, height, width = array_shape
ValueError: need more than 1 value to unpack

Code where the error originates
https://github.com/apple/coremltools/blob/master/coremltools/models/neural_network.py

        # Add image inputs
        for input_ in spec.description.input:
            if input_.name in image_input_names:
                if input_.type.WhichOneof('Type') == 'multiArrayType':
                    array_shape = tuple(input_.type.multiArrayType.shape)
                    channels, height, width = array_shape

Any idea why the keras model is converting to a CoreML model with input MultiArray type instead of an image?

There have been MobileNet models that have been successfully converted to a CoreML model with image as an input e.g. https://github.com/hollance/MobileNet-CoreML

Most helpful comment

This seems related to issue #189
Check the the Keras model has the input shapes fully defined.

All 2 comments

This seems related to issue #189
Check the the Keras model has the input shapes fully defined.

Thanks @aseemw !

For future reference. I was able to generate the coreml model with the image input by modifying how I generated the keras model:

base_model = MobileNet((None, None, 3), alpha=1, include_top=False, pooling='avg', weights=None)

became

base_model = MobileNet((224, 224, 3), alpha=1, include_top=False, pooling='avg', weights=None)

then
coreml_model = coremltools.converters.keras.convert(model, image_input_names='img', input_names='img')

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lutzroeder picture lutzroeder  路  4Comments

G-mel picture G-mel  路  4Comments

ghost picture ghost  路  3Comments

HussainHaris picture HussainHaris  路  5Comments

xternalz picture xternalz  路  3Comments