Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras - All layer names should be unique

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" is used 2 times in the model. All layer names should be unique.

I was confused because I only use prediction layer once in my code:

from keras.layers import Dense
import keras
from keras.models import  Model
model1 = keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',
                                input_tensor=None, input_shape=None,
                                pooling=None,
                                classes=1000)
model1.layers.pop()

model2 =  keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',
                                input_tensor=None, input_shape=None,
                                pooling=None,
                                classes=1000)
model2.layers.pop()
for layer in model2.layers:
    layer.name = layer.name + str("two")
model1.summary()
model2.summary()
featureLayer1 = model1.output
featureLayer2 = model2.output
combineFeatureLayer = keras.layers.concatenate([featureLayer1, featureLayer2])
prediction = Dense(1, activation='sigmoid', name='main_output')(combineFeatureLayer)

model = Model(inputs=[model1.input, model2.input], outputs= prediction)
model.summary()

Thanks for @putonspectacles help, I follow his instruction and find some interesting part. If you use model2.layers.pop() and combine the last layer of two models using "model.layers.keras.layers.concatenate([model1.output, model2.output])", you will find that the last layer information is still showed using the model.summary(). But actually they do not exist in the structure. So instead, you can use model.layers.keras.layers.concatenate([model1.layers[-1].output, model2.layers[-1].output]). It looks tricky but it works.. I think it is a problem about synchronization of the log and structure.

like image 310
dashenswen Avatar asked Apr 17 '17 13:04

dashenswen


People also ask

How do you name layers in keras?

Rename the layers keras API now do not allow renaming layers via layer.name = "new_name" . Instead you must assign your new name to the private attribute, layer. _name . So we see the layers now have the new names!

What is hidden layer in keras?

What is a Hidden Layer? In neural networks, a hidden layer is located between the input and output of the algorithm, in which the function applies weights to the inputs and directs them through an activation function as the output.

What is layers in TensorFlow?

Defining models and layers in TensorFlow. Most models are made of layers. Layers are functions with a known mathematical structure that can be reused and have trainable variables. In TensorFlow, most high-level implementations of layers and models, such as Keras or Sonnet, are built on the same foundational class: tf.


2 Answers

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()
like image 118
Phan Van Hoai Duc Avatar answered Nov 16 '22 01:11

Phan Van Hoai Duc


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'
like image 37
parsethis Avatar answered Nov 16 '22 00:11

parsethis