我试图对模型进行更改(替换层),但当我试图编译模型时,会遇到以下错误:
“batch_normalization_1”这个名字在模型中使用了2次
我不知道我做错了什么:
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_modelStackTrace:
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.发布于 2019-01-16 09:44:02
我的猜测是,您的模型已经具有批处理规范化层,当您添加一个新的层时,它的名称与现有的批处理规范化层中的一个相同。
在这种情况下,您应该手动定义新批处理规范化层的名称,这样就不会出现名称冲突,例如:
x = BatchNormalization(name='batch_normalization_11')(x)https://stackoverflow.com/questions/54213855
复制相似问题