keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 913 forks source link

Using duplicate pretrained models in Model() causes a layer name conflict #119

Open mekaneeky opened 5 years ago

mekaneeky commented 5 years ago

Basically, I suspect the duplicate layer names error below comes from the xception pretrained models. Will try and cook up a fix and set a pull request ASAP The model definition:

from keras.applications.xception import Xception

xception_weights_path = "/home/leila/Code/siamese_traffic_density/pretrained/xception_weights_tf_dim_ordering_tf_kernels_notop.h5"
xception_conv_base = Xception(include_top=False, weights=None, input_tensor=None, input_shape=(224, 224, 3), pooling=None, classes=None)
xception_conv_base.load_weights(xception_weights_path)
xception_conv_base_previous = Xception(include_top=False, weights=None, input_tensor=None, input_shape=(224, 224, 3), pooling=None, classes=None)
xception_conv_base_previous.load_weights(xception_weights_path)

#xception_conv_final_predictor = Xception(include_top=False, weights="imagenet", input_tensor=None, input_shape=(224, 224, 3), pooling=None, classes=None)

xception_conv_base.summary()
#xception_conv_base.load_weights(xception_weights_path)

from keras.models import Sequential, Model
from keras.layers import Flatten, Dense, Lambda, Input, Multiply, Dot, Add, Concatenate, Average
from keras import backend as K

def initialize_weights(shape, name=None):
    """
        The paper, http://www.cs.utoronto.ca/~gkoch/files/msc-thesis.pdf
        suggests to initialize CNN layer weights with mean as 0.0 and standard deviation of 0.01
    """
    return np.random.normal(loc = 0.0, scale = 1e-2, size = shape)

def initialize_bias(shape, name=None):
    """
        The paper, http://www.cs.utoronto.ca/~gkoch/files/msc-thesis.pdf
        suggests to initialize CNN layer bias with mean as 0.5 and standard deviation of 0.01
    """
    return np.random.normal(loc = 0.5, scale = 1e-2, size = shape)

current_input = Input(shape= (224, 224, 3), name="current_input")
previous_input = Input(shape= (224, 224, 3), name="previous_input")

xception_model_embeddings_current = xception_conv_base (current_input)
xception_model_embeddings_previous = xception_conv_base_previous (previous_input)

## Add necessary max pooling and convs mentioned in the original thesis

pre_L1_flatten = Flatten( name="flatten")(xception_model_embeddings_current)
pre_L1_flatten_previous = Flatten(name="previous_flatten")(xception_model_embeddings_previous)

L1_layer = Lambda(lambda tensors:K.abs(tensors[0] - tensors[1]), name="l1")
L1_distance = L1_layer([pre_L1_flatten_previous, pre_L1_flatten])
similarity_prediction = Dense(1,activation='sigmoid',bias_initializer=initialize_bias, name="similarity_pred")(L1_distance)

#Here we try adding #should I add the functional version and should I include the previous embeddings as inputs
diffrentiable_conditional_add = Lambda( lambda x:K.switch(x>=0.5,Add(name="add_inner")([xception_model_embeddings_current, xception_model_embeddings_previous]) ,xception_model_embeddings_current ), name="add_conditional")(similarity_prediction)
#diffrentiable_conditional_dot = Lambda( lambda x:K.where(x>=0.5, Dot()([xception_model_embeddings_current, xception_model_embeddings_previous]), xception_model_embeddings_current))(similarity_prediction)
diffrentiable_conditional_multiply = Lambda( lambda x:K.switch(x>=0.5,Multiply()([xception_model_embeddings_current, xception_model_embeddings_previous]) , xception_model_embeddings_current))(similarity_prediction)
diffrentiable_conditional_average = Lambda( lambda x:K.switch(x>=0.5,Average()([xception_model_embeddings_current, xception_model_embeddings_previous]) , xception_model_embeddings_current))(similarity_prediction)
#diffrentiable_conidtional_concatenate = Lambda( lambda x:K.where(x>=0.5,Concatenate()([xception_model_embeddings_current, xception_model_embeddings_previous])  , xception_model_embeddings_current))(similarity_prediction)

#Here we try convolution

#Be sure to initialize a different one with var input sizes for concat version
#predictor_convolution = xception_conv_final_predictor(diffrentiable_conditional_add)
final_predictor_1 = Flatten() (diffrentiable_conditional_add)
final_predictor_2 = Dense(1024, activation='relu') (final_predictor_1)
final_predictor_3 = Dense(512, activation='relu') (final_predictor_2)
final_predictor_4 = Dense(256, activation='relu') (final_predictor_3)
final_prediction = Dense(3, activation='softmax') (final_predictor_4)

siamese_model_full = Model(inputs=[current_input, previous_input], outputs=[similarity_prediction,final_prediction])
siamese_model_full.compile(optimizer="adagrad",loss="categorical_crossentropy", metrics=["accuracy"])

The traceback:


ValueError Traceback (most recent call last)

in 50 final_prediction = Dense(3, activation='softmax') (final_predictor_4) 51 ---> 52 siamese_model_full = Model(inputs=[current_input, previous_input], outputs=[similarity_prediction,final_prediction]) 53 siamese_model_full.compile(optimizer="adagrad",loss="categorical_crossentropy", metrics=["accuracy"]) 54 siamese ~/Code/siamese_traffic_density/lib/python3.5/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs) 89 warnings.warn('Update your `' + object_name + '` call to the ' + 90 'Keras 2 API: ' + signature, stacklevel=2) ---> 91 return func(*args, **kwargs) 92 wrapper._original_function = func 93 return wrapper ~/Code/siamese_traffic_density/lib/python3.5/site-packages/keras/engine/network.py in __init__(self, *args, **kwargs) 91 'inputs' in kwargs and 'outputs' in kwargs): 92 # Graph network ---> 93 self._init_graph_network(*args, **kwargs) 94 else: 95 # Subclassed network ~/Code/siamese_traffic_density/lib/python3.5/site-packages/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name) 229 # Keep track of the network's nodes and layers. 230 nodes, nodes_by_depth, layers, layers_by_depth = _map_graph_network( --> 231 self.inputs, self.outputs) 232 self._network_nodes = nodes 233 self._nodes_by_depth = nodes_by_depth ~/Code/siamese_traffic_density/lib/python3.5/site-packages/keras/engine/network.py in _map_graph_network(inputs, outputs) 1453 raise ValueError('The name "' + name + '" is used ' + 1454 str(all_names.count(name)) + -> 1455 ' times in the model. ' 1456 'All layer names should be unique.') 1457 return network_nodes, nodes_by_depth, layers, layers_by_depth ValueError: The name "xception" is used 2 times in the model. All layer names should be unique.