-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataloader.py
More file actions
176 lines (156 loc) · 5.95 KB
/
dataloader.py
File metadata and controls
176 lines (156 loc) · 5.95 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
import random
import os
# os.environ[ 'NUMBA_CACHE_DIR' ] = '/tmp/'
import sys
import json
from pathlib import Path
from loguru import logger
# from librosa.filters import mel as librosa_mel_fn
import torch
import torchaudio
import torch.nn.functional as F
# torchaudio backend selection is version-dependent; newer torchaudio removed set_audio_backend.
if hasattr(torchaudio, "set_audio_backend"):
try:
torchaudio.set_audio_backend("sox_io")
except Exception:
# Fall back to torchaudio default backend if sox_io is unavailable.
pass
from glob import glob
def find_audio_files(path, exts=[".wav"], progress=True):
"""
dump all files in the given path to a json file with the format:
[(audio_path, audio_length),...]
"""
audio_files = []
for root, folders, files in os.walk(path, followlinks=True):
for file in files:
file = Path(root) / file
if file.suffix.lower() in exts:
audio_files.append(str(file.resolve()))
meta = []
for idx, file in enumerate(audio_files):
if "._" in file:
continue
siginfo = torchaudio.info(file)
length = siginfo.num_frames // siginfo.num_channels
meta.append((file, length))
if progress:
print(format((1 + idx) / len(audio_files), " 3.1%"), end='\r', file=sys.stderr)
meta.sort()
return meta
def find_audio_files_libricss(path, exts=[".wav"], progress=True):
"""
dump all files in the given path to a json file with the format:
[(audio_path, audio_length),...]
"""
audio_files = []
for root, folders, files in os.walk(path, followlinks=True):
for file in files:
file = Path(root) / file
if file.suffix.lower() in exts:
audio_files.append(str(file.resolve()))
meta = []
for idx, file in enumerate(audio_files):
if "._" in file:
continue
if "mix.wav" not in file:
continue
siginfo = torchaudio.info(file)
length = siginfo.num_frames // siginfo.num_channels
meta.append((file, length))
if progress:
print(format((1 + idx) / len(audio_files), " 3.1%"), end='\r', file=sys.stderr)
meta.sort()
return meta
def find_audio_files_callHome(path, exts=[".wav"], progress=True):
"""
dump all files in the given path to a json file with the format:
[(audio_path, audio_length),...]
"""
audio_files = []
for root, folders, files in os.walk(path, followlinks=True):
for file in files:
file = Path(root) / file
if file.suffix.lower() in exts:
audio_files.append(str(file.resolve()))
meta = []
for idx, file in enumerate(audio_files):
if "._" in file:
continue
if "chunk" not in file:
continue
siginfo = torchaudio.info(file)
length = siginfo.num_frames // siginfo.num_channels
meta.append((file, length))
if progress:
print(format((1 + idx) / len(audio_files), " 3.1%"), end='\r', file=sys.stderr)
meta.sort()
return meta
def sample_segment(audio, n_samples, rand, ret_idx=False):
"""
samples a random segment of `n_samples` from `audio`.
if audio is shorter than `n_samples` then the original audio is zero padded.
audio - tensor of shape [1, T]
n_samples - int, this will be the new length of audio
ret_idx - if True then the start and end indices will be returned
"""
start, end = 0, audio.shape[1]
if audio.shape[1] > n_samples:
diff = audio.shape[1] - n_samples
if rand:
start = random.randint(0, diff)
end = start + n_samples
audio = audio[:, start:end]
elif audio.shape[1] < n_samples:
diff = n_samples - audio.shape[1]
audio = F.pad(audio, (0, diff))
if ret_idx:
return audio, (start, end)
return audio
class AudioDataset(torch.utils.data.Dataset):
def __init__(self, json_manifest, n_samples=None, min_duration=0, max_duration=float("inf"), rand=True):
self.n_samples = n_samples
# load list of files
logger.info(f"loading from: {json_manifest}")
self.files = json.load(open(json_manifest, "r"))
logger.info(f"files in manifest: {len(self.files)}")
# filter files that are incorrect duration
self.files = list(filter(lambda x: min_duration <= x[1] <= max_duration, self.files))
logger.info(f"files after duration filtering: {len(self.files)}")
self.rand=rand
print(rand, self.rand)
def __len__(self):
return len(self.files)
def __getitem__(self, i):
path, length = self.files[i]
audio, sr = torchaudio.load(path)
if self.n_samples:
audio = sample_segment(audio, self.n_samples, self.rand)
return audio
class AudioDatasetEval(torch.utils.data.Dataset):
def __init__(self, json_manifest, n_samples=None, min_duration=0, max_duration=float("inf"), rand=True):
self.n_samples = n_samples
# load list of files
logger.info(f"loading from: {json_manifest}")
self.files = json.load(open(json_manifest, "r"))
logger.info(f"files in manifest: {len(self.files)}")
# filter files that are incorrect duration
self.files = list(filter(lambda x: min_duration <= x[1] <= max_duration, self.files))
logger.info(f"files after duration filtering: {len(self.files)}")
self.rand=rand
def __len__(self):
return len(self.files)
def __getitem__(self, i):
path, length = self.files[i]
audio, sr = torchaudio.load(path)
if self.n_samples:
audio = sample_segment(audio, self.n_samples, self.rand)
return audio, path
if __name__ == "__main__":
meta = []
outJson = sys.argv[1]
for path in sys.argv[2:]:
meta += find_audio_files(path)
with open(outJson, "w") as outfile:
json.dump(meta, outfile, indent=4)