-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
133 lines (104 loc) · 3.36 KB
/
utils.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
import os
import random
from typing import List, Tuple
import torch
import torchaudio
import torch.nn.functional as F
import numpy as np
from torch import nn
from torch.utils.data import Subset
class Featurizer(nn.Module):
def __init__(self):
super(Featurizer, self).__init__()
self.featurizer = torchaudio.transforms.MelSpectrogram(
sample_rate=16_000,
n_fft=1024,
win_length=1024,
hop_length=256,
n_mels=64,
center=True
)
def forward(self, wav, length=None):
mel_spectrogram = self.featurizer(wav)
mel_spectrogram = mel_spectrogram.clamp(min=1e-5).log()
if length is not None:
length = (length - self.featurizer.win_length) // self.featurizer.hop_length
# We add `4` because in MelSpectrogram center==True
length += 1 + 4
return mel_spectrogram, length
return mel_spectrogram
class Collator:
def __call__(self, batch: List[Tuple[torch.Tensor, int]]):
wav_lengths = []
label_lengths = []
wavs, labels = zip(*batch)
for wav in wavs:
try:
wav_lengths.append(wav.size(-1))
except:
pass
for label in labels:
label_lengths.append(len(label))
max_wav_length = max(wav_lengths)
max_label_length = max(label_lengths)
batch_wavs = torch.cat(
list(
map(lambda x: F.pad(
x,
pad=(0, max_wav_length-x.size(-1)),
mode="constant",
value=0
), wavs)
)
)
batch_labels = torch.cat(
list(
map(lambda x: F.pad(
x.unsqueeze(0),
pad=(0, max_label_length-x.size(-1)),
mode="constant",
value=99
), labels)
)
)
batch_labels = batch_labels.long()
wav_lengths = torch.tensor(wav_lengths).long()
label_lengths = torch.tensor(label_lengths).long()
return {
"wav": batch_wavs,
"label": batch_labels,
"wav_lengths": wav_lengths,
"label_lengths": label_lengths
}
class AverageMeter(object):
"""
Computes and stores the average and current value
"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def train_val_splitter(dataset, ratio=0.9):
train_size = int(len(dataset)*ratio)
indexes = torch.randperm(len(dataset))
train_indexes = indexes[:train_size]
validation_indexes = indexes[train_size:]
train_dataset = Subset(dataset, train_indexes)
validation_dataset = Subset(dataset, validation_indexes)
return train_dataset, validation_dataset
def set_deterministic(seed=404, determenistic=True):
if determenistic:
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.determenistic = determenistic