简体   繁体   English

名称“ batch_normalization_1”在模型中使用了2次

[英]The name “batch_normalization_1” is used 2 times in the model

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' 名称“ batch_normalization_1”在模型中使用了2次”

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM