2

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!

Jacob
  • 21
  • 6
  • You need to be more specific. Do you want people to implement the algorithm for you? – Pablo Jun 29 '17 at 01:15
  • Yes! That would be nice. Alright I will edit the post to be more specific! thanks – Jacob Jun 29 '17 at 01:20
  • your questions is answered [here](https://stackoverflow.com/questions/44991470/using-tensorflow-layers-in-keras). – cLottzen Jul 17 '17 at 06:19
  • Not ideal, but you can alternate between upscale and downscale to effectively get 2/3 and 3/2. – ldmtwo Oct 28 '18 at 17:08

2 Answers2

0

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.

An Phú
  • 457
  • 3
  • 10
  • Alright thank for the advice! As i am new to it, would you kindly explain what's going on with the code? thanks – Jacob Jun 29 '17 at 12:55
  • I means that you should create the custom layer for your purpose. The custom layer is extended from Layer class. Some important method in your layer is: - build: create the trainable variables if it is needed. - call: perform your calculation to get the output - compute_output_shape: return the output shape (what is the shape of data after it goes through your layer) – An Phú Jun 30 '17 at 03:14
0

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