forked from mymuli/Deep-Learning-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSENet_improved.py
More file actions
36 lines (18 loc) · 822 Bytes
/
SENet_improved.py
File metadata and controls
36 lines (18 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import torch
import torch.nn as nn
import torch.tensor as tensor
from torch.nn import functional as F
# 通道注意力模块(SENet的改进版)
class ChannelAttn(nn.Module):
def __init__(self, in_channels, reduction_rate=16):
super(ChannelAttn, self).__init__()
assert in_channels%reduction_rate == 0
self.conv1 = nn.Conv2d(in_channels, in_channels // reduction_rate, kernel_size=1, stride=1, padding=0)
self.conv2 = nn.Conv2d(in_channels // reduction_rate, in_channels, kernel_size=1, stride=1, padding=0)
def forward(self, x):
# squeeze operation (global average pooling)
x = F.avg_pool2d(x, x.size()[2:])
# excitation operation (2 conv layers)
x = F.relu(self.conv1(x))
x = self.conv2(x)
return torch.sigmoid(x)