-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
685 lines (551 loc) · 26.6 KB
/
model.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch.autograd import Function
from torch import Tensor
import os
import numpy as np
## Adapted from https://github.com/joaomonteirof/e2e_antispoofing
## https://github.com/nii-yamagishilab/project-NN-Pytorch-scripts/blob/newfunctions/
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 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 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.bn2 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=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 = self.conv3(F.relu(self.bn3(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]
}
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=(9, 3), stride=(3, 1), padding=(1, 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=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.conv5 = nn.Conv2d(512 * block.expansion, 256, kernel_size=(num_nodes, 3), stride=(1, 1), padding=(0, 1),
bias=False)
self.bn5 = nn.BatchNorm2d(256)
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.activation(self.bn1(x))
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(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)
return feat, mu
class MaxFeatureMap2D(nn.Module):
""" Max feature map (along 2D)
MaxFeatureMap2D(max_dim=1)
l_conv2d = MaxFeatureMap2D(1)
data_in = torch.rand([1, 4, 5, 5])
data_out = l_conv2d(data_in)
Input:
------
data_in: tensor of shape (batch, channel, ...)
Output:
-------
data_out: tensor of shape (batch, channel//2, ...)
Note
----
By default, Max-feature-map is on channel dimension,
and maxout is used on (channel ...)
"""
def __init__(self, max_dim=1):
super(MaxFeatureMap2D, self).__init__()
self.max_dim = max_dim
def forward(self, inputs):
# suppose inputs (batchsize, channel, length, dim)
shape = list(inputs.size())
if self.max_dim >= len(shape):
print("MaxFeatureMap: maximize on %d dim" % (self.max_dim))
print("But input has %d dimensions" % (len(shape)))
sys.exit(1)
if shape[self.max_dim] // 2 * 2 != shape[self.max_dim]:
print("MaxFeatureMap: maximize on %d dim" % (self.max_dim))
print("But this dimension has an odd number of data")
sys.exit(1)
shape[self.max_dim] = shape[self.max_dim] // 2
shape.insert(self.max_dim, 2)
# view to (batchsize, 2, channel//2, ...)
# maximize on the 2nd dim
m, i = inputs.view(*shape).max(self.max_dim)
return m
class LCNN(nn.Module):
def __init__(self, num_nodes, args, nclasses=2):
super(LCNN, self).__init__()
self.num_nodes = num_nodes
self.enc_dim = args.enc_dim
self.nclasses = nclasses
self.conv1 = nn.Sequential(nn.Conv2d(1, 64, (5, 5), 1, padding=(2, 2)),
MaxFeatureMap2D(),
nn.MaxPool2d((2, 2), (2, 2)))
self.conv2 = nn.Sequential(nn.Conv2d(32, 64, (1, 1), 1, padding=(0, 0)),
MaxFeatureMap2D(),
nn.BatchNorm2d(32, affine=False))
self.conv3 = nn.Sequential(nn.Conv2d(32, 96, (3, 3), 1, padding=(1, 1)),
MaxFeatureMap2D(),
nn.MaxPool2d((2, 2), (2, 2)),
nn.BatchNorm2d(48, affine=False))
self.conv4 = nn.Sequential(nn.Conv2d(48, 96, (1, 1), 1, padding=(0, 0)),
MaxFeatureMap2D(),
nn.BatchNorm2d(48, affine=False))
self.conv5 = nn.Sequential(nn.Conv2d(48, 128, (3, 3), 1, padding=(1, 1)),
MaxFeatureMap2D(),
nn.MaxPool2d((2, 2), (2, 2)))
self.conv6 = nn.Sequential(nn.Conv2d(64, 128, (1, 1), 1, padding=(0, 0)),
MaxFeatureMap2D(),
nn.BatchNorm2d(64, affine=False))
self.conv7 = nn.Sequential(nn.Conv2d(64, 64, (3, 3), 1, padding=(1, 1)),
MaxFeatureMap2D(),
nn.BatchNorm2d(32, affine=False))
self.conv8 = nn.Sequential(nn.Conv2d(32, 64, (1, 1), 1, padding=(0, 0)),
MaxFeatureMap2D(),
nn.BatchNorm2d(32, affine=False))
self.conv9 = nn.Sequential(nn.Conv2d(32, 64, (3, 3), 1, padding=[1, 1]),
MaxFeatureMap2D(),
nn.MaxPool2d((2, 2), (2, 2)))
self.out = nn.Sequential(nn.Dropout(0.7),
nn.Linear((args.feat_len // 16) * (60 // 16) * 32, 160),
MaxFeatureMap2D(),
nn.Linear(80, self.enc_dim))
self.fc_mu = nn.Linear(self.enc_dim, nclasses) if nclasses >= 2 else nn.Linear(self.enc_dim, 1)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.conv6(x)
x = self.conv7(x)
x = self.conv8(x)
x = self.conv9(x)
feat = torch.flatten(x, 1)
feat = self.out(feat)
out = self.fc_mu(feat)
return feat, out
class GradientReversalFunction(Function):
"""
Gradient Reversal Layer from:
Unsupervised Domain Adaptation by Backpropagation (Ganin & Lempitsky, 2015)
Forward pass is the identity function. In the backward pass,
the upstream gradients are multiplied by -lambda (i.e. gradient is reversed)
"""
@staticmethod
def forward(ctx, x, lambda_):
ctx.lambda_ = lambda_
return x.clone()
@staticmethod
def backward(ctx, grads):
lambda_ = ctx.lambda_
lambda_ = grads.new_tensor(lambda_)
dx = -lambda_ * grads
return dx, None
class GradientReversal(nn.Module):
def __init__(self, lambda_=1):
super(GradientReversal, self).__init__()
self.lambda_ = lambda_
def forward(self, x):
return GradientReversalFunction.apply(x, self.lambda_)
class ChannelClassifier(nn.Module):
def __init__(self, enc_dim, nclasses, lambda_=0.05, ADV=True):
super(ChannelClassifier, self).__init__()
self.adv = ADV
if self.adv:
self.grl = GradientReversal(lambda_)
self.classifier = nn.Sequential(nn.Linear(enc_dim, enc_dim // 2),
nn.Dropout(0.3),
nn.ReLU(),
nn.Linear(enc_dim // 2, nclasses),
nn.ReLU())
def initialize_params(self):
for layer in self.modules():
if isinstance(layer, torch.nn.Linear):
init.kaiming_uniform_(layer.weight)
def forward(self, x):
if self.adv:
x = self.grl(x)
return self.classifier(x)
class SincConv(nn.Module):
@staticmethod
def to_mel(hz):
return 2595 * np.log10(1 + hz / 700)
@staticmethod
def to_hz(mel):
return 700 * (10 ** (mel / 2595) - 1)
def __init__(self, device, out_channels, kernel_size, in_channels=1, sample_rate=16000,
stride=1, padding=0, dilation=1, bias=False, groups=1):
super(SincConv, self).__init__()
if in_channels != 1:
msg = "SincConv only support one input channel (here, in_channels = {%i})" % (in_channels)
raise ValueError(msg)
self.device = device
self.out_channels = out_channels
self.kernel_size = kernel_size
self.sample_rate = sample_rate
# Forcing the filters to be odd (i.e, perfectly symmetrics)
if kernel_size % 2 == 0:
self.kernel_size = self.kernel_size + 1
self.stride = stride
self.padding = padding
self.dilation = dilation
if bias:
raise ValueError('SincConv does not support bias.')
if groups > 1:
raise ValueError('SincConv does not support groups.')
# initialize filterbanks using Mel scale
NFFT = 512
f = int(self.sample_rate / 2) * np.linspace(0, 1, int(NFFT / 2) + 1)
fmel = self.to_mel(f) # Hz to mel conversion
fmelmax = np.max(fmel)
fmelmin = np.min(fmel)
filbandwidthsmel = np.linspace(fmelmin, fmelmax, self.out_channels + 1)
filbandwidthsf = self.to_hz(filbandwidthsmel) # Mel to Hz conversion
self.mel = filbandwidthsf
self.hsupp = torch.arange(-(self.kernel_size - 1) / 2, (self.kernel_size - 1) / 2 + 1)
self.band_pass = torch.zeros(self.out_channels, self.kernel_size)
def forward(self, x):
for i in range(len(self.mel) - 1):
fmin = self.mel[i]
fmax = self.mel[i + 1]
hHigh = (2 * fmax / self.sample_rate) * np.sinc(2 * fmax * self.hsupp / self.sample_rate)
hLow = (2 * fmin / self.sample_rate) * np.sinc(2 * fmin * self.hsupp / self.sample_rate)
hideal = hHigh - hLow
self.band_pass[i, :] = Tensor(np.hamming(self.kernel_size)) * Tensor(hideal)
band_pass_filter = self.band_pass.to(self.device)
self.filters = (band_pass_filter).view(self.out_channels, 1, self.kernel_size)
return F.conv1d(x, self.filters, stride=self.stride,
padding=self.padding, dilation=self.dilation,
bias=None, groups=1)
class Residual_block(nn.Module):
def __init__(self, nb_filts, first=False):
super(Residual_block, self).__init__()
self.first = first
if not self.first:
self.bn1 = nn.BatchNorm1d(num_features=nb_filts[0])
self.lrelu = nn.LeakyReLU(negative_slope=0.3)
self.conv1 = nn.Conv1d(in_channels=nb_filts[0],
out_channels=nb_filts[1],
kernel_size=3,
padding=1,
stride=1)
self.bn2 = nn.BatchNorm1d(num_features=nb_filts[1])
self.conv2 = nn.Conv1d(in_channels=nb_filts[1],
out_channels=nb_filts[1],
padding=1,
kernel_size=3,
stride=1)
if nb_filts[0] != nb_filts[1]:
self.downsample = True
self.conv_downsample = nn.Conv1d(in_channels=nb_filts[0],
out_channels=nb_filts[1],
padding=0,
kernel_size=1,
stride=1)
else:
self.downsample = False
self.mp = nn.MaxPool1d(3)
def forward(self, x):
identity = x
if not self.first:
out = self.bn1(x)
out = self.lrelu(out)
else:
out = x
out = self.conv1(x)
out = self.bn2(out)
out = self.lrelu(out)
out = self.conv2(out)
if self.downsample:
identity = self.conv_downsample(identity)
out += identity
out = self.mp(out)
return out
class RawNet(nn.Module):
def __init__(self, d_args, args):
super(RawNet, self).__init__()
self.device = args.device
self.Sinc_conv = SincConv(device=self.device,
out_channels=d_args['filts'][0],
kernel_size=d_args['first_conv'],
in_channels=d_args['in_channels']
)
self.first_bn = nn.BatchNorm1d(num_features=d_args['filts'][0])
self.selu = nn.SELU(inplace=True)
self.block0 = nn.Sequential(Residual_block(nb_filts=d_args['filts'][1], first=True))
self.block1 = nn.Sequential(Residual_block(nb_filts=d_args['filts'][1]))
self.block2 = nn.Sequential(Residual_block(nb_filts=d_args['filts'][2]))
d_args['filts'][2][0] = d_args['filts'][2][1]
self.block3 = nn.Sequential(Residual_block(nb_filts=d_args['filts'][2]))
self.block4 = nn.Sequential(Residual_block(nb_filts=d_args['filts'][2]))
self.block5 = nn.Sequential(Residual_block(nb_filts=d_args['filts'][2]))
self.avgpool = nn.AdaptiveAvgPool1d(1)
self.fc_attention0 = self._make_attention_fc(in_features=d_args['filts'][1][-1],
l_out_features=d_args['filts'][1][-1])
self.fc_attention1 = self._make_attention_fc(in_features=d_args['filts'][1][-1],
l_out_features=d_args['filts'][1][-1])
self.fc_attention2 = self._make_attention_fc(in_features=d_args['filts'][2][-1],
l_out_features=d_args['filts'][2][-1])
self.fc_attention3 = self._make_attention_fc(in_features=d_args['filts'][2][-1],
l_out_features=d_args['filts'][2][-1])
self.fc_attention4 = self._make_attention_fc(in_features=d_args['filts'][2][-1],
l_out_features=d_args['filts'][2][-1])
self.fc_attention5 = self._make_attention_fc(in_features=d_args['filts'][2][-1],
l_out_features=d_args['filts'][2][-1])
self.bn_before_gru = nn.BatchNorm1d(num_features=d_args['filts'][2][-1])
self.gru = nn.GRU(input_size=d_args['filts'][2][-1],
hidden_size=d_args['gru_node'],
num_layers=d_args['nb_gru_layer'],
batch_first=True)
self.fc1_gru = nn.Linear(in_features=d_args['gru_node'],
out_features=args.enc_dim)
self.fc2_gru = nn.Linear(in_features=args.enc_dim,
out_features=d_args['nb_classes'], bias=True)
self.sig = nn.Sigmoid()
self.logsoftmax = nn.LogSoftmax(dim=1)
def forward(self, x, y=None):
x = self.Sinc_conv(x)
x = F.max_pool1d(torch.abs(x), 3)
x = self.first_bn(x)
x = self.selu(x)
x0 = self.block0(x)
y0 = self.avgpool(x0).view(x0.size(0), -1) # torch.Size([batch, filter])
y0 = self.fc_attention0(y0)
y0 = self.sig(y0).view(y0.size(0), y0.size(1), -1) # torch.Size([batch, filter, 1])
x = x0 * y0 + y0 # (batch, filter, time) x (batch, filter, 1)
x1 = self.block1(x)
y1 = self.avgpool(x1).view(x1.size(0), -1) # torch.Size([batch, filter])
y1 = self.fc_attention1(y1)
y1 = self.sig(y1).view(y1.size(0), y1.size(1), -1) # torch.Size([batch, filter, 1])
x = x1 * y1 + y1 # (batch, filter, time) x (batch, filter, 1)
x2 = self.block2(x)
y2 = self.avgpool(x2).view(x2.size(0), -1) # torch.Size([batch, filter])
y2 = self.fc_attention2(y2)
y2 = self.sig(y2).view(y2.size(0), y2.size(1), -1) # torch.Size([batch, filter, 1])
x = x2 * y2 + y2 # (batch, filter, time) x (batch, filter, 1)
x3 = self.block3(x)
y3 = self.avgpool(x3).view(x3.size(0), -1) # torch.Size([batch, filter])
y3 = self.fc_attention3(y3)
y3 = self.sig(y3).view(y3.size(0), y3.size(1), -1) # torch.Size([batch, filter, 1])
x = x3 * y3 + y3 # (batch, filter, time) x (batch, filter, 1)
x4 = self.block4(x)
y4 = self.avgpool(x4).view(x4.size(0), -1) # torch.Size([batch, filter])
y4 = self.fc_attention4(y4)
y4 = self.sig(y4).view(y4.size(0), y4.size(1), -1) # torch.Size([batch, filter, 1])
x = x4 * y4 + y4 # (batch, filter, time) x (batch, filter, 1)
x5 = self.block5(x)
y5 = self.avgpool(x5).view(x5.size(0), -1) # torch.Size([batch, filter])
y5 = self.fc_attention5(y5)
y5 = self.sig(y5).view(y5.size(0), y5.size(1), -1) # torch.Size([batch, filter, 1])
x = x5 * y5 + y5 # (batch, filter, time) x (batch, filter, 1)
x = self.bn_before_gru(x)
x = self.selu(x)
x = x.permute(0, 2, 1) # (batch, filt, time) >> (batch, time, filt)
self.gru.flatten_parameters()
x, _ = self.gru(x)
x = x[:, -1, :]
feat = self.fc1_gru(x)
output = self.fc2_gru(feat)
# output = self.logsoftmax(x)
return feat, output
def _make_attention_fc(self, in_features, l_out_features):
l_fc = []
l_fc.append(nn.Linear(in_features=in_features,
out_features=l_out_features))
return nn.Sequential(*l_fc)
def _make_layer(self, nb_blocks, nb_filts, first=False):
layers = []
# def __init__(self, nb_filts, first = False):
for i in range(nb_blocks):
first = first if i == 0 else False
layers.append(Residual_block(nb_filts=nb_filts,
first=first))
if i == 0: nb_filts[0] = nb_filts[1]
return nn.Sequential(*layers)
def summary(self, input_size, batch_size=-1, device="cuda", print_fn=None):
if print_fn == None: printfn = print
model = self
def register_hook(module):
def hook(module, input, output):
class_name = str(module.__class__).split(".")[-1].split("'")[0]
module_idx = len(summary)
m_key = "%s-%i" % (class_name, module_idx + 1)
summary[m_key] = OrderedDict()
summary[m_key]["input_shape"] = list(input[0].size())
summary[m_key]["input_shape"][0] = batch_size
if isinstance(output, (list, tuple)):
summary[m_key]["output_shape"] = [
[-1] + list(o.size())[1:] for o in output
]
else:
summary[m_key]["output_shape"] = list(output.size())
if len(summary[m_key]["output_shape"]) != 0:
summary[m_key]["output_shape"][0] = batch_size
params = 0
if hasattr(module, "weight") and hasattr(module.weight, "size"):
params += torch.prod(torch.LongTensor(list(module.weight.size())))
summary[m_key]["trainable"] = module.weight.requires_grad
if hasattr(module, "bias") and hasattr(module.bias, "size"):
params += torch.prod(torch.LongTensor(list(module.bias.size())))
summary[m_key]["nb_params"] = params
if (
not isinstance(module, nn.Sequential)
and not isinstance(module, nn.ModuleList)
and not (module == model)
):
hooks.append(module.register_forward_hook(hook))
device = device.lower()
assert device in [
"cuda",
"cpu",
], "Input device is not valid, please specify 'cuda' or 'cpu'"
if device == "cuda" and torch.cuda.is_available():
dtype = torch.cuda.FloatTensor
else:
dtype = torch.FloatTensor
if isinstance(input_size, tuple):
input_size = [input_size]
x = [torch.rand(2, *in_size).type(dtype) for in_size in input_size]
summary = OrderedDict()
hooks = []
model.apply(register_hook)
model(*x)
for h in hooks:
h.remove()
print_fn("----------------------------------------------------------------")
line_new = "{:>20} {:>25} {:>15}".format("Layer (type)", "Output Shape", "Param #")
print_fn(line_new)
print_fn("================================================================")
total_params = 0
total_output = 0
trainable_params = 0
for layer in summary:
# input_shape, output_shape, trainable, nb_params
line_new = "{:>20} {:>25} {:>15}".format(
layer,
str(summary[layer]["output_shape"]),
"{0:,}".format(summary[layer]["nb_params"]),
)
total_params += summary[layer]["nb_params"]
total_output += np.prod(summary[layer]["output_shape"])
if "trainable" in summary[layer]:
if summary[layer]["trainable"] == True:
trainable_params += summary[layer]["nb_params"]
print_fn(line_new)
if __name__ == "__main__":
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# cqcc = torch.randn((32,1,90,788)).cuda()
# resnet = ResNet(4, 2, resnet_type='18', nclasses=2).cuda()
# _, output = resnet(cqcc)
# print(output.shape)
lfcc = torch.randn((1, 1, 60, 750)).cuda()
lcnn = LCNN(4, 2, nclasses=2).cuda()
feat, output = lcnn(lfcc)
print(output.shape)
# cnn = ConvNet(num_classes = 2, num_nodes = 47232, enc_dim = 256).cuda()
# _, output = cnn(lfcc)
# print(output.shape)