You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Keras搭建多输入CNN时concatenate层报缺少inputs参数错误

核心错误原因

混淆了Keras中layers.concatenate函数和layers.Concatenate层类的调用逻辑,同时存在维度参数、输入类型、层绑定、变量名多处错误,修正方案如下:

所有需要修正的问题点

  • 调用逻辑错误:layers.concatenate是函数式接口,必须第一个参数传入要拼接的张量列表,不需要二次调用。如果要使用类实例化后调用的写法,需要使用大写开头的layers.Concatenate层类。
  • 拼接轴参数错误:三个预训练模型输出的特征图为4维张量,维度顺序为[批次大小, 高度, 宽度, 通道数],通道维度的索引为-1或3,你设置的axis=4超出维度范围。
  • 拼接输入类型错误:你传入的是模型对象,而非模型输出的特征张量,需要取每个模型的.output属性作为拼接输入。
  • 输出层未绑定输入:最终的分类Dense层没有传入上游的输出张量,直接将层对象作为模型输出传入了Model参数。
  • 变量名不匹配:你导入的预训练模型为InceptionResNetV2,代码中使用的inceptionV3_resnet152属于未定义变量,需要先按规则初始化该模型。

修正后完整代码

首先补充另外两个预训练模型的初始化逻辑(你之前只写了ResNet152的初始化):

# 初始化InceptionV3
inceptionV3 = InceptionV3(
    weights='imagenet',
    include_top=False,
    input_shape=(224, 224, 3)
)
for layer in inceptionV3.layers:
    layer.trainable = True

# 初始化InceptionResNetV2
inception_resnetV2 = InceptionResNetV2(
    weights='imagenet',
    include_top=False,
    input_shape=(224, 224, 3)
)
for layer in inception_resnetV2.layers:
    layer.trainable = True

合并模型部分修正后的代码:

from tensorflow.keras import Model

# 直接拼接三个模型的输出张量,axis=-1代表沿通道维度拼接
concat_feature_layer = layers.concatenate([resnet152.output, inceptionV3.output, inception_resnetV2.output], axis=-1)
fully_connected_dense_big = layers.Dense(1024, activation='relu')(concat_feature_layer)
dropout_one = layers.Dropout(0.5)(fully_connected_dense_big)
flatten_layer = layers.Flatten()(dropout_one)
fully_connected_dense_small = layers.Dense(512, activation='relu')(flatten_layer)
dropout_two = layers.Dropout(0.5)(fully_connected_dense_small)
# 分类层绑定上游dropout层的输出
fully_connected_dense_class = layers.Dense(4, activation='softmax')(dropout_two)

model = Model(
    inputs=[resnet152.input, inceptionV3.input, inception_resnetV2.input],
    outputs=fully_connected_dense_class
)

如果要使用层类的写法实现拼接,替换拼接行代码即可:

concat_feature_layer = layers.Concatenate(axis=-1)([resnet152.output, inceptionV3.output, inception_resnetV2.output])

内容的提问来源于stack exchange,提问作者Sensei Munk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 03:36:03