当前位置:网站首页>全局池化–Pytorch
全局池化–Pytorch
2022-06-11 09:17:00 【liiiiiiiiiiiiike】
全局平均池化
卷积神经网络可以解决回归跟分类问题,但是常见的卷积神经网络到最后都要通过全连接层实现分类,这个其实会导致很多时候神经元数目跟计算量在全连接层暴增,特别对一些回归要求比较高的网络往往会带来一些后遗症。所以陆陆续续有人提出了不同全连接层解决方案,最常见的两个就是把最后卷积层flatten改为全局最大/均值池化,对比一下这两种方式,图示如下:
可以看到全局池化会根据需要产生神经元,神经元个数可控,可调。而flatten方式就是一个硬链接,无法在flatten的时候调整链接数目。全局均值池化输出最常见的做法是把每个通道feature map输出一个神经元(均值结果输出),图示如下:
全局最大池化图示如下,它是取每个feature map的最大值:
全局均值池化跟全局最大池化的输入一般为NxCxHxW,输出为NxCx1x1但是实际上有时候我们还会有另外一个需求,就是全局深度池化,它的输出是Nx1xHxW。这个方式的池化通常会先把数据转换为NxH*WxC的方式,然后使用一维度最大/均值池化在C上完成,最后在转换为Nx1xHxW即可。了解常见几种全局池化方式之后,下面就来一起看看Pytorch中支持的相关函数。
Pytorch全局池化代码演示
- 全局最大池化
torch.nn.AdaptiveMaxPool2d(output_size, return_indices=False)
- 全局平均池化
torch.nn.AdaptiveAvgPool2d(output_size)
其中output_size表示输出HxW正常设为HxW=1x1=(1, 1)。代码演示如下:
## 输入为N x C x H x W=1 x 8 x 4 x 4
# 全局均值池化
avg_pooling = torch.nn.AdaptiveAvgPool2d((1,1))
B, C, H, W = input.size()
output = avg_pooling(input).view(B, -1)
print("全局均值池化:", output.size())
print(output, "\n")
# 全局最大池化
avg_pooling = torch.nn.AdaptiveMaxPool2d((1, 1))
B, C, H, W = input.size()
output = avg_pooling(input).view(B, -1)
print("全局最大池化:", output.size())
print(output, "\n")

- 全局深度池化: 将特征图维度改成任意维度,且宽高都为1
class DeepWise_Pool(torch.nn.MaxPool1d):
def __init__(self, channels, isize):
super(DeepWise_Pool, self).__init__(channels)
self.kernel_size = channels
self.stride = isize
def forward(self, input):
n, c, w, h = input.size()
input = input.view(n,c,w*h).permute(0,2,1)
pooled = torch.nn.functional.max_pool1d(input, self.kernel_size, self.stride,
self.padding, self.dilation, self.ceil_mode,
self.return_indices)
_, _, c = pooled.size()
pooled = pooled.permute(0,2,1)
return pooled.view(n,c,w,h).view(w, h)
input = torch.randn(1, 8, 4, 4)
print("input data:/n", input)
print("input data:", input.size())
B, C, W, H = input.size()
dw_max_pool = DeepWise_Pool(C, W*H)
output = dw_max_pool(input)
print("全局深度池化:", output.view(-1, 16).size())
print(output, "\n")

全局池化优点:
CNN在图像分类中,把卷积层作为特征提取,FC层+softmax作为回归分类,缺点就是FC层神经元太多容易过拟合,一般用dropout提高过拟合。但还是避免不了参数量多,速度慢的缺陷。可以用GAP方式来避免FC层的处理,直接通过全局池化+softmax进行分类,参数量低。此外还有一个优点:全局池化还部分保留输入图像的空间结构信息!
边栏推荐
- Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
- Four data-driven behaviors of integral system
- Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods
- CVPR 2021: learning continuous image representation with local implicit image function
- Error [detectionnetwork (1)][warning]network compiled for 6 shapes, maximum available 10, compiling for 5 S
- Machine learning notes - spatial transformer network using tensorflow
- 「INS-30131」 安装程序验证所需的初始设置失败
- Day45 storage engine data type integer floating point character type date type enumeration and set type constraints table to table relationships
- Automation operation and maintenance articles collection
- Zhiyun health submitted the statement to HKEx again: the loss in 2021 exceeded 4billion yuan, an increase of 43% year-on-year
猜你喜欢

Redis transaction details

Opencv image basic operation (IV) -- image feature extraction (corner detection)

Thread theory

OpenCV CEO教你用OAK(五):基于OAK-D和DepthAI的反欺骗人脸识别系统

An error will be reported when the RAC modifies the scanip to different network segments

Opencv CEO teaches you to use oak (IV): create complex pipelines

MSF SMB based information collection

报错Version mismatch between installed depthai lib and the required one by the scrip.

js中关键字this的理解

How do online app stores of laundry chain stores do?
随机推荐
ORACLE DG物理备库使用别名数据文件改变路径到OMF路径
Day 47 how to query a table
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s
Product list display
js基础--Array对象
Suffix Array
Analysis of Kube scheduler disk scheduling source code
Day41 process pool and thread pool
PD chip ga670-10 for OTG while charging
Openstack explanation (24) -- registration of neutron service
Modularnotfounderror: no module named 'find_ version’
js基础--关于DOM
DOS command virtual environment
Detailed explanation of the difference between construction method and method
[intelligent development] scheme design and hardware development of sphygmomanometer
Image quality evaluation including Matlab source code
Opencv image basic operation (III) -- image feature extraction (corner detection)
OpenCV OAK相机对比及介绍
Concurrent programming
js基础--运算符