我正在尝试对模型进行更改(替换图层),但是当我尝试编译模型时遇到了错误:名称“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_model堆栈跟踪: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
1 回答

拉丁的传说
TA贡献1789条经验 获得超8个赞
我的猜测是您的模型已经有 Batch Normalization 层,当您添加一个新层时,它的名称与现有的 Batch Normalization 层之一具有相同的名称。
在这种情况下,您应该手动定义新的 Batch Normalization 层的名称,这样就不会出现名称冲突,例如:
x = BatchNormalization(name='batch_normalization_11')(x)
添加回答
举报
0/150
提交
取消