📜  美国有线电视新闻网 |池化层简介

📅  最后修改于: 2022-05-13 01:55:35.762000             🧑  作者: Mango

美国有线电视新闻网 |池化层简介

池化操作包括在特征图的每个通道上滑动一个二维过滤器,并总结过滤器覆盖区域内的特征。
对于维度为 n h xn w xn c的特征图,经过池化层后得到的输出维度为

(nh - f + 1) / s x (nw - f + 1)/s x nc

在哪里,

-> nh - height of feature map
-> nw - width of feature map
-> nc - number of channels in the feature map
-> f  - size of filter
-> s  - stride length

一种常见的 CNN 模型架构是将多个卷积层和池化层一个接一个地堆叠起来。

为什么要使用池化层?

  • 池化层用于减少特征图的维度。因此,它减少了要学习的参数数量和网络中执行的计算量。
  • 池化层总结了卷积层生成的特征图区域中存在的特征。因此,对汇总特征执行进一步操作,而不是卷积层生成的精确定位特征。这使得模型对输入图像中特征位置的变化更加鲁棒。

池化层的类型:最大池化

  1. 最大池化是一种池化操作,它从过滤器覆盖的特征图区域中选择最大元素。因此,最大池化层之后的输出将是一个包含前一个特征图最突出特征的特征图。

  1. 这可以使用 keras 中的 MaxPooling2D 层来实现,如下所示:
    代码 #1:使用 keras 执行最大池化
Python3
import numpy as np
from keras.models import Sequential
from keras.layers import MaxPooling2D
 
# define input image
image = np.array([[2, 2, 7, 3],
                  [9, 4, 6, 1],
                  [8, 5, 2, 4],
                  [3, 1, 2, 6]])
image = image.reshape(1, 4, 4, 1)
 
# define model containing just a single max pooling layer
model = Sequential(
    [MaxPooling2D(pool_size = 2, strides = 2)])
 
# generate pooled output
output = model.predict(image)
 
# print output image
output = np.squeeze(output)
print(output)


Python3
import numpy as np
from keras.models import Sequential
from keras.layers import AveragePooling2D
 
# define input image
image = np.array([[2, 2, 7, 3],
                  [9, 4, 6, 1],
                  [8, 5, 2, 4],
                  [3, 1, 2, 6]])
image = image.reshape(1, 4, 4, 1)
 
# define model containing just a single average pooling layer
model = Sequential(
    [AveragePooling2D(pool_size = 2, strides = 2)])
 
# generate pooled output
output = model.predict(image)
 
# print output image
output = np.squeeze(output)
print(output)


Python3
import numpy as np
from keras.models import Sequential
from keras.layers import GlobalMaxPooling2D
from keras.layers import GlobalAveragePooling2D
 
# define input image
image = np.array([[2, 2, 7, 3],
                  [9, 4, 6, 1],
                  [8, 5, 2, 4],
                  [3, 1, 2, 6]])
image = image.reshape(1, 4, 4, 1)
 
# define gm_model containing just a single global-max pooling layer
gm_model = Sequential(
    [GlobalMaxPooling2D()])
 
# define ga_model containing just a single global-average pooling layer
ga_model = Sequential(
    [GlobalAveragePooling2D()])
 
# generate pooled output
gm_output = gm_model.predict(image)
ga_output = ga_model.predict(image)
 
# print output image
gm_output = np.squeeze(gm_output)
ga_output = np.squeeze(ga_output)
print("gm_output: ", gm_output)
print("ga_output: ", ga_output)


  1. 输出:
[[9. 7.]
[8. 6.]]

平均池化

  1. 平均池计算过滤器覆盖的特征图区域中存在的元素的平均值。因此,虽然最大池化给出了特征图中特定块中最突出的特征,但平均池化给出了块中存在的特征的平均值。

  1. 代码 #2:使用 keras 执行平均池化

Python3

import numpy as np
from keras.models import Sequential
from keras.layers import AveragePooling2D
 
# define input image
image = np.array([[2, 2, 7, 3],
                  [9, 4, 6, 1],
                  [8, 5, 2, 4],
                  [3, 1, 2, 6]])
image = image.reshape(1, 4, 4, 1)
 
# define model containing just a single average pooling layer
model = Sequential(
    [AveragePooling2D(pool_size = 2, strides = 2)])
 
# generate pooled output
output = model.predict(image)
 
# print output image
output = np.squeeze(output)
print(output)
  1. 输出:
[[4.25 4.25]
[4.25 3.5 ]]

全局池

  1. 全局池化将特征图中的每个通道减少为单个值。因此,一个n h xn w xn c特征图被缩减为1 x 1 xn c特征图。这等效于使用维度为n h xn w的过滤器,即特征图的维度。
    此外,它可以是全局最大池化或全局平均池化。
    代码 #3:使用 keras 执行全局池化

Python3

import numpy as np
from keras.models import Sequential
from keras.layers import GlobalMaxPooling2D
from keras.layers import GlobalAveragePooling2D
 
# define input image
image = np.array([[2, 2, 7, 3],
                  [9, 4, 6, 1],
                  [8, 5, 2, 4],
                  [3, 1, 2, 6]])
image = image.reshape(1, 4, 4, 1)
 
# define gm_model containing just a single global-max pooling layer
gm_model = Sequential(
    [GlobalMaxPooling2D()])
 
# define ga_model containing just a single global-average pooling layer
ga_model = Sequential(
    [GlobalAveragePooling2D()])
 
# generate pooled output
gm_output = gm_model.predict(image)
ga_output = ga_model.predict(image)
 
# print output image
gm_output = np.squeeze(gm_output)
ga_output = np.squeeze(ga_output)
print("gm_output: ", gm_output)
print("ga_output: ", ga_output)
  1. 输出:
gm_output:  9.0
ga_output:  4.0625