2
votes

The existing function in keras lib including max-pooling, average pooling, etc.

However, I would like to implement fractional max-pooling in keras based on the paper https://arxiv.org/abs/1412.6071.

My implementation are as follow:

model = Sequential()
......
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

So, instead of model.add(MaxPooling2D(pool_size=(2, 2))), I would like to implement something like the following:

model.add(fractionalMaxpool2D(..............))

Is it possible? I am currently using keras as backend in tensorflow.

Appreciate if someone would provide the algorithm/code.

I am quite new to this as I didn't wrote any custom layer before so could anyone kindly help out? Thanks!

2
You need to be more specific. Do you want people to implement the algorithm for you? - Pablo
Yes! That would be nice. Alright I will edit the post to be more specific! thanks - Jacob
your questions is answered here. - cLottzen
Not ideal, but you can alternate between upscale and downscale to effectively get 2/3 and 3/2. - ldmtwo

2 Answers

0
votes

In my opinion, you can do that by implementing your custom layer

class FractionalMaxpool2D(Layer):
    def __init__(self, output_dim):
        super(FractionalMaxpool2D, self).__init__()
        self.output_dim = output_dim
    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        # This kind of layer doesn't have any variable
        pass
    def call(self, x):
        # Handle you algorithm here
        return ....  
    def compute_output_shape(self, input_shape):
        # return the output shape
        return (input_shape[0], self.output_dim)

The problem is it's difficult to implement the core function for the Fractional max pooling that uses GPU. Please check this discussion from Keras's Github.

0
votes

You Can Use Keras Lambda Layer to Wrap tf.nn.fractional_max_pool, like

FMP = Lambda(lambda img, pool_size: tf.nn.fractional_max_pool(img, pool_size))

Now You can Use FMP in your Keras Code like other layers with Two Arguments

  1. Img: with dimensions like [batch, height, width, channels]
  2. pool_size: [1.0, pool_size_you_want, pool_size_you_want, 1.0]

The first and last are 1.0, which is because tf doesnot perform pooling on batch_size and channels, it performs on height and width