I"m trying to make changes to a model (replace layers), but when I try to compile the model I run into the error:
The name 'batch_normalization_1" is used 2 times in the model'
I can't figure out what I'm doing wrong:
def add_batch_normalization(model_path):
model = load_model(model_path)
weights = model.get_weights()
dense_idx = [index for index,layer in enumerate(model.layers) if type(layer) is Dense][-1] #get indices for dense layers
x = model.layers[dense_idx -1].output
new_model = Model(inputs = model.input, outputs = x)
x= BatchNormalization()(new_model.output)
x = Dense(2048, activation='relu')(x)
x =BatchNormalization()(x)
x = Dropout(.10)(x)
x= Dense(512, activation='relu')(x)
x= BatchNormalization()(x)
predictions = Dense(num_of_classes, activation='softmax')(x)
new_model = Model(inputs= new_model.input, outputs=predictions)
print(new_model.summary())
model.set_weights(weights)
return new_model
StackTrace:
Traceback (most recent call last):
File "E:\test\APP test PROJECT\test\PYTHON SCRIPTS\testing_saved_models.py", line 542, in <module>
MODEL = add_batch_normalization(PATH) #{load_model(PATH), add_conv_layers(PATH, how_many = 1), change_dropout(PATH, .5) }
File "E:\test\APP test PROJECT\test\PYTHON SCRIPTS\testing_saved_models.py", line 104, in add_batch_normalization
new_model = Model(inputs= new_model.input, outputs=predictions)
File "C:\Users\yomog\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\yomog\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 93, in __init__
self._init_graph_network(*args, **kwargs)
File "C:\Users\yomog\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 231, in _init_graph_network
self.inputs, self.outputs)
File "C:\Users\yomog\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1455, in _map_graph_network
' times in the model. '
ValueError: The name "batch_normalization_1" is used 2 times in the model. All layer names should be unique.
My guess is that your model already has Batch Normalization layers, and when you add a new one, it has the same name than one of the already existing Batch Normalization layers.
In this case, you should define the name of your new Batch Normalization layers manually, so there is no name clash, for example:
x = BatchNormalization(name='batch_normalization_11')(x)