简体   繁体   中英

Keras 'InputLayer object has no attribute 'inbound_nodes' when converting to CoreML

I get the error 'InputLayer object has no attribute 'inbound_nodes' when trying to convert my Keras model to CoreML model.

Here is my code:

     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")

The "diffinception.h5" is an Inception model imported from Keras with additional layers that I trained for transfer learning.

Here is my code for generating that model:

    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"])

I am up to date with the version of Keras. Using Python 2.7

I updated the _topology2.py code on my machine to match the version below (updated Jan 17th 2018):

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

This fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM