Keras - All layer names should be unique

后端 未结 3 683
滥情空心
滥情空心 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:40

    You can change the layer's name in keras, don't use 'tensorflow.python.keras'.

    Here is my sample code:

    from keras.layers import Dense, concatenate
    from keras.applications import vgg16
    
    num_classes = 10
    
    model = vgg16.VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=(64,64,3), pooling='avg')
    inp = model.input
    out = model.output
    
    model2 = vgg16.VGG16(include_top=False,weights='imagenet', input_tensor=None, input_shape=(64,64,3), pooling='avg')
    
    for layer in model2.layers:
        layer.name = layer.name + str("_2")
    
    inp2 = model2.input
    out2 = model2.output
    
    merged = concatenate([out, out2])
    merged = Dense(1024, activation='relu')(merged)
    merged = Dense(num_classes, activation='softmax')(merged)
    
    model_fusion = Model([inp, inp2], merged)
    model_fusion.summary()
    

提交回复
热议问题