forked from yzyouzhang/HBAS_chapter_voice3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_dataset.py
More file actions
213 lines (176 loc) · 9.21 KB
/
Copy pathraw_dataset.py
File metadata and controls
213 lines (176 loc) · 9.21 KB
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
#!/usr/bin/python3
import numpy as np
import torch
from torch import Tensor
from torch.utils.data import Dataset
import scipy.io as sio
import pickle
import os
import librosa
from torch.utils.data.dataloader import default_collate
import warnings
def torchaudio_load(filepath):
wave, sr = librosa.load(filepath, sr=16000)
wave = librosa.util.normalize(wave)
waveform = torch.Tensor(np.expand_dims(wave, axis=0))
return [waveform, sr]
class ASVspoof2019Raw(Dataset):
def __init__(self, access_type, path_to_database, path_to_protocol, part='train'):
super(ASVspoof2019Raw, self).__init__()
self.access_type = access_type
self.ptd = path_to_database
self.part = part
self.path_to_audio = os.path.join(self.ptd, access_type, 'ASVspoof2019_'+access_type+'_'+ self.part +'/flac/')
self.path_to_protocol = path_to_protocol
protocol = os.path.join(self.path_to_protocol, 'ASVspoof2019.'+access_type+'.cm.'+ self.part + '.trl.txt')
if self.part == "eval":
protocol = os.path.join(self.ptd, access_type, 'ASVspoof2019_' + access_type +
'_cm_protocols/ASVspoof2019.' + access_type + '.cm.' + self.part + '.trl.txt')
if self.access_type == 'LA':
self.tag = {"-": 0, "A01": 1, "A02": 2, "A03": 3, "A04": 4, "A05": 5, "A06": 6, "A07": 7, "A08": 8, "A09": 9,
"A10": 10, "A11": 11, "A12": 12, "A13": 13, "A14": 14, "A15": 15, "A16": 16, "A17": 17, "A18": 18,
"A19": 19}
else:
self.tag = {"-": 0, "AA": 1, "AB": 2, "AC": 3, "BA": 4, "BB": 5, "BC": 6, "CA": 7, "CB": 8, "CC": 9}
self.label = {"spoof": 1, "bonafide": 0}
with open(protocol, 'r') as f:
audio_info = [info.strip().split() for info in f.readlines()]
self.all_info = audio_info
def __len__(self):
return len(self.all_info)
def __getitem__(self, idx):
speaker, filename, _, tag, label = self.all_info[idx]
filepath = os.path.join(self.path_to_audio, filename + ".flac")
waveform, sr = torchaudio_load(filepath)
return waveform, filename, tag, label
def collate_fn(self, samples):
return default_collate(samples)
class VCC2020Raw(Dataset):
def __init__(self, path_to_spoof="/data2/neil/nii-yamagishilab-VCC2020-listeningtest-31f913c", path_to_bonafide="/data2/neil/nii-yamagishilab-VCC2020-database-0b2fb2e"):
super(VCC2020Raw, self).__init__()
self.all_spoof = librosa.util.find_files(path_to_spoof, ext="wav")
self.all_bonafide = librosa.util.find_files(path_to_bonafide, ext="wav")
def __len__(self):
# print(len(self.all_spoof), len(self.all_bonafide))
return len(self.all_spoof) + len(self.all_bonafide)
def __getitem__(self, idx):
if idx < len(self.all_bonafide):
filepath = self.all_bonafide[idx]
label = "bonafide"
filename = "_".join(filepath.split("/")[-3:])[:-4]
tag = "-"
else:
filepath = self.all_spoof[idx - len(self.all_bonafide)]
filename = os.path.basename(filepath)[:-4]
label = "spoof"
tag = filepath.split("/")[-3]
waveform, sr = torchaudio_load(filepath)
return waveform, filename, tag, label
def collate_fn(self, samples):
return default_collate(samples)
class ASVspoof2015Raw(Dataset):
def __init__(self, path_to_database="/data/neil/ASVspoof2015/wav", path_to_protocol="/data/neil/ASVspoof2015/CM_protocol", part='train'):
super(ASVspoof2015Raw, self).__init__()
self.ptd = path_to_database
self.part = part
self.path_to_audio = os.path.join(self.ptd, self.part)
self.path_to_protocol = path_to_protocol
cm_pro_dict = {"train": "cm_train.trn", "dev": "cm_develop.ndx", "eval": "cm_evaluation.ndx"}
protocol = os.path.join(self.path_to_protocol, cm_pro_dict[self.part])
self.tag = {"human": 0, "S1": 1, "S2": 2, "S3": 3, "S4": 4, "S5": 5,
"S6": 6, "S7": 7, "S8": 8, "S9": 9, "S10": 10}
self.label = {"spoof": 1, "human": 0}
with open(protocol, 'r') as f:
audio_info = [info.strip().split() for info in f.readlines()]
self.all_info = audio_info
def __len__(self):
return len(self.all_info)
def __getitem__(self, idx):
speaker, filename, tag, label = self.all_info[idx]
filepath = os.path.join(self.path_to_audio, speaker, filename + ".wav")
waveform, sr = torchaudio_load(filepath)
filename = filename.replace("_", "-")
return waveform, filename, tag, label
def collate_fn(self, samples):
return default_collate(samples)
class ASVspoof2019LARaw_withChannel(Dataset):
def __init__(self, access_type="LA", path_to_database="/data/shared/ASVspoof2019Channel", path_to_protocol="/data/neil/DS_10283_3336/LA/ASVspoof2019_LA_cm_protocols/", part='train'):
super(ASVspoof2019LARaw_withChannel, self).__init__()
self.access_type = access_type
self.ptd = path_to_database
self.part = part
self.path_to_audio = path_to_database
self.path_to_protocol = path_to_protocol
protocol = os.path.join(self.path_to_protocol,
'ASVspoof2019.' + access_type + '.cm.' + self.part + '.trl.txt')
if self.part == "eval":
protocol = os.path.join(self.ptd, access_type, 'ASVspoof2019_' + access_type +
'_cm_protocols/ASVspoof2019.' + access_type + '.cm.' + self.part + '.trl.txt')
self.tag = {"-": 0, "A01": 1, "A02": 2, "A03": 3, "A04": 4, "A05": 5, "A06": 6, "A07": 7, "A08": 8,
"A09": 9,
"A10": 10, "A11": 11, "A12": 12, "A13": 13, "A14": 14, "A15": 15, "A16": 16, "A17": 17,
"A18": 18,
"A19": 19}
self.label = {"spoof": 1, "bonafide": 0}
self.channel = ['amr[br=5k15]', 'amrwb[br=15k85]', 'g711[law=u]', 'g722[br=56k]',
'g722[br=64k]', 'g726[law=a,br=16k]', 'g728', 'g729a', 'gsmfr',
'silk[br=20k]', 'silk[br=5k]', 'silkwb[br=10k,loss=5]', 'silkwb[br=30k]']
with open(protocol, 'r') as f:
audio_info = [info.strip().split() for info in f.readlines()]
self.all_info = audio_info
def __len__(self):
return len(self.all_info) * len(self.channel)
def __getitem__(self, idx):
file_idx = idx // len(self.channel)
channel_idx = idx % len(self.channel)
speaker, filename, _, tag, label = self.all_info[file_idx]
channel = self.channel[channel_idx]
filepath = os.path.join(self.path_to_audio, filename + "_" + channel + ".wav")
waveform, sr = torchaudio_load(filepath)
return waveform, filename, tag, label, channel
def collate_fn(self, samples):
return default_collate(samples)
class ASVspoof2019LARaw_withDevice(Dataset):
def __init__(self, access_type="LA", path_to_database="/data/shared/ASVspoof2019LA-Sim", path_to_protocol="/data/neil/DS_10283_3336/LA/ASVspoof2019_LA_cm_protocols/", part='eval'):
super(ASVspoof2019LARaw_withDevice, self).__init__()
self.access_type = access_type
self.ptd = path_to_database
self.part = part
self.path_to_audio = path_to_database
self.path_to_protocol = path_to_protocol
protocol = os.path.join(self.path_to_protocol,
'ASVspoof2019.' + access_type + '.cm.' + self.part + '.trl.txt')
self.tag = {"-": 0, "A01": 1, "A02": 2, "A03": 3, "A04": 4, "A05": 5, "A06": 6, "A07": 7, "A08": 8,
"A09": 9,
"A10": 10, "A11": 11, "A12": 12, "A13": 13, "A14": 14, "A15": 15, "A16": 16, "A17": 17,
"A18": 18,
"A19": 19}
self.label = {"spoof": 1, "bonafide": 0}
self.devices = ['AKSPKRS80sUk002-16000', 'AKSPKRSVinUk002-16000', 'Doremi-16000', 'RCAPB90-16000',
'ResloRBRedLabel-16000', 'AKSPKRSSpeaker002-16000', 'BehritoneirRecording-16000',
'OktavaML19-16000', 'ResloRB250-16000', 'SonyC37Fet-16000', 'iPadirRecording-16000', 'iPhoneirRecording-16000']
with open(protocol, 'r') as f:
audio_info = [info.strip().split() for info in f.readlines()]
self.all_info = audio_info
def __len__(self):
return len(self.all_info) * len(self.devices)
def __getitem__(self, idx):
file_idx = idx // len(self.devices)
device_idx = idx % len(self.devices)
speaker, filename, _, tag, label = self.all_info[file_idx]
device = self.devices[device_idx]
filepath = os.path.join(self.path_to_audio, device, filename + ".wav")
waveform, sr = torchaudio_load(filepath)
return waveform, filename, tag, label, device
def collate_fn(self, samples):
return default_collate(samples)
if __name__ == "__main__":
asvspoof2019channel = ASVspoof2019LARaw_withChannel()
print(len(asvspoof2019channel))
waveform, filename, tag, label, channel = asvspoof2019channel[123]
print(waveform.shape)
print(filename)
print(tag)
print(label)
print(channel)
pass