0

我试图将两个网络融合在一起形成最终的管道网络,但最终模型将管道模型显示为单个顺序层而不是其各个层(见图)。

模型表示

使用 model.summary() 也给了我相同的结果:

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
conv_lst_m2d_input (InputLayer) [(None, 1000, 216, 1 0                                            
__________________________________________________________________________________________________
...            
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D)  (None, 44, 1162, 1)  0           conv_lst_m2d_1[0][0]             
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 44, 1162, 2)  0           max_pooling2d[0][0]              
                                                                 max_pooling2d_1[0][0]            
__________________________________________________________________________________________________
sequential_2 (Sequential)       (None, 216, 1162, 12 5000534     concatenate[0][0]                
==================================================================================================

这是我用来将网络合并在一起的代码:

def fuse_model(output_channels, lrval=0.0001):
    cnn1_mel = cnn_mls(output_channels, lrval=lrval)
    cnn1_sslm = cnn_sslm(output_channels, lrval=lrval)
    combined = keras.layers.concatenate([cnn1_mel.output, cnn1_sslm.output])
    cnn2_in = cnn2(output_channels, lrval=lrval)(combined)
    opt = keras.optimizers.Adam(lr=lrval)  # learning rate
    model = keras.models.Model(inputs=[cnn1_mel.input, cnn1_sslm.input], outputs=[cnn2_in])
    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

    model.summary()
    if not os.path.isfile('Model_Diagram.png'):
        plot_model(model, to_file='Model_Diagram.png', show_shapes=True, show_layer_names=True, expand_nested=True)

    return model

这是管道模型本身的代码:

def cnn2(output_channels, lrval=0.0001):
    model = tf.keras.Sequential()
    model.add(layers.Conv2D(filters=(output_channels * 2),
                            kernel_size=(3, 5), strides=(1, 1),
                            padding='same',  # ((3 - 1) // 2, (5 - 1) * 3 // 2),
                            dilation_rate=(1, 3),
                            activation=layers.LeakyReLU(alpha=lrval), input_shape=(216, 1162, 2)  # (out_chnls,)
                            ))

    model.add(layers.SpatialDropout2D(rate=0.5))
    model.add(
        layers.Conv2D(output_channels * 152, 128, (1, 1), activation=layers.LeakyReLU(alpha=lrval), padding='same'))

    model.add(layers.SpatialDropout2D(rate=0.5))
    model.add(layers.Conv2D(128, 1, (1, 1), padding='same'))

    return model

如果有帮助,这里是完整的代码:https ://gist.github.com/danielathome19/f02fff7f95bf241dddcfe424de87087a

4

1 回答 1

0

感谢评论中的@TFer2。

model.get_layer(name='...') 

这正是我需要的功能。

于 2021-09-09T01:36:57.043 回答