-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeakerNet.py
279 lines (197 loc) · 9.3 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy, sys, random
import time, itertools, importlib
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, label)
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, logger, optimizer, scheduler, gpu, mixedprec, **kwargs):
self.__model__ = speaker_model
self.logger = logger
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 step, (data, data_label) in enumerate(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().item()
top1 += prec1.detach().cpu().item()
counter += 1
index += stepsize
telapsed = time.time() - tstart
tstart = time.time()
if verbose:
clr = [x['lr'] for x in self.__optimizer__.param_groups]
sys.stdout.write("\rProcessing {:d} of {:d}:".format(index, loader.__len__() * loader.batch_size))
sys.stdout.write("Loss {:f} TEER/TAcc {:2.3f}% - {:.2f} Hz, LR: {:.6f}".format(loss / counter, top1 / counter, stepsize / telapsed, max(clr)))
sys.stdout.flush()
if self.lr_step == "iteration":
self.__scheduler__.step()
if self.lr_step == "epoch":
self.__scheduler__.step()
return (loss / counter, top1 / counter)
## ===== ===== ===== ===== ===== ===== ===== =====
## Evaluate from list
## ===== ===== ===== ===== ===== ===== ===== =====
def evaluateFromList(self, test_list, test_path, nDataLoaderThread, distributed, print_interval=100, num_eval=10, shortmode=None, ref_feats=None, **kwargs):
if distributed:
rank = torch.distributed.get_rank()
else:
rank = 0
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 = list(itertools.chain(*[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, shortmode=shortmode, num_eval=num_eval, **kwargs)
if distributed:
sampler = torch.utils.data.distributed.DistributedSampler(test_dataset, shuffle=False)
else:
sampler = None
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=nDataLoaderThread, drop_last=False, sampler=sampler)
## Extract features for every image
for idx, data in enumerate(test_loader):
inp1 = data[0][0].cuda()
with torch.no_grad():
ref_feat = self.__model__(inp1).detach().cpu()
feats[data[1][0]] = ref_feat
telapsed = time.time() - tstart
if idx % print_interval == 0 and rank == 0:
sys.stdout.write(
"\rReading {:d} of {:d}: {:.2f} Hz, embedding size {:d}".format(idx, test_loader.__len__(), idx / telapsed, ref_feat.size()[1])
)
all_scores = []
all_labels = []
all_trials = []
if distributed:
## Gather features from all GPUs
feats_all = [None for _ in range(0, torch.distributed.get_world_size())]
torch.distributed.all_gather_object(feats_all, feats)
if rank == 0:
tstart = time.time()
print("")
## Combine gathered features
if distributed:
feats = feats_all[0]
for feats_batch in feats_all[1:]:
feats.update(feats_batch)
## 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 = ref_feats[data[1]].cuda() if ref_feats else 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)
if kwargs["eval_frames"] == 0 or shortmode:
dist = torch.cdist(ref_feat.reshape(1, -1), com_feat.reshape(1, -1)).detach().cpu().numpy()
else:
dist = torch.cdist(ref_feat.reshape(num_eval, -1), com_feat.reshape(num_eval, -1)).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".format(idx, len(lines), idx / telapsed))
sys.stdout.flush()
return (all_scores, all_labels, all_trials, feats)
## ===== ===== ===== ===== ===== ===== ===== =====
## 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)
if len(loaded_state.keys()) == 1 and "model" in loaded_state:
loaded_state = loaded_state["model"]
newdict = {}
delete_list = []
for name, param in loaded_state.items():
new_name = "__S__."+name
newdict[new_name] = param
delete_list.append(name)
loaded_state.update(newdict)
for name in delete_list:
del loaded_state[name]
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("{} is not in the model.".format(origname))
continue
if self_state[name].size() != loaded_state[origname].size():
print("Wrong parameter length: {}, model: {}, loaded: {}".format(origname, self_state[name].size(), loaded_state[origname].size()))
continue
self_state[name].copy_(param)