介绍
全局最大池化层,简称gmp
详情见:
https://www.malaoshi.top/show_1EF5LFfo4DHY.html
常用别名
- tf.keras.layers.GlobalMaxPooling2D
语法
tf.keras.layers.GlobalMaxPool2D(
data_format=None, **kwargs
)
参数
- data_format: string类型, 表示输入数据维度的顺序,
channels_last(默认值) 或者channels_first。- channels_last: (batch, height, width, channels)
- channels_first:(batch, channels, height, width)
在Keras 配置文件 ~/.keras/keras.json修改默认值,默认值为 “channels_last”.
返回值
tensor
输入形状
- 如果
data_format='channels_last‘,4D tensor 形状是 (batch_size, rows, cols, channels). - 如果
data_format='channels_first',4D tensor 形状是 (batch_size, channels, rows, cols).
例子
灰度图计算gmp
生成灰度图
import numpy as np
import tensorflow as tf
# 创建二维数据,模拟灰度图
np.random.seed(1)
arr=np.random.randint(1,9,(4,4))
print(arr)
执行结果:
[[6 4 5 1]
[8 2 4 6]
[8 1 1 2]
[5 8 6 5]]
计算gmp:
arr=arr.reshape(1,4,4,1)
inp=tf.keras.Input((4,4,1))
x=tf.keras.layers.GlobalMaxPooling2D()(inp)
model=tf.keras.Model(inp,x)
res=model(arr)
print(res)
执行结果:
tf.Tensor([[8]], shape=(1, 1), dtype=int64)
3通道图计算gmp
生成3通道图片
import numpy as np
import tensorflow as tf
np.random.seed(30)
arr=np.random.randint(1,50,(4,4,3))
print(arr)
执行结果:
[[[38 38 46]
[46 13 24]
[ 3 18 47]
[ 4 42 8]]
[[ 2 46 36]
[19 19 39]
[13 17 7]
[28 47 46]]
[[ 1 12 16]
[24 37 14]
[34 29 28]
[15 41 12]]
[[32 46 45]
[38 12 40]
[10 34 48]
[40 37 23]]]
计算gmp
arr=arr.reshape(1,4,4,3)
inp=tf.keras.Input((4,4,3))
x=tf.keras.layers.GlobalMaxPooling2D()(inp)
model=tf.keras.Model(inp,x)
res=model(arr)
tf.print(res)
执行结果:
[[46 47 48]]