Keras - 所有图层名称都应该是唯一的

Keras - All layer names should be unique

我将keras中的两个VGG网络结合在一起做分类任务。当我运行程序时,它显示错误:

RuntimeError: The name "predictions" is used 2 times in the model. All layer names should be unique.

我很困惑,因为我在代码中只使用了一次 prediction 图层:

from keras.layers import Dense
import keras
from keras.models import  Model
model1 = keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',
                                input_tensor=None, input_shape=None,
                                pooling=None,
                                classes=1000)
model1.layers.pop()

model2 =  keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',
                                input_tensor=None, input_shape=None,
                                pooling=None,
                                classes=1000)
model2.layers.pop()
for layer in model2.layers:
    layer.name = layer.name + str("two")
model1.summary()
model2.summary()
featureLayer1 = model1.output
featureLayer2 = model2.output
combineFeatureLayer = keras.layers.concatenate([featureLayer1, featureLayer2])
prediction = Dense(1, activation='sigmoid', name='main_output')(combineFeatureLayer)

model = Model(inputs=[model1.input, model2.input], outputs= prediction)
model.summary()

感谢@putonspectacles 的帮助,我按照他的指示找到了一些有趣的部分。如果你使用model2.layers.pop(),并使用“model.layers.keras.layers.concatenate([model1.output, model2.output])”组合两个模型的最后一层,你会发现最后一层信息仍然使用model.summary()显示。但实际上它们并不存在于结构中。因此,您可以使用 model.layers.keras.layers.concatenate([model1.layers[-1].output, model2.layers[-1].output])。它看起来很棘手,但它确实有效。我认为这是一个关于日志和结构同步的问题。

首先,根据您发布的代码,您 没有 层具有名称属性 'predictions',因此此错误与您的层无关 Denseprediction:即:

prediction = Dense(1, activation='sigmoid', 
             name='main_output')(combineFeatureLayer)

VGG16 模型有一个 Densename predictions。特别是这一行:

x = Dense(classes, activation='softmax', name='predictions')(x)

并且由于您使用的是这些模型中的两个,所以您的层具有重名。

你可以做的是将第二个模型中的图层重命名为预测以外的其他名称,可能 predictions_1,如下所示:

model2 =  keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',
                                input_tensor=None, input_shape=None,
                                pooling=None,
                                classes=1000)

# now change the name of the layer inplace.
model2.get_layer(name='predictions').name='predictions_1'

你可以在keras中更改层的名称,不要使用'tensorflow.python.keras'。

这是我的示例代码:

from keras.layers import Dense, concatenate
from keras.applications import vgg16

num_classes = 10

model = vgg16.VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=(64,64,3), pooling='avg')
inp = model.input
out = model.output

model2 = vgg16.VGG16(include_top=False,weights='imagenet', input_tensor=None, input_shape=(64,64,3), pooling='avg')

for layer in model2.layers:
    layer.name = layer.name + str("_2")

inp2 = model2.input
out2 = model2.output

merged = concatenate([out, out2])
merged = Dense(1024, activation='relu')(merged)
merged = Dense(num_classes, activation='softmax')(merged)

model_fusion = Model([inp, inp2], merged)
model_fusion.summary()

示例:

# Network for affine transform estimation
affine_transform_estimator = MobileNet(
                            input_tensor=None,
                            input_shape=(config.IMAGE_H // 2, config.IMAGE_W //2, config.N_CHANNELS),
                            alpha=1.0,
                            depth_multiplier=1,
                            include_top=False,
                            weights='imagenet'
                            )
affine_transform_estimator.name = 'affine_transform_estimator'
for layer in affine_transform_estimator.layers:
    layer.name = layer.name + str("_1")

# Network for landmarks regression
landmarks_regressor = MobileNet(
                        input_tensor=None,
                        input_shape=(config.IMAGE_H // 2, config.IMAGE_W // 2, config.N_CHANNELS),
                        alpha=1.0,
                        depth_multiplier=1,
                        include_top=False,
                        weights='imagenet'
                        )
landmarks_regressor.name = 'landmarks_regressor'
for layer in landmarks_regressor.layers:
    layer.name = layer.name + str("_2")

input_image = Input(shape=(config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))
downsampled_image = MaxPooling2D(pool_size=(2,2))(input_image)
x1 = affine_transform_estimator(downsampled_image)
x2 = landmarks_regressor(downsampled_image)
x3 = add([x1,x2])

model = Model(inputs=input_image, outputs=x3)
optimizer = Adadelta()
model.compile(optimizer=optimizer, loss=mae_loss_masked)