如何将两个 keras 模型连接到一个模型中?

How do I connect two keras models into one model?

假设我有一个 ResNet50 模型,我希望将该模型的输出层连接到 VGG 模型的输入层。

这是ResNet模型和ResNet50的输出张量:

img_shape = (164, 164, 3)
resnet50_model = ResNet50(include_top=False, input_shape=img_shape, weights = None)

print(resnet50_model.output.shape)

我得到输出:

TensorShape([Dimension(None), Dimension(6), Dimension(6), Dimension(2048)])

现在我想要一个新层,在其中我将这个输出张量重塑为 (64,64,18)

然后我有一个VGG16模型:

VGG_model = VGG_model = VGG16(include_top=False, weights=None)

我希望 ResNet50 的输出重塑为所需的张量,并作为 VGG 模型的输入。所以基本上我想连接两个模型。有人可以帮我做吗? 谢谢!

有多种方法可以做到这一点。这是使用顺序模型 API 的一种方法。

import tensorflow as tf
from tensorflow.keras.applications import ResNet50, VGG16

model = tf.keras.Sequential()
img_shape = (164, 164, 3)
model.add(ResNet50(include_top=False, input_shape=img_shape, weights = None))

model.add(tf.keras.layers.Reshape(target_shape=(64,64,18)))
model.add(tf.keras.layers.Conv2D(3,kernel_size=(3,3),name='Conv2d'))

VGG_model = VGG16(include_top=False, weights=None)
model.add(VGG_model)

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.summary()

模型总结如下

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
resnet50 (Model)             (None, 6, 6, 2048)        23587712  
_________________________________________________________________
reshape (Reshape)            (None, 64, 64, 18)        0         
_________________________________________________________________
Conv2d (Conv2D)              (None, 62, 62, 3)         489       
_________________________________________________________________
vgg16 (Model)                multiple                  14714688  
=================================================================
Total params: 38,302,889
Trainable params: 38,249,769
Non-trainable params: 53,120
_________________________________________________________________

完整代码为 here