-
Notifications
You must be signed in to change notification settings - Fork 108
/
meldataset.py
155 lines (125 loc) · 5.04 KB
/
meldataset.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
#coding: utf-8
import os
import time
import random
import random
import torch
import torchaudio
import numpy as np
import soundfile as sf
import torch.nn.functional as F
from torch import nn
from torch.utils.data import DataLoader
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
np.random.seed(1)
random.seed(1)
SPECT_PARAMS = {
"n_fft": 2048,
"win_length": 1200,
"hop_length": 300
}
MEL_PARAMS = {
"n_mels": 80,
"n_fft": 2048,
"win_length": 1200,
"hop_length": 300
}
class MelDataset(torch.utils.data.Dataset):
def __init__(self,
data_list,
sr=24000,
validation=False,
):
_data_list = [l[:-1].split('|') for l in data_list]
self.data_list = [(path, int(label)) for path, label in _data_list]
self.data_list_per_class = {
target: [(path, label) for path, label in self.data_list if label == target] \
for target in list(set([label for _, label in self.data_list]))}
self.sr = sr
self.to_melspec = torchaudio.transforms.MelSpectrogram(**MEL_PARAMS)
self.mean, self.std = -4, 4
self.validation = validation
self.max_mel_length = 192
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
data = self.data_list[idx]
mel_tensor, label = self._load_data(data)
ref_data = random.choice(self.data_list)
ref_mel_tensor, ref_label = self._load_data(ref_data)
ref2_data = random.choice(self.data_list_per_class[ref_label])
ref2_mel_tensor, _ = self._load_data(ref2_data)
return mel_tensor, label, ref_mel_tensor, ref2_mel_tensor, ref_label
def _load_data(self, path):
wave_tensor, label = self._load_tensor(path)
if not self.validation: # random scale for robustness
random_scale = 0.5 + 0.5 * np.random.random()
wave_tensor = random_scale * wave_tensor
mel_tensor = self.to_melspec(wave_tensor)
mel_tensor = (torch.log(1e-5 + mel_tensor) - self.mean) / self.std
mel_length = mel_tensor.size(1)
if mel_length > self.max_mel_length:
random_start = np.random.randint(0, mel_length - self.max_mel_length)
mel_tensor = mel_tensor[:, random_start:random_start + self.max_mel_length]
return mel_tensor, label
def _preprocess(self, wave_tensor, ):
mel_tensor = self.to_melspec(wave_tensor)
mel_tensor = (torch.log(1e-5 + mel_tensor) - self.mean) / self.std
return mel_tensor
def _load_tensor(self, data):
wave_path, label = data
label = int(label)
wave, sr = sf.read(wave_path)
wave_tensor = torch.from_numpy(wave).float()
return wave_tensor, label
class Collater(object):
"""
Args:
adaptive_batch_size (bool): if true, decrease batch size when long data comes.
"""
def __init__(self, return_wave=False):
self.text_pad_index = 0
self.return_wave = return_wave
self.max_mel_length = 192
self.mel_length_step = 16
self.latent_dim = 16
def __call__(self, batch):
batch_size = len(batch)
nmels = batch[0][0].size(0)
mels = torch.zeros((batch_size, nmels, self.max_mel_length)).float()
labels = torch.zeros((batch_size)).long()
ref_mels = torch.zeros((batch_size, nmels, self.max_mel_length)).float()
ref2_mels = torch.zeros((batch_size, nmels, self.max_mel_length)).float()
ref_labels = torch.zeros((batch_size)).long()
for bid, (mel, label, ref_mel, ref2_mel, ref_label) in enumerate(batch):
mel_size = mel.size(1)
mels[bid, :, :mel_size] = mel
ref_mel_size = ref_mel.size(1)
ref_mels[bid, :, :ref_mel_size] = ref_mel
ref2_mel_size = ref2_mel.size(1)
ref2_mels[bid, :, :ref2_mel_size] = ref2_mel
labels[bid] = label
ref_labels[bid] = ref_label
z_trg = torch.randn(batch_size, self.latent_dim)
z_trg2 = torch.randn(batch_size, self.latent_dim)
mels, ref_mels, ref2_mels = mels.unsqueeze(1), ref_mels.unsqueeze(1), ref2_mels.unsqueeze(1)
return mels, labels, ref_mels, ref2_mels, ref_labels, z_trg, z_trg2
def build_dataloader(path_list,
validation=False,
batch_size=4,
num_workers=1,
device='cpu',
collate_config={},
dataset_config={}):
dataset = MelDataset(path_list, validation=validation)
collate_fn = Collater(**collate_config)
data_loader = DataLoader(dataset,
batch_size=batch_size,
shuffle=(not validation),
num_workers=num_workers,
drop_last=True,
collate_fn=collate_fn,
pin_memory=(device != 'cpu'))
return data_loader