Search code examples
tensorflowkerasconv-neural-networkcaffe

keras implementation of a parallel convolution layer


learning keras and cnn in general, so tried to implement a network i found in a paper, in it there is a parallel convolution layer of 3 convs where each conv apply a different filter on the input, here how i tried to solve it:

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))

-this gives me the following err : A Concatenate layer requires inputs with matching shapes except for the concat axis....etc.

  • i tried solving it by adding the arg input_shape = inp in every Conv2D function, now it gives me Cannot iterate over a tensor with unknown first dimension.

ps : the paper writers implemented this network with caffe, the input to this layer is (32,32,192) and the output after the merge is (32,32,224).


Solution

  • Unless you add the padding to match the array shapes, Concatenate will not be able to match them. Try running this

    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()