-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
144 lines (123 loc) · 5.49 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
import json
import os
import h5py
import math
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset
import random
import soundfile as sf
from config import *
EPSILON = 1e-20
class To_Tensor(object):
def __call__(self, x, type='float'):
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, **kw):
self.data_loader = DataLoader(dataset=data_set,
shuffle=1,
collate_fn=self.collate_fn,
**kw)
@staticmethod
def collate_fn(batch):
feats, labels, noises, frame_mask_list = generate_feats_labels(batch, 'train')
return BatchInfo(feats, labels, noises, frame_mask_list)
def get_data_loader(self):
return self.data_loader
class CvDataLoader(object):
def __init__(self, data_set, **kw):
self.data_loader = DataLoader(dataset=data_set,
shuffle=1,
collate_fn=self.collate_fn,
**kw)
@staticmethod
def collate_fn(batch):
feats, labels, noises, frame_mask_list = generate_feats_labels(batch, 'dev')
return BatchInfo(feats, labels, noises, frame_mask_list)
def get_data_loader(self):
return self.data_loader
def generate_feats_labels(batch, data_type):
batch = batch[0]
feat_list, label_list, noise_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(file_path, data_type, 'mix', mix_file_name))
label_wav, _ = sf.read(os.path.join(file_path, data_type, 'clean', clean_file_name))
noise_wav = feat_wav - label_wav
if is_scale: # as c is a global coefficient, when is_scale is True, the system is strictly non-causal
c = np.sqrt(len(feat_wav) / np.sum(feat_wav ** 2.0))
feat_wav, label_wav, noise_wav = to_tensor(feat_wav * c), to_tensor(label_wav * c), to_tensor(noise_wav * c)
else:
feat_wav, label_wav, noise_wav = to_tensor(feat_wav), to_tensor(label_wav), to_tensor(noise_wav)
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]
noise_wav = noise_wav[wav_start:wav_start + chunk_length]
frame_num = (len(feat_wav) - win_size + fft_num) // win_shift + 1
frame_mask_list.append(frame_num)
feat_list.append(feat_wav)
label_list.append(label_wav)
noise_list.append(noise_wav)
feat_list = nn.utils.rnn.pad_sequence(feat_list, batch_first=True)
label_list = nn.utils.rnn.pad_sequence(label_list, batch_first=True)
noise_list = nn.utils.rnn.pad_sequence(noise_list, batch_first=True)
feat_list = torch.stft(feat_list, n_fft=fft_num, hop_length=win_shift, win_length=win_size,
window=torch.hann_window(fft_num)).permute(0,3,2,1)
label_list = torch.stft(label_list, n_fft=fft_num, hop_length=win_shift, win_length=win_size,
window=torch.hann_window(fft_num)).permute(0,3,2,1)
noise_list = torch.stft(noise_list, n_fft=fft_num, hop_length=win_shift, win_length=win_size,
window=torch.hann_window(fft_num)).permute(0,3,2,1)
return feat_list, label_list, noise_list, frame_mask_list
class BatchInfo(object):
def __init__(self, feats, labels, noises, frame_mask_list):
self.feats = feats
self.labels = labels
self.noises = noises
self.frame_mask_list = frame_mask_list