-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·404 lines (355 loc) · 12.1 KB
/
train.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
# pylint: disable=R0915, R0914
import argparse
import json
import os
import yaml
from pathlib import Path
from collections import OrderedDict, defaultdict
from tqdm import tqdm
import numpy as np
import torch
import torch.nn.functional as F
from utils.generic.setup import setup_seed
from utils.train.schedulers import OptimizerWrapper
from utils.audio.data import (
CMDataset,
SamplerBlockShuffleByLen,
customize_collate,
)
from utils.eval.eer_tools import cal_roc_eer
from utils.eval.model_loaders import load_cm
from utils.eval.init import init_system, init_dataloader
def to(vec, device):
if isinstance(vec, list):
return [v.to(device) for v in vec]
else:
return vec.to(device)
def custom_sampler(data_params, lens_file, labels, sampler):
if sampler == "block_shuffle_by_length":
with open(lens_file, "r", encoding="utf8") as f:
lengths = [int(line.strip().split(" ")[1]) for line in f]
data_params["sampler"] = SamplerBlockShuffleByLen(
lengths, data_params["batch_size"]
)
data_params["collate_fn"] = customize_collate
data_params["shuffle"] = False
else:
with open(labels) as f:
lines = set([line.strip().split(" ")[2] for line in f])
data_params["sampler"] = torch.utils.data.sampler.SubsetRandomSampler(
range(len(lines) * 20)
)
return data_params
def get_loaders(spec, config, system, base, extract_device):
data_params = config["training_parameters"]["data_loader"]
if config["training_parameters"]["sampler"] is not None:
sampler = config["training_parameters"]["sampler"]
data_params = custom_sampler(
data_params,
config["training_parameters"].get("lens_file"),
os.path.join(
os.path.join(base, "labels"), config["training_parameters"]["labels"]
),
sampler,
)
data_params["eval"] = False
train_loader, transform, label_fn, final_layer = init_dataloader(
spec,
system,
base,
config["training_parameters"]["labels"].split(".")[0],
data_params,
extract_device,
aug=config["arch"]["type"] == "WAV2VEC",
)
data_params["eval"] = True
data_params["shuffle"] = False
val_loader = init_dataloader(
spec,
system,
base,
config["training_parameters"]["labels"].split(".")[0],
data_params,
extract_device,
)[0]
return val_loader, train_loader, transform, label_fn, final_layer
def resume_state(path, model, optimizer, val_metric, lr, num_batch, state_dict, device):
parent = Path(path).parent.absolute()
results_f = os.path.join(parent, "log.log")
with open(results_f, "r") as f:
lines = [line for line in f]
results = [float(line.strip().split(" ")[7][:-1]) for line in lines if line != "\n"]
best = [float(line.strip().split(" ")[9]) for line in lines if line != "\n"][-1]
idx = np.where(np.array(results) == best)[0][0]
name = os.path.basename(path)
itrs = idx + 1 if name == "model.pth" else int(name.split(".")[0][5:]) + 1
optimizer.load(itrs, results, lr, num_batch, val_metric)
model = load_cm(model, path, state_dict, device).to(device)
model.train()
lines = lines[:itrs]
with open(results_f, "w") as f:
f.writelines(lines)
return model, optimizer, itrs
def make_model(
spec, config, system, train_device, out, data_len, state_dict, resume=None
):
if config["arch"]["type"] == "DartsRaw":
config["arch"]["args"]["is_mask"] = True
model, loss = init_system(spec, system, train_device, load_checkpoint=False)
optimizer = model.optimizer(
config["training_parameters"]["optimizer"]["type"],
**config["training_parameters"]["optimizer"]["params"],
)
optimizer_wrapper = OptimizerWrapper(
optimizer, config["training_parameters"]["scheduler"]
)
if resume:
val_metric = config["training_parameters"]["val_metric"]
lr = config["training_parameters"]["optimizer"]["params"]["lr"]
model, optimizer_wrapper, itr = resume_state(
os.path.join(out, resume),
model,
optimizer_wrapper,
val_metric,
lr,
data_len,
state_dict,
train_device,
)
else:
f = open(os.path.join(out, "log.log"), "w", encoding="utf8")
f.close()
itr = 0
return model, optimizer_wrapper, loss, itr
def unsqueeze_like(tensor: torch.Tensor, like: torch.Tensor):
n_unsqueezes = like.ndim - tensor.ndim
if n_unsqueezes < 0:
raise ValueError(f"tensor.ndim={tensor.ndim} > like.ndim={like.ndim}")
if n_unsqueezes == 0:
return tensor
return tensor[(...,) + (None,) * n_unsqueezes]
def process_batch(test_batch, model, loss, device, **kwargs):
# pylint: disable=W0621
test_sample, test_label = test_batch
test_sample = to(test_sample, device)
test_label = test_label.to(device)
out = model(test_sample, **kwargs)
try:
Loss = loss(out, test_label)
return Loss, out
except:
raise
return None, out
def calc_metric(val_metric, probs, lossDict):
# pylint: disable=W0621
if val_metric == "eer":
res = cal_roc_eer(probs)
elif val_metric == "acc":
out = np.argmax(probs[:, :-1].numpy().reshape((probs.shape[0], 2)), axis=1)
target = probs[:, -1].numpy()
res = np.where(out == target)
res = np.where(out == target)[0].shape[0] / out.shape[0]
else:
res = np.nanmean(lossDict["loss"])
return res
def train_epoch(
system, epoch_num, optimizer, model, loss, train_loader, lr, device, **train_args
):
model.train()
for test_batch in tqdm(train_loader):
Loss, out = process_batch(test_batch, model, loss, device, **train_args)
optimizer.zero_grad()
Loss.backward()
optimizer.step()
optimizer.update(lr, epoch_num, False)
optimizer.update(lr, epoch_num, True)
return optimizer, model
def validate_epoch(
system,
model,
loss,
val_loader,
val_metric,
transform,
final_layer,
label_fn,
eval_mode_for_validation,
batch_size,
device,
):
lossDict = defaultdict(list)
if eval_mode_for_validation:
model.eval()
with torch.no_grad():
probs = torch.empty(0, 2).to(device)
if val_metric == "acc":
probs = torch.empty(0, 3).to(device)
for test_batch in val_loader:
Loss, out = process_batch(test_batch, model, loss, device, eval=True)
test_label = test_batch[1].to(device)
t1 = transform(final_layer(out))
t2 = label_fn(test_label.unsqueeze(-1))
t1 = unsqueeze_like(t1, t2)
if val_metric != "acc":
t1 = unsqueeze_like(t1[:, -1], t2)
row = torch.cat((t1, t2), dim=-1)
probs = torch.cat((probs, row), dim=0)
try:
lossDict["loss"].append(Loss.item())
except:
pass
probs = probs.to("cpu")
res = calc_metric(val_metric, probs, lossDict)
return res
def log_epoch(model, res, epoch_num, val_metric, is_best, best_res, out_f):
if is_best:
model.save_state(os.path.join(out_f, "model.pth"))
Message = (
"\nEpoch: "
+ str(epoch_num)
+ " - Val metric: "
+ val_metric
+ ", value: "
+ str(res)
+ ", best: "
+ str(best_res)
)
with open(os.path.join(out_f, "log.log"), "a", encoding="utf8") as log:
log.write(Message + "\n")
print(Message)
model.save_state(os.path.join(out_f, "model" + str(epoch_num) + ".pth"))
def train(
system,
model,
loss,
optimizer,
train_loader,
val_loader,
num_epochs,
lr,
out_f,
eval_mode_for_validation,
no_best_epoch_num,
val_metric,
drop_path_prob,
transform,
label_fn,
final_layer,
batch_size,
train_args,
device,
start=0,
):
# pylint: disable=R0913,W0621
for epoch_num in tqdm(range(start, num_epochs)):
if drop_path_prob:
model.drop_path_prob = drop_path_prob * epoch_num / num_epochs
optimizer, model = train_epoch(
system,
epoch_num,
optimizer,
model,
loss,
train_loader,
lr,
device,
**train_args,
)
res = validate_epoch(
system,
model,
loss,
val_loader,
val_metric,
transform,
final_layer,
label_fn,
eval_mode_for_validation,
batch_size,
device,
)
is_best = epoch_num == 0 or (
res < best_res if val_metric in ("eer", "loss") else res > best_res
)
if is_best:
best_res, best_epoch, best_epoch_tmp = res, epoch_num, epoch_num
log_epoch(model, res, epoch_num, val_metric, is_best, best_res, out_f)
if epoch_num - best_epoch_tmp > 2:
optimizer.increase_delta()
best_epoch_tmp = epoch_num
if (epoch_num - best_epoch) >= no_best_epoch_num:
print("terminating - early stopping")
break
return model
def run_train(conf_path, base, resume, devices):
with Path(conf_path["config"]).open("rt", encoding="utf8") as handle:
config = json.load(handle, object_hook=OrderedDict)
train_device, extract_device = devices.split(",")
lr = config["training_parameters"]["optimizer"]["params"]["lr"]
num_epochs = config["training_parameters"]["num_epochs"]
eval_mode_for_validation = config["training_parameters"]["eval_mode_for_validation"]
no_best_epoch_num = config["training_parameters"]["no_best_epoch_num"]
val_metric = config["training_parameters"]["val_metric"]
drop_path_prob = config["training_parameters"].get("drop_path_prob", None)
train_args = config["training_parameters"].get("train_args", {})
out = "/".join(config["path"].split("/")[:-1])
batch_size = config["training_parameters"]["data_loader"]["batch_size"]
Path(out).mkdir(parents=True, exist_ok=True)
setup_seed(config["training_parameters"]["seed"])
val_loader, train_loader, transform, label_fn, final_layer = get_loaders(
conf_path,
config,
config["system"],
base,
extract_device,
)
model, optimizer, loss, itr = make_model(
conf_path,
config,
config["system"],
train_device,
out,
len(train_loader),
config["state_dict"],
resume=resume,
)
model = train(
config["system"],
model,
loss,
optimizer,
train_loader,
val_loader,
num_epochs,
lr,
out,
eval_mode_for_validation,
no_best_epoch_num,
val_metric,
drop_path_prob,
transform,
label_fn,
final_layer,
batch_size,
train_args,
train_device,
itr,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config")
parser.add_argument("--system")
parser.add_argument("--subset", default="dev")
parser.add_argument("--task", default="cm")
parser.add_argument("--base", default="datasets/asvspoofWavs")
parser.add_argument("--devices", default="cuda:0,cuda:1")
parser.add_argument("--resume")
args = parser.parse_args()
with open(args.config) as f:
config = yaml.load(f, Loader=yaml.Loader)
run_train(
config[args.task][args.subset][args.system],
args.base,
args.resume,
args.devices,
)