Keras MaxPooling2D
Keras MaxPooling2D
2D最大池化层
keras.layers.MaxPooling2D(pool_size=(2, 2),
strides=None,
padding='valid',
data_format=None)
参数详解
pool_size: 池化窗口大小
strides: 池化步长,默认值等于 pool_size
padding: 'VALID' 或 'SAME','VALID'表示无填充,'SAME'表示用0填充
data_format: 表示输入张量的维度顺序,默认为 [batch, height, width, channel]
示例
from tensorflow.keras.layers import MaxPool2D
import tensorflow as tf
import numpy as np
# 定义一个最大池化层,用0填充
pool1 = MaxPool2D(pool_size=(2, 2),
strides=None,
padding='SAME',
data_format=None)
# 定义一个最大池化层,不填充
pool2 = MaxPool2D(pool_size=(2, 2),
strides=None,
padding='VALID',
data_format=None)
# 生成一个维度为[64, 101, 101, 3]的矩阵
x = np.random.random((64, 101, 101, 3))
# 转成tensor类型,第一个维度64表示batch
x = tf.convert_to_tensor(x)
print(x.shape) # [64, 101, 101, 3]
# 进行最大池化
y1 = pool1(x)
print(y1.shape) # [64, 51, 51, 3]
# 进行最大池化
y2 = pool2(x)
print(y2.shape) # [64, 50, 50, 3]
————————————————
链接:https://blog.csdn.net/weixin_46566663/article/details/127621307
==================================================
一. pooling的总类
根据维度可将其分为:1D,2D,3D
根据取值运算可将其分为:max,average
根据步长可将其分为:正常,global
二. keras中的API(函数调用接口)
from .pooling import MaxPooling1D
from .pooling import MaxPooling2D
from .pooling import MaxPooling3D
from .pooling import AveragePooling1D
from .pooling import AveragePooling2D
from .pooling import AveragePooling3D
from .pooling import GlobalMaxPooling1D
from .pooling import GlobalMaxPooling2D
from .pooling import GlobalMaxPooling3D
from .pooling import GlobalAveragePooling2D
from .pooling import GlobalAveragePooling1D
from .pooling import GlobalAveragePooling3D
三. 分析其输入输出
在keras中使用时直接调用即可
from keras.layers import MaxPooling1D, MaxPooling2D, MaxPooling3D, AveragePooling1D, AveragePooling2D, AveragePooling3D, GlobalMaxPooling1D, GlobalMaxPooling2D, GlobalMaxPooling3D, GlobalAveragePooling2D, GlobalAveragePooling1D, GlobalAveragePooling3D
from keras.layers import Input
1. 1D
max和average只是一个取最大值,一个取均值,这里为了说明,只使用MaxPooling1D来测试:
inputs = Input(shape=(4,3))
x = MaxPooling1D()(inputs)
model = Model(inputs=inputs, outputs=x)
model.summary()
输出:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 4, 3) 0
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 2, 3) 0
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
所以:可以将MaxPooling1D的输入看作(batch, col, channel), 在col维度上取最大值,函数默认的步长为2,所以在输入为(None,4,3),输出为(None,2,3)。
2. Global1D
和1D的输入输出的维度一样,只是Global1D不在kernel上取值,而是在整个第二维度上取值,例如输入为(None,4,3),输出为(None,3).
3. 2D
以MaxPooling2D为例:
inputs = Input(shape=(5,4,3))
x = MaxPooling2D()(inputs)
model = Model(inputs=inputs, outputs=x)
model.summary()
输出:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 5, 4, 3) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 2, 2, 3) 0
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
所以:可以将MaxPooling2D的输入看作(batch,row, col, channel), 在row, col维度上取最大值,函数默认的步长为2,所以在输入为(None,5,4,3),输出为(None,2,2,3)。
4. Global2D
和2D的输入输出的维度一样,只是Global2D不在kernel上取值,而是在整个第二、三维度上取值,例如输入为(None,5,4,3),输出为(None,3).
5. 3D
以MaxPooling3D为例:
inputs = Input(shape=(6,5,4,3))
x = MaxPooling3D()(inputs)
model = Model(inputs=inputs, outputs=x)
model.summary()
输出:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 6, 5, 4, 3) 0
_________________________________________________________________
max_pooling3d_1 (MaxPooling3 (None, 3, 2, 2, 3) 0
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
所以:可以将MaxPooling2D的输入看作(batch,row, col,depth, channel), 在row, col, depth维度上取最大值,函数默认的步长为2,所以在输入为(None,6,5,4,3),输出为(None,3,2,2,3)。
4. Global3D
和2D的输入输出的维度一样,只是Global3D不在kernel上取值,而是在整个第二、三、四维度上取值,例如输入为(None,6,5,4,3),输出为(None,3).
链接:https://blog.csdn.net/weixin_41910772/article/details/109288701
==================================================
http://www.manongjc.com/detail/30-cxueilzhganqalk.html
https://www.codenong.com/39815518/