-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresnet.py
More file actions
337 lines (262 loc) · 12.9 KB
/
resnet.py
File metadata and controls
337 lines (262 loc) · 12.9 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
import os
import random
import numpy as np
## Adapted from https://github.com/joaomonteirof/e2e_antispoofing
class SelfAttention(nn.Module):
def __init__(self, hidden_size, mean_only=False):
super(SelfAttention, self).__init__()
#self.output_size = output_size
self.hidden_size = hidden_size
self.att_weights = nn.Parameter(torch.Tensor(1, hidden_size),requires_grad=True)
self.mean_only = mean_only
init.kaiming_uniform_(self.att_weights)
def forward(self, inputs):
batch_size = inputs.size(0)
weights = torch.bmm(inputs, self.att_weights.permute(1, 0).unsqueeze(0).repeat(batch_size, 1, 1))
if inputs.size(0)==1:
attentions = F.softmax(torch.tanh(weights),dim=1)
weighted = torch.mul(inputs, attentions.expand_as(inputs))
else:
attentions = F.softmax(torch.tanh(weights.squeeze()),dim=1)
weighted = torch.mul(inputs, attentions.unsqueeze(2).expand_as(inputs))
if self.mean_only:
return weighted.sum(1)
else:
noise = 1e-5*torch.randn(weighted.size())
if inputs.is_cuda:
noise = noise.to(inputs.device)
avg_repr, std_repr = weighted.sum(1), (weighted+noise).std(1)
representations = torch.cat((avg_repr,std_repr),1)
return representations
class ChannelAttention(nn.Module):
def __init__(self, in_channel, ratio=16):
"""
: params: in_planes 输入模块的feature map的channel
: params: ratio 降维/升维因子
通道注意力则是将一个通道内的信息直接进行全局处理,容易忽略通道内的信息交互
"""
super(ChannelAttention, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1) # 平均池化,是取整个channel所有元素的均值 [3,5,5] => [3,1,1]
self.max_pool = nn.AdaptiveMaxPool2d(1) # 最大池化,是取整个channel所有元素的最大值[3,5,5] => [3,1,1]
# fc = shared MLP
self.fc = nn.Sequential(nn.Conv2d(in_channel, in_channel // ratio, 1, bias=False),
nn.ReLU(),
nn.Conv2d(in_channel // ratio, in_channel, 1, bias=False))
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = self.fc(self.avg_pool(x))
max_out = self.fc(self.max_pool(x))
out = avg_out + max_out
return self.sigmoid(out)
class SpatialAttention(nn.Module):
def __init__(self, kernel_size=7):
"""对空间注意力来说,由于将每个通道中的特征都做同等处理,容易忽略通道间的信息交互"""
super(SpatialAttention, self).__init__()
# 这里要保持卷积后的feature尺度不变,必须要padding=kernel_size//2
self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=kernel_size // 2, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x): # 输入x = [b, c, 56, 56]
avg_out = torch.mean(x, dim=1, keepdim=True) # avg_out = [b, 1, 56, 56] 求x的每个像素在所有channel相同位置上的平均值
max_out, _ = torch.max(x, dim=1, keepdim=True) # max_out = [b, 1, 56, 56] 求x的每个像素在所有channel相同位置上的最大值
x = torch.cat([avg_out, max_out], dim=1) # x = [b, 2, 56, 56] concat操作
x = self.sigmoid(self.conv1(x)) # x = [b, 1, 56, 56] 卷积操作,融合avg和max的信息,全方面考虑
return x
class PreActBlock(nn.Module):
'''Pre-activation version of the BasicBlock.'''
expansion = 1
def __init__(self, in_planes, planes, stride, *args, **kwargs):
super(PreActBlock, self).__init__()
self.bn1 = nn.BatchNorm2d(in_planes)
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False))
def forward(self, x):
out = F.relu(self.bn1(x))
shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x
out = self.conv1(out)
out = self.conv2(F.relu(self.bn2(out)))
out += shortcut
return out
class SELayer(nn.Module):
def __init__(self, channel, reduction=8):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel),
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
# https://blog.csdn.net/weixin_51331359/article/details/124772274
class eca_layer(nn.Module):
"""Constructs a ECA module.
Args:
channel: Number of channels of the input feature map
k_size: Adaptive selection of kernel size
"""
def __init__(self, channel, k_size=3):
super(eca_layer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
# feature descriptor on the global spatial information
y = self.avg_pool(x)
# Two different branches of ECA module
y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)
# Multi-scale information fusion
y = self.sigmoid(y)
return x * y.expand_as(x)
class MHSA(nn.Module):
def __init__(self, n_dims, width=14, height=14, heads=4):
super(MHSA, self).__init__()
self.heads = heads
self.query = nn.Conv2d(n_dims, n_dims, kernel_size=1)
self.key = nn.Conv2d(n_dims, n_dims, kernel_size=1)
self.value = nn.Conv2d(n_dims, n_dims, kernel_size=1)
self.rel_h = nn.Parameter(torch.randn([1, heads, n_dims // heads, 1, height]), requires_grad=True)
self.rel_w = nn.Parameter(torch.randn([1, heads, n_dims // heads, width, 1]), requires_grad=True)
self.softmax = nn.Softmax(dim=-1)
def forward(self, x):
n_batch, C, width, height = x.size()
q = self.query(x).view(n_batch, self.heads, C // self.heads, -1)
k = self.key(x).view(n_batch, self.heads, C // self.heads, -1)
v = self.value(x).view(n_batch, self.heads, C // self.heads, -1)
content_content = torch.matmul(q.permute(0, 1, 3, 2), k)
content_position = (self.rel_h + self.rel_w).view(1, self.heads, C // self.heads, -1).permute(0, 1, 3, 2)
content_position = torch.matmul(content_position, q)
energy = content_content + content_position
attention = self.softmax(energy)
out = torch.matmul(v, attention.permute(0, 1, 3, 2))
out = out.view(n_batch, C, width, height)
return out
# https://blog.51cto.com/u_16106623/6261705
class PreActBottleneck(nn.Module):
'''Pre-activation version of the original Bottleneck module.'''
expansion = 4
def __init__(self, in_planes, planes, stride, *args, **kwargs):
super(PreActBottleneck, self).__init__()
self.bn1 = nn.BatchNorm2d(in_planes)
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1, stride=stride, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.bn3 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)
self.eca = eca_layer(planes)
# # 加入CBAM
# self.ca = ChannelAttention(planes * self.expansion)
# self.sa = SpatialAttention()
# self.se = SELayer(planes * self.expansion, 8)
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False))
def forward(self, x):
out = F.relu(self.bn1(x))
shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x
out = self.conv1(out)
out = self.conv2(F.relu(self.bn2(out)))
out = self.conv3(F.relu(self.bn3(out)))
# # 加入CBAM
# out = self.ca(out) * out
# out = self.sa(out) * out
# # 加入se
#out = self.se(out)
# 加入eca
#out = self.eca(out)
out += shortcut
return out
def conv3x3(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
def conv1x1(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
RESNET_CONFIGS = {'18': [[2, 2, 2, 2], PreActBlock],
'28': [[3, 4, 6, 3], PreActBlock],
'34': [[3, 4, 6, 3], PreActBlock],
'50': [[3, 4, 6, 3], PreActBottleneck],
'101': [[3, 4, 23, 3], PreActBottleneck]
}
def setup_seed(random_seed, cudnn_deterministic=True):
# initialization
torch.manual_seed(random_seed)
random.seed(random_seed)
np.random.seed(random_seed)
os.environ['PYTHONHASHSEED'] = str(random_seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(random_seed)
torch.backends.cudnn.deterministic = cudnn_deterministic
torch.backends.cudnn.benchmark = False
class ResNet(nn.Module):
def __init__(self, num_nodes, enc_dim, resnet_type='18', nclasses=2):
self.in_planes = 16
super(ResNet, self).__init__()
layers, block = RESNET_CONFIGS[resnet_type]
self._norm_layer = nn.BatchNorm2d
self.conv1 = nn.Conv2d(1, 16, kernel_size=(2, 3), stride=(1, 1), padding=(1, 1), bias=False)
self.conv2 = nn.Conv2d(16, 16, kernel_size=(2, 3), stride=(1, 1), padding=(0, 1), bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.activation = nn.ReLU()
self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
self.layer2 = self._make_layer(block, 128, layers[1], stride=1)
self.layer3 = self._make_layer(block, 256, layers[2], stride=1)
self.layer4 = self._make_layer(block, 512, layers[3], stride=1)
self.conv5 = nn.Conv2d(512 * block.expansion, 256, kernel_size=(2, 2), stride=(1, 1), padding=(0, 1),
bias=False)
self.convout = nn.Conv2d(512 * block.expansion, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
self.bn5 = nn.BatchNorm2d(512 * block.expansion)
self.fc = nn.Linear(256 * 2, enc_dim)
self.fc_mu = nn.Linear(enc_dim, nclasses) if nclasses >= 2 else nn.Linear(enc_dim, 1)
self.initialize_params()
self.attention = SelfAttention(256)
def initialize_params(self):
for layer in self.modules():
if isinstance(layer, torch.nn.Conv2d):
init.kaiming_normal_(layer.weight, a=0, mode='fan_out')
elif isinstance(layer, torch.nn.Linear):
init.kaiming_uniform_(layer.weight)
elif isinstance(layer, torch.nn.BatchNorm2d) or isinstance(layer, torch.nn.BatchNorm1d):
layer.weight.data.fill_(1)
layer.bias.data.zero_()
def _make_layer(self, block, planes, num_blocks, stride=1):
norm_layer = self._norm_layer
downsample = None
if stride != 1 or self.in_planes != planes * block.expansion:
downsample = nn.Sequential(conv1x1(self.in_planes, planes * block.expansion, stride),
norm_layer(planes * block.expansion))
layers = []
layers.append(block(self.in_planes, planes, stride, downsample, 1, 64, 1, norm_layer))
self.in_planes = planes * block.expansion
for _ in range(1, num_blocks):
layers.append(
block(self.in_planes, planes, 1, groups=1, base_width=64, dilation=False, norm_layer=norm_layer))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.activation(self.bn1(x))
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.activation(self.bn5(x))
# x = self.conv5(x)
# x = self.activation(self.bn5(x)).squeeze(2)
#
# stats = self.attention(x.permute(0, 2, 1).contiguous())
#
# feat = self.fc(stats)
#
# mu = self.fc_mu(feat)
x = self.convout(x)
return x
# https://blog.csdn.net/qq_38253797/article/details/117292848 CBAM
# self_attention