【问题标题】:Can't convert Keras model to Coreml无法将 Keras 模型转换为 Coreml
【发布时间】:2020-01-24 03:29:51
【问题描述】:

我尝试了各种方法来使用 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', optimizer='adam', metrics=['accuracy'])

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

导入 keras 导入coremltools

fcn_mlmodel = coremltools.converters.keras.convert(model, 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 层。 ——

【问题讨论】:

  • 欢迎来到 SO;您的模型看起来如何与您的问题无关;请发布您使用您提到的转换工具所尝试的具体内容,以及遇到的问题
  • @desertnaut 这是我在网上找到的将 Keras 模型转换为核心机器学习工具的代码。 import keras import coremltools model.summary() fcn_mlmodel = coremltools.converters.keras.convert( model, input_names = 'image', image_input_names = 'image', output_names = 'class_label' ) fcn_mlmodel.input_description['image']="Image size (224,224,3)" fcn_mlmodel.output_description['class_label']="Class label" fcn_mlmodel.save("Test_my.mlmodel") Err: Keras layer '' 不支持。
  • 不要在 cmets 空间中发布代码 - 它实际上是不可读的! 编辑和更新您的帖子。

标签: keras coreml


【解决方案1】:

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

pip install -U coremltools==3.0b6 tensorflow==1.13.1 keras==2.2.4 配合得很好。

【讨论】:

  • 解决了。只需使用 keras 即可重建模型。从张量流中删除所有导入
猜你喜欢
  • 2020-05-21
  • 2017-11-12
  • 2017-12-10
  • 2019-03-23
  • 2017-11-23
  • 2017-12-09
  • 2019-09-05
  • 2020-03-26
  • 2018-06-11
相关资源
最近更新 更多