forked from clovaai/voxceleb_trainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeakerNet.py
249 lines (165 loc) · 7.64 KB
/
SpeakerNet.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
#!/usr/bin/python
#-*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy, math, pdb, sys, random
import time, os, itertools, shutil, importlib
from tuneThreshold import tuneThresholdfromScore
from DatasetLoader import test_dataset_loader
from torch.cuda.amp import autocast, GradScaler
class WrappedModel(nn.Module):
## The purpose of this wrapper is to make the model structure consistent between single and multi-GPU
def __init__(self, model):
super(WrappedModel, self).__init__()
self.module = model
def forward(self, x, label=None):
return self.module(x, label)
class SpeakerNet(nn.Module):
def __init__(self, model, optimizer, trainfunc, nPerSpeaker, **kwargs):
super(SpeakerNet, self).__init__();
SpeakerNetModel = importlib.import_module('models.'+model).__getattribute__('MainModel')
self.__S__ = SpeakerNetModel(**kwargs);
LossFunction = importlib.import_module('loss.'+trainfunc).__getattribute__('LossFunction')
self.__L__ = LossFunction(**kwargs);
self.nPerSpeaker = nPerSpeaker
def forward(self, data, label=None):
data = data.reshape(-1,data.size()[-1]).cuda()
outp = self.__S__.forward(data)
if label == None:
return outp
else:
outp = outp.reshape(self.nPerSpeaker,-1,outp.size()[-1]).transpose(1,0).squeeze(1)
nloss, prec1 = self.__L__.forward(outp,label)
return nloss, prec1
class ModelTrainer(object):
def __init__(self, speaker_model, optimizer, scheduler, gpu, mixedprec, **kwargs):
self.__model__ = speaker_model
Optimizer = importlib.import_module('optimizer.'+optimizer).__getattribute__('Optimizer')
self.__optimizer__ = Optimizer(self.__model__.parameters(), **kwargs)
Scheduler = importlib.import_module('scheduler.'+scheduler).__getattribute__('Scheduler')
self.__scheduler__, self.lr_step = Scheduler(self.__optimizer__, **kwargs)
self.scaler = GradScaler()
self.gpu = gpu
self.mixedprec = mixedprec
assert self.lr_step in ['epoch', 'iteration']
# ## ===== ===== ===== ===== ===== ===== ===== =====
# ## Train network
# ## ===== ===== ===== ===== ===== ===== ===== =====
def train_network(self, loader, verbose):
self.__model__.train();
stepsize = loader.batch_size;
counter = 0;
index = 0;
loss = 0;
top1 = 0 # EER or accuracy
tstart = time.time()
for data, data_label in loader:
data = data.transpose(1,0)
self.__model__.zero_grad();
label = torch.LongTensor(data_label).cuda()
if self.mixedprec:
with autocast():
nloss, prec1 = self.__model__(data, label)
self.scaler.scale(nloss).backward();
self.scaler.step(self.__optimizer__);
self.scaler.update();
else:
nloss, prec1 = self.__model__(data, label)
nloss.backward();
self.__optimizer__.step();
loss += nloss.detach().cpu();
top1 += prec1.detach().cpu()
counter += 1;
index += stepsize;
telapsed = time.time() - tstart
tstart = time.time()
if verbose:
sys.stdout.write("\rProcessing (%d) "%(index));
sys.stdout.write("Loss %f TEER/TAcc %2.3f%% - %.2f Hz "%(loss/counter, top1/counter, stepsize/telapsed));
sys.stdout.flush();
if self.lr_step == 'iteration': self.__scheduler__.step()
if self.lr_step == 'epoch': self.__scheduler__.step()
sys.stdout.write("\n");
return (loss/counter, top1/counter);
## ===== ===== ===== ===== ===== ===== ===== =====
## Evaluate from list
## ===== ===== ===== ===== ===== ===== ===== =====
def evaluateFromList(self, test_list, test_path, nDataLoaderThread, print_interval=100, num_eval=10, **kwargs):
self.__model__.eval();
lines = []
files = []
feats = {}
tstart = time.time()
## Read all lines
with open(test_list) as f:
lines = f.readlines()
## Get a list of unique file names
files = sum([x.strip().split()[-2:] for x in lines],[])
setfiles = list(set(files))
setfiles.sort()
## Define test data loader
test_dataset = test_dataset_loader(setfiles, test_path, num_eval=num_eval, **kwargs)
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=1,
shuffle=False,
num_workers=nDataLoaderThread,
drop_last=False,
)
## Extract features for every image
for idx, data in enumerate(test_loader):
inp1 = data[0][0].cuda()
ref_feat = self.__model__(inp1).detach().cpu()
feats[data[1][0]] = ref_feat
telapsed = time.time() - tstart
if idx % print_interval == 0:
sys.stdout.write("\rReading %d of %d: %.2f Hz, embedding size %d"%(idx,len(setfiles),idx/telapsed,ref_feat.size()[1]));
print('')
all_scores = [];
all_labels = [];
all_trials = [];
tstart = time.time()
## Read files and compute all scores
for idx, line in enumerate(lines):
data = line.split();
## Append random label if missing
if len(data) == 2: data = [random.randint(0,1)] + data
ref_feat = feats[data[1]].cuda()
com_feat = feats[data[2]].cuda()
if self.__model__.module.__L__.test_normalize:
ref_feat = F.normalize(ref_feat, p=2, dim=1)
com_feat = F.normalize(com_feat, p=2, dim=1)
dist = F.pairwise_distance(ref_feat.unsqueeze(-1), com_feat.unsqueeze(-1).transpose(0,2)).detach().cpu().numpy();
score = -1 * numpy.mean(dist);
all_scores.append(score);
all_labels.append(int(data[0]));
all_trials.append(data[1]+" "+data[2])
if idx % print_interval == 0:
telapsed = time.time() - tstart
sys.stdout.write("\rComputing %d of %d: %.2f Hz"%(idx,len(lines),idx/telapsed));
sys.stdout.flush();
print('')
return (all_scores, all_labels, all_trials);
## ===== ===== ===== ===== ===== ===== ===== =====
## Save parameters
## ===== ===== ===== ===== ===== ===== ===== =====
def saveParameters(self, path):
torch.save(self.__model__.module.state_dict(), path);
## ===== ===== ===== ===== ===== ===== ===== =====
## Load parameters
## ===== ===== ===== ===== ===== ===== ===== =====
def loadParameters(self, path):
self_state = self.__model__.module.state_dict();
loaded_state = torch.load(path, map_location="cuda:%d"%self.gpu);
for name, param in loaded_state.items():
origname = name;
if name not in self_state:
name = name.replace("module.", "");
if name not in self_state:
print("%s is not in the model."%origname);
continue;
if self_state[name].size() != loaded_state[origname].size():
print("Wrong parameter length: %s, model: %s, loaded: %s"%(origname, self_state[name].size(), loaded_state[origname].size()));
continue;
self_state[name].copy_(param);