转换 keras 模型后 coreml 中的输入形状不正确

Incorrect input shape in coreml after converting keras model

我有这样的keras模型:

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)

我将其转换为 coreml:

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

所以,我希望看到 MultiArray (Double 126x12),但我看到 MultiArray (Double 12)

你能帮忙说说我做错了什么吗?

As identified by G-mel 出现此错误是因为输入的长度为 2。然后 CoreMLtools 假定您的输入具有 [Seq, D] 的形状。您可以通过添加重塑层来绕过此购买:

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)

然后您的应用程序必须展平输入。然而,这并不理想,因为它在 GPU 上的效率不是很高。希望问题能尽快解决。