Keras - All layer names should be unique

后端 未结 3 666
滥情空心
滥情空心 2021-01-11 23:25

I combine two VGG net in keras together to make classification task. When I run the program, it shows an error:

RuntimeError: The name \"predictions\"

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-11 23:44

    First, based on the code you posted you have no layers with a name attribute 'predictions', so this error has nothing to do with your layer Dense layer prediction: i.e:

    prediction = Dense(1, activation='sigmoid', 
                 name='main_output')(combineFeatureLayer)
    

    The VGG16 model has a Dense layer with name predictions. In particular this line:

    x = Dense(classes, activation='softmax', name='predictions')(x)
    

    And since you're using two of these models you have layers with duplicate names.

    What you could do is rename the layer in the second model to something other than predictions, maybe predictions_1, like so:

    model2 =  keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',
                                    input_tensor=None, input_shape=None,
                                    pooling=None,
                                    classes=1000)
    
    # now change the name of the layer inplace.
    model2.get_layer(name='predictions').name='predictions_1'
    

提交回复
热议问题