1

keras layers provide keras.layers.GlobalAvgPool2D and keras.layers.GlobalAvgPool2D api to implement global average 2d pooling and max pooling. But, Min Pooling also may be useful,and now I want to use GlobalMinPool2D, which the keras layers api haven't implement.So How to write the code to implement the keras layers GlobalMinPool2D?

mqzhang
  • 39
  • 4
  • Note that `min_pool(inputs) == -1 * max_pool(-1 * inputs)`. That is, negating the inputs and taking the maximum is taking the minimum of the original inputs, and negating again restores the original. – xdurch0 Nov 05 '21 at 09:57

1 Answers1

0

We can create a custom layer and call tf.keras.backend.min() with axis=[1,2] to perform the global min pooling operation. This is similar to GlobalMaxPool2D as seen in its source.

 class GlobalMinPool2D( tf.keras.layers.Layer ):

    def call(self , inputs ):
        return tf.keras.backend.min( inputs , axis=[ 1 , 2 ] )

We can try it with a tensor like,

layer = GlobalMinPool2D()
x = np.random.randn( 32 , 128 , 128 , 3 )
print( layer(x).shape )

The output,

(32, 3)
Shubham Panchal
  • 4,061
  • 2
  • 11
  • 36