【问题标题】:Tensorflow concat two transfer learning modelTensorflow concat 二次迁移学习模型
【发布时间】:2022-11-27 21:20:28
【问题描述】:

我想连接两个具有相同输入的迁移学习模型,这两个模型将并行运行,然后将组合特征展平以进行图像分类。但是我不知道为什么会出现这个错误。谢谢!

input = tf.keras.layers.Input(shape=(300,300,3))
from tensorflow.keras.applications import ResNet50V2
base_model2 = ResNet50V2(weights='imagenet', include_top=False, input_tensor=input)
for layers in (base_model2.layers)[:90]:
  layers.trainable = False
from tensorflow.keras.applications import InceptionResNetV2
base_model1 = InceptionResNetV2(weights='imagenet', include_top=False, input_tensor=input)
for layers in (base_model1.layers)[:90]:
  layers.trainable = False
output = Concatenate()([base_model1, base_model2] , axis= 1)
output = Flatten()(output)
output = Dropout(0.8)(output)
output = Dense(1, activation='sigmoid')(output)
combine = Model(input = input, output = output)

错误信息: enter image description here

我尝试连接两个迁移学习模型,所以我将有一个模型、输入图像和两个用于特征提取的迁移学习模型并并行运行并进行图像分类

【问题讨论】:

  • 尝试base_model1.outputbase_model2.output(假设它们的形状相同)。
  • 非常感谢您!!!!

标签: tensorflow concatenation tensorflow2.0 transfer-learning image-classification


【解决方案1】:

您想要连接模型的输出,即 base_model1.outputbase_model2.output。它们的形状不同,所以你必须在连接之前把它们弄平:

output = Concatenate()([Flatten()(base_model1.output), Flatten()(base_model2.output)])
output = Dropout(0.8)(output)
output = Dense(1, activation='sigmoid')(output)
combine = Model(inputs = input, outputs = output)

【讨论】:

  • 非常感谢您!!!!!