``````nn.AdaptiveAvgPool2d(output_size)
``````

output_size：指定的输出大小，可以是元组(H，W)，或者是单个的数，如果是单个的数，则表示输出的高和宽尺寸一样，output_size大小可以大于输入的图片尺寸大小。

``````
import torch # target output size of 5x7
import torch.nn as nn
input = torch.randn(1, 64, 8, 9)
output = m(input)
print(output.shape)
# target output size of 7x7 (square)
input = torch.randn(1, 64, 10, 9)
output = m(input)
print(output.shape)
# target output size of 10x7
input = torch.randn(1, 64, 10, 9)
output = m(input)
print(output.shape)
input = torch.randn(1, 64, 10, 9)
output = m(input)
print(output.shape)
``````

``````torch.Size([1, 64, 5, 7])
torch.Size([1, 64, 7, 7])
torch.Size([1, 64, 10, 7])
torch.Size([1, 64, 1, 1])
``````

## 应用

``````class SELayer(nn.Module):
def __init__(self, channel, reduction=16):
super(SELayer, self).__init__()
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
nn.Sigmoid()
)

def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x)
y=y.view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
``````

``````    self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
``````

# nn.AvgPool2d

kernel_size：池化核的尺寸大小
stride：窗口的移动步幅，默认与kernel_size大小一致

# 应用

``````class SqueezeAndExcite(nn.Module):
def __init__(self, in_channels, out_channels,se_kernel_size, divide=4):
super(SqueezeAndExcite, self).__init__()
mid_channels = in_channels // divide
self.pool = nn.AvgPool2d(kernel_size=se_kernel_size,stride=1)
self.SEblock = nn.Sequential(
nn.Linear(in_features=in_channels, out_features=mid_channels),
nn.ReLU6(inplace=True),
nn.Linear(in_features=mid_channels, out_features=out_channels),
HardSwish(inplace=True),
)

def forward(self, x):
b, c, h, w = x.size()
out = self.pool(x)
out = out.view(b, -1)
out = self.SEblock(out)
out = out.view(b, c, 1, 1)
return out * x
``````

``````  self.avgpool = nn.AvgPool2d(7, stride=1)
``````

THE END