首页 > 解决方案 > 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.

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

标签: tensorflowkerasconv-neural-networkcaffe

解决方案


除非您添加填充以匹配数组形状,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()

推荐阅读