forked from ghua-ac/end-to-end-synthetic-speech-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
166 lines (146 loc) · 6.1 KB
/
data.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
import pandas as pd
import torch
from torch.utils.data.dataloader import Dataset
import soundfile as sf
class PrepASV19Dataset(Dataset):
def __init__(self, protocol_file_path, data_path, data_type='time_frame'):
self.train_protocol = pd.read_csv(protocol_file_path, sep=' ', header=None)
self.data_path = data_path
self.data_type = data_type
def __len__(self):
return self.train_protocol.shape[0]
def __getitem__(self, index):
data_file_path = self.data_path + self.train_protocol.iloc[index, 1]
if self.data_type == 'time_frame':
sample, _ = sf.read(data_file_path + '.flac')
sample = torch.tensor(sample, dtype=torch.float32)
sample = torch.unsqueeze(sample, 0)
label = self.train_protocol.iloc[index, 4]
label = label_encode(label)
sub_class = self.train_protocol.iloc[index, 3]
sub_class = sub_class_encode_19(sub_class)
return sample, label, sub_class
if self.data_type == 'CQT':
sample = torch.load(data_file_path + '.pt')
sample = torch.tensor(sample, dtype=torch.float32)
sample = torch.unsqueeze(sample, 0)
label = self.train_protocol.iloc[index, 4]
label = label_encode(label)
sub_class = self.train_protocol.iloc[index, 3]
sub_class = sub_class_encode_19(sub_class)
return sample, label, sub_class
def get_weights(self):
label_info = self.train_protocol.iloc[:, 4]
num_zero_class = (label_info == 'bonafide').sum()
num_one_class = (label_info == 'spoof').sum()
weights = torch.tensor([num_one_class, num_zero_class], dtype=torch.float32)
weights = weights / (weights.sum())
return weights
class PrepASV15Dataset(Dataset):
def __init__(self, protocol_file_path, data_path, data_type='time_frame'):
self.train_protocol = pd.read_csv(protocol_file_path, sep=' ', header=None)
self.data_path = data_path
self.data_type = data_type
def __len__(self):
return self.train_protocol.shape[0]
def __getitem__(self, index):
data_file_path = self.data_path + self.train_protocol.iloc[index, 1]
if self.data_type == 'time_frame':
sample, _ = sf.read(data_file_path + '.wav')
sample = torch.tensor(sample, dtype=torch.float32)
sample = torch.unsqueeze(sample, 0)
label = self.train_protocol.iloc[index, 3]
label = label_encode(label)
sub_class = self.train_protocol.iloc[index, 2]
sub_class = sub_class_encode_15(sub_class)
return sample, label, sub_class
if self.data_type == 'CQT':
sample = torch.load(data_file_path + '.pt')
sample = torch.tensor(sample, dtype=torch.float32)
sample = torch.unsqueeze(sample, 0)
label = self.train_protocol.iloc[index, 3]
label = label_encode(label)
sub_class = self.train_protocol.iloc[index, 2]
sub_class = sub_class_encode_15(sub_class)
return sample, label, sub_class
def get_weights(self):
label_info = self.train_protocol.iloc[:, 3]
num_zero_class = (label_info == 'human').sum()
num_one_class = (label_info == 'spoof').sum()
weights = torch.tensor([num_one_class, num_zero_class], dtype=torch.float32)
weights = weights / (weights.sum())
return weights
def label_encode(label):
if label == 'bonafide':
label = torch.tensor(0, dtype=torch.int64)
elif label == 'human':
label = torch.tensor(0, dtype=torch.int64)
else:
label = torch.tensor(1, dtype=torch.int64)
return label
def sub_class_encode_19(label):
if label == '-':
label = torch.tensor(0, dtype=torch.int64)
elif label == 'A01':
label = torch.tensor(1, dtype=torch.int64)
elif label == 'A02':
label = torch.tensor(2, dtype=torch.int64)
elif label == 'A03':
label = torch.tensor(3, dtype=torch.int64)
elif label == 'A04':
label = torch.tensor(4, dtype=torch.int64)
elif label == 'A05':
label = torch.tensor(5, dtype=torch.int64)
elif label == 'A06':
label = torch.tensor(6, dtype=torch.int64)
elif label == 'A07':
label = torch.tensor(7, dtype=torch.int64)
elif label == 'A08':
label = torch.tensor(8, dtype=torch.int64)
elif label == 'A09':
label = torch.tensor(9, dtype=torch.int64)
elif label == 'A10':
label = torch.tensor(10, dtype=torch.int64)
elif label == 'A11':
label = torch.tensor(11, dtype=torch.int64)
elif label == 'A12':
label = torch.tensor(12, dtype=torch.int64)
elif label == 'A13':
label = torch.tensor(13, dtype=torch.int64)
elif label == 'A14':
label = torch.tensor(14, dtype=torch.int64)
elif label == 'A15':
label = torch.tensor(15, dtype=torch.int64)
elif label == 'A16':
label = torch.tensor(16, dtype=torch.int64)
elif label == 'A17':
label = torch.tensor(17, dtype=torch.int64)
elif label == 'A18':
label = torch.tensor(18, dtype=torch.int64)
elif label == 'A19':
label = torch.tensor(19, dtype=torch.int64)
return label
def sub_class_encode_15(label):
if label == 'human':
label = torch.tensor(0, dtype=torch.int64)
elif label == 'S1':
label = torch.tensor(1, dtype=torch.int64)
elif label == 'S2':
label = torch.tensor(2, dtype=torch.int64)
elif label == 'S3':
label = torch.tensor(3, dtype=torch.int64)
elif label == 'S4':
label = torch.tensor(4, dtype=torch.int64)
elif label == 'S5':
label = torch.tensor(5, dtype=torch.int64)
elif label == 'S6':
label = torch.tensor(6, dtype=torch.int64)
elif label == 'S7':
label = torch.tensor(7, dtype=torch.int64)
elif label == 'S8':
label = torch.tensor(8, dtype=torch.int64)
elif label == 'S9':
label = torch.tensor(9, dtype=torch.int64)
elif label == 'S10':
label = torch.tensor(10, dtype=torch.int64)
return label