forked from xiely-123/SHISRCNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSKnet.py
158 lines (131 loc) · 5.98 KB
/
SKnet.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import torch
from torch import nn
#from thop import profile
#from thop import clever_format
class SKConv(nn.Module):
def __init__(self, features, M=2, G=32, r=16, stride=1 ,L=32):
""" Constructor
Args:
features: input channel dimensionality.
M: the number of branchs.
G: num of convolution groups.
r: the ratio for compute d, the length of z.
stride: stride, default 1.
L: the minimum dim of the vector z in paper, default 32.
"""
super(SKConv, self).__init__()
d = max(int(features/r), L)
self.M = M
self.features = features
self.convs = nn.ModuleList([])
for i in range(M):
self.convs.append(nn.Sequential(
nn.Conv2d(features, features, kernel_size=3, stride=stride, padding=1+i, dilation=1+i, groups=G, bias=False),
nn.BatchNorm2d(features),
nn.ReLU(inplace=True)
))
self.gap = nn.AdaptiveAvgPool2d((1,1))
self.fc = nn.Sequential(nn.Conv2d(features, d, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(d),
nn.ReLU(inplace=True))
self.fcs = nn.ModuleList([])
for i in range(M):
self.fcs.append(
nn.Conv2d(d, features, kernel_size=1, stride=1)
)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
batch_size = x.shape[0]
feats = [conv(x) for conv in self.convs]
feats = torch.cat(feats, dim=1)
feats = feats.view(batch_size, self.M, self.features, feats.shape[2], feats.shape[3])
feats_U = torch.sum(feats, dim=1)
feats_S = self.gap(feats_U)
feats_Z = self.fc(feats_S)
attention_vectors = [fc(feats_Z) for fc in self.fcs]
attention_vectors = torch.cat(attention_vectors, dim=1)
attention_vectors = attention_vectors.view(batch_size, self.M, self.features, 1, 1)
attention_vectors = self.softmax(attention_vectors)
feats_V = torch.sum(feats*attention_vectors, dim=1)
return feats_V
class SKUnit(nn.Module):
def __init__(self, in_features, mid_features, out_features, M=2, G=32, r=16, stride=1, L=32):
""" Constructor
Args:
in_features: input channel dimensionality.
out_features: output channel dimensionality.
M: the number of branchs.
G: num of convolution groups.
r: the ratio for compute d, the length of z.
mid_features: the channle dim of the middle conv with stride not 1, default out_features/2.
stride: stride.
L: the minimum dim of the vector z in paper.
"""
super(SKUnit, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_features, mid_features, 1, stride=1, bias=False),
nn.BatchNorm2d(mid_features),
nn.ReLU(inplace=True)
)
self.conv2_sk = SKConv(mid_features, M=M, G=G, r=r, stride=stride, L=L)
self.conv3 = nn.Sequential(
nn.Conv2d(mid_features, out_features, 1, stride=1, bias=False),
nn.BatchNorm2d(out_features)
)
if in_features == out_features: # when dim not change, input_features could be added diectly to out
self.shortcut = nn.Sequential()
else: # when dim not change, input_features should also change dim to be added to out
self.shortcut = nn.Sequential(
nn.Conv2d(in_features, out_features, 1, stride=stride, bias=False),
nn.BatchNorm2d(out_features)
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.conv2_sk(out)
out = self.conv3(out)
return self.relu(out + self.shortcut(residual))
class SKNet(nn.Module):
def __init__(self, class_num, nums_block_list = [3, 4, 6, 3], strides_list = [1, 2, 1, 2]):
super(SKNet, self).__init__()
self.basic_conv = nn.Sequential(
nn.Conv2d(3, 32, 7, 2, 3, bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
)
self.maxpool = nn.MaxPool2d(3,2,1)
self.stage_1 = self._make_layer(32, 32, 64, nums_block=nums_block_list[0], stride=strides_list[0])
self.stage_2 = self._make_layer(64, 64, 128, nums_block=nums_block_list[1], stride=strides_list[1])
self.stage_3 = self._make_layer(128, 128, 128, nums_block=nums_block_list[2], stride=strides_list[2])
#self.stage_4 = self._make_layer(512, 512, 1024, nums_block=nums_block_list[3], stride=strides_list[3])
# self.gap = nn.AdaptiveAvgPool2d((1, 1))
# self.classifier = nn.Linear(2048, class_num)
def _make_layer(self, in_feats, mid_feats, out_feats, nums_block, stride=1):
layers=[SKUnit(in_feats, mid_feats, out_feats, stride=stride)]
for _ in range(1,nums_block):
layers.append(SKUnit(out_feats, mid_feats, out_feats))
return nn.Sequential(*layers)
def forward(self, x):
fea = self.basic_conv(x)
fea = self.maxpool(fea)
fea = self.stage_1(fea)
fea = self.stage_2(fea)
fea = self.stage_3(fea)
#fea = self.stage_4(fea)
# fea = self.gap(fea)
# fea = torch.squeeze(fea)
# fea = self.classifier(fea)
return fea
def SKNet26(nums_class=1000):
return SKNet(nums_class, [2, 2, 4])
def SKNet50(nums_class=1000):
return SKNet(nums_class, [3, 4, 9])
def SKNet101(nums_class=1000):
return SKNet(nums_class, [3, 4, 23, 3])
"""
if __name__=='__main__':
x = torch.rand(8, 3, 224, 224)
model = SKNet26()
out = model(x)
"""