-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdataset.py
More file actions
59 lines (39 loc) · 1.61 KB
/
Copy pathdataset.py
File metadata and controls
59 lines (39 loc) · 1.61 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
from config import Arguments as args
from utils.path import get_path
from utils.dataset import get_label_dictionary, normalize
import numpy as np
import torch
import glob
from torch.utils.data import dataset as dset
class SpeechDataset(dset.Dataset):
"""
class: Dataset
returns:
text: (Tx, Dim)
mel: (Ty, Dim)
explanation:
dataloader for training model
mem_mode (in config.py): mags cannot be loaded onto the memory due to the large # of features
"""
def __init__(self, mem_mode, meta_dir, dataset_name, mel_stat_path, max_frame_length=120):
self.__mel_file_paths = self.__get_mel_filename(meta_dir=meta_dir)
self.__label_dictionary = get_label_dictionary(dataset_name)
self.max_frame_length = max_frame_length
self.mel_mean, self.mel_std = np.load(mel_stat_path).astype(np.float)
if args.mem_mode:
self.__mels = list(map(lambda mel_file_path: torch.tensor(np.load(mel_file_path)), self.__mel_file_paths))
def __len__(self):
return len(self.__mel_file_paths)
def __getitem__(self, index):
mel = self.__mels[index] if args.mem_mode else np.load(self.__mel_file_paths[index])
T_mel, _ = mel.shape
while T_mel <= self.max_frame_length:
mel = torch.cat((mel, mel), dim=0)
T_mel, _ = mel.shape
index = np.random.randint(T_mel - self.max_frame_length + 1)
normalized_mel = normalize(mel[index: index + self.max_frame_length], mean=self.mel_mean, std=self.mel_std)
return normalized_mel, 0
def __get_mel_filename(self, meta_dir):
with open(meta_dir, "r") as f:
mel_file_paths = list(map(lambda filename : filename.rstrip(), f.readlines()))
return mel_file_paths