forked from eurecom-asp/RawGAT-ST-antispoofing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils.py
150 lines (115 loc) · 5.06 KB
/
data_utils.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
import torch
import collections
import os
import soundfile as sf
from torch.utils.data import DataLoader, Dataset
import numpy as np
from joblib import Parallel, delayed
ASVFile = collections.namedtuple('ASVFile',
['speaker_id', 'file_name', 'path', 'sys_id', 'key'])
class ASVDataset(Dataset):
""" Utility class to load train/dev/Eval datatsets """
def __init__(self, database_path=None,protocols_path=None,transform=None,
is_train=True, sample_size=None,
is_logical=True, feature_name=None, is_eval=False,
eval_part=0):
track = 'LA'
data_root=protocols_path
assert feature_name is not None, 'must provide feature name'
self.track = track
self.is_logical = is_logical
self.prefix = 'ASVspoof2019_{}'.format(track)
v1_suffix = ''
if is_eval and track == 'LA':
v1_suffix='_v1'
self.sysid_dict = {
'-': 0, # bonafide speech
'A07': 1,
'A08': 2,
'A09': 3,
'A10': 4,
'A11': 5,
'A12': 6,
'A13': 7,
'A14': 8,
'A15': 9,
'A16': 10,
'A17': 11,
'A18': 12,
'A19': 13,
}
else:
self.sysid_dict = {
'-': 0, # bonafide speech
'A01': 1,
'A02': 2,
'A03': 3,
'A04': 4,
'A05': 5,
'A06': 6,
}
self.data_root_dir=database_path
self.is_eval = is_eval
self.sysid_dict_inv = {v:k for k,v in self.sysid_dict.items()}
print('sysid_dict_inv',self.sysid_dict_inv)
self.data_root = data_root
print('data_root',self.data_root)
self.dset_name = 'eval' if is_eval else 'train' if is_train else 'dev'
print('dset_name',self.dset_name)
self.protocols_fname = 'eval.trl' if is_eval else 'train.trn' if is_train else 'dev.trl'
print('protocols_fname',self.protocols_fname)
self.protocols_dir = os.path.join(self.data_root)
print('protocols_dir',self.protocols_dir)
self.files_dir = os.path.join(self.data_root_dir, '{}_{}'.format(
self.prefix, self.dset_name ), 'flac')
print('files_dir',self.files_dir)
self.protocols_fname = os.path.join(self.protocols_dir,
'ASVspoof2019.{}.cm.{}.txt'.format(track, self.protocols_fname))
print('protocols_file',self.protocols_fname)
self.cache_fname = 'cache_{}_{}_{}.npy'.format(self.dset_name,track,feature_name)
print('cache_fname',self.cache_fname)
self.transform = transform
if os.path.exists(self.cache_fname):
self.data_x, self.data_y, self.data_sysid, self.files_meta = torch.load(self.cache_fname)
print('Dataset loaded from cache ', self.cache_fname)
else:
self.files_meta = self.parse_protocols_file(self.protocols_fname)
data = list(map(self.read_file, self.files_meta))
self.data_x, self.data_y, self.data_sysid = map(list, zip(*data))
if self.transform:
self.data_x = Parallel(n_jobs=4, prefer='threads')(delayed(self.transform)(x) for x in self.data_x)
torch.save((self.data_x, self.data_y, self.data_sysid, self.files_meta), self.cache_fname)
if sample_size:
select_idx = np.random.choice(len(self.files_meta), size=(sample_size,), replace=True).astype(np.int32)
self.files_meta= [self.files_meta[x] for x in select_idx]
self.data_x = [self.data_x[x] for x in select_idx]
self.data_y = [self.data_y[x] for x in select_idx]
self.data_sysid = [self.data_sysid[x] for x in select_idx]
self.length = len(self.data_x)
def __len__(self):
return self.length
def __getitem__(self, idx):
x = self.data_x[idx]
y = self.data_y[idx]
return x, y, self.files_meta[idx]
def read_file(self, meta):
data_x, sample_rate = sf.read(meta.path)
data_y = meta.key
return data_x, float(data_y), meta.sys_id
def _parse_line(self, line):
tokens = line.strip().split(' ')
if self.is_eval:
return ASVFile(speaker_id=tokens[0],
file_name=tokens[1],
path=os.path.join(self.files_dir, tokens[1] + '.flac'),
sys_id=self.sysid_dict[tokens[3]],
key=int(tokens[4] == 'bonafide'))
return ASVFile(speaker_id=tokens[0],
file_name=tokens[1],
path=os.path.join(self.files_dir, tokens[1] + '.flac'),
sys_id=self.sysid_dict[tokens[3]],
key=int(tokens[4] == 'bonafide'))
def parse_protocols_file(self, protocols_fname):
lines = open(protocols_fname).readlines()
files_meta = map(self._parse_line, lines)
return list(files_meta)