-
Notifications
You must be signed in to change notification settings - Fork 6
/
augmentations.py
362 lines (305 loc) · 12.4 KB
/
augmentations.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright 2023 by Ismail Khalfaoui-Hassani, ANITI Toulouse.
#
# All rights reserved.
#
# This file is part of the ConvNeXt-Dcls-Audio package, and
# is released under the "MIT License Agreement".
# Please see the LICENSE file that should have been included as part
# of this package.
import random
from typing import Any, List, Tuple, Union
import torch
from torch import nn, Tensor
from torch.distributions import Uniform
from torch.nn import functional as F
from torchaudio.transforms import Resample as TorchAudioResample
class Crop(nn.Module):
def __init__(
self, target_length: int, align: str = "left", dim: int = -1, p: float = 1.0
) -> None:
super().__init__()
self.target_length = target_length
self.align = align
self.dim = dim
self.p = p
def extra_repr(self) -> str:
return (
f"target_length={self.target_length}, "
f"align={self.align}, "
f"dim={self.dim}"
)
def forward(self, x):
if self.p >= 1.0 or random.random() <= self.p:
return self.process(x)
else:
return x
def process(self, data: Tensor) -> Tensor:
if self.align == "center":
return self.crop_align_center(data)
elif self.align == "left":
return self.crop_align_left(data)
elif self.align == "random":
return self.crop_align_random(data)
elif self.align == "right":
return self.crop_align_right(data)
else:
raise ValueError(
f'Unknown alignment "{self.align}". Must be one of {str(["left", "right", "center", "random"])}.'
)
def crop_align_center(self, data: Tensor) -> Tensor:
if data.shape[self.dim] > self.target_length:
diff = data.shape[self.dim] - self.target_length
start = diff // 2 + diff % 2
end = start + self.target_length
slices = [slice(None)] * len(data.shape)
slices[self.dim] = slice(start, end)
data = data[slices]
data = data.contiguous()
return data
def crop_align_left(self, data: Tensor) -> Tensor:
if data.shape[self.dim] > self.target_length:
slices = [slice(None)] * len(data.shape)
slices[self.dim] = slice(self.target_length)
data = data[slices]
data = data.contiguous()
return data
def crop_align_random(self, data: Tensor) -> Tensor:
if data.shape[self.dim] > self.target_length:
diff = data.shape[self.dim] - self.target_length
start = torch.randint(low=0, high=diff, size=()).item()
end = start + self.target_length
slices = [slice(None)] * len(data.shape)
slices[self.dim] = slice(start, end)
data = data[slices]
data = data.contiguous()
return data
def crop_align_right(self, data: Tensor) -> Tensor:
if data.shape[self.dim] > self.target_length:
start = data.shape[self.dim] - self.target_length
slices = [slice(None)] * len(data.shape)
slices[self.dim] = slice(start, None)
data = data[slices]
data = data.contiguous()
return data
class Pad(nn.Module):
def __init__(
self,
target_length: int,
align: str = "left",
fill_value: float = 0.0,
dim: int = -1,
mode: str = "constant",
p: float = 1.0,
) -> None:
"""
Example :
>>> import torch; from torch import tensor
>>> x = torch.ones(6)
>>> zero_pad = F.pad(10, align='left')
>>> x_pad = zero_pad(x)
... tensor([1, 1, 1, 1, 1, 1, 0, 0, 0, 0])
:param target_length: The target length of the dimension.
:param align: The alignment type. Can be 'left', 'right', 'center' or 'random'. (default: 'left')
:param fill_value: The fill value used for constant padding. (default: 0.0)
:param dim: The dimension to pad. (default: -1)
:param mode: The padding mode. Can be 'constant', 'reflect', 'replicate' or 'circular'. (default: 'constant')
:param p: The probability to apply the transform. (default: 1.0)
"""
super().__init__()
self.target_length = target_length
self.align = align
self.fill_value = fill_value
self.dim = dim
self.mode = mode
self.p = p
def extra_repr(self) -> str:
return (
f"target_length={self.target_length}, "
f"align={self.align}, "
f"fill_value={self.fill_value}, "
f"dim={self.dim}, "
f"mode={self.mode}"
)
def forward(self, x):
if self.p >= 1.0 or random.random() <= self.p:
return self.process(x)
else:
return x
def process(self, data: Tensor) -> Tensor:
if self.align == "left":
return self.pad_align_left(data)
elif self.align == "right":
return self.pad_align_right(data)
elif self.align == "center":
return self.pad_align_center(data)
elif self.align == "random":
return self.pad_align_random(data)
else:
raise ValueError(
f'Unknown alignment "{self.align}". Must be one of {str(["left", "right", "center", "random"])}.'
)
def pad_align_left(self, x: Tensor) -> Tensor:
# Note: pad_seq : [pad_left_dim_-1, pad_right_dim_-1, pad_left_dim_-2, pad_right_dim_-2, ...)
idx = len(x.shape) - (self.dim % len(x.shape)) - 1
pad_seq = [0 for _ in range(len(x.shape) * 2)]
missing = max(self.target_length - x.shape[self.dim], 0)
pad_seq[idx * 2 + 1] = missing
x = F.pad(x, pad_seq, mode=self.mode, value=self.fill_value)
return x
def pad_align_right(self, x: Tensor) -> Tensor:
idx = len(x.shape) - (self.dim % len(x.shape)) - 1
pad_seq = [0 for _ in range(len(x.shape) * 2)]
missing = max(self.target_length - x.shape[self.dim], 0)
pad_seq[idx * 2] = missing
x = F.pad(x, pad_seq, mode=self.mode, value=self.fill_value)
return x
def pad_align_center(self, x: Tensor) -> Tensor:
idx = len(x.shape) - (self.dim % len(x.shape)) - 1
pad_seq = [0 for _ in range(len(x.shape) * 2)]
missing = max(self.target_length - x.shape[self.dim], 0)
missing_left = missing // 2 + missing % 2
missing_right = missing // 2
pad_seq[idx * 2] = missing_left
pad_seq[idx * 2 + 1] = missing_right
x = F.pad(x, pad_seq, mode=self.mode, value=self.fill_value)
return x
def pad_align_random(self, x: Tensor) -> Tensor:
idx = len(x.shape) - (self.dim % len(x.shape)) - 1
pad_seq = [0 for _ in range(len(x.shape) * 2)]
missing = max(self.target_length - x.shape[self.dim], 0)
missing_left = torch.randint(low=0, high=missing + 1, size=()).item()
missing_right = missing - missing_left
pad_seq[idx * 2] = missing_left # type: ignore
pad_seq[idx * 2 + 1] = missing_right # type: ignore
x = F.pad(x, pad_seq, mode=self.mode, value=self.fill_value)
return x
class Resample(nn.Module):
INTERPOLATIONS = ("nearest", "linear")
def __init__(
self,
rates: Tuple[float, float] = (0.5, 1.5),
interpolation: str = "nearest",
dim: int = -1,
p: float = 1.0,
) -> None:
"""Resample an audio waveform signal.
:param rates: The rate of the stretch. Ex: use 2.0 for multiply the signal length by 2. (default: (0.5, 1.5))
:param interpolation: Interpolation for resampling. Can be one of ("nearest", "linear").
(default: "nearest")
:param dim: The dimension to modify. (default: -1)
:param p: The probability to apply the transform. (default: 1.0)
"""
if interpolation not in self.INTERPOLATIONS:
raise ValueError(
f'Invalid argument mode interpolation={interpolation}. Must be one of {self.INTERPOLATIONS}.'
)
super().__init__()
self.rates = rates
self.interpolation = interpolation
self.dim = dim
self.p = p
def extra_repr(self) -> str:
return f"rates={str(self.rates)}"
def forward(self, x):
if self.p >= 1.0 or random.random() <= self.p:
return self.process(x)
else:
return x
def process(self, data: Tensor) -> Tensor:
if self.rates[0] == self.rates[1]:
rate = self.rates[0]
else:
sampler = Uniform(*self.rates)
rate = sampler.sample().item()
if self.interpolation == "nearest":
data = self.resample_nearest(data, rate)
elif self.interpolation == "linear":
sampling_rate = 32000
tchaudio_resample = TorchAudioResample(sampling_rate, int(sampling_rate * rate))
data = tchaudio_resample(data)
else:
raise ValueError(
f"Invalid argument interpolation={self.interpolation}. Must be one of {self.INTERPOLATIONS}."
)
return data
def resample_nearest(self, data: Tensor, rate: float) -> Tensor:
length = data.shape[self.dim]
step = 1.0 / rate
indexes = torch.arange(0, length, step)
indexes = indexes.round().long().clamp(max=length - 1)
slices: List[Any] = [slice(None)] * len(data.shape)
slices[self.dim] = indexes
data = data[slices]
data = data.contiguous()
return data
class SpeedPerturbation(nn.Module):
def __init__(
self,
rates: Tuple[float, float] = (0.9, 1.1),
target_length: Union[int, str] = "same",
align: str = "random",
fill_value: float = 0.0,
dim: int = -1,
p: float = 1.0,
) -> None:
"""
Resample, Pad and Crop the signal.
:param rates: The ratio of the signal used for resize. (default: (0.9, 1.1))
:param target_length: Optional target length of the signal dimension.
If 'auto', the output will have the same shape than the input.
(default: 'auto')
:param align: Alignment to use for cropping and padding. Can be 'left', 'right', 'center' or 'random'.
(default: 'random')
:param fill_value: The fill value when padding the waveform. (default: 0.0)
:param dim: The dimension to stretch and pad or crop. (default: -1)
:param p: The probability to apply the transform. (default: 1.0)
"""
super().__init__()
self.rates = rates
self._target_length = target_length
self.align = align
self.fill_value = fill_value
self.dim = dim
self.p = p
target_length = self.target_length if isinstance(self.target_length, int) else 1
self.stretch = Resample(rates, dim=dim)
self.pad = Pad(target_length, align, fill_value, dim, mode="constant")
self.crop = Crop(target_length, align, dim)
def forward(self, x):
if self.p >= 1.0 or random.random() <= self.p:
return self.process(x)
else:
return x
def process(self, data: Tensor) -> Tensor:
if self.target_length == "same":
target_length = data.shape[self.dim]
self.pad.target_length = target_length
self.crop.target_length = target_length
data = self.stretch(data)
data = self.pad(data)
data = self.crop(data)
return data
@property
def target_length(self) -> Union[int, str]:
return self._target_length
class PadOrTruncate(object):
"""Pad all audio to specific length."""
def __init__(self, audio_length):
self.audio_length = audio_length
def __call__(self, sample):
if len(sample) <= self.audio_length:
return F.pad(sample, (0, self.audio_length - sample.size(-1)))
else:
return sample[0: self.audio_length]
def __repr__(self):
return f"PadOrTruncate(audio_length={self.audio_length})"
class RandomRoll(object):
def __init__(self, dims):
self.dims = dims
def __call__(self, sample):
shifts = [torch.randint(-sample.size(dim),
sample.size(dim),
size=(1,)) for dim in self.dims]
return torch.roll(sample, shifts, self.dims)
def __repr__(self):
return f"RandomRoll(dims={self.dims})"