Repository navigation
Expand file tree
/
Copy pathvqvae_dataloader.py
More file actions
148 lines (125 loc) · 4.68 KB
/
Copy pathvqvae_dataloader.py
File metadata and controls
148 lines (125 loc) · 4.68 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
#%%
import os
import random
import numpy as np
import math
import torch
import soundfile as sf
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import torchaudio
import time
#%%
class MyDataLoader(DataLoader):
def __init__(self, dataset, batch_size=1, num_workers=10):
self.bs = batch_size
self.n_samples = len(dataset)
self.num_workers = num_workers
super(self.__class__, self).__init__(batch_sampler=self.batch_gen(), num_workers=self.num_workers, dataset=dataset)
def __len__(self):
return math.floor(self.n_samples / self.bs)
def batch_gen(self):
for i in range(math.floor(self.n_samples / self.bs)):
ind = np.arange(self.bs).reshape(self.bs, 1) + i * self.bs
yield ind
class VQVAE_Dataset(Dataset):
def __init__(self, utt2data, sequence_size=22000*2, need_aug=False, shuffle=True, with_output=True):
self.utt2data = utt2data
self.dataset_size = len(self.utt2data)
self.shuffle = shuffle
self.need_aug = need_aug
self.sequence_size = sequence_size
self.with_output=with_output
if shuffle:
random.shuffle(self.utt2data)
def __len__(self):
return self.dataset_size
def read_feat(self, filename):
samples = self.sequence_size
try:
feat, sr = torchaudio.load(filename)
if feat.shape[1] == samples:
new_feat = feat
elif feat.shape[1] > samples:
start_point = random.randrange(0, feat.shape[1] - samples)
new_feat = np.array(feat[:,start_point:start_point + samples])
#print(new_feat.shape)
else:
new_feat = np.zeros((1, samples))
pad_beg = int((samples - feat.shape[1])/ 2)
new_feat[:,pad_beg:pad_beg + feat.shape[1]] = feat
#print(new_feat.shape)
assert new_feat.shape[1] == samples
return new_feat
except:
print(filename)
return 1
def __getitem__(self, sample_idx):
# try:
# idx = int(sample_idx[0])
# except:
# idx = sample_idx
idx = int(sample_idx[0])
assert 0 <= idx and idx < self.dataset_size, "invalid index"
utt, filename = self.utt2data[idx]
feat = self.read_feat(filename)
feat = feat.astype('float32')
#print(feat.shape)
if self.with_output:
return utt, feat, feat
else:
return utt, feat
class VQVAE_Mel_Dataset(VQVAE_Dataset):
def __init__(self, utt2data, sequence_size=int(256), need_aug=False, shuffle=True, with_output=True):
super().__init__(utt2data, sequence_size, need_aug, shuffle, with_output)
def read_feat(self, filename):
samples = self.sequence_size
try:
feat=np.load(filename)
if feat.shape[1] == samples:
new_feat = feat
elif feat.shape[1] > samples:
start_point = random.randrange(0, feat.shape[1] - samples)
new_feat = np.array(feat[:,start_point:start_point + samples])
#print(new_feat.shape)
else:
new_feat = np.zeros((feat.shape[0], samples))
pad_beg = int((samples - feat.shape[1])/ 2)
new_feat[:,pad_beg:pad_beg + feat.shape[1]] = feat
#print(new_feat.shape)
assert new_feat.shape[1] == samples
return torch.tensor(new_feat, dtype=torch.float)
except:
print(filename)
return 1
def __getitem__(self, sample_idx):
# try:
# idx = int(sample_idx[0])
# except:
# idx = sample_idx
idx = int(sample_idx[0])
assert 0 <= idx and idx < self.dataset_size, "invalid index"
utt, filename = self.utt2data[idx]
feat = self.read_feat(filename)
#print(feat.shape)
if self.with_output:
return utt, feat, feat
else:
return utt, feat
#%%
##############################
def test():
batch_size = 1
num_workers = 1
index_root_dir = './index'
utt2wav = [line.split() for line in open(os.path.join(index_root_dir, 'train.index'))]
dev_dataset = VQVAE_Mel_Dataset(utt2wav, need_aug=False, with_output=True, shuffle=False)
dear_dev_dataloader = MyDataLoader(dev_dataset, batch_size=batch_size, num_workers=1)
print("Data loader prepared...")
for utt, feat_in, feat_out in dear_dev_dataloader:
# print(utt)
print(feat_in.shape)
time.sleep(1)
if __name__ == "__main__":
test()
# %%