-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdatasets.py
172 lines (129 loc) · 5.45 KB
/
datasets.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
import os
import librosa
import numpy as np
import pandas as pd
import torch
from torch.utils.data import Dataset
from tqdm import tqdm
import hyperparams as hp
from audio import load_wav, wav_to_spectrogram
class LJSpeechDataset(Dataset):
def __init__(self, path, text_transforms=None, audio_transforms=None, cache=False, sort=True):
self.path = path
self.metadata = pd.read_csv(f'{path}/metadata.csv', sep='|',
names=['wav', 'transcription', 'text'],
usecols=['wav', 'text'])
self.metadata.dropna(inplace=True)
self.audio_transforms = audio_transforms
self.text_transforms = text_transforms
self.cache = cache
if sort:
self.metadata['length'] = self.metadata['wav'].apply(
lambda x: librosa.get_duration(filename=f'{path}/wavs/{x}.wav'))
self.metadata.sort_values(by=['length'], inplace=True)
if cache:
self.cache_spectrograms()
def cache_spectrograms(self):
wav_filenames = self.metadata['wav']
spectrograms_path = f'{self.path}/spectrograms'
if not os.path.exists(spectrograms_path):
os.makedirs(spectrograms_path)
print('Building Cache..')
for name in tqdm(wav_filenames, total=len(wav_filenames)):
audio, _ = load_wav(f'{self.path}/wavs/{name}.wav')
S = self.audio_transforms(audio)
np.save(f'{spectrograms_path}/{name}.npy', S)
def __getitem__(self, index):
text = self.metadata.iloc[index]['text']
filename = self.metadata.iloc[index]['wav']
if self.text_transforms:
text = self.text_transforms(text)
if self.cache:
audio = np.load(f'{self.path}/spectrograms/{filename}.npy')
return text, audio
audio, _ = load_wav(f'{self.path}/wavs/{filename}.wav')
if self.audio_transforms:
audio = self.audio_transforms(audio)
return text, audio
def __len__(self):
return len(self.metadata)
class WaveNetDataset(Dataset):
"""
loads spectrogram and raw audio pairs for testing wavnet
real spectrogramnet outputs much be cached and used in a separate dataset
"""
def __init__(self, path):
self.path = path
self.metadata = pd.read_csv(f'{path}/metadata.csv', sep='|',
names=['wav', 'transcription', 'text'],
usecols=['wav', 'text'])
self.metadata.dropna(inplace=True)
def __getitem__(self, index):
wav_filename = self.metadata.iloc[index]['wav']
audio, _ = load_wav(f'{self.path}/wavs/{wav_filename}.wav')
S = wav_to_spectrogram(audio)
return S, audio
def __len__(self):
return len(self.metadata)
def wav_collate(batch):
spec = [item[0] for item in batch]
audio = [item[1] for item in batch]
spec_lengths = [len(x) for x in spec]
audio_lengths = [len(x) for x in audio]
max_spec = max(spec_lengths)
max_audio = max(audio_lengths)
spec_batch = np.stack(pad2d(x, max_spec) for x in spec)
audio_batch = np.stack(pad1d(x, max_audio) for x in audio)
return (torch.FloatTensor(spec_batch).permute(0, 2, 1), # (batch, channel, time)
torch.FloatTensor(audio_batch),
spec_lengths, audio_lengths)
def collate_fn(batch):
"""
Pads Variable length sequence to size of longest sequence.
Args:
batch:
Returns: Padded sequences and original sizes.
"""
text = [item[0] for item in batch]
audio = [item[1] for item in batch]
text_lengths = [len(x) for x in text]
audio_lengths = [len(x) for x in audio]
max_text = max(text_lengths)
max_audio = max(audio_lengths)
text_batch = np.stack(pad1d(x, max_text) for x in text)
audio_batch = np.stack(pad2d(x, max_audio) for x in audio)
return (torch.LongTensor(text_batch),
torch.FloatTensor(audio_batch).permute(1, 0, 2),
text_lengths, audio_lengths)
def pad1d(seq, max_len):
return np.pad(seq, (0, max_len - len(seq)), mode='constant', constant_values=hp.padding_idx)
def pad2d(seq, max_len, dim=hp.num_mels, pad_value=hp.spectrogram_pad):
padded = np.zeros((max_len, dim)) + pad_value
padded[:len(seq), :] = seq
return padded
class RandomBatchSampler:
"""Yields of mini-batch of indices, sequential within the batch, random between batches.
Incomplete last batch will appear randomly with this setup.
Helpful for minimizing padding while retaining randomness with variable length inputs.
Args:
sampler (Sampler): Base sampler.
batch_size (int): Size of mini-batch.
Example:
>>> list(RandomBatchSampler(range(10), 3))
[[0, 1, 2], [6, 7, 8], [3, 4, 5], [9]]
"""
def __init__(self, sampler, batch_size):
self.sampler = sampler
self.batch_size = batch_size
self.random_batches = self.make_batches(sampler, batch_size)
def make_batches(self, sampler, batch_size):
indices = [i for i in sampler]
batches = [indices[i:i+batch_size]
for i in range(0, len(indices), batch_size)]
random_indices = torch.randperm(len(batches)).long()
return [batches[i] for i in random_indices]
def __iter__(self):
for batch in self.random_batches:
yield batch
def __len__(self):
return len(self.random_batches)