简体   繁体   English

如何将ResNet50隐藏层与另一个模型输入连接?

[英]How to concatenate ResNet50 hidden layer with another model input?

I am trying to concatenate the output of a hidden layer in ResNet with the input of another model but I get the following error: 我试图将ResNet中隐藏层的输出与另一个模型的输入连接起来,但是出现以下错误:

ValueError: Output tensors to a Model must be the output of a Keras Layer (thus holding past layer metadata) ValueError:模型的输出张量必须是Keras Layer的输出(因此保留了过去的层元数据)

I am using the Concatenate layer from Keras as recommended in How to concatenate two layers in keras? 我正在按照如何在keras中连接两层中的建议使用Keras的Concatenate层 , however it did not work. ,但是它不起作用。 What may I be missing? 我可能会缺少什么? Do I have to add a dense layer to it too? 我也必须在上面添加一个密集层吗? The idea is not to change the second input until it is concatenated with the first input (the merged input will be an input of a third model). 这个想法是直到第二个输入与第一个输入连接起来才改变它(合并的输入将是第三个模型的输入)。

resnet_features = resnet.get_layer('avg_pool').output
model2_features = Input(shape=(None, 32))
all_features = Concatenate([resnet_features, model2_features])

mixer = Model(inputs=[resnet.input, model2_features], 
                             outputs=all_features)

It looks like you are missing two brackets at your concatenation layer. 看起来您在串联层缺少两个括号。 It should look like this: 它看起来应该像这样:

all_features = Concatenate()([resnet_features, model2_features])

Moreover, you have to make sure that the shapes of resnet_features and model2_features are the same except for the concatenation axis since otherwise you won't be able to concatenate them. 此外,您必须确保resnet_featuresmodel2_features的形状相同(除了串联轴),否则,将无法对其进行串联。

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

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