在keras、tensorflow中连接两层

问题描述 投票:0回答:1

我想构建以下神经网络层架构

enter image description here

我有一个CNN层:

cnn1 = keras.Sequential([
    layers.Input((32, 32, 3)),
    layers.Conv2D(32, (5, 5), activation='relu')
    ]
)

和模块:

from tensorflow.keras.layers import Concatenate, Dense

'''Module 1'''
module1_left = keras.Sequential([
    layers.Input((28, 28, 32)),
    layers.Conv2D(32, (1, 1), activation='relu', padding='same')
    ]
)
module1_middle = keras.Sequential([
    layers.Input((28, 28, 32)),
    layers.Conv2D(32, (1, 1), activation='relu', padding='same'),
    layers.Conv2D(64, (3, 3), activation='relu', padding='same')
    ]
)
module1_right = keras.Sequential([
    layers.Input((28, 28, 32)),
    layers.MaxPooling2D((3, 3), padding='same', strides=(1, 1)),
    layers.Conv2D(32, (1, 1), activation='relu', padding='same')
    ]
)
module1 = keras.layers.concatenate([module1_left.outputs[0], module1_middle.outputs[0], module1_right.outputs[0]], axis=-1)

然后我尝试将 cnn1 和模块 1 结合起来,

cnn1.add(module1)
,这是最后一行的错误:

TypeError:添加的图层必须是 Layer 类的实例。找到:张量(“concatenate_27/Identity:0”,形状=(无,28,28,128),dtype=float32)

然后我尝试另一种方法来连接:

module1 = Concatenate([module1_left, module1_middle, module1_right])

然后我收到错误:

ValueError:应在至少 2 个输入的列表上调用

Concatenate

请让我知道这些方法有什么问题。谢谢!

python tensorflow keras deep-learning
1个回答
3
投票

最好的(最灵活、优雅)的解决方案是使用

Functional API
中的
Keras

这是一个可行的解决方案。请注意,我使用的是

Model()
(函数式 API)实例化,而不是
Sequential()
:

from tensorflow.keras import Model
image_input = keras.layers.Input((32, 32, 3))
output_cnn_1 = cnn1(image_input)
output_left = module1_left(output_cnn_1)
output_middle = module1_middle(output_cnn_1)
output_right = module1_right(output_cnn_1)
concatenated_output = keras.layers.Concatenate()([output_left,output_middle,output_right])
final_model = Model(inputs=image_input, outputs=concatenated_output)
final_model.summary()



Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_29 (InputLayer)           [(None, 32, 32, 3)]  0                                            
__________________________________________________________________________________________________
sequential_11 (Sequential)      (None, 28, 28, 32)   2432        input_29[0][0]                   
__________________________________________________________________________________________________
sequential_12 (Sequential)      (None, 28, 28, 32)   1056        sequential_11[8][0]              
__________________________________________________________________________________________________
sequential_13 (Sequential)      (None, 28, 28, 64)   19552       sequential_11[8][0]              
__________________________________________________________________________________________________
sequential_14 (Sequential)      (None, 28, 28, 32)   1056        sequential_11[8][0]              
__________________________________________________________________________________________________
concatenate_15 (Concatenate)    (None, 28, 28, 128)  0           sequential_12[8][0]              
                                                                 sequential_13[8][0]              
                                                                 sequential_14[8][0]              
==================================================================================================
Total params: 24,096
Trainable params: 24,096
Non-trainable params: 0
__________________________________________________________________________________________________
    

定义略有变化(我们为每个模块声明

input
,因为我们正在使用cnn1的输出)。

import tensorflow
from tensorflow import keras
from tensorflow.keras import layers

cnn1 = keras.Sequential([
    layers.Conv2D(32, (5, 5), activation='relu')
    ]
)

'''Module 1'''
module1_left = keras.Sequential([
    layers.Conv2D(32, (1, 1), activation='relu', padding='same')
    ]
)
module1_middle = keras.Sequential([
    layers.Conv2D(32, (1, 1), activation='relu', padding='same'),
    layers.Conv2D(64, (3, 3), activation='relu', padding='same')
    ]
)
module1_right = keras.Sequential([
    layers.MaxPooling2D((3, 3), padding='same', strides=(1, 1)),
    layers.Conv2D(32, (1, 1), activation='relu', padding='same')
    ]
)
© www.soinside.com 2019 - 2024. All rights reserved.