forked from clovaai/aasist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
357 lines (303 loc) · 12.1 KB
/
main.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
"""
Main script that trains, validates, and evaluates
various models including AASIST.
AASIST
Copyright (c) 2021-present NAVER Corp.
MIT license
"""
import argparse
import json
import os
import sys
import warnings
from importlib import import_module
from pathlib import Path
from shutil import copy
from typing import Dict, List, Union
import random
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torchcontrib.optim import SWA
from sklearn.preprocessing import MinMaxScaler
from data_utils import (Dataset_ASVspoof2019_train,
Dataset_ASVspoof2019_devNeval, genSpoof_list)
from evaluation import calculate_eer_acc
from utils import create_optimizer, seed_worker, set_seed, str_to_bool
warnings.filterwarnings("ignore", category=FutureWarning)
def main(args: argparse.Namespace) -> None:
"""
Main function.
Trains, validates, and evaluates the ASVspoof detection model.
"""
# load experiment configurations
with open(args.config, "r") as f_json:
config = json.loads(f_json.read())
model_config = config["model_config"]
optim_config = config["optim_config"]
optim_config["epochs"] = config["num_epochs"]
track = config["track"]
assert track in ["LA", "PA", "DF"], "Invalid track given"
if "eval_all_best" not in config:
config["eval_all_best"] = "True"
if "freq_aug" not in config:
config["freq_aug"] = "False"
# make experiment reproducible
set_seed(args.seed, config)
# define database related paths
output_dir = Path(args.output_dir)
prefix_2019 = "ASVspoof2019.{}".format(track)
database_path = Path(config["database_path"])
dev_trial_path = (database_path /
"ASVspoof2019_{}_cm_protocols/{}.cm.dev.trl.txt".format(
track, prefix_2019))
eval_trial_path = (
database_path /
"ASVspoof2019_{}_cm_protocols/{}.cm.eval.trl.txt".format(
track, prefix_2019))
# define model related paths
model_tag = "{}_{}_ep{}_bs{}".format(
track,
os.path.splitext(os.path.basename(args.config))[0],
config["num_epochs"], config["batch_size"])
if args.comment:
model_tag = model_tag + "_{}".format(args.comment)
model_tag = output_dir / model_tag
model_save_path = model_tag / "weights"
eval_score_path = model_tag / config["eval_output"]
writer = SummaryWriter(model_tag)
os.makedirs(model_save_path, exist_ok=True)
copy(args.config, model_tag / "config.conf")
# set device
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Device: {}".format(device))
if device == "cpu":
raise ValueError("GPU not detected!")
# define model architecture
model = get_model(model_config, device)
# define dataloaders
trn_loader, dev_loader, eval_loader = get_loader(
database_path, args.seed, config)
# evaluates pretrained model and exit script
if args.eval:
model.load_state_dict(
torch.load(config["model_path"], map_location=device))
print("Model loaded : {}".format(config["model_path"]))
print("Start evaluation...")
produce_evaluation_file(eval_loader, model, device,
eval_score_path, eval_trial_path)
calculate_eer_acc(cm_scores_file=eval_score_path)
print("DONE.")
sys.exit(0)
# get optimizer and scheduler
optim_config["steps_per_epoch"] = len(trn_loader)
optimizer, scheduler = create_optimizer(model.parameters(), optim_config)
optimizer_swa = SWA(optimizer)
best_dev_eer = 1.
best_eval_eer = 100.
best_dev_tdcf = 0.05
best_eval_tdcf = 1.
n_swa_update = 0 # number of snapshots of model to use in SWA
f_log = open(model_tag / "metric_log.txt", "a")
f_log.write("=" * 5 + "\n")
# make directory for metric logging
metric_path = model_tag / "metrics"
os.makedirs(metric_path, exist_ok=True)
# Training
for epoch in range(config["num_epochs"]):
print("Start training epoch{:03d}".format(epoch))
running_loss = train_epoch(trn_loader, model, optimizer, device,
scheduler, config)
produce_evaluation_file(dev_loader, model, device,
metric_path/"dev_score.txt", dev_trial_path)
calculate_eer_acc(cm_scores_file=metric_path/"dev_score.txt")
print("DONE.\nLoss:{:.5f},".format(
running_loss))
writer.add_scalar("loss", running_loss, epoch)
print("Start final evaluation")
epoch += 1
if n_swa_update > 0:
optimizer_swa.swap_swa_sgd()
optimizer_swa.bn_update(trn_loader, model, device=device)
produce_evaluation_file(eval_loader, model, device, eval_score_path,
eval_trial_path)
acc, eer = calculate_eer_acc(cm_scores_file=eval_score_path)
f_log = open(model_tag / "metric_log.txt", "a")
f_log.write("=" * 5 + "\n")
f_log.write("EER: {:.3f}, acc: {:.5f}".format(eer, acc))
f_log.close()
torch.save(model.state_dict(),
model_save_path / "swa.pth")
def get_model(model_config: Dict, device: torch.device):
"""Define DNN model architecture"""
module = import_module("models.{}".format(model_config["architecture"]))
_model = getattr(module, "Model")
model = _model(model_config).to(device)
nb_params = sum([param.view(-1).size()[0] for param in model.parameters()])
print("no. model params:{}".format(nb_params))
return model
def get_loader(
database_path: str,
seed: int,
config: dict) -> List[torch.utils.data.DataLoader]:
"""Make PyTorch DataLoaders for train / developement / evaluation"""
database_path = r"/content/drive/MyDrive/q3"
files_real = os.listdir(r"/content/drive/MyDrive/q3/Recorded/new/converted")
files_fake = os.listdir(r"/content/drive/MyDrive/q3/Generated/English/converted")
n_real = len(files_real)
n_fake = len(files_fake)
files_real_train = files_real[:int(n_real*0.7)]
files_fake_train = files_fake[:int(n_fake*0.7)]
files_real_dev = files_real[int(n_real*0.7): int(n_real*0.9)]
files_fake_dev = files_fake[int(n_real*0.7):int(n_fake*0.9)]
files_real_eval = files_real[int(n_real*0.9):]
files_fake_eval = files_fake[int(n_fake*0.9):]
d_label_trn = {}
for file in files_real_train:
d_label_trn[file] = 1
for file in files_fake_train:
d_label_trn[file] = 0
file_train = files_real_train + files_fake_train
print("no. training files:", len(file_train))
train_set = Dataset_ASVspoof2019_train(list_IDs=file_train,
labels=d_label_trn,
base_dir=database_path)
gen = torch.Generator()
gen.manual_seed(seed)
trn_loader = DataLoader(train_set,
batch_size=config["batch_size"],
shuffle=True,
drop_last=True,
pin_memory=True,
worker_init_fn=seed_worker,
generator=gen)
d_label_dev = {}
for file in files_real_dev:
d_label_dev[file] = 1
for file in files_fake_dev:
d_label_dev[file] = 0
file_dev = files_real_dev + files_fake_dev
print("no. validation files:", len(file_dev))
dev_set = Dataset_ASVspoof2019_devNeval(list_IDs=file_dev,
labels=d_label_dev,
base_dir=database_path)
dev_loader = DataLoader(dev_set,
batch_size=config["batch_size"],
shuffle=False,
drop_last=False,
pin_memory=True)
d_label_eval = {}
for file in files_real_eval:
d_label_eval[file] = 1
for file in files_fake_eval:
d_label_eval[file] = 0
file_eval = files_real_eval + files_fake_eval
eval_set = Dataset_ASVspoof2019_devNeval(list_IDs=file_eval,
labels=d_label_eval,
base_dir=database_path)
eval_loader = DataLoader(eval_set,
batch_size=config["batch_size"],
shuffle=False,
drop_last=False,
pin_memory=True)
return trn_loader, dev_loader, eval_loader
def produce_evaluation_file(
data_loader: DataLoader,
model,
device: torch.device,
save_path: str,
trial_path: str) -> None:
"""Perform evaluation and save the score to a file"""
model.eval()
# with open(trial_path, "r") as f_trl:
# trial_lines = f_trl.readlines()
fname_list = []
score_list = []
y_list = []
for batch_x, batch_y, utt_id in data_loader:
batch_x = batch_x.to(device)
with torch.no_grad():
_, batch_out = model(batch_x)
batch_score = (batch_out[:, 1]).data.cpu().numpy().ravel()
# add outputs
fname_list.extend(utt_id)
y_list.extend(batch_y)
score_list.extend(batch_score.tolist())
v_min = np.min(score_list)
v_range = np.max(score_list) - v_min
normalized_values = (score_list - v_min) / v_range
# assert len(trial_lines) == len(fname_list) == len(score_list)
with open(save_path, "w") as fh:
for fn, y, no, sco in zip(fname_list, y_list, normalized_values, score_list):
# _, utt_id, _, src, key = trl.strip().split(' ')
# assert fn == utt_id
fh.write("{} {} {} {}\n".format(fn, y, no, sco))
print("Scores saved to {}".format(save_path))
def train_epoch(
trn_loader: DataLoader,
model,
optim: Union[torch.optim.SGD, torch.optim.Adam],
device: torch.device,
scheduler: torch.optim.lr_scheduler,
config: argparse.Namespace):
"""Train the model for one epoch"""
running_loss = 0
num_total = 0.0
ii = 0
model.train()
# set objective (Loss) functions
weight = torch.FloatTensor([0.1, 0.9]).to(device)
criterion = nn.CrossEntropyLoss(weight=weight)
for batch_x, batch_y in trn_loader:
batch_size = batch_x.size(0)
num_total += batch_size
ii += 1
batch_x = batch_x.to(device)
batch_y = batch_y.view(-1).type(torch.int64).to(device)
_, batch_out = model(batch_x, Freq_aug=str_to_bool(config["freq_aug"]))
batch_loss = criterion(batch_out, batch_y)
running_loss += batch_loss.item() * batch_size
optim.zero_grad()
batch_loss.backward()
optim.step()
if config["optim_config"]["scheduler"] in ["cosine", "keras_decay"]:
scheduler.step()
elif scheduler is None:
pass
else:
raise ValueError("scheduler error, got:{}".format(scheduler))
running_loss /= num_total
return running_loss
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="ASVspoof detection system")
parser.add_argument("--config",
dest="config",
type=str,
help="configuration file",
required=True)
parser.add_argument(
"--output_dir",
dest="output_dir",
type=str,
help="output directory for results",
default="./exp_result",
)
parser.add_argument("--seed",
type=int,
default=1234,
help="random seed (default: 1234)")
parser.add_argument(
"--eval",
action="store_true",
help="when this flag is given, evaluates given model and exit")
parser.add_argument("--comment",
type=str,
default=None,
help="comment to describe the saved model")
parser.add_argument("--eval_model_weights",
type=str,
default=None,
help="directory to the model weight file (can be also given in the config file)")
main(parser.parse_args())