-
Notifications
You must be signed in to change notification settings - Fork 252
/
data_loaders.py
250 lines (221 loc) · 9.03 KB
/
data_loaders.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
import os
import random
import re
import numpy as np
import librosa
import torch
import random
from tqdm import tqdm
from torch.utils.data import Dataset
def traverse_dir(
root_dir,
extensions,
amount=None,
str_include=None,
str_exclude=None,
is_pure=False,
is_sort=False,
is_ext=True):
file_list = []
cnt = 0
for root, _, files in os.walk(root_dir):
for file in files:
if any([file.endswith(f".{ext}") for ext in extensions]):
# path
mix_path = os.path.join(root, file)
pure_path = mix_path[len(root_dir)+1:] if is_pure else mix_path
# amount
if (amount is not None) and (cnt == amount):
if is_sort:
file_list.sort()
return file_list
# check string
if (str_include is not None) and (str_include not in pure_path):
continue
if (str_exclude is not None) and (str_exclude in pure_path):
continue
if not is_ext:
ext = pure_path.split('.')[-1]
pure_path = pure_path[:-(len(ext)+1)]
file_list.append(pure_path)
cnt += 1
if is_sort:
file_list.sort()
return file_list
def get_data_loaders(args, whole_audio=False):
data_train = AudioDataset(
args.data.train_path,
waveform_sec=args.data.duration,
hop_size=args.data.block_size,
sample_rate=args.data.sampling_rate,
load_all_data=args.train.cache_all_data,
whole_audio=whole_audio,
extensions=args.data.extensions,
n_spk=args.model.n_spk,
device=args.train.cache_device,
fp16=args.train.cache_fp16,
use_aug=True)
loader_train = torch.utils.data.DataLoader(
data_train ,
batch_size=args.train.batch_size if not whole_audio else 1,
shuffle=True,
num_workers=args.train.num_workers if args.train.cache_device=='cpu' else 0,
persistent_workers=(args.train.num_workers > 0) if args.train.cache_device=='cpu' else False,
pin_memory=True if args.train.cache_device=='cpu' else False
)
data_valid = AudioDataset(
args.data.valid_path,
waveform_sec=args.data.duration,
hop_size=args.data.block_size,
sample_rate=args.data.sampling_rate,
load_all_data=args.train.cache_all_data,
whole_audio=True,
extensions=args.data.extensions,
n_spk=args.model.n_spk)
loader_valid = torch.utils.data.DataLoader(
data_valid,
batch_size=1,
shuffle=False,
num_workers=0,
pin_memory=True
)
return loader_train, loader_valid
class AudioDataset(Dataset):
def __init__(
self,
path_root,
waveform_sec,
hop_size,
sample_rate,
load_all_data=True,
whole_audio=False,
extensions=['wav'],
n_spk=1,
device = 'cpu',
fp16 = False,
use_aug = False
):
super().__init__()
self.waveform_sec = waveform_sec
self.sample_rate = sample_rate
self.hop_size = hop_size
self.path_root = path_root
self.paths = traverse_dir(
os.path.join(path_root, 'audio'),
extensions=extensions,
is_pure=True,
is_sort=True,
is_ext=True
)
self.whole_audio = whole_audio
self.use_aug = use_aug
self.data_buffer={}
if load_all_data:
print('Load all the data from :', path_root)
else:
print('Load the f0, volume data from :', path_root)
for name_ext in tqdm(self.paths, total=len(self.paths)):
path_audio = os.path.join(self.path_root, 'audio', name_ext)
duration = librosa.get_duration(filename = path_audio, sr = self.sample_rate)
path_f0 = os.path.join(self.path_root, 'f0', name_ext) + '.npy'
f0 = np.load(path_f0)
f0 = torch.from_numpy(f0).float().unsqueeze(-1).to(device)
path_volume = os.path.join(self.path_root, 'volume', name_ext) + '.npy'
volume = np.load(path_volume)
volume = torch.from_numpy(volume).float().unsqueeze(-1).to(device)
if n_spk is not None and n_spk > 1:
dirname_split = re.split(r"_|\-", os.path.dirname(name_ext), 2)[0]
spk_id = int(dirname_split) if str.isdigit(dirname_split) else 0
if spk_id < 1 or spk_id > n_spk:
raise ValueError(' [x] Muiti-speaker traing error : spk_id must be a positive integer from 1 to n_spk ')
else:
spk_id = 1
spk_id = torch.LongTensor(np.array([spk_id])).to(device)
if load_all_data:
audio, sr = librosa.load(path_audio, sr=self.sample_rate)
if len(audio.shape) > 1:
audio = librosa.to_mono(audio)
audio = torch.from_numpy(audio).to(device)
path_units = os.path.join(self.path_root, 'units', name_ext) + '.npy'
units = np.load(path_units)
units = torch.from_numpy(units).to(device)
if fp16:
audio = audio.half()
units = units.half()
self.data_buffer[name_ext] = {
'duration': duration,
'audio': audio,
'units': units,
'f0': f0,
'volume': volume,
'spk_id': spk_id
}
else:
self.data_buffer[name_ext] = {
'duration': duration,
'f0': f0,
'volume': volume,
'spk_id': spk_id
}
def __getitem__(self, file_idx):
name_ext = self.paths[file_idx]
data_buffer = self.data_buffer[name_ext]
# check duration. if too short, then skip
if data_buffer['duration'] < (self.waveform_sec + 0.1):
return self.__getitem__( (file_idx + 1) % len(self.paths))
# get item
return self.get_data(name_ext, data_buffer)
def get_data(self, name_ext, data_buffer):
name = os.path.splitext(name_ext)[0]
frame_resolution = self.hop_size / self.sample_rate
duration = data_buffer['duration']
waveform_sec = duration if self.whole_audio else self.waveform_sec
# load audio
idx_from = 0 if self.whole_audio else random.uniform(0, duration - waveform_sec - 0.1)
start_frame = int(idx_from / frame_resolution)
units_frame_len = int(waveform_sec / frame_resolution)
audio = data_buffer.get('audio')
if audio is None:
path_audio = os.path.join(self.path_root, 'audio', name_ext)
audio, sr = librosa.load(
path_audio,
sr = self.sample_rate,
offset = start_frame * frame_resolution,
duration = waveform_sec)
if len(audio.shape) > 1:
audio = librosa.to_mono(audio)
# clip audio into N seconds
audio = audio[ : audio.shape[-1] // self.hop_size * self.hop_size]
audio = torch.from_numpy(audio).float()
else:
audio = audio[start_frame * self.hop_size : (start_frame + units_frame_len) * self.hop_size]
# load units
units = data_buffer.get('units')
if units is None:
units = os.path.join(self.path_root, 'units', name_ext) + '.npy'
units = np.load(units)
units = units[start_frame : start_frame + units_frame_len]
units = torch.from_numpy(units).float()
else:
units = units[start_frame : start_frame + units_frame_len]
# load f0
f0 = data_buffer.get('f0')
f0_frames = f0[start_frame : start_frame + units_frame_len]
# load volume
volume = data_buffer.get('volume')
volume_frames = volume[start_frame : start_frame + units_frame_len]
# load spk_id
spk_id = data_buffer.get('spk_id')
# volume augmentation
if self.use_aug:
max_amp = float(torch.max(torch.abs(audio))) + 1e-5
max_shift = min(1, np.log10(1/max_amp))
log10_vol_shift = random.uniform(-1, max_shift)
audio_aug = audio * (10 ** log10_vol_shift)
volume_frames_aug = volume_frames * (10 ** log10_vol_shift)
else:
audio_aug = audio
volume_frames_aug = volume_frames
return dict(audio=audio_aug, f0=f0_frames, volume=volume_frames_aug, units=units, spk_id=spk_id, name=name)
def __len__(self):
return len(self.paths)