-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSE_Net.py
36 lines (29 loc) · 989 Bytes
/
SE_Net.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import torch
from torch import nn
class SE_Block(nn.Module):
def __init__(self, in_channels, ratio = 16):
super(SE_Block, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(in_channels, in_channels // ratio, bias = False),
nn.ReLU(),
nn.Linear(in_channels // ratio, in_channels, bias = False),
nn.Sigmoid()
)
def forward(self, x):
"""
b: Batch_size
c: channels
h: height
w: width
"""
b, c, h, w = x.size()
# after avg_pool, the x is become: b, c, h, w -> b, c, 1, 1
avg = self.avg_pool(x).view([b, c])
# b, c -> (b, c) // ratio -> b, c -> b, c, 1, 1
fc = self.fc(avg).view([b, c, 1, 1])
return x * fc
model = SE_Block(512)
print(model)
inputs = torch.ones([2, 512, 26, 26])
outputs = model(inputs)