10

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

RuntimeError:名称“预测”在模型中使用了 2 次。所有图层名称都应该是唯一的。

我很困惑,因为我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]). 它看起来很棘手,但它确实有效。我认为这是关于日志和结构同步的问题。

4

3 回答 3

7

首先,根据您发布的代码,您没有Dense具有名称属性“预测”的图层,因此此错误与您的图层图层无关 prediction:即:

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'
于 2017-04-17T13:51:40.530 回答
3

您可以在 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()
于 2018-07-29T13:32:00.080 回答
0

例子:

# 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)
于 2018-11-26T14:38:08.113 回答