Skip to content

Commit

Permalink
add gem layer
Browse files Browse the repository at this point in the history
  • Loading branch information
innat committed Jul 3, 2022
1 parent 6bde237 commit 54e15d5
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 68 deletions.
12 changes: 9 additions & 3 deletions keras/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,16 @@
from keras.layers.pooling.max_pooling3d import MaxPool3D
from keras.layers.pooling.max_pooling3d import MaxPooling3D
from keras.layers.pooling.generalized_mean_pooling1d import GeneralizedPooling1D
from keras.layers.pooling.generalized_mean_pooling1d import GeneralizedMeanPooling1D
from keras.layers.pooling.generalized_mean_pooling2d import GeneralizedMeanPooling2D
from keras.layers.pooling.generalized_mean_pooling1d import (
GeneralizedMeanPooling1D,
)
from keras.layers.pooling.generalized_mean_pooling2d import (
GeneralizedMeanPooling2D,
)
from keras.layers.pooling.generalized_mean_pooling2d import GeneralizedPooling2D
from keras.layers.pooling.generalized_mean_pooling3d import GeneralizedMeanPooling3D
from keras.layers.pooling.generalized_mean_pooling3d import (
GeneralizedMeanPooling3D,
)
from keras.layers.pooling.generalized_mean_pooling3d import GeneralizedPooling3D
from keras.layers.rnn.abstract_rnn_cell import AbstractRNNCell

