转换为CoreML

时间:2018-01-18 19:43:48

标签: python tensorflow keras coreml coremltools

在尝试将我的Keras模型转换为CoreML模型时,我收到错误'InputLayer对象没有属性'inbound_nodes'。

这是我的代码:

     loaded_model = load_model("diffinception.h5")
     coreml_model = coremltools.converters.keras.convert(loaded_model, 
         input_names="imageSculp", output_names="category")
     coreml_model.save("transfertestinception.mlmodel")

“diffinception.h5”是从Keras导入的Inception模型,其中包含我为转移学习而训练的其他图层。

以下是我生成该模型的代码:

    model = applications.InceptionV3(weights = "imagenet", include_top=False,         
    input_shape = (img_width, img_height, 3), pooling = max)

    # Freeze layers
    for layer in model.layers:
        layer.trainable = False

    #Adding custom Layers
    x = model.output
    x = Flatten()(x)
    x = Dense(1024, activation="relu")(x)
    x = Dropout(0.5)(x)
    x = Dense(1024, activation="relu")(x)
    predictions = Dense(2, activation="softmax")(x)

    # creating the final model
    model_final = Model(inputs = model.input, outputs = predictions)
    # compile the model
    model_final.compile(loss = "categorical_crossentropy", optimizer =                                         
        optimizers.SGD(lr=0.001, momentum=0.9), metrics=["accuracy"])

我与Keras的版本保持同步。使用Python 2.7

1 个答案:

答案 0 :(得分:0)

我在我的机器上更新了_topology2.py代码以匹配以下版本(2018年1月17日更新):

https://github.com/apple/coremltools/blob/master/coremltools/converters/keras/_topology2.py

这解决了问题。