-
Notifications
You must be signed in to change notification settings - Fork 11
/
models.py
485 lines (388 loc) · 16.4 KB
/
models.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import torch
from torch import nn
import math
from torch.autograd import Function
from torch.nn.utils import weight_norm
import torch.nn.functional as F
import torch.fft as FFT
# from utils import weights_init
def get_backbone_class(backbone_name):
"""Return the algorithm class with the given name."""
if backbone_name not in globals():
raise NotImplementedError("Algorithm not found: {}".format(backbone_name))
return globals()[backbone_name]
##################################################
########## BACKBONE NETWORKS ###################
##################################################
########## CNN #############################
class CNN(nn.Module):
def __init__(self, configs):
super(CNN, self).__init__()
self.conv_block1 = nn.Sequential(
nn.Conv1d(configs.input_channels, configs.mid_channels, kernel_size=configs.kernel_size,
stride=configs.stride, bias=False, padding=(configs.kernel_size // 2)),
nn.BatchNorm1d(configs.mid_channels),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2, stride=2, padding=1),
nn.Dropout(configs.dropout)
)
self.conv_block2 = nn.Sequential(
nn.Conv1d(configs.mid_channels, configs.mid_channels * 2, kernel_size=8, stride=1, bias=False, padding=4),
nn.BatchNorm1d(configs.mid_channels * 2),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2, stride=2, padding=1)
)
self.conv_block3 = nn.Sequential(
nn.Conv1d(configs.mid_channels * 2, configs.final_out_channels, kernel_size=8, stride=1, bias=False,
padding=4),
nn.BatchNorm1d(configs.final_out_channels),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2, stride=2, padding=1),
)
self.adaptive_pool = nn.AdaptiveAvgPool1d(configs.features_len)
def forward(self, x_in):
x = self.conv_block1(x_in)
x = self.conv_block2(x)
x = self.conv_block3(x)
x = self.adaptive_pool(x)
x_flat = x.reshape(x.shape[0], -1)
return x_flat
class classifier(nn.Module):
def __init__(self, configs):
super(classifier, self).__init__()
model_output_dim = configs.out_dim
self.logits = nn.Linear(model_output_dim, configs.num_classes, bias=False)
self.tmp= 0.1
def forward(self, x):
predictions = self.logits(x)/self.tmp
return predictions
class ResClassifier_MME(nn.Module):
def __init__(self, configs):
super(ResClassifier_MME, self).__init__()
self.norm = True
self.tmp = 0.02
num_classes = configs.num_classes
input_size = configs.out_dim
self.fc = nn.Linear(input_size, num_classes, bias=False)
def set_lambda(self, lambd):
self.lambd = lambd
def forward(self, x, dropout=False, return_feat=False):
if return_feat:
return x
x = self.fc(x)/self.tmp
return x
def weight_norm(self):
w = self.fc.weight.data
norm = w.norm(p=2, dim=1, keepdim=True)
self.fc.weight.data = w.div(norm.expand_as(w))
def weights_init(self):
self.fc.weight.data.normal_(0.0, 0.1)
########## TCN #############################
torch.backends.cudnn.benchmark = True # might be required to fasten TCN
class Chomp1d(nn.Module):
def __init__(self, chomp_size):
super(Chomp1d, self).__init__()
self.chomp_size = chomp_size
def forward(self, x):
return x[:, :, :-self.chomp_size].contiguous()
class TCN(nn.Module):
def __init__(self, configs):
super(TCN, self).__init__()
in_channels0 = configs.input_channels
out_channels0 = configs.tcn_layers[1]
kernel_size = configs.tcn_kernel_size
stride = 1
dilation0 = 1
padding0 = (kernel_size - 1) * dilation0
self.net0 = nn.Sequential(
weight_norm(nn.Conv1d(in_channels0, out_channels0, kernel_size, stride=stride, padding=padding0,
dilation=dilation0)),
nn.ReLU(),
weight_norm(nn.Conv1d(out_channels0, out_channels0, kernel_size, stride=stride, padding=padding0,
dilation=dilation0)),
nn.ReLU(),
)
self.downsample0 = nn.Conv1d(in_channels0, out_channels0, 1) if in_channels0 != out_channels0 else None
self.relu = nn.ReLU()
in_channels1 = configs.tcn_layers[0]
out_channels1 = configs.tcn_layers[1]
dilation1 = 2
padding1 = (kernel_size - 1) * dilation1
self.net1 = nn.Sequential(
nn.Conv1d(in_channels0, out_channels1, kernel_size, stride=stride, padding=padding1, dilation=dilation1),
nn.ReLU(),
nn.Conv1d(out_channels1, out_channels1, kernel_size, stride=stride, padding=padding1, dilation=dilation1),
nn.ReLU(),
)
self.downsample1 = nn.Conv1d(out_channels1, out_channels1, 1) if in_channels1 != out_channels1 else None
self.conv_block1 = nn.Sequential(
nn.Conv1d(in_channels0, out_channels0, kernel_size=kernel_size, stride=stride, bias=False, padding=padding0,
dilation=dilation0),
Chomp1d(padding0),
nn.BatchNorm1d(out_channels0),
nn.ReLU(),
nn.Conv1d(out_channels0, out_channels0, kernel_size=kernel_size, stride=stride, bias=False,
padding=padding0, dilation=dilation0),
Chomp1d(padding0),
nn.BatchNorm1d(out_channels0),
nn.ReLU(),
)
self.conv_block2 = nn.Sequential(
nn.Conv1d(out_channels0, out_channels1, kernel_size=kernel_size, stride=stride, bias=False,
padding=padding1, dilation=dilation1),
Chomp1d(padding1),
nn.BatchNorm1d(out_channels1),
nn.ReLU(),
nn.Conv1d(out_channels1, out_channels1, kernel_size=kernel_size, stride=stride, bias=False,
padding=padding1, dilation=dilation1),
Chomp1d(padding1),
nn.BatchNorm1d(out_channels1),
nn.ReLU(),
)
def forward(self, inputs):
"""Inputs have to have dimension (N, C_in, L_in)"""
x0 = self.conv_block1(inputs)
res0 = inputs if self.downsample0 is None else self.downsample0(inputs)
out_0 = self.relu(x0 + res0)
x1 = self.conv_block2(out_0)
res1 = out_0 if self.downsample1 is None else self.downsample1(out_0)
out_1 = self.relu(x1 + res1)
out = out_1[:, :, -1]
return out
######## RESNET ##############################################
class RESNET18(nn.Module):
def __init__(self, configs):
layers = [2, 2, 2, 2]
block = BasicBlock
self.inplanes = configs.input_channels
super(RESNET18, self).__init__()
self.layer1 = self._make_layer(block, configs.mid_channels, layers[0], stride=configs.stride)
self.layer2 = self._make_layer(block, configs.mid_channels * 2, layers[1], stride=1)
self.layer3 = self._make_layer(block, configs.final_out_channels, layers[2], stride=1)
self.layer4 = self._make_layer(block, configs.final_out_channels, layers[3], stride=1)
self.avgpool = nn.MaxPool1d(kernel_size=2, stride=2, padding=1)
self.adaptive_pool = nn.AdaptiveAvgPool1d(configs.features_len)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv1d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm1d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.adaptive_pool(x)
x_flat = x.reshape(x.shape[0], -1)
return x_flat
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv1d(inplanes, planes, kernel_size=1, stride=stride,
bias=False)
self.bn1 = nn.BatchNorm1d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = F.relu(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = F.relu(out)
return out
##################################################
########## OTHER NETWORKS ######################
##################################################
class codats_classifier(nn.Module):
def __init__(self, configs):
super(codats_classifier, self).__init__()
model_output_dim = configs.features_len
self.hidden_dim = configs.hidden_dim
self.logits = nn.Sequential(
nn.Linear(model_output_dim * configs.final_out_channels, self.hidden_dim),
nn.ReLU(),
nn.Linear(self.hidden_dim, self.hidden_dim),
nn.ReLU(),
nn.Linear(self.hidden_dim, configs.num_classes))
def forward(self, x_in):
predictions = self.logits(x_in)
return predictions
class Discriminator(nn.Module):
"""Discriminator model for source domain."""
def __init__(self, configs):
"""Init discriminator."""
super(Discriminator, self).__init__()
self.layer = nn.Sequential(
nn.Linear(configs.features_len * configs.final_out_channels , configs.disc_hid_dim),
nn.ReLU(),
nn.Linear(configs.disc_hid_dim, configs.disc_hid_dim),
nn.ReLU(),
nn.Linear(configs.disc_hid_dim, 2)
# nn.LogSoftmax(dim=1)
)
def forward(self, input):
"""Forward the discriminator."""
out = self.layer(input)
return out
#### Codes required by DANN ##############
class ReverseLayerF(Function):
@staticmethod
def forward(ctx, x, alpha):
ctx.alpha = alpha
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
output = grad_output.neg() * ctx.alpha
return output, None
#### Codes required by CDAN ##############
class RandomLayer(nn.Module):
def __init__(self, input_dim_list=[], output_dim=1024):
super(RandomLayer, self).__init__()
self.input_num = len(input_dim_list)
self.output_dim = output_dim
self.random_matrix = [torch.randn(input_dim_list[i], output_dim) for i in range(self.input_num)]
def forward(self, input_list):
return_list = [torch.mm(input_list[i], self.random_matrix[i]) for i in range(self.input_num)]
return_tensor = return_list[0] / math.pow(float(self.output_dim), 1.0 / len(return_list))
for single in return_list[1:]:
return_tensor = torch.mul(return_tensor, single)
return return_tensor
def cuda(self):
super(RandomLayer, self).cuda()
self.random_matrix = [val.cuda() for val in self.random_matrix]
class Discriminator_CDAN(nn.Module):
"""Discriminator model for CDAN ."""
def __init__(self, configs):
"""Init discriminator."""
super(Discriminator_CDAN, self).__init__()
self.restored = False
self.layer = nn.Sequential(
nn.Linear(configs.features_len * configs.final_out_channels * configs.num_classes, configs.disc_hid_dim),
nn.ReLU(),
nn.Linear(configs.disc_hid_dim, configs.disc_hid_dim),
nn.ReLU(),
nn.Linear(configs.disc_hid_dim, 2)
# nn.LogSoftmax(dim=1)
)
def forward(self, input):
"""Forward the discriminator."""
out = self.layer(input)
return out
#### Codes required by AdvSKM ##############
class Cosine_act(nn.Module):
def __init__(self):
super(Cosine_act, self).__init__()
def forward(self, input):
return torch.cos(input)
cos_act = Cosine_act()
class AdvSKM_Disc(nn.Module):
"""Discriminator model for source domain."""
def __init__(self, configs):
"""Init discriminator."""
super(AdvSKM_Disc, self).__init__()
self.input_dim = configs.features_len * configs.final_out_channels
self.hid_dim = configs.DSKN_disc_hid
self.branch_1 = nn.Sequential(
nn.Linear(self.input_dim, self.hid_dim),
nn.Linear(self.hid_dim, self.hid_dim),
nn.BatchNorm1d(self.hid_dim),
cos_act,
nn.Linear(self.hid_dim, self.hid_dim // 2),
nn.Linear(self.hid_dim // 2, self.hid_dim // 2),
nn.BatchNorm1d(self.hid_dim // 2),
cos_act
)
self.branch_2 = nn.Sequential(
nn.Linear(configs.features_len * configs.final_out_channels, configs.disc_hid_dim),
nn.Linear(configs.disc_hid_dim, configs.disc_hid_dim),
nn.BatchNorm1d(configs.disc_hid_dim),
nn.ReLU(),
nn.Linear(configs.disc_hid_dim, configs.disc_hid_dim // 2),
nn.Linear(configs.disc_hid_dim // 2, configs.disc_hid_dim // 2),
nn.BatchNorm1d(configs.disc_hid_dim // 2),
nn.ReLU())
def forward(self, input):
"""Forward the discriminator."""
out_cos = self.branch_1(input)
out_rel = self.branch_2(input)
total_out = torch.cat((out_cos, out_rel), dim=1)
return total_out
#### Codes for attention ##############
class ScaledDotProductAttention(nn.Module):
"""Scaled dot-product attention mechanism."""
def __init__(self, attention_dropout=0.0):
super(ScaledDotProductAttention, self).__init__()
self.dropout = nn.Dropout(attention_dropout)
self.softmax = nn.Softmax(dim=2)
def forward(self, q, k, v, scale=None, attn_mask=None):
"""前向传播.
Args:
q: Queries张量,形状为[B, L_q, D_q]
k: Keys张量,形状为[B, L_k, D_k]
v: Values张量,形状为[B, L_v, D_v],一般来说就是k
scale: 缩放因子,一个浮点标量
attn_mask: Masking张量,形状为[B, L_q, L_k]
Returns:
上下文张量和attetention张量
"""
attention = torch.bmm(q, k.transpose(1, 2))
if scale:
attention = attention * scale
# if attn_mask:
# # 给需要mask的地方设置一个负无穷
# attention = attention.masked_fill_(attn_mask, -np.inf)
# 计算softmax
attention = self.softmax(attention)
# 添加dropout
attention = self.dropout(attention)
# 和V做点积
context = torch.bmm(attention, v)
return context, attention
class Projection(nn.Module):
"""
Creates projection head
Args:
n_in (int): Number of input features
n_hidden (int): Number of hidden features
n_out (int): Number of output features
use_bn (bool): Whether to use batch norm
"""
def __init__(self, n_in: int, n_hidden: int, n_out: int,
use_bn: bool = True):
super().__init__()
# No point in using bias if we've batch norm
self.lin1 = nn.Linear(n_in, n_hidden, bias=not use_bn)
self.bn = nn.BatchNorm1d(n_hidden) if use_bn else nn.Identity()
self.relu = nn.ReLU()
# No bias for the final linear layer
self.lin2 = nn.Linear(n_hidden, n_out, bias=False)
def forward(self, x):
x = self.lin1(x)
# x = self.bn(x)
x = self.relu(x)
return x
class SimCLRModel(nn.Module):
def __init__(self, encoder: nn.Module, projection_n_in: int = 128,
projection_n_hidden: int = 128, projection_n_out: int = 128,
projection_use_bn: bool = True):
super().__init__()
self.encoder = encoder
self.projection = Projection(projection_n_in, projection_n_hidden,
projection_n_out, projection_use_bn)
def forward(self, x):
h = self.encoder(x)
z = self.projection(h)
return h, z