Search code examples
pythonioskerascoremlcoremltools

Incorrect input shape in coreml after converting keras model


I have keras model like that:

inputlayer = Input(shape=(126,12))

model = BatchNormalization()(inputlayer)
model = Conv1D(16, 25, activation='relu')(model)
model = Flatten()(model)
model = Dense(output_size, activation='sigmoid')(model)

model = Model(inputs=inputlayer, outputs=model)

Which I convert to coreml:

coreml_model = coremltools.converters.keras.convert(model,
                                                    class_labels=classes)
coreml_model.save('speech_model.mlmodel')

So, I expect to see MultiArray (Double 126x12), but I see MultiArray (Double 12)

enter image description here

Could you help to say what I'm doing wrong?


Solution

  • As identified by G-mel It appears that this bug happens because the input is length 2. CoreMLtools then assumes your input has shape [Seq, D]. You can get around this buy adding a reshape layer:

    inputlayer = Input(shape=(126 * 12,))
    
    model = Reshape((126,12))(inputlayer)
    model = BatchNormalization()(model)
    model = Conv1D(16, 25, activation='relu')(model)
    model = Flatten()(model)
    model = Dense(output_size, activation='sigmoid')(model)
    
    model = Model(inputs=inputlayer, outputs=model)
    

    Your app then has to flatten the input. This is not ideal however because it is not very efficient on the GPU. Hopefully the problem will soon be fixed.