-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata.py
106 lines (88 loc) · 3.46 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
""" Load and preprocess data.
"""
import torch
import torchaudio
import os
import pandas as pd
import numpy as np
import torch.nn.utils.rnn as rnn_utils
from torch.utils.data import Dataset, DataLoader
class ASR(Dataset):
"""
Stores a Pandas DataFrame in __init__, and reads and preprocesses examples in __getitem__.
"""
def __init__(self, split):
self.df = pd.read_csv('%s.csv' % split.upper())
self.tokenizer = torch.load('tokenizer.pth')
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
"""
Returns:
x (torch.FloatTensor, [seq_length, dim_features]): The FBANK features.
y (torch.LongTensor, [n_tokens]): The label sequence.
"""
x, y = self.df.iloc[idx]
x, _ = torchaudio.load(x)
# Compute filter bank features
x = torchaudio.compliance.kaldi.fbank(x, num_mel_bins=80) # [n_windows, 80]
# Stack every 3 frames and down-sample frame rate by 3, following https://arxiv.org/pdf/1712.01769.pdf.
x = x[:(x.shape[0]//3)*3].view(-1,3*80) # [n_windows, 80] --> [n_windows//3, 240]
# Tokenization
y = self.tokenizer.encode(y)
return x, y
def generateBatch(self, batch):
"""
Generate a mini-batch of data. For DataLoader's 'collate_fn'.
Args:
batch (list(tuple)): A mini-batch of (FBANK features, label sequences) pairs.
Returns:
xs (torch.FloatTensor, [batch_size, (padded) seq_length, dim_features]): A mini-batch of FBANK features.
xlens (torch.LongTensor, [batch_size]): Sequence lengths before padding.
ys (torch.LongTensor, [batch_size, (padded) n_tokens]): A mini-batch of label sequences.
"""
xs, ys = zip(*batch)
xlens = torch.tensor([x.shape[0] for x in xs])
xs = rnn_utils.pad_sequence(xs, batch_first=True) # [batch_size, (padded) seq_length, dim_features]
ys = rnn_utils.pad_sequence(ys, batch_first=True) # [batch_size, (padded) n_tokens]
return xs, xlens, ys
def load(split, batch_size, workers=0):
"""
Args:
split (string): Which of the subset of data to take. One of 'train', 'dev' or 'test'.
batch_size (integer): Batch size.
workers (integer): How many subprocesses to use for data loading.
Returns:
loader (DataLoader): A DataLoader can generate batches of (FBANK features, FBANK lengths, label sequence).
"""
assert split in ['train', 'dev', 'test']
dataset = ASR(split)
print ("%s set size:"%split.upper(), len(dataset))
loader = DataLoader(dataset,
batch_size=batch_size,
collate_fn=dataset.generateBatch,
shuffle=True,
num_workers=workers,
pin_memory=True)
return loader
def inspect_data():
"""
Test the functionality of input pipeline and visualize a few samples.
"""
import matplotlib.pyplot as plt
BATCH_SIZE = 64
SPLIT = 'dev'
loader = load(SPLIT, BATCH_SIZE)
tokenizer = torch.load('tokenizer.pth')
print ("Vocabulary size:", len(tokenizer.vocab))
print (tokenizer.vocab)
xs, xlens, ys = next(iter(loader))
print (xs.shape, ys.shape)
for i in range(BATCH_SIZE):
print (ys[i])
print (tokenizer.decode(ys[i]))
plt.figure()
plt.imshow(xs[i].T)
plt.show()
if __name__ == '__main__':
inspect_data()