如何将两个 keras 模型连接到一个模型中? [英] How do I connect two keras models into one model?

查看:46
本文介绍了如何将两个 keras 模型连接到一个模型中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's say I have a ResNet50 model and I wish to connect the output layer of this model to the input layer of a VGG model.

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

This is the ResNet model and the output tensor of 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)

Now I want a new layer where I reshape this output tensor to (64,64,18)

然后我有一个 VGG16 模型:

Then I have a VGG16 model:

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

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

I want the output of the ResNet50 to reshape into the desired tensor and fed in as an input to the VGG model. So essentially I want to concatenate two models. Can someone help me do that? Thank you!

推荐答案

有多种方法可以做到这一点.这是使用 Sequential 模型 API 的一种方法.

There are multiple ways you can do this. Here is one way of using Sequential model API to do it.

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
_________________________________________________________________

完整代码在这里.

这篇关于如何将两个 keras 模型连接到一个模型中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