一句话理解GlobalAveragePooling2D的算法效果

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)>
  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值