并行卷积层的keras实现

keras implementation of a parallel convolution layer

一般学习keras和cnn,所以尝试实现我在论文中找到的网络,其中有一个3个convs的并行卷积层,其中每个conv对输入应用不同的过滤器,这里是我如何尝试的解决它:

inp = Input(shape=(32,32,192))

conv2d_1 = Conv2D(
        filters = 32,
        kernel_size = (1, 1),
        strides =(1, 1),
        activation = 'relu')(inp)
conv2d_2 = Conv2D(
        filters = 64,
        kernel_size = (3, 3),
        strides =(1, 1),
        activation = 'relu')(inp)
conv2d_3 = Conv2D(
        filters = 128,
        kernel_size = (5, 5),
        strides =(1, 1),
        activation = 'relu')(inp)
out = Concatenate([conv2d_1, conv2d_2, conv2d_3])
model.add(Model(inp, out))

-这给了我以下错误:A Concatenate layer requires inputs with matching shapes except for the concat axis....etc.

ps : 论文作者用caffe实现了这个网络,这一层的输入是(32,32,192),合并后的输出是(32,32,224)。

除非您添加填充以匹配数组形状,否则 Concatenate 将无法匹配它们。试试 运行 这个

import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, Concatenate

inp = Input(shape=(32,32,192))

conv2d_1 = Conv2D(
        filters = 32,
        kernel_size = (1, 1),
        strides =(1, 1),
        padding = 'SAME',
        activation = 'relu')(inp)
conv2d_2 = Conv2D(
        filters = 64,
        kernel_size = (3, 3),
        strides =(1, 1),
        padding = 'SAME',
        activation = 'relu')(inp)
conv2d_3 = Conv2D(
        filters = 128,
        kernel_size = (5, 5),
        strides =(1, 1),
        padding = 'SAME',
        activation = 'relu')(inp)
out = Concatenate()([conv2d_1, conv2d_2, conv2d_3])

model = tf.keras.models.Model(inputs=inp, outputs=out)

model.summary()