-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
164 lines (126 loc) · 4.71 KB
/
utils.py
File metadata and controls
164 lines (126 loc) · 4.71 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
161
162
163
164
import torch
import torch.nn as nn
from torch import Tensor
from torchaudio import functional as F
from torch.utils.data import Dataset
import librosa
import torchaudio.transforms as T
import numpy as np
class Compose:
'''
Data augmentation module that transforms any given data
example with a chain of audio augmentations.
'''
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, x):
x = self.transform(x)
return x
def __repr__(self):
format_string = self.__class__.__name__ + "("
for t in self.transforms:
format_string += "\n"
format_string += "\t{0}".format(t)
format_string += "\n)"
return format_string
def transform(self, x):
for t in self.transforms:
x = t(x)
return x
class DataFromSubset(Dataset):
def __init__(self, subset, transform=None):
self.subset = subset
self.transform = transform
def __len__(self):
return len(self.subset)
def __getitem__(self, index):
x, y = self.subset[index]
if self.transform:
x = self.transform(x)
return x, y
""" class SelectSamples(Dataset):
def __init__(self, subset, samples=None, transform=None):
'''
samples must be a list of labels which some transformation
will be applied
'''
self.subset = subset
self.transform = transform
def __len__(self):
return len(self.subset)
def __getitem__(self, idx):
x, y = self.subset[idx]
if y in samples:
x = self.transform(x)
else:
pass
return x, y """
class SpecPermute(torch.nn.Module):
def __init__(self):
super(SpecPermute, self).__init__()
def forward(self, x):
return torch.permute(x, (0, 2, 1))
class Permute(torch.nn.Module):
def __init__(self):
super(Permute, self).__init__()
def forward(self, x):
return torch.permute(x, (1, 0, 2))
class addChannel(object):
def __init__(self):
self.channels
def __cal__(self,x):
x = x.unsqueeze(dim=0)
return x
def __repr__(self):
return print("Add two channel in a gray scaled imaged")
# extracting mel features
class Mel(object):
def __init__(self, sr = 22050, n_mels=96):
self.sr = sr
self.n_mels = n_mels
super(Mel, self).__init__()
def __cal__(self,x):
return librosa.feature.melspectrogram(x, sr=self.sr, n_mels=self.n_mels, fmax=8000)
def __repr__(self):
return print("Mel Features")
# decibel scale
class dbScale(object):
def __init__(self):
super(dbScale, self).__init__()
def __cal__(self,x):
return librosa.power_to_db(x,)
def __repr__(self):
return print("Decibel Scale")
# extracting mfcc features
class MFCC(object):
def __init__(self, sr=22050, n_mfcc=96):
self.sr = sr
self.n_mfcc = n_mfcc
def __call__(self, x):
x = librosa.feature.mfcc(x, sr=self.sr, n_mfcc=self.n_mfcc, hop_length=512, htk=True)
x = torch.from_numpy(x)
return x.unsqueeze(dim=0)
def __repr__(self):
return self.__class__.__name__ + '()'
# extracting mfcc features
class MakeImage(object):
def __init__(self, sr=22050, n_bins=96):
self.sr = sr
self.n_bins = n_bins
def __call__(self, x):
mfcc = librosa.power_to_db(librosa.feature.mfcc(x, sr=self.sr, n_mfcc=self.n_bins, hop_length=512, htk=True),ref=np.max)
mel = librosa.power_to_db(librosa.feature.melspectrogram(x, sr=self.sr, n_mels=self.n_bins),ref=np.max)
cqt_abs = np.abs(librosa.cqt(x, sr=self.sr, hop_length=512, n_bins=self.n_bins))
cqt = librosa.amplitude_to_db(cqt_abs,ref=np.max)
image = np.stack((mfcc, mel, cqt), axis=0)
return torch.from_numpy(image)
def __repr__(self):
return self.__class__.__name__ + '()'
def mel_layer(n_mels=96, sample_rate=22050, f_min=0, f_max=8000, n_fft=2048, win_length=2048, hop_length=512):
"""extract Mel Spectrogram features"""
return T.MelSpectrogram(sample_rate=sample_rate,n_fft=n_fft,win_length=win_length,hop_length=hop_length,
center=True,pad_mode="reflect",power=2.0,norm="slaney",onesided=True,n_mels=n_mels,mel_scale="htk",)
def mfcc_layer(n_mfcc=96, n_mels=96, sample_rate=22050, f_min=0, f_max=8000, n_fft=2048, win_length=2048, hop_length=512):
"""Extract MFCC features"""
return T.MFCC(sample_rate=sample_rate,n_mfcc=n_mfcc,melkwargs={"n_fft": n_fft,"n_mels": n_mels,"hop_length": hop_length,
"mel_scale": "htk",},)