-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
56 lines (43 loc) · 1.39 KB
/
dataset.py
File metadata and controls
56 lines (43 loc) · 1.39 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
import os
import numpy as np
import torch
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
class MelDataset(Dataset):
def __init__(self, root_dir):
self.files = [os.path.join(root_dir, f)
for f in os.listdir(root_dir)
if f.endswith(".npy")]
assert len(self.files) > 0, f"No .npy files found in {root_dir}"
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
mel = np.load(self.files[idx]) # (n_mels, T)
mel = torch.from_numpy(mel).float()
mel = mel.unsqueeze(0)
if mel.shape[2] % 2 == 1:
mel = F.pad(mel, (0, 1), "constant", 0)
return mel
def collate_mel(batch):
lengths = torch.tensor([mel.shape[-1] for mel in batch], dtype=torch.long)
T_max = max(lengths).item()
padded_batch = []
for mel in batch:
pad_T = T_max - mel.shape[-1]
if pad_T > 0:
mel = torch.nn.functional.pad(mel, (0, pad_T))
padded_batch.append(mel)
padded_mels = torch.stack(padded_batch)
return padded_mels
def load_data(args):
dataset = MelDataset(args.dataset_path)
loader = DataLoader(
dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=8,
drop_last=True,
pin_memory=True,
collate_fn=collate_mel
)
return loader