首页 > 解决方案 > 无法将 Keras 模型转换为 Coreml

问题描述

我尝试了各种方法来使用 core ml 工具将我的 Keras 模型转换为 core ml,但它给了我这个错误。

不支持 Keras 层。

我正在尝试将 .h5 模型转换为核心 ml,以便我可以在我的应用程序中使用它,但它给了我一些我无法解决的错误。另外,我尝试将 .h5 模型转换为 PB(冻结图),但那里出现错误。

这就是我的模型的外观。

img_input = layers.Input(shape=(224, 224, 3))


seed = 230

numpy.random.seed(seed)


x = layers.Conv2D(16, 3, activation='relu')(img_input)

x = layers.MaxPooling2D(2)(x)

x = layers.Conv2D(32, 3, activation='relu')(x)

x = layers.MaxPooling2D(2)(x)

x = layers.Flatten()(x)

x = layers.Dense(128, activation='relu')(x)

x = layers.Dropout(0.4)(x)

output = layers.Dense(3, activation='softmax')(x)

model = Model(img_input, output)

model.compile(loss='sparse_categorical_crossentropy',优化器='adam',metrics=['accuracy'])

这是我在网上找到的将 Keras 模型转换为核心机器学习工具的代码。

导入 keras 导入 coremltools

fcn_mlmodel = coremltools.converters.keras.convert(模型,input_names = 'image',image_input_names = 'image',output_names = 'class_label')

fcn_mlmodel.input_description['image']="图像大小(224,224,3)"

fcn_mlmodel.output_description['class_label']="类标签"

fcn_mlmodel.save("Test_my.mlmodel")

错误:不支持 Keras 层。——</p>

标签: kerascoreml

解决方案


无法重现您的问题,复制了所有内容。可能是你的版本有问题:

pip install -U coremltools==3.0b6 tensorflow==1.13.1 keras==2.2.4一起工作很好。


推荐阅读