GlobalAveragePooling2D就是将每张图片的每个通道值各自加起来再求平均,最后结果是没有了宽高维度,只剩下个数与平均值两个维度。可以理解为变成了多张单像素图片。
从形状上看:[B,H,W,C] ->GlobalAveragePooling2D -> [B,C]
下面有1张图片,高宽分别是2像素,RGB3个通道。即[1,2,2,3]
经过GlobalAveragePooling2D得到一个单通道单像素图片[1,3].
其中RGB各分通道的像素值都加起来之后得到了一个平均值。
import tensorflow as tf
x = tf.random.normal((1,2,2,3))
x.shape
# TensorShape([1, 2, 2, 3])
x
#
<tf.Tensor: shape=(1, 2, 2, 3), dtype=float32, numpy=
array([[[[ 1.0732665 , -0.316923 , 0.34387082],
[ 0.85719526, 0.383818 , 0.0714375 ]],
[[ 2.07628 , 1.0945694 , 0.6519038 ],
[ 0.00441903, 1.2396817 , 0.10125866]]]], dtype=float32)>
ax = tf.keras.layers.GlobalAveragePooling2D()(x)
ax.shape
# TensorShape([1, 3])
ax
#
<tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[1.0027902 , 0.60028654, 0.29211769]], dtype=float32)>
下面是2张,宽高都是2像素,RGB3个通道的图片。经过GlobalAveragePooling2D得到2张单通道单像素图片[2,3]。 每张图片的R通道所有像素值求了一个平均,G通道与B通道的像素值也求了一个平均。
a1 = tf.random.normal((2,2,2,3))
a1.shape
# TensorShape([2, 2, 2, 3])
a1
<tf.Tensor: shape=(2, 2, 2, 3), dtype=float32, numpy=
array([[[[-0.76654285, 1.79131 , 1.0236188 ],
[-0.6119361 , -1.5272698 , -0.364413 ]],
[[ 0.07261603, 0.57860726, 0.4255542 ],
[-0.57528603, 0.74275184, 0.07588626]]],
[[[ 0.84789306, 1.2030426 , -1.7836232 ],
[ 0.6010358 , 1.3016247 , -0.7656161 ]],
[[ 0.34526846, 2.0315964 , 0.1813532 ],
[-0.7089366 , 0.6344181 , 0.973302 ]]]], dtype=float32)>
pa1 = tf.keras.layers.GlobalAveragePooling2D()(a1)
pa1.shape
# TensorShape([2, 3])
pa1
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[-0.47028726, 0.3963498 , 0.29016155],
[ 0.2713152 , 1.2926704 , -0.34864607]], dtype=float32)>