Tensorflow concat二次迁移学习模型

nkoocmlb  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(137)

我想用相同的输入连接两个迁移学习模型,两个模型并行运行,然后将合并的特征扁平化用于图像分类。但是我不知道为什么我得到了这个错误。谢谢!

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)

错误消息:

我尝试将两个迁移学习模型串联起来,这样我将有一个模型,输入图像,并有两个迁移学习模型进行特征提取,并并行运行它,进行图像分类

bxgwgixi

bxgwgixi1#

您需要连接模型的输出,即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)

相关问题