Can I use a an AveragePooling2D layer with the pool_size equal to the size of the feature map instead of a GlobalAveragePooling2D layer? the purpose of this is to replace a dense layer after an FCN. IS GlobalAveragePooling2D a special case of AveragePooling2D??
CodePudding user response:
GlobalAveragePooling2D
will downsample an input by taking the average value along the spatial dimensions and return a 1D output by default, unless you set keepdims= True
. AveragePooling2D
also downsamples an input but takes the average value over an input window defined by the pool_size
parameter. So, it will return a 3D output:
import tensorflow as tf
x = tf.constant([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
x = tf.reshape(x, [1, 3, 3, 1]) # Add batch dimension
avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
strides=(1, 1), padding='valid')
avg_global_2d = tf.keras.layers.GlobalAveragePooling2D()
print(avg_pool_2d(x).shape)
print(avg_global_2d(x).shape)
(1, 2, 2, 1)
(1, 1)
You will probably have to flatten your output from the AveragePooling2D
layer if you want to feed it to a Dense
layer afterwards:
import tensorflow as tf
x = tf.constant([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
x = tf.reshape(x, [1, 3, 3, 1]) # Add batch dimension
avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
strides=(1, 1), padding='valid')
avg_global_2d = tf.keras.layers.GlobalAveragePooling2D()
print(tf.keras.layers.Flatten()(avg_pool_2d(x)).shape)
print(avg_global_2d(x).shape)
(1, 4)
(1, 1)
If that is not the case you can just leave it as it is.
Update: GlobalAveragePooling2D
and AveragePooling2D
can sometimes behave similarly if you adjust the strides
and pool_size
parameters accordingly:
import tensorflow as tf
x = tf.constant([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
x = tf.reshape(x, [1, 3, 3, 1]) # Add batch dimension
avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
strides=(3, 3), padding='valid')
avg_global_2d = tf.keras.layers.GlobalAveragePooling2D()
print(avg_pool_2d(x))
print(avg_global_2d(x))
tf.Tensor([[[[3.]]]], shape=(1, 1, 1, 1), dtype=float32)
tf.Tensor([[5.]], shape=(1, 1), dtype=float32)
Or
avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(3, 3),
strides=(2, 2), padding='valid')
avg_global_2d = tf.keras.layers.GlobalAveragePooling2D()
tf.Tensor([[[[5.]]]], shape=(1, 1, 1, 1), dtype=float32)
tf.Tensor([[5.]], shape=(1, 1), dtype=float32)
CodePudding user response:
Adding to the answer above, global average pooling can be used for taking variable size images as inputs. If the input shape before global pooling is (N,H,W,C)
then output will be (N,1,1,C)
for keras when keepdims=True
. This makes the output of images with different (H,W)
produce similar shape outputs.