Search code examples
deep-learningkeras-layermax-pooling

maxpooling of channels in CNN


I need to decrease the number of channels in CNN network. my input is a 4D object (samples, rows, column, channels). number of channels is 3 and my output for training has just one channel. is there anyway to do kind of max-pooling in channel direction during training?

Thanks in advance


Solution

  • You can follow a few options, sum the channels, take the max channel, make a standard RGB to B&W trasform, etc.

    All of them are doable inside a Lambda layer, with a defined function:

    import keras.backend as K
    
    def channelPool(x):
        return K.sum(x,axis=-1)
    
        #or
        return K.mean(x,axis=-1)
    
        #or
        return K.max(x,axis=-1)
    
        #or 
        return (.21*x[:,:,:,:1]) + (0.72*x[:,:,:,1:2]) + (0.07*x[:,:,:,-1:])
    

    The layer would be:

    Lambda(channelPool, output_shape=optionalInTensorflow)
    

    PS: If you're using "channels_first", the axes will be 1, and the transform will take x[:,channel,:,:].