Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将两个keras模型连接到一个模型中?_Python_Tensorflow_Keras_Resnet_Vgg Net - Fatal编程技术网

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

Python 如何将两个keras模型连接到一个模型中?,python,tensorflow,keras,resnet,vgg-net,Python,Tensorflow,Keras,Resnet,Vgg Net,假设我有一个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)

假设我有一个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
_________________________________________________________________

完整的代码是

谢谢,如果我可以问一下,我们如何确定conv2d是否将输出3个通道?conv2d内核如何将张量转换为3个以上的通道?在上面的示例中,
conv2d
之前的层有18个通道(过滤器),因此我在
conv2d
中定义了过滤器=3,因此形成了3个通道。您可以将
过滤器
从3更改为任意数量,以增加通道数。