-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaELayer_torch.py
77 lines (69 loc) · 2.38 KB
/
SaELayer_torch.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.model_zoo import load_url
# 定义SE模块
class SELayer(nn.Module):
def __init__(self, channel, reduction=16):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
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).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
# 定义SaE模块
class SaELayer(nn.Module):
def __init__(self, in_channel, reduction=32):
super(SaELayer, self).__init__()
assert in_channel>=reduction and in_channel%reduction==0,'invalid in_channel in SaElayer'
self.reduction = reduction
self.cardinality=4
self.avg_pool = nn.AdaptiveAvgPool2d(1)
#cardinality 1
self.fc1 = nn.Sequential(
nn.Linear(in_channel,in_channel//self.reduction, bias=False),
nn.ReLU(inplace=True)
)
# cardinality 2
self.fc2 = nn.Sequential(
nn.Linear(in_channel, in_channel // self.reduction, bias=False),
nn.ReLU(inplace=True)
)
# cardinality 3
self.fc3 = nn.Sequential(
nn.Linear(in_channel, in_channel // self.reduction, bias=False),
nn.ReLU(inplace=True)
)
# cardinality 4
self.fc4 = nn.Sequential(
nn.Linear(in_channel, in_channel // self.reduction, bias=False),
nn.ReLU(inplace=True)
)
self.fc = nn.Sequential(
nn.Linear(in_channel//self.reduction*self.cardinality, in_channel, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y1 = self.fc1(y)
y2 = self.fc2(y)
y3 = self.fc3(y)
y4 = self.fc4(y)
y_concate = torch.cat([y1,y2,y3,y4],dim=1)
y_ex_dim = self.fc(y_concate).view(b,c,1,1)
return x * y_ex_dim.expand_as(x)
# import ipdb
# ipdb.set_trace()
se_v2 = SaELayer(64)
# 示例输入
input = torch.randn(3, 64, 224, 224)
output = se_v2(input)
print(output.shape)#torch.Size([3, 64, 224, 224])