Expand Down
18 changes: 12 additions & 6 deletions keras/layers/pooling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
from keras.layers.pooling.average_pooling2d import AvgPool2D
from keras.layers.pooling.average_pooling3d import AveragePooling3D
from keras.layers.pooling.average_pooling3d import AvgPool3D
from keras.layers.pooling.generalized_mean_pooling1d import (
GeneralizedMeanPooling1D,
)
from keras.layers.pooling.generalized_mean_pooling1d import GeneralizedPooling1D
from keras.layers.pooling.generalized_mean_pooling2d import (
GeneralizedMeanPooling2D,
)
from keras.layers.pooling.generalized_mean_pooling2d import GeneralizedPooling2D
from keras.layers.pooling.generalized_mean_pooling3d import (
GeneralizedMeanPooling3D,
)
from keras.layers.pooling.generalized_mean_pooling3d import GeneralizedPooling3D
from keras.layers.pooling.global_average_pooling1d import GlobalAveragePooling1D
from keras.layers.pooling.global_average_pooling1d import GlobalAvgPool1D
from keras.layers.pooling.global_average_pooling2d import GlobalAveragePooling2D
Expand All @@ -41,9 +53,3 @@
from keras.layers.pooling.max_pooling2d import MaxPooling2D
from keras.layers.pooling.max_pooling3d import MaxPool3D
from keras.layers.pooling.max_pooling3d import MaxPooling3D
from keras.layers.pooling.generalized_mean_pooling1d import GeneralizedPooling1D
from keras.layers.pooling.generalized_mean_pooling1d import GeneralizedMeanPooling1D
from keras.layers.pooling.generalized_mean_pooling2d import GeneralizedMeanPooling2D
from keras.layers.pooling.generalized_mean_pooling2d import GeneralizedPooling2D
from keras.layers.pooling.generalized_mean_pooling3d import GeneralizedMeanPooling3D
from keras.layers.pooling.generalized_mean_pooling3d import GeneralizedPooling3D
8 changes: 4 additions & 4 deletions keras/layers/pooling/base_generalized_pooling1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ def __init__(

def call(self, inputs):
raise NotImplementedError

def get_config(self):
config = {
"power": self.power,
"pool_size": self.pool_size,
"pool_size": self.pool_size,
"strides": self.strides,
"padding": self.padding,
"data_format": self.data_format
}
"data_format": self.data_format,
}
base_config = super().get_config()
return dict(list(base_config.items()) + list(config.items()))
6 changes: 3 additions & 3 deletions keras/layers/pooling/base_generalized_pooling2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def call(self, inputs):
def get_config(self):
config = {
"power": self.power,
"pool_size": self.pool_size,
"pool_size": self.pool_size,
"strides": self.strides,
"padding": self.padding,
"data_format": self.data_format
}
"data_format": self.data_format,
}
base_config = super().get_config()
return dict(list(base_config.items()) + list(config.items()))
8 changes: 4 additions & 4 deletions keras/layers/pooling/base_generalized_pooling3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ def __init__(

def call(self, inputs):
raise NotImplementedError

def get_config(self):
config = {
"power": self.power,
"pool_size": self.pool_size,
"pool_size": self.pool_size,
"strides": self.strides,
"padding": self.padding,
"data_format": self.data_format
}
"data_format": self.data_format,
}
base_config = super().get_config()
return dict(list(base_config.items()) + list(config.items()))
41 changes: 22 additions & 19 deletions keras/layers/pooling/generalized_mean_pooling1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ class GeneralizedMeanPooling1D(GeneralizedPooling1D):
"""Generalized mean pooling operation for temporal data.
Generalized Mean Pooling (GeM) computes the generalized mean of each
channel in a tensor. It provides a parameter `p` that sets an exponent
enabling the pooling to increase or decrease the contrast between salient
features in the feature map.
channel in a tensor. It provides a parameter `power` that sets an
exponent enabling the pooling to increase or decrease the contrast
between salient features in the feature map.
The GeM layer is an generalization of the average pooling layer and spatial
max pooling layer. When `p` = 1`, it will act as a average pooling layer and
when `p = inf`, it will act as a spatial max pooling layer.
The GeM layer is an generalization of the average pooling layer and
spatial max pooling layer. When `power` = 1`, it will act as a average
pooling layer and when `power = inf`, it will act as a spatial
max-pooling layer.
Examples:
Expand All @@ -24,7 +25,8 @@ class GeneralizedMeanPooling1D(GeneralizedPooling1D):
>>> input_shape = (2, 3, 4)
>>> x = tf.random.normal(input_shape)
>>> gem_pool_1d = tf.keras.layers.GeneralizedMeanPooling1D(power=3,
... pool_size=2, strides=1, padding='valid', data_format='channels_last')
... pool_size=2, strides=1, padding='valid',
... data_format='channels_last')
>>> gem_pool_1d(x)
<tf.Tensor: shape=(1, 4, 1), dtype=float32, numpy=
array([[[1.6509637],
Expand All @@ -37,7 +39,8 @@ class GeneralizedMeanPooling1D(GeneralizedPooling1D):
>>> input_shape = (2, 3, 4)
>>> x = tf.random.normal(input_shape)
>>> gem_pool_1d = tf.keras.layers.GeneralizedMeanPooling1D(power=3,
... pool_size=2, strides=2, padding='valid', data_format='channels_last')
... pool_size=2, strides=2, padding='valid',
... data_format='channels_last')
>>> gem_pool_1d(x)
<tf.Tensor: shape=(1, 2, 1), dtype=float32, numpy=
array([[[1.6509637],
Expand All @@ -48,7 +51,8 @@ class GeneralizedMeanPooling1D(GeneralizedPooling1D):
>>> input_shape = (2, 3, 4)
>>> x = tf.random.normal(input_shape)
>>> gem_pool_1d = tf.keras.layers.GeneralizedMeanPooling1D(power=3,
... pool_size=2, strides=1, padding='same', data_format='channels_last')
... pool_size=2, strides=1, padding='same',
... data_format='channels_last')
>>> gem_pool_1d(x)
<tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
array([[[1.6509637],
Expand All @@ -62,23 +66,22 @@ class GeneralizedMeanPooling1D(GeneralizedPooling1D):
the generalized mean pooling computation. Setting this exponent as
power > 1 increases the contrast of the pooled feature map and focuses
on the salient features of the image. GeM is a generalization of the
average pooling commonly used in classification networks (power = 1)
and of spatial max-pooling layer (power = inf).
average pooling when `power` = 1 and of spatial max-pooling layer when
`power` = inf or a large number.
pool_size: Integer, size of the average pooling windows.
strides: Integer, or None. Factor by which to downscale.
E.g. 2 will halve the input.
If None, it will default to `pool_size`.
padding: One of `"valid"` or `"same"` (case-insensitive).
`"valid"` means no padding. `"same"` results in padding evenly to
padding: A string. The padding method, either 'valid' or 'same'.
`'valid'` means no padding. `'same'` results in padding evenly to
the left/right or up/down of the input such that output has the same
height/width dimension as the input.
data_format: A string,
one of `channels_last` (default) or `channels_first`.
The ordering of the dimensions in the inputs.
data_format: A string, one of `channels_last` (default) or
`channels_first`. The ordering of the dimensions in the inputs.
`channels_last` corresponds to inputs with shape
`(batch, steps, features)` while `channels_first`
corresponds to inputs with shape
`(batch, features, steps)`.
`(batch, steps, features)` while `channels_first` corresponds
to inputs with shape `(batch, features, steps)`.
name: A string, the name of the layer.
Input shape:
- If `data_format='channels_last'`:
Expand Down
31 changes: 17 additions & 14 deletions keras/layers/pooling/generalized_mean_pooling2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ class GeneralizedMeanPooling2D(GeneralizedPooling2D):
"""Generalized mean pooling operation for temporal data.
Generalized Mean Pooling (GeM) computes the generalized mean of each
channel in a tensor. It provides a parameter `p` that sets an exponent
enabling the pooling to increase or decrease the contrast between salient
features in the feature map.
channel in a tensor. It provides a parameter `power` that sets an
exponent enabling the pooling to increase or decrease the contrast
between salient features in the feature map.
The GeM layer is an generalization of the average pooling layer and spatial
max pooling layer. When `p` = 1`, it will act as a average pooling layer and
when `p = inf`, it will act as a spatial max pooling layer.
The GeM layer is an generalization of the average pooling layer and
spatial max pooling layer. When `power` = 1`, it will act as a average
pooling layer and when `power = inf`, it will act as a spatial
max-pooling layer.
Examples:
Expand All @@ -26,7 +27,8 @@ class GeneralizedMeanPooling2D(GeneralizedPooling2D):
... [7., 8., 9.]])
>>> x = tf.reshape(x, [1, 3, 3, 1])
>>> gem_pool_2d = tf.keras.layers.GeneralizedMeanPooling2D(power=3,
... pool_size=2, strides=1, padding='valid', data_format='channels_last')
... pool_size=2, strides=1, padding='valid',
... data_format='channels_last')
>>> gem_pool_2d(x)
<tf.Tensor: shape=(1, 2, 2, 1), dtype=float32, numpy=
array([[[[3.6717105],
Expand All @@ -42,7 +44,8 @@ class GeneralizedMeanPooling2D(GeneralizedPooling2D):
... [9., 10., 11., 12.]])
>>> x = tf.reshape(x, [1, 3, 4, 1])
>>> gem_pool_2d = tf.keras.layers.GeneralizedMeanPooling2D(power=3,
... pool_size=2, strides=2, padding='valid', data_format='channels_last')
... pool_size=2, strides=2, padding='valid',
... data_format='channels_last')
>>> gem_pool_2d(x)
<tf.Tensor: shape=(1, 1, 2, 1), dtype=float32, numpy=
array([[[[4.4395204],
Expand All @@ -55,7 +58,8 @@ class GeneralizedMeanPooling2D(GeneralizedPooling2D):
... [7., 8., 9.]])
>>> x = tf.reshape(x, [1, 3, 3, 1])
>>> gem_pool_2d = tf.keras.layers.GeneralizedMeanPooling1D(power=3,
... pool_size=2, strides=1, padding='same', data_format='channels_last')
... pool_size=2, strides=1, padding='same',
... data_format='channels_last')
>>> gem_pool_2d(x)
<tf.Tensor: shape=(1, 3, 3, 1), dtype=float32, numpy=
array([[[[3.6717105],
Expand All @@ -75,8 +79,8 @@ class GeneralizedMeanPooling2D(GeneralizedPooling2D):
the generalized mean pooling computation. Setting this exponent as
power > 1 increases the contrast of the pooled feature map and focuses
on the salient features of the image. GeM is a generalization of the
average pooling commonly used in classification networks (power = 1)
and of spatial max-pooling layer (power = inf).
average pooling when `power` = 1 and of spatial max-pooling layer when
`power` = inf or a large number.
pool_size: An integer or tuple/list of 2 integers:
(pool_height, pool_width) specifying the size of the pooling window.
Can be a single integer to specify the same value for all spatial
Expand All @@ -85,12 +89,11 @@ class GeneralizedMeanPooling2D(GeneralizedPooling2D):
of the pooling operation. Can be a single integer to specify the same
value for all spatial dimensions.
padding: A string. The padding method, either 'valid' or 'same'.
Case-insensitive.
data_format: A string, one of `channels_last` (default) or
`channels_first`. The ordering of the dimensions in the inputs.
`channels_last` corresponds to inputs with shape
`(batch, height, width, channels)` while `channels_first` corresponds
to inputs with shape `(batch, channels, height, width)`.
`(batch, height, width, channels)` while `channels_first`
corresponds to inputs with shape `(batch, channels, height, width)`.
name: A string, the name of the layer.
Input shape:
Expand Down
30 changes: 15 additions & 15 deletions keras/layers/pooling/generalized_mean_pooling3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
class GeneralizedMeanPooling3D(GeneralizedPooling3D):
"""Generalized mean pooling operation for temporal data.
Generalized Mean Pooling (GeM) computes the generalized mean of each
channel in a tensor. It provides a parameter `p` that sets an exponent
enabling the pooling to increase or decrease the contrast between salient
features in the feature map.
Generalized Mean Pooling (GeM) computes the generalized mean of
each channel in a tensor. It provides a parameter `p` that sets
an exponent enabling the pooling to increase or decrease the contrast
between salient features in the feature map.
The GeM layer is an generalization of the average pooling layer and spatial
max pooling layer. When `p` = 1`, it will act as a average pooling layer and
when `p = inf`, it will act as a spatial max pooling layer.
The GeM layer is an generalization of the average pooling layer and
spatial max pooling layer. When `power` = 1`, it will act as a average
pooling layer and when `power = inf`, it will act as a spatial
max-pooling layer.
Examples:
Expand Down Expand Up @@ -63,24 +64,23 @@ class GeneralizedMeanPooling3D(GeneralizedPooling3D):
Args:
power: Float power > 0 is an inverse exponent parameter, used during
the generalized mean pooling computation. Setting this exponent as
power > 1 increases the contrast of the pooled feature map and
focuses on the salient features of the image. GeM is a generalization
of the average pooling commonly used in classification networks
(power = 1) and of spatial max-pooling layer (power = inf).
power > 1 increases the contrast of the pooled feature map and focuses
on the salient features of the image. GeM is a generalization of the
average pooling when `power` = 1 and of spatial max-pooling layer when
`power` = inf or a large number.
pool_size: An integer or tuple/list of 3 integers:
(pool_depth, pool_height, pool_width) specifying the size of the
`(pool_depth, pool_height, pool_width)` specifying the size of the
pooling window. Can be a single integer to specify the same value for
all spatial dimensions.
strides: An integer or tuple/list of 3 integers, specifying the strides
of the pooling operation. Can be a single integer to specify the same
value for all spatial dimensions.
padding: A string. The padding method, either 'valid' or 'same'.
Case-insensitive.
data_format: A string, one of `channels_last` (default) or
`channels_first`. The ordering of the dimensions in the inputs.
`channels_last` corresponds to inputs with shape
`(batch, depth, height, width, channels)` while
`channels_first` corresponds to inputs with shape
`(batch, depth, height, width, channels)` while `channels_first`
corresponds to inputs with shape
`(batch, channels, depth, height, width)`.
name: A string, the name of the layer.
Expand Down

0 comments on commit 54e15d5

Please sign in to comment.