-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript.py
More file actions
160 lines (129 loc) · 5.55 KB
/
transcript.py
File metadata and controls
160 lines (129 loc) · 5.55 KB
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
import torch
import torchaudio
import torch.nn as nn
import pandas as pd
import numpy as np
from utils import TextProcess
# NOTE: add time stretch
class SpecAugment(nn.Module):
def __init__(self, rate, policy=3, freq_mask=15, time_mask=35):
super(SpecAugment, self).__init__()
self.rate = rate
self.specaug = nn.Sequential(
torchaudio.transforms.FrequencyMasking(freq_mask_param=freq_mask),
torchaudio.transforms.TimeMasking(time_mask_param=time_mask)
)
self.specaug2 = nn.Sequential(
torchaudio.transforms.FrequencyMasking(freq_mask_param=freq_mask),
torchaudio.transforms.TimeMasking(time_mask_param=time_mask),
torchaudio.transforms.FrequencyMasking(freq_mask_param=freq_mask),
torchaudio.transforms.TimeMasking(time_mask_param=time_mask)
)
policies = { 1: self.policy1, 2: self.policy2, 3: self.policy3 }
self._forward = policies[policy]
def forward(self, x):
return self._forward(x)
def policy1(self, x):
probability = torch.rand(1, 1).item()
if self.rate > probability:
return self.specaug(x)
return x
def policy2(self, x):
probability = torch.rand(1, 1).item()
if self.rate > probability:
return self.specaug2(x)
return x
def policy3(self, x):
probability = torch.rand(1, 1).item()
if probability > 0.5:
return self.policy1(x)
return self.policy2(x)
class LogMelSpec(nn.Module):
def __init__(self, sample_rate=8000, n_mels=128, win_length=160, hop_length=80):
super(LogMelSpec, self).__init__()
self.transform = torchaudio.transforms.MelSpectrogram(
sample_rate=sample_rate, n_mels=n_mels,
win_length=win_length, hop_length=hop_length)
def forward(self, x):
x = self.transform(x) # mel spectrogram
x = np.log(x + 1e-14) # logrithmic, add small value to avoid inf
return x
def get_featurizer(sample_rate, n_feats=81):
return LogMelSpec(sample_rate=sample_rate, n_mels=n_feats, win_length=160, hop_length=80)
class Data(torch.utils.data.Dataset):
# this makes it easier to be ovveride in argparse
parameters = {
"sample_rate": 8000, "n_feats": 81,
"specaug_rate": 0.5, "specaug_policy": 3,
"time_mask": 70, "freq_mask": 15
}
def __init__(self, json_path, sample_rate, n_feats, specaug_rate, specaug_policy,
time_mask, freq_mask, valid=False, shuffle=True, text_to_int=True, log_ex=True):
self.log_ex = log_ex
self.text_process = TextProcess()
print("Loading data json file from", json_path)
self.data = pd.read_json(json_path, lines=True)
if valid:
self.audio_transforms = torch.nn.Sequential(
LogMelSpec(sample_rate=sample_rate, n_mels=n_feats, win_length=160, hop_length=80)
)
else:
self.audio_transforms = torch.nn.Sequential(
LogMelSpec(sample_rate=sample_rate, n_mels=n_feats, win_length=160, hop_length=80),
SpecAugment(specaug_rate, specaug_policy, freq_mask, time_mask)
)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.item()
try:
file_path = self.data.key.iloc[idx]
waveform, _ = torchaudio.load(file_path)
label = self.text_process.text_to_int_sequence(self.data['text'].iloc[idx])
spectrogram = self.audio_transforms(waveform) # (channel, feature, time)
spec_len = spectrogram.shape[-1] // 2
label_len = len(label)
if spec_len < label_len:
raise Exception('spectrogram len is bigger then label len')
if spectrogram.shape[0] > 1:
raise Exception('dual channel, skipping audio file %s'%file_path)
if spectrogram.shape[2] > 1650:
raise Exception('spectrogram to big. size %s'%spectrogram.shape[2])
if label_len == 0:
raise Exception('label len is zero... skipping %s'%file_path)
except Exception as e:
if self.log_ex:
print(str(e), file_path)
return self.__getitem__(idx - 1 if idx != 0 else idx + 1)
return spectrogram, label, spec_len, label_len
def describe(self):
return self.data.describe()
def collate_fn_padd(data):
'''
Padds batch of variable length
note: it converts things ToTensor manually here since the ToTensor transform
assume it takes in images rather than arbitrary tensors.
'''
# print(data)
spectrograms = []
labels = []
input_lengths = []
label_lengths = []
for (spectrogram, label, input_length, label_length) in data:
if spectrogram is None:
continue
# print(spectrogram.shape)
spectrograms.append(spectrogram.squeeze(0).transpose(0, 1))
labels.append(torch.Tensor(label))
input_lengths.append(input_length)
label_lengths.append(label_length)
spectrograms = nn.utils.rnn.pad_sequence(spectrograms, batch_first=True).unsqueeze(1).transpose(2, 3)
labels = nn.utils.rnn.pad_sequence(labels, batch_first=True)
input_lengths = input_lengths
# print(spectrograms.shape)
label_lengths = label_lengths
# ## compute mask
# mask = (batch != 0).cuda(gpu)
# return batch, lengths, mask
return spectrograms, labels, input_lengths, label_lengths