-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessing.py
103 lines (85 loc) · 3.92 KB
/
preprocessing.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
# MIT License
#
# Copyright (c) 2022 Tada Makepeace
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""This module contains utility functions for preprocessing the speech
recognition datasets."""
import jiwer
import torchaudio
import torch.nn as nn
transformation = jiwer.Compose(\
[jiwer.RemovePunctuation(), jiwer.ToLowerCase()])
# NOTE: Hyperparameters are set to match the transduction model
train_audio_transforms = nn.Sequential(
torchaudio.transforms.MelSpectrogram(
sample_rate=16_000,
n_mels=128,
hop_length=160,
win_length=432,
n_fft=512,
center=False),
torchaudio.transforms.FrequencyMasking(freq_mask_param=15),
torchaudio.transforms.TimeMasking(time_mask_param=35))
valid_audio_transforms = torchaudio.transforms.MelSpectrogram()
def data_processing(data, encoder, data_type="train"):
"""Function used to pre-process individual utterances from a ground truth
audio dataset. Also supports collecting multiple mel spectrograms
and padding them for training in a recurrent neural network."""
spectrograms = []
labels = []
input_lengths = []
label_lengths = []
for cur in data:
waveform, _, utterance, dataset_type = cur
if data_type == 'train':
spec = train_audio_transforms(waveform).squeeze(0).transpose(0, 1)
elif data_type == "valid":
spec = valid_audio_transforms(waveform).squeeze(0).transpose(0, 1)
else:
raise Exception('data_type should be train or valid')
spectrograms.append(spec)
label = transformation(utterance)
label = encoder.batch_encode(utterance.lower())
labels.append(label)
input_lengths.append(spec.shape[0]//2)
label_lengths.append(len(label))
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)
return spectrograms, labels, input_lengths, label_lengths
def data_processing_preds(data, encoder):
"""Function used to pre-process individual utterances from a dataset
made from predicted mel spectrograms from the transduction model.
Also supports collecting multiple mel spectrograms and padding them
for training in a recurrent neural network."""
spectrograms = []
labels = []
input_lengths = []
label_lengths = []
for cur in data:
mel_spectrogram, utterance, _ = cur
spectrograms.append(mel_spectrogram)
label = transformation(utterance)
label = encoder.batch_encode(utterance.lower())
labels.append(label)
input_lengths.append(mel_spectrogram.shape[0]//2)
label_lengths.append(len(label))
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)
return spectrograms, labels, input_lengths, label_lengths