forked from YiwenShaoStephen/pychain_example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
206 lines (176 loc) · 6.95 KB
/
dataset.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Copyright (c) Yiwen Shao
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import re
from collections import OrderedDict
import json
from io import BytesIO
import librosa
from subprocess import run, PIPE
import torchaudio
import torch
import simplefst
import kaldi_io
from tqdm import tqdm
import numpy as np
import torch.utils.data as data
from torch.utils.data import DataLoader
from torch.utils.data.sampler import Sampler
from pychain import ChainGraph, ChainGraphBatch
def parse_rxfile(file):
# separate offset from filename
if re.search(':[0-9]+$', file):
(file, offset) = file.rsplit(':', 1)
return file, int(offset)
def _collate_fn_train(batch):
# sort the batch by its feature length in a descending order
batch = sorted(
batch, key=lambda sample: sample[1], reverse=True)
max_seqlength = batch[0][1]
feat_dim = batch[0][0].size(1)
minibatch_size = len(batch)
feats = torch.zeros(minibatch_size, max_seqlength, feat_dim)
feat_lengths = torch.zeros(minibatch_size, dtype=torch.int)
graph_list = []
utt_ids = []
max_num_transitions = 0
max_num_states = 0
for i in range(minibatch_size):
feat, length, utt_id, graph = batch[i]
feats[i, :length, :].copy_(feat)
utt_ids.append(utt_id)
feat_lengths[i] = length
graph_list.append(graph)
if graph.num_transitions > max_num_transitions:
max_num_transitions = graph.num_transitions
if graph.num_states > max_num_states:
max_num_states = graph.num_states
num_graphs = ChainGraphBatch(
graph_list, max_num_transitions=max_num_transitions, max_num_states=max_num_states)
return feats, feat_lengths, utt_ids, num_graphs
def _collate_fn_test(batch):
# sort the batch by its feature length in a descending order
batch = sorted(
batch, key=lambda sample: sample[1], reverse=True)
max_seqlength = batch[0][1]
feat_dim = batch[0][0].size(1)
minibatch_size = len(batch)
feats = torch.zeros(minibatch_size, max_seqlength, feat_dim)
feat_lengths = torch.zeros(minibatch_size, dtype=torch.int)
utt_ids = []
for i in range(minibatch_size):
feat, length, utt_id = batch[i]
feats[i, :length, :].copy_(feat)
feat_lengths[i] = length
utt_ids.append(utt_id)
return feats, feat_lengths, utt_ids
class AudioDataLoader(DataLoader):
def __init__(self, *args, **kwargs):
"""
Creates a data loader for ChainDatasets.
"""
super(AudioDataLoader, self).__init__(*args, **kwargs)
if self.dataset.train:
self.collate_fn = _collate_fn_train
else:
self.collate_fn = _collate_fn_test
class BucketSampler(Sampler):
def __init__(self, data_source, batch_size=1):
"""
Samples batches assuming they are in order of size to batch similarly sized samples together.
codes from deepspeech.pytorch
https://github.com/SeanNaren/deepspeech.pytorch/blob/master/data/data_loader.py
"""
super(BucketSampler, self).__init__(data_source)
self.data_source = data_source
ids = list(range(0, len(data_source)))
self.bins = [ids[i:i + batch_size]
for i in range(0, len(ids), batch_size)]
def __iter__(self):
for ids in self.bins:
np.random.shuffle(ids)
yield ids
def __len__(self):
return len(self.bins)
def shuffle(self, epoch):
np.random.shuffle(self.bins)
class ChainDataset(data.Dataset):
def __init__(self, data_json_path, train=True, cache_graph=True, sort=True, no_feat=False):
super(ChainDataset, self).__init__()
self.train = train
self.cache_graph = cache_graph
self.sort = sort
self.no_feat = no_feat
self.samples = [] # list of dicts
with open(data_json_path, 'rb') as f:
loaded_json = json.load(f, object_pairs_hook=OrderedDict)
print("Initializing dataset...")
for utt_id, val in tqdm(loaded_json.items()):
sample = {}
sample['utt_id'] = utt_id
sample['wav'] = val['wav']
sample['text'] = val['text']
sample['duration'] = float(val['duration'])
if not self.no_feat:
sample['feat'] = val['feat']
sample['length'] = int(val['length'])
if self.train: # only training data has fst (graph)
fst_rxf = val['numerator_fst']
if self.cache_graph: # cache all fsts at once
filename, offset = parse_rxfile(fst_rxf)
fst = simplefst.StdVectorFst.read_ark(filename, offset)
graph = ChainGraph(fst, log_domain=True)
if graph.is_empty:
continue
sample['graph'] = graph
else:
sample['graph'] = fst_rxf
self.samples.append(sample)
if self.sort:
# sort the samples by their feature length
self.samples = sorted(
self.samples, key=lambda sample: sample['duration'])
def __getitem__(self, index):
sample = self.samples[index]
utt_id = sample['utt_id']
if not self.no_feat:
feat_ark = sample['feat']
feat = torch.from_numpy(kaldi_io.read_mat(feat_ark))
feat_length = sample['length']
else: # raw waveform
wav_file = sample['wav']
if len(wav_file.split()) > 1:
# wav_file is in command format
source = BytesIO(run(wav_file, shell=True, stdout=PIPE).stdout)
wav, sampling_rate = librosa.load(
source,
sr=None, # 'None' uses the native sampling rate
mono=False, # Retain multi-channel if it's there
)
wav = torch.from_numpy(wav).unsqueeze(0)
else:
wav, sampling_rate = torchaudio.load(wav_file)
feat = wav.transpose(0, 1)
feat_length = feat.size(0)
if self.train:
if self.cache_graph:
graph = sample['graph']
else:
fst_rxf = sample['graph']
filename, offset = parse_rxfile(fst_rxf)
fst = simplefst.StdVectorFst.read_ark(filename, offset)
graph = ChainGraph(fst)
return feat, feat_length, utt_id, graph
else:
utt_id = sample['utt_id']
return feat, feat_length, utt_id
def __len__(self):
return len(self.samples)
if __name__ == '__main__':
json_path = 'data/valid_monophone.json'
trainset = ChainDataset(json_path, no_feat=False)
trainloader = AudioDataLoader(trainset, batch_size=2, shuffle=False)
feat, feat_lengths, utt_ids, graphs = next(iter(trainloader))
print(feat.size())
print(feat_lengths)