Repair of ML Lib -> Transformations back to np from torch

This commit is contained in:
Si11ium
2020-12-17 11:00:42 +01:00
parent 62d9eb6e8f
commit 93103aba01
3 changed files with 81 additions and 85 deletions

@ -1,4 +1,3 @@
import torch
import numpy as np
from ml_lib.utils.transforms import _BaseTransformation
@ -13,10 +12,12 @@ class NoiseInjection(_BaseTransformation):
self.sigma = sigma
self.noise_factor = noise_factor
def __call__(self, x):
def __call__(self, x: np.ndarray):
if self.noise_factor:
noise = torch.normal(self.mu, self.sigma, size=x.shape, device=x.device) * self.noise_factor
noise = np.random.normal(self.mu, self.sigma, size=x.shape) * self.noise_factor
augmented_data = x + x * noise
# Cast back to same data type
augmented_data = augmented_data.astype(x.dtype)
return augmented_data
else:
return x
@ -32,7 +33,9 @@ class LoudnessManipulator(_BaseTransformation):
def __call__(self, x):
if self.max_factor:
augmented_data = x + x * (torch.rand(1, device=x.device) * self.max_factor)
augmented_data = x + x * (np.random.random() * self.max_factor)
# Cast back to same data type
augmented_data = augmented_data.astype(x.dtype)
return augmented_data
else:
return x
@ -49,18 +52,17 @@ class ShiftTime(_BaseTransformation):
self.max_shift_ratio = max_shift_ratio
self.shift_direction = shift_direction.lower()
def __call__(self, x):
def __call__(self, x: np.ndarray):
if self.max_shift_ratio:
shift = torch.randint(max(int(self.max_shift_ratio * x.shape[-1]), 1), (1,)).item()
shift = np.random.randint(max(int(self.max_shift_ratio * x.shape[-1]), 1))
if self.shift_direction == 'right':
shift = -1 * shift
elif self.shift_direction == 'any':
# The ugly pytorch alternative
# direction = [-1, 1][torch.multinomial(torch.as_tensor([1, 2]).float(), 1).item()]
direction = np.asscalar(np.random.choice([1, -1], 1))
shift = direction * shift
augmented_data = torch.roll(x, shift, dims=-1)
augmented_data = np.roll(x, shift)
# Set to silence for heading/ tailing
shift = int(shift)
if shift > 0:
augmented_data[:shift, :] = 0
else:
@ -89,20 +91,15 @@ class MaskAug(_BaseTransformation):
else (duration_ratio_max, duration_ratio_max)
def __call__(self, x):
assert x.ndim == 3, "This function was made to wotk with two-dimensional inputs"
for dim in (self.w_idx, self.h_idx):
if self.duration_ratio_max[dim]:
if dim == self.w_idx and x.shape[dim] == 0:
print(x)
start = np.asscalar(np.random.choice(x.shape[dim], 1))
v_max = int(x.shape[dim] * self.duration_ratio_max[dim])
size = torch.randint(0, v_max, (1,)).item()
size = np.asscalar(np.random.randint(0, v_max, 1))
end = int(min(start + size, x.shape[dim]))
size = end - start
if dim == self.w_idx:
mask = torch.randn(size=(x.shape[self.h_idx], size), device=x.device) if self.mask_with_noise else 0
x[:, :, start:end] = mask
x[:, start:end] = np.random.random((x.shape[self.h_idx], size)) if self.mask_with_noise else 0
else:
mask = torch.randn((size, x.shape[self.w_idx]), device=x.device) if self.mask_with_noise else 0
x[:, start:end, :] = mask
x[start:end, :] = np.random.random((size, x.shape[self.w_idx])) if self.mask_with_noise else 0
return x