forked from dipjyoti92/StarGAN-Voice-Conversion-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
154 lines (128 loc) · 6.21 KB
/
data_loader.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
from torch.utils import data
import torch
import os
import random
import glob
from os.path import join, basename, dirname, split
import numpy as np
# Below is the accent info for the used 10 speakers.
#spk2acc = {'bdl': 'Male', #M
# 'clb': 'Female', #F
# 'slt': 'Female', #F
# 'rms': 'Male'}#M
min_length = 512 # Since we slice 512 frames from each utterance when training.
speakers = ['p225', 'p227', 'p228', 'p229', 'p230', 'p231', 'p232', 'p233', 'p234', 'p236', 'p237','p238', 'p239', 'p240', 'p241', 'p243', 'p244', 'p245', 'p246', 'p247', 'p248', 'p249', 'p250', 'p251', 'p252', 'p253', 'p254', 'p255', 'p256', 'p257', 'p258', 'p259', 'p260', 'p261', 'p262', 'p263', 'p264', 'p265', 'p266', 'p267', 'p268', 'p269', 'p270', 'p271', 'p272', 'p273', 'p274', 'p275', 'p276', 'p277', 'p278', 'p279', 'p280', 'p281', 'p282', 'p283', 'p284', 'p285', 'p286', 'p287', 'p288', 'p292', 'p293', 'p294', 'p295', 'p297', 'p298', 'p299', 'p300', 'p360'] # 70 speakers are choosen from VCTK dataset
spk2idx = dict(zip(speakers, range(len(speakers))))
def to_categorical(y, num_classes=None):
"""Converts a class vector (integers) to binary class matrix.
E.g. for use with categorical_crossentropy.
# Arguments
y: class vector to be converted into a matrix
(integers from 0 to num_classes).
num_classes: total number of classes.
# Returns
A binary matrix representation of the input. The classes axis
is placed last.
From Keras np_utils
"""
y = np.array(y, dtype='int')
input_shape = y.shape
if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
input_shape = tuple(input_shape[:-1])
y = y.ravel()
if not num_classes:
num_classes = np.max(y) + 1
n = y.shape[0]
categorical = np.zeros((n, num_classes), dtype=np.float32)
categorical[np.arange(n), y] = 1
output_shape = input_shape + (num_classes,)
categorical = np.reshape(categorical, output_shape)
return categorical
class MyDataset(data.Dataset):
"""Dataset for MCEP features and speaker labels."""
def __init__(self, data_dir):
mc_files = glob.glob(join(data_dir, '*.npy'))
mc_files = [i for i in mc_files if basename(i)[:4] in speakers]
self.mc_files = self.rm_too_short_utt(mc_files)
self.num_files = len(self.mc_files)
print("\t Number of training samples: ", self.num_files)
for f in self.mc_files:
mc = np.load(f)
if mc.shape[0] <= min_length:
print(f)
raise RuntimeError("The data may be corrupted! We need all MCEP features having more than {min_length} frames!")
def rm_too_short_utt(self, mc_files, min_length=min_length):
new_mc_files = []
for mcfile in mc_files:
mc = np.load(mcfile)
if mc.shape[0] > min_length:
new_mc_files.append(mcfile)
return new_mc_files
def sample_seg(self, feat, sample_len=min_length):
assert feat.shape[0] - sample_len >= 0
s = np.random.randint(0, feat.shape[0] - sample_len + 1)
return feat[s:s+sample_len, :]
def __len__(self):
return self.num_files
def __getitem__(self, index):
filename = self.mc_files[index]
spk = basename(filename).split('_')[0]
spk_idx = spk2idx[spk]
mc = np.load(filename)
mc = self.sample_seg(mc)
mc = np.transpose(mc, (1, 0)) # (T, D) -> (D, T), since pytorch need feature having shape
# to one-hot
spk_cat = np.squeeze(to_categorical([spk_idx], num_classes=len(speakers)))
return torch.FloatTensor(mc), torch.LongTensor([spk_idx]).squeeze_(), torch.FloatTensor(spk_cat)
class TestDataset(object):
"""Dataset for testing."""
def __init__(self, data_dir, wav_dir, src_spk='p225', trg_spk='p232'):
self.src_spk = src_spk
self.trg_spk = trg_spk
self.mc_files = sorted(glob.glob(join(data_dir, '{}*.npy'.format(self.src_spk))))
self.src_spk_stats = np.load(join(data_dir.replace('test', 'train'), '{}_stats.npz'.format(src_spk)))
self.trg_spk_stats = np.load(join(data_dir.replace('test', 'train'), '{}_stats.npz'.format(trg_spk)))
self.logf0s_mean_src = self.src_spk_stats['log_f0s_mean']
self.logf0s_std_src = self.src_spk_stats['log_f0s_std']
self.logf0s_mean_trg = self.trg_spk_stats['log_f0s_mean']
self.logf0s_std_trg = self.trg_spk_stats['log_f0s_std']
self.mcep_mean_src = self.src_spk_stats['coded_sps_mean']
self.mcep_std_src = self.src_spk_stats['coded_sps_std']
self.mcep_mean_trg = self.trg_spk_stats['coded_sps_mean']
self.mcep_std_trg = self.trg_spk_stats['coded_sps_std']
self.src_wav_dir = wav_dir+'/'+src_spk
self.spk_idx = spk2idx[trg_spk]
spk_cat = to_categorical([self.spk_idx], num_classes=len(speakers))
self.spk_c_trg = spk_cat
self.spk_idx_org = spk2idx[src_spk]
spk_cat_org = to_categorical([self.spk_idx_org], num_classes=len(speakers))
self.spk_c_org = spk_cat_org
def get_batch_test_data(self, batch_size=8):
batch_data = []
for i in range(batch_size):
mcfile = self.mc_files[i]
filename = basename(mcfile).split('-')[-1]
wavfile_path = join(self.src_wav_dir, filename.replace('npy', 'wav'))
batch_data.append(wavfile_path)
return batch_data
def get_loader(data_dir, batch_size=32, mode='train', num_workers=1):
dataset = MyDataset(data_dir)
data_loader = data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=(mode=='train'),
num_workers=num_workers,
drop_last=True)
return data_loader
if __name__ == '__main__':
loader = get_loader('./data/mc/train')
data_iter = iter(loader)
for i in range(10):
mc, spk_idx, acc_idx, spk_acc_cat = next(data_iter)
print('-'*50)
print(mc.size())
print(spk_idx.size())
print(acc_idx.size())
print(spk_acc_cat.size())
print(spk_idx.squeeze_())
print(spk_acc_cat)
print('-'*50)