-
Notifications
You must be signed in to change notification settings - Fork 95
/
model.py
366 lines (314 loc) · 16.1 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
from __future__ import print_function
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import math
## PyTorch implementation of CDCK2, CDCK5, CDCK6, speaker classifier models
# CDCK2: base model from the paper 'Representation Learning with Contrastive Predictive Coding'
# CDCK5: CDCK2 with a different decoder
# CDCK6: CDCK2 with a shared encoder and double decoders
# SpkClassifier: a simple NN for speaker classification
class CDCK6(nn.Module):
''' CDCK2 with double decoder and a shared encoder '''
def __init__(self, timestep, batch_size, seq_len):
super(CDCK6, self).__init__()
self.batch_size = batch_size
self.seq_len = seq_len
self.timestep = timestep
self.encoder = nn.Sequential( # downsampling factor = 160
nn.Conv1d(1, 512, kernel_size=10, stride=5, padding=3, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=8, stride=4, padding=2, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True)
)
self.gru1 = nn.GRU(512, 128, num_layers=1, bidirectional=False, batch_first=True)
self.Wk1 = nn.ModuleList([nn.Linear(128, 512) for i in range(timestep)])
self.gru2 = nn.GRU(512, 128, num_layers=1, bidirectional=False, batch_first=True)
self.Wk2 = nn.ModuleList([nn.Linear(128, 512) for i in range(timestep)])
self.softmax = nn.Softmax()
self.lsoftmax = nn.LogSoftmax()
def _weights_init(m):
if isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if isinstance(m, nn.Conv1d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm1d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
# initialize gru1 and gru2
for layer_p in self.gru1._all_weights:
for p in layer_p:
if 'weight' in p:
nn.init.kaiming_normal_(self.gru1.__getattr__(p), mode='fan_out', nonlinearity='relu')
for layer_p in self.gru2._all_weights:
for p in layer_p:
if 'weight' in p:
nn.init.kaiming_normal_(self.gru2.__getattr__(p), mode='fan_out', nonlinearity='relu')
self.apply(_weights_init)
def init_hidden1(self, batch_size): # initialize gru1
#return torch.zeros(1, batch_size, 128).cuda()
return torch.zeros(1, batch_size, 128)
def init_hidden2(self, batch_size): # initialize gru2
#return torch.zeros(1, batch_size, 128).cuda()
return torch.zeros(1, batch_size, 128)
def forward(self, x, x_reverse, hidden1, hidden2):
batch = x.size()[0]
nce = 0 # average over timestep and batch and gpus
t_samples = torch.randint(self.seq_len/160-self.timestep, size=(1,)).long() # randomly pick time stamps. ONLY DO THIS ONCE FOR BOTH GRU.
# first gru
# input sequence is N*C*L, e.g. 8*1*20480
z = self.encoder(x)
# encoded sequence is N*C*L, e.g. 8*512*128
# reshape to N*L*C for GRU, e.g. 8*128*512
z = z.transpose(1,2)
encode_samples = torch.empty((self.timestep,batch,512)).float() # e.g. size 12*8*512
for i in np.arange(1, self.timestep+1):
encode_samples[i-1] = z[:,t_samples+i,:].view(batch,512) # z_tk e.g. size 8*512
forward_seq = z[:,:t_samples+1,:] # e.g. size 8*100*512
output1, hidden1 = self.gru1(forward_seq, hidden1) # output size e.g. 8*100*256
c_t = output1[:,t_samples,:].view(batch, 128) # c_t e.g. size 8*256
pred = torch.empty((self.timestep,batch,512)).float() # e.g. size 12*8*512
for i in np.arange(0, self.timestep):
linear = self.Wk1[i]
pred[i] = linear(c_t) # Wk*c_t e.g. size 8*512
for i in np.arange(0, self.timestep):
total = torch.mm(encode_samples[i], torch.transpose(pred[i],0,1)) # e.g. size 8*8
correct1 = torch.sum(torch.eq(torch.argmax(self.softmax(total), dim=0), torch.arange(0, batch))) # correct is a tensor
nce += torch.sum(torch.diag(self.lsoftmax(total))) # nce is a tensor
# second gru
# input sequence is N*C*L, e.g. 8*1*20480
z = self.encoder(x_reverse)
# encoded sequence is N*C*L, e.g. 8*512*128
# reshape to N*L*C for GRU, e.g. 8*128*512
z = z.transpose(1,2)
encode_samples = torch.empty((self.timestep,batch,512)).float() # e.g. size 12*8*512
for i in np.arange(1, self.timestep+1):
encode_samples[i-1] = z[:,t_samples+i,:].view(batch,512) # z_tk e.g. size 8*512
forward_seq = z[:,:t_samples+1,:] # e.g. size 8*100*512
output2, hidden2 = self.gru2(forward_seq, hidden2) # output size e.g. 8*100*256
c_t = output2[:,t_samples,:].view(batch, 128) # c_t e.g. size 8*256
pred = torch.empty((self.timestep,batch,512)).float() # e.g. size 12*8*512
for i in np.arange(0, self.timestep):
linear = self.Wk2[i]
pred[i] = linear(c_t) # Wk*c_t e.g. size 8*512
for i in np.arange(0, self.timestep):
total = torch.mm(encode_samples[i], torch.transpose(pred[i],0,1)) # e.g. size 8*8
correct2 = torch.sum(torch.eq(torch.argmax(self.softmax(total), dim=0), torch.arange(0, batch))) # correct is a tensor
nce += torch.sum(torch.diag(self.lsoftmax(total))) # nce is a tensor
nce /= -1.*batch*self.timestep
nce /= 2. # over two grus
accuracy = 1.*(correct1.item()+correct2.item())/(batch*2) # accuracy over batch and two grus
#print(torch.cat((output1, output2), dim=2).shape)
return accuracy, nce, hidden1, hidden2
def predict(self, x, x_reverse, hidden1, hidden2):
batch = x.size()[0]
# first gru
# input sequence is N*C*L, e.g. 8*1*20480
z1 = self.encoder(x)
# encoded sequence is N*C*L, e.g. 8*512*128
# reshape to N*L*C for GRU, e.g. 8*128*512
z1 = z1.transpose(1,2)
output1, hidden1 = self.gru1(z1, hidden1) # output size e.g. 8*128*256
# second gru
z2 = self.encoder(x_reverse)
z2 = z2.transpose(1,2)
output2, hidden2 = self.gru2(z2, hidden2)
return torch.cat((output1, output2), dim=2) # size (64, seq_len, 256)
#return torch.cat((z1, z2), dim=2) # size (64, seq_len, 512*2)
class CDCK5(nn.Module):
''' CDCK2 with a different decoder '''
def __init__(self, timestep, batch_size, seq_len):
super(CDCK5, self).__init__()
self.batch_size = batch_size
self.seq_len = seq_len
self.timestep = timestep
self.encoder = nn.Sequential( # downsampling factor = 160
nn.Conv1d(1, 512, kernel_size=10, stride=5, padding=3, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=8, stride=4, padding=2, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True)
)
self.gru = nn.GRU(512, 40, num_layers=2, bidirectional=False, batch_first=True)
self.Wk = nn.ModuleList([nn.Linear(40, 512) for i in range(timestep)])
self.softmax = nn.Softmax()
self.lsoftmax = nn.LogSoftmax()
def _weights_init(m):
if isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if isinstance(m, nn.Conv1d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm1d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
# initialize gru
for layer_p in self.gru._all_weights:
for p in layer_p:
if 'weight' in p:
nn.init.kaiming_normal_(self.gru.__getattr__(p), mode='fan_out', nonlinearity='relu')
self.apply(_weights_init)
def init_hidden(self, batch_size):
#return torch.zeros(2*1, batch_size, 40).cuda()
return torch.zeros(2*1, batch_size, 40)
def forward(self, x, hidden):
batch = x.size()[0]
t_samples = torch.randint(self.seq_len/160-self.timestep, size=(1,)).long() # randomly pick time stamps
# input sequence is N*C*L, e.g. 8*1*20480
z = self.encoder(x)
# encoded sequence is N*C*L, e.g. 8*512*128
# reshape to N*L*C for GRU, e.g. 8*128*512
z = z.transpose(1,2)
nce = 0 # average over timestep and batch
encode_samples = torch.empty((self.timestep,batch,512)).float() # e.g. size 12*8*512
for i in np.arange(1, self.timestep+1):
encode_samples[i-1] = z[:,t_samples+i,:].view(batch,512) # z_tk e.g. size 8*512
forward_seq = z[:,:t_samples+1,:] # e.g. size 8*100*512
output, hidden = self.gru(forward_seq, hidden) # output size e.g. 8*100*40
c_t = output[:,t_samples,:].view(batch, 40) # c_t e.g. size 8*40
pred = torch.empty((self.timestep,batch,512)).float() # e.g. size 12*8*512
for i in np.arange(0, self.timestep):
decoder = self.Wk[i]
pred[i] = decoder(c_t) # Wk*c_t e.g. size 8*512
for i in np.arange(0, self.timestep):
total = torch.mm(encode_samples[i], torch.transpose(pred[i],0,1)) # e.g. size 8*8
correct = torch.sum(torch.eq(torch.argmax(self.softmax(total), dim=0), torch.arange(0, batch))) # correct is a tensor
nce += torch.sum(torch.diag(self.lsoftmax(total))) # nce is a tensor
nce /= -1.*batch*self.timestep
accuracy = 1.*correct.item()/batch
return accuracy, nce, hidden
def predict(self, x, hidden):
batch = x.size()[0]
# input sequence is N*C*L, e.g. 8*1*20480
z = self.encoder(x)
# encoded sequence is N*C*L, e.g. 8*512*128
# reshape to N*L*C for GRU, e.g. 8*128*512
z = z.transpose(1,2)
output, hidden = self.gru(z, hidden) # output size e.g. 8*128*40
return output, hidden # return every frame
#return output[:,-1,:], hidden # only return the last frame per utt
class CDCK2(nn.Module):
def __init__(self, timestep, batch_size, seq_len):
super(CDCK2, self).__init__()
self.batch_size = batch_size
self.seq_len = seq_len
self.timestep = timestep
self.encoder = nn.Sequential( # downsampling factor = 160
nn.Conv1d(1, 512, kernel_size=10, stride=5, padding=3, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=8, stride=4, padding=2, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True)
)
self.gru = nn.GRU(512, 256, num_layers=1, bidirectional=False, batch_first=True)
self.Wk = nn.ModuleList([nn.Linear(256, 512) for i in range(timestep)])
self.softmax = nn.Softmax()
self.lsoftmax = nn.LogSoftmax()
def _weights_init(m):
if isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if isinstance(m, nn.Conv1d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm1d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
# initialize gru
for layer_p in self.gru._all_weights:
for p in layer_p:
if 'weight' in p:
nn.init.kaiming_normal_(self.gru.__getattr__(p), mode='fan_out', nonlinearity='relu')
self.apply(_weights_init)
def init_hidden(self, batch_size, use_gpu=True):
if use_gpu: return torch.zeros(1, batch_size, 256).cuda()
else: return torch.zeros(1, batch_size, 256)
def forward(self, x, hidden):
batch = x.size()[0]
t_samples = torch.randint(self.seq_len/160-self.timestep, size=(1,)).long() # randomly pick time stamps
# input sequence is N*C*L, e.g. 8*1*20480
z = self.encoder(x)
# encoded sequence is N*C*L, e.g. 8*512*128
# reshape to N*L*C for GRU, e.g. 8*128*512
z = z.transpose(1,2)
nce = 0 # average over timestep and batch
encode_samples = torch.empty((self.timestep,batch,512)).float() # e.g. size 12*8*512
for i in np.arange(1, self.timestep+1):
encode_samples[i-1] = z[:,t_samples+i,:].view(batch,512) # z_tk e.g. size 8*512
forward_seq = z[:,:t_samples+1,:] # e.g. size 8*100*512
output, hidden = self.gru(forward_seq, hidden) # output size e.g. 8*100*256
c_t = output[:,t_samples,:].view(batch, 256) # c_t e.g. size 8*256
pred = torch.empty((self.timestep,batch,512)).float() # e.g. size 12*8*512
for i in np.arange(0, self.timestep):
linear = self.Wk[i]
pred[i] = linear(c_t) # Wk*c_t e.g. size 8*512
for i in np.arange(0, self.timestep):
total = torch.mm(encode_samples[i], torch.transpose(pred[i],0,1)) # e.g. size 8*8
correct = torch.sum(torch.eq(torch.argmax(self.softmax(total), dim=0), torch.arange(0, batch))) # correct is a tensor
nce += torch.sum(torch.diag(self.lsoftmax(total))) # nce is a tensor
nce /= -1.*batch*self.timestep
accuracy = 1.*correct.item()/batch
return accuracy, nce, hidden
def predict(self, x, hidden):
batch = x.size()[0]
# input sequence is N*C*L, e.g. 8*1*20480
z = self.encoder(x)
# encoded sequence is N*C*L, e.g. 8*512*128
# reshape to N*L*C for GRU, e.g. 8*128*512
z = z.transpose(1,2)
output, hidden = self.gru(z, hidden) # output size e.g. 8*128*256
return output, hidden # return every frame
#return output[:,-1,:], hidden # only return the last frame per utt
class SpkClassifier(nn.Module):
''' linear classifier '''
def __init__(self, spk_num):
super(SpkClassifier, self).__init__()
self.classifier = nn.Sequential(
nn.Linear(256, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512, spk_num)
#nn.Linear(256, spk_num)
)
def _weights_init(m):
if isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if isinstance(m, nn.Conv1d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm1d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
self.apply(_weights_init)
def forward(self, x):
x = self.classifier(x)
return F.log_softmax(x, dim=-1)