-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_df.py
executable file
·121 lines (96 loc) · 4.34 KB
/
eval_df.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
import argparse
import torch
import torch.nn.functional as F
from tqdm import tqdm
import data_loader.data_loaders as module_data
import model.loss as module_loss
import model.metric as module_metric
import model as module_arch
from metrics.evaluate_tDCF_asvspoof19_func import evaluate_tdcf_eer, evaluate_eer
from parse_config import ConfigParser
from pathlib import Path
from collections import defaultdict
from functools import reduce
import numpy as np
torch.manual_seed(1234) #cpu
torch.cuda.manual_seed(1234) #gpu
np.random.seed(1234) #numpy
# random.seed(1234) #random and transforms
torch.backends.cudnn.benchmark = True
def main(config, resume, protocol_file, asv_score_file):
logger = config.get_logger('evaluation')
data_loader = getattr(module_data, config['dev_data_loader']['type'])(
"/data/longnv/trn_dev_eval_scps/ASVspoof2021_DF_eval.scp",
config['dev_data_loader']['args']['data_dir'],
batch_size=32,
shuffle=False,
validation_split=0.0,
num_workers=2,
eval=True
)
# build model architecture
model = config.initialize('arch', module_arch)
# logger.info(model)
metric_fns = [getattr(module_metric, met) for met in config['metrics']]
logger.info('Loading checkpoint: {} ...'.format(resume))
checkpoint = torch.load(resume)
state_dict = checkpoint['state_dict']
if config['n_gpu'] > 1:
model = torch.nn.DataParallel(model)
model.load_state_dict(state_dict)
# prepare model for testing
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
model.eval()
utt2scores = defaultdict()
# total_loss = 0.0
total_metrics = torch.zeros(len(metric_fns))
with torch.no_grad():
for i, (utt_list, data, target) in enumerate(tqdm(data_loader)):
data, target = data.to(device), target.to(device)
output = model(data, eval=True)
# loss = loss_fn(output, target)
batch_size = data.shape[0]
# total_loss += loss.item() * batch_size
for i, metric in enumerate(metric_fns):
total_metrics[i] += metric(output, target) * batch_size
score = output[:, 1] # use the bonafide class for scoring
# logger.info({"utt_list":utt_list})
for index, utt_id in enumerate(utt_list):
utt2scores[utt_id] = score[index].item()
# logger.info({"score[index].item()":score[index].item()})
logger.info({"utt_id":utt_id, "index":index, "utt2score":utt2scores[utt_id]})
n_samples = len(data_loader.sampler)
# log = {'loss': total_loss / n_samples}
log = { }
log.update({
met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns)
})
logger.info(log)
# compute t-DCF and eer
with open(protocol_file, 'r') as f:
protocol_file_lines = [line.strip().split(' ') for line in f]
cm_score_file = Path(resume).parent / 'cm_score_eval_allSys.txt'
with open(cm_score_file, 'w') as f:
for line in protocol_file_lines:
# logger.info({"line":line[0]})
utt_id = line[0]
label = line[-1]
sco = utt2scores[utt_id]
f.write(utt_id+" "+"-"+" "+label+" "+str(sco)+"\n")
# tdcf, eer = evaluate_tdcf_eer(cm_score_file, asv_score_file, print_cost=True)
eer, eer_point = evaluate_eer(cm_score_file)
logger.info({"EER": eer, "EER_point": eer_point})
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='ASVSpoof2019 project')
parser.add_argument('-r', '--resume', default=None, type=str,
help='path to latest checkpoint (default: None)')
parser.add_argument('-f', '--protocol_file', default=None, type=str,
help='Protocol file: e.g., data/ASVspoof2019.PA.cm.dev.trl.txt')
parser.add_argument('-a', '--asv_score_file', default=None, type=str,
help='Protocol file: e.g., data/ASVspoof2019_PA_dev_asv_scores_v1.txt')
parser.add_argument('-d', '--device', default=None, type=str,
help='indices of GPUs to enable (default: all)')
args = parser.parse_args()
config = ConfigParser(args)
main(config, args.resume, args.protocol_file, args.asv_score_file)