2020-06-17 14:20:20 +08:00
|
|
|
import torch.nn as nn
|
|
|
|
|
|
|
|
|
|
|
|
class SELayer(nn.Module):
|
|
|
|
"""Squeeze-and-Excitation Module.
|
|
|
|
|
|
|
|
Args:
|
2020-06-25 11:57:50 +08:00
|
|
|
channels (int): The input (and output) channels of the SE layer.
|
|
|
|
ratio (int): Squeeze ratio in SELayer, the intermediate channel will be
|
|
|
|
``int(channels/ratio)``. Default: 16.
|
2020-06-17 14:20:20 +08:00
|
|
|
"""
|
|
|
|
|
2020-06-25 11:57:50 +08:00
|
|
|
def __init__(self, channels, ratio=16):
|
2020-06-17 14:20:20 +08:00
|
|
|
super(SELayer, self).__init__()
|
|
|
|
self.global_avgpool = nn.AdaptiveAvgPool2d(1)
|
|
|
|
self.conv1 = nn.Conv2d(
|
2020-06-25 11:57:50 +08:00
|
|
|
channels, int(channels / ratio), kernel_size=1, stride=1)
|
2020-06-17 14:20:20 +08:00
|
|
|
self.conv2 = nn.Conv2d(
|
2020-06-25 11:57:50 +08:00
|
|
|
int(channels / ratio), channels, kernel_size=1, stride=1)
|
2020-06-17 14:20:20 +08:00
|
|
|
self.relu = nn.ReLU(inplace=True)
|
|
|
|
self.sigmoid = nn.Sigmoid()
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
out = self.global_avgpool(x)
|
|
|
|
|
|
|
|
out = self.conv1(out)
|
|
|
|
out = self.relu(out)
|
|
|
|
|
|
|
|
out = self.conv2(out)
|
|
|
|
out = self.sigmoid(out)
|
|
|
|
return x * out
|