-
Notifications
You must be signed in to change notification settings - Fork 18
/
data.py
172 lines (141 loc) · 6.44 KB
/
data.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import json
import os
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset
import librosa
import random
import soundfile as sf
from config import win_size, win_shift, fft_num, dataset_path, chunk_length
class To_Tensor(object):
def __call__(self, x, type):
if type == 'float':
return torch.FloatTensor(x)
elif type == 'int':
return torch.IntTensor(x)
class TrainDataset(Dataset):
def __init__(self, json_dir, batch_size):
self.json_dir = json_dir
self.batch_size = batch_size
json_pos = os.path.join(json_dir, 'train', 'files.json')
with open(json_pos, 'r') as f:
json_list = json.load(f)
minibatch = []
start = 0
while True:
end = min(len(json_list), start+ batch_size)
minibatch.append(json_list[start:end])
start = end
if end == len(json_list):
break
self.minibatch = minibatch
def __len__(self):
return len(self.minibatch)
def __getitem__(self, index):
return self.minibatch[index]
class CvDataset(Dataset):
def __init__(self, json_dir, batch_size):
self.json_dir = json_dir
self.batch_size = batch_size
json_pos = os.path.join(json_dir, 'dev', 'files.json')
with open(json_pos, 'r') as f:
json_list = json.load(f)
minibatch = []
start = 0
while True:
end = min(len(json_list), start+ batch_size)
minibatch.append(json_list[start:end])
start = end
if end == len(json_list):
break
self.minibatch = minibatch
def __len__(self):
return len(self.minibatch)
def __getitem__(self, index):
return self.minibatch[index]
class TrainDataLoader(object):
def __init__(self, data_set, batch_size, num_workers=0):
self.data_loader = DataLoader(dataset=data_set,
batch_size=batch_size,
shuffle=1,
num_workers=num_workers,
collate_fn=self.collate_fn)
@staticmethod
def collate_fn(batch):
feats, labels, frame_mask_list = generate_feats_labels(batch)
return BatchInfo(feats, labels, frame_mask_list)
def get_data_loader(self):
return self.data_loader
def generate_feats_labels(batch):
batch = batch[0]
feat_list, label_list, frame_mask_list = [], [], []
to_tensor = To_Tensor()
for id in range(len(batch)):
clean_file_name = '%s_%s.wav' % (batch[id].split('_')[0], batch[id].split('_')[1])
mix_file_name = '%s.wav' % (batch[id])
feat_wav, _ = sf.read(os.path.join(dataset_path, 'train', 'mix', mix_file_name))
label_wav, _ = sf.read(os.path.join(dataset_path, 'train', 'clean', clean_file_name))
c = np.sqrt(len(feat_wav) / np.sum(feat_wav ** 2.0))
feat_wav = feat_wav * c
label_wav = label_wav * c
if len(feat_wav) > chunk_length:
wav_start = random.randint(0, len(feat_wav) - chunk_length)
feat_wav = feat_wav[wav_start:wav_start + chunk_length]
label_wav = label_wav[wav_start:wav_start + chunk_length]
# Note that centre setting is given for librosa-based fft for default, so fft_num is added
frame_num = (len(feat_wav) - win_size + fft_num) // win_shift + 1
frame_mask_list.append(frame_num)
feat_x = np.abs(librosa.stft(feat_wav, n_fft=fft_num, hop_length=win_shift, window='hanning').T)
label_x = np.abs(librosa.stft(label_wav, n_fft=fft_num, hop_length=win_shift, window='hanning').T)
feat_x, label_x = to_tensor(feat_x, 'float'), to_tensor(label_x, 'float')
feat_list.append(feat_x)
label_list.append(label_x)
feat_list = nn.utils.rnn.pad_sequence(feat_list, batch_first=True)
label_list = nn.utils.rnn.pad_sequence(label_list, batch_first=True)
return feat_list, label_list, frame_mask_list
def cv_generate_feats_labels(batch):
batch = batch[0]
feat_list, label_list, frame_mask_list = [], [], []
to_tensor = To_Tensor()
for id in range(len(batch)):
clean_file_name = '%s_%s.wav' % (batch[id].split('_')[0], batch[id].split('_')[1])
mix_file_name = '%s.wav' % (batch[id])
feat_wav, _ = sf.read(os.path.join(dataset_path, 'dev', 'mix', mix_file_name))
label_wav, _ = sf.read(os.path.join(dataset_path, 'dev', 'clean', clean_file_name))
c = np.sqrt(len(feat_wav) / np.sum(feat_wav ** 2.0))
feat_wav = feat_wav * c
label_wav = label_wav * c
if len(feat_wav) > chunk_length:
wav_start = random.randint(0, len(feat_wav) - chunk_length)
feat_wav = feat_wav[wav_start:wav_start + chunk_length]
label_wav = label_wav[wav_start:wav_start + chunk_length]
# Note that centre setting is given for librosa-based fft for default, so fft_num is added
frame_num = (len(feat_wav) - win_size + fft_num) // win_shift + 1
frame_mask_list.append(frame_num)
feat_x = np.abs(librosa.stft(feat_wav, n_fft=fft_num, hop_length=win_shift, window='hanning').T)
label_x = np.abs(librosa.stft(label_wav, n_fft=fft_num, hop_length=win_shift, window='hanning').T)
feat_x, label_x = to_tensor(feat_x, 'float'), to_tensor(label_x, 'float')
feat_list.append(feat_x)
label_list.append(label_x)
feat_list = nn.utils.rnn.pad_sequence(feat_list, batch_first=True)
label_list = nn.utils.rnn.pad_sequence(label_list, batch_first=True)
return feat_list, label_list, frame_mask_list
class CvDataLoader(object):
def __init__(self, data_set, batch_size, num_workers=0):
self.data_loader = DataLoader(dataset=data_set,
batch_size=batch_size,
shuffle=1,
num_workers=num_workers,
collate_fn=self.collate_fn)
@staticmethod
def collate_fn(batch):
feats, labels, frame_mask_list = cv_generate_feats_labels(batch)
return BatchInfo(feats, labels, frame_mask_list)
def get_data_loader(self):
return self.data_loader
class BatchInfo(object):
def __init__(self, feats, labels, frame_mask_list):
self.feats = feats
self.labels = labels
self.frame_mask_list = frame_mask_list