-
Notifications
You must be signed in to change notification settings - Fork 34
/
model.py
executable file
·503 lines (415 loc) · 16.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
import torch
import torch.nn as nn
import math
import torch.utils.model_zoo as model_zoo
import numpy as np
from scipy import linalg as la
import torch.nn.functional as F
from densenet import densenet62
# Author: Nanxin Chen, Cheng-I Lai
def conv3x3(in_planes, out_planes, stride=1):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)
class SELayer(nn.Module):
def __init__(self, channel, reduction=16):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1) # F_squeeze
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)
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class SEBasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None, reduction=16):
super(SEBasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes, 1)
self.bn2 = nn.BatchNorm2d(planes)
self.se = SELayer(planes, reduction)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.se(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class Bottleneck(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ThinResNet(nn.Module):
"""ResNet with smaller channel dimensions
"""
def __init__(self, block, layers):
self.inplanes = 8
super(ThinResNet, self).__init__()
self.conv1 = nn.Conv2d(1, 8, kernel_size=7, stride=1, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(8)
self.relu = nn.ReLU(inplace=True)
#self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 8, layers[0])
self.layer2 = self._make_layer(block, 16, layers[1], stride=2)
self.layer3 = self._make_layer(block, 32, layers[2], stride=2)
self.layer4 = self._make_layer(block, 64, layers[3], stride=2)
self.avgpool = nn.AvgPool2d((1, 3))
#self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(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 = x.view(x.size(0), 1, x.size(1), x.size(2))
#print(x.shape)
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
#print(x.shape)
#x = self.maxpool(x)
x = self.layer1(x)
#print(x.shape)
x = self.layer2(x)
#print(x.shape)
x = self.layer3(x)
#print(x.shape)
x = self.layer4(x)
#print(x.shape)
x = self.avgpool(x)
#print(x.shape)
x = x.view(x.size(0), x.size(1), x.size(2)).permute(0, 2, 1)
return x
class ResNet(nn.Module):
def __init__(self, block, layers):
self.inplanes = 16
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=7, stride=1, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.relu = nn.ReLU(inplace=True)
#self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 16, layers[0])
self.layer2 = self._make_layer(block, 32, layers[1], stride=2)
self.layer3 = self._make_layer(block, 64, layers[2], stride=2)
self.layer4 = self._make_layer(block, 128, layers[3], stride=2)
self.avgpool = nn.AvgPool2d((1, 3))
#self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(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 = x.view(x.size(0), 1, x.size(1), x.size(2))
#print(x.shape) # 128, 1, 800, 30
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
#print(x.shape)
#x = self.maxpool(x)
x = self.layer1(x)
#print(x.shape)
x = self.layer2(x)
#print(x.shape)
x = self.layer3(x)
#print(x.shape)
x = self.layer4(x)
#print(x.shape) # 128, 128, 100, 4
x = self.avgpool(x)
#print(x.shape) # 128, 128, 100, 1
x = x.view(x.size(0), x.size(1), x.size(2)).permute(0, 2, 1)
#print(x.shape) # 128, 100, 128
return x
def resnet18(**kwargs):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
return model
def resnet34(**kwargs):
"""Constructs a ResNet-34 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
return model
def thin_resnet34(**kwargs):
"""Constructs a ResNet-34 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ThinResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
return model
def se_resnet34(**kwargs):
model = ResNet(SEBasicBlock, [3, 4, 6, 3], **kwargs)
return model
def resnet50(**kwargs):
"""Constructs a ResNet-50 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
return model
def resnet101(**kwargs):
"""Constructs a ResNet-101 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
return model
def resnet152(**kwargs):
"""Constructs a ResNet-152 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
return model
class LDE(nn.Module):
def __init__(self, D, input_dim, with_bias=False, distance_type='norm', network_type='att', pooling='mean'):
"""LDE layer
"""
super(LDE, self).__init__()
self.dic = nn.Parameter(torch.randn(D, input_dim)) # input_dim by D (dictionary components)
nn.init.uniform_(self.dic.data, -1, 1)
self.wei = nn.Parameter(torch.ones(D)) # non-negative assigning weight in Eq(4) in LDE paper
if with_bias: # Eq(4) in LDE paper
self.bias = nn.Parameter(torch.zeros(D))
else:
self.bias = 0
assert distance_type == 'norm' or distance_type == 'sqr'
if distance_type == 'norm':
self.dis = lambda x: torch.norm(x, p=2, dim=-1)
else:
self.dis = lambda x: torch.sum(x**2, dim=-1)
assert network_type == 'att' or network_type == 'lde'
if network_type == 'att':
self.norm = lambda x: F.softmax(-self.dis(x) * self.wei + self.bias, dim = -2)
else:
self.norm = lambda x: F.softmax(-self.dis(x) * (self.wei ** 2) + self.bias, dim = -1)
assert pooling == 'mean' or pooling == 'mean+std'
self.pool = pooling
def forward(self, x):
#print(x.size()) # (B, T, F)
#print(self.dic.size()) # (D, F)
r = x.view(x.size(0), x.size(1), 1, x.size(2)) - self.dic # residaul vector
#print(r.size()) # (B, T, D, F)
w = self.norm(r).view(r.size(0), r.size(1), r.size(2), 1) # numerator without r in Eq(5) in LDE paper
#print(self.norm(r).size()) # (B, T, D)
#print(w.size()) # (B, T, D, 1)
w = w / (torch.sum(w, dim=1, keepdim=True) + 1e-9) #batch_size, timesteps, component # denominator of Eq(5) in LDE paper
if self.pool == 'mean':
x = torch.sum(w * r, dim=1) # Eq(5) in LDE paper
else:
x1 = torch.sum(w * r, dim=1) # Eq(5) in LDE paper
x2 = torch.sqrt(torch.sum(w * r ** 2, dim=1)+1e-8) # std vector
x = torch.cat([x1, x2], dim=-1)
return x.view(x.size(0), -1)
class NeuralSpeakerModel(nn.Module):
"""Neural Speaker Model
@model: resnet model
@input_dim: feature dim
@output_dim: number of speakers
@D: LDE dictionary components
@hidden_dim: speaker embedding dim
@distance_tpye: 1) norm (Frobenius Norm) or 2) sqr (square norm) --> distance metric in Eq(4) in LDE paper, for calculating the weight over the residual vectors
@network_type: 1) att (multi-head attention, or attention over T) or 2) lde (LDE, or attention over dictionary components).
@pooling: aggregation step over the residual vectors 1) mean only or 2) mean and std
@m: m for A-Softmax
Note: use the pairing ('norm', 'att') and ('sqr', 'lde')
"""
def __init__(self, model, input_dim, output_dim, D, hidden_dim=128, distance_type='norm', network_type='att', pooling='mean', asoftmax=False, m=2):
super(NeuralSpeakerModel, self).__init__()
if model == 'resnet34':
self.res = resnet34()
_feature_dim = 128
elif model == 'thin-resnet34':
self.res = thin_resnet34()
_feature_dim = 64
elif model == 'se-resnet34':
self.res = se_resnet34()
_feature_dim = 128
elif model == 'densenet62':
self.res = densenet62()
_feature_dim = 128
else:
raise NotImplementedError
self.pool = LDE(D, _feature_dim, distance_type=distance_type, network_type=network_type, pooling=pooling, with_bias=False)
if pooling=='mean':
self.fc11 = nn.Linear(_feature_dim*D, hidden_dim)
if pooling=='mean+std':
self.fc11 = nn.Linear(_feature_dim*2*D, hidden_dim)
self.bn1 = nn.BatchNorm1d(hidden_dim)
self.fc2 = nn.Linear(hidden_dim, output_dim)
self.asoftmax = asoftmax
self.m = m
self.mlambda = [
lambda x: x**0,
lambda x: x**1,
lambda x: 2*x**2-1,
lambda x: 4*x**3-3*x,
lambda x: 8*x**4-8*x**2+1,
lambda x: 16*x**5-20*x**3+5*x
]
def forward(self, x):
x = self.res(x)
#print(x.shape)
x = self.pool(x)
#print(x.shape)
x = self.fc11(x)
#print(x.shape)
x = self.bn1(x)
if self.asoftmax == 'True':
# source: https://github.com/clcarwin/sphereface_pytorch
# AngleLinear class
w = torch.transpose(self.fc2.weight, 0, 1) # size=(F,Classnum) F=in_features Classnum=out_features
ww = w.renorm(2,1,1e-5).mul(1e5)
xlen = x.pow(2).sum(1).pow(0.5) # size=B
wlen = ww.pow(2).sum(0).pow(0.5) # size=Classnum
cos_theta = x.mm(ww) # size=(B,Classnum)
cos_theta = cos_theta / xlen.view(-1,1) / wlen.view(1,-1)
cos_theta = cos_theta.clamp(-1,1)
cos_m_theta = self.mlambda[self.m](cos_theta)
theta = torch.cuda.FloatTensor(cos_theta.data.acos())
k = (self.m*theta/3.14159265).floor()
n_one = k*0.0 - 1
phi_theta = (n_one**k) * cos_m_theta - 2*k
cos_theta = cos_theta * xlen.view(-1,1)
phi_theta = phi_theta * xlen.view(-1,1)
#print(cos_theta.shape, phi_theta.shape)
return (cos_theta, phi_theta)
else:
x = F.relu(x)
x = self.fc2(x)
return F.log_softmax(x, dim=-1)
def predict(self, x):
x = self.res(x)
x = self.pool(x)
if type(x) is tuple:
x = x[0]
x = self.fc11(x)
return x
class AngleLoss(nn.Module):
# source: https://github.com/clcarwin/sphereface_pytorch
# AngleLoss class
def __init__(self, gamma=0):
super(AngleLoss, self).__init__()
self.gamma = gamma
self.it = 0
self.LambdaMin = 5.0
self.LambdaMax = 1500.0
self.lamb = 1500.0
def forward(self, input, target):
self.it += 1
cos_theta,phi_theta = input
target = target.view(-1,1) #size=(B,1)
index = cos_theta.data * 0.0 #size=(B,Classnum)
index.scatter_(1,target.data.view(-1,1),1)
index = index.byte().detach()
#index = Variable(index)
self.lamb = max(self.LambdaMin,self.LambdaMax/(1+0.01*self.it ))
output = cos_theta * 1.0 #size=(B,Classnum)
output[index] -= cos_theta[index]*(1.0+0)/(1+self.lamb)
output[index] += phi_theta[index]*(1.0+0)/(1+self.lamb)
logpt = F.log_softmax(output)
logpt = logpt.gather(1,target)
logpt = logpt.view(-1)
pt = logpt.exp().detach()
loss = -1 * (1-pt)**self.gamma * logpt
loss = loss.mean()
return loss