Parameter Adjustmens and Ensemble Model Implementation

This commit is contained in:
Si11ium
2020-05-08 16:30:54 +02:00
parent 3c776f13c5
commit d2e74ff33a
6 changed files with 126 additions and 56 deletions

View File

@ -0,0 +1,58 @@
import numpy as np
class NoiseInjection(object):
def __init__(self, noise_factor: float, sigma=0.5, mu=0.5):
assert noise_factor > 0, f'max_shift_ratio has to be greater then 0, but was: {noise_factor}.'
self.mu = mu
self.sigma = sigma
self.noise_factor = noise_factor
def __call__(self, x: np.ndarray):
noise = np.random.normal(loc=self.mu, scale=self.sigma, size=x.shape)
augmented_data = x + self.noise_factor * noise
# Cast back to same data type
augmented_data = augmented_data.astype(x.dtype)
return augmented_data
class LoudnessManipulator(object):
def __init__(self, max_factor: float):
assert 1 > max_factor > 0, f'max_shift_ratio has to be between [0,1], but was: {max_factor}.'
self.max_factor = max_factor
def __call__(self, x: np.ndarray):
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
class ShiftTime(object):
valid_shifts = ['right', 'left', 'any']
def __init__(self, max_shift_ratio: float, shift_direction: str = 'any'):
assert 1 > max_shift_ratio > 0, f'max_shift_ratio has to be between [0,1], but was: {max_shift_ratio}.'
assert shift_direction.lower() in self.valid_shifts, f'shift_direction has to be one of: {self.valid_shifts}'
self.max_shift_ratio = max_shift_ratio
self.shift_direction = shift_direction.lower()
def __call__(self, x: np.ndarray):
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':
direction = np.random.choice([1, -1], 1)
shift = direction * shift
augmented_data = np.roll(x, shift)
# Set to silence for heading/ tailing
shift = int(shift)
if shift > 0:
augmented_data[:shift] = 0
else:
augmented_data[shift:] = 0
return augmented_data

View File

@ -1,5 +1,4 @@
import librosa
import torch
from scipy.signal import butter, lfilter
import numpy as np
@ -36,31 +35,34 @@ class MFCC(object):
class NormalizeLocal(object):
def __init__(self):
self.cache: torch.Tensor
self.cache: np.ndarray
pass
def __call__(self, x: torch.Tensor):
def __call__(self, x: np.ndarray):
mean = x.mean()
std = x.std()
std = x.std() + 0.0001
x = x.__sub__(mean).__div__(std)
x[torch.isnan(x)] = 0
x[torch.isinf(x)] = 0
# Pytorch Version:
# x = x.__sub__(mean).__div__(std)
# Numpy Version
x = (x - mean) / std
x[np.isnan(x)] = 0
x[np.isinf(x)] = 0
return x
class NormalizeMelband(object):
def __init__(self):
self.cache: torch.Tensor
self.cache: np.ndarray
pass
def __call__(self, x: torch.Tensor):
def __call__(self, x: np.ndarray):
mean = x.mean(-1).unsqueeze(-1)
std = x.std(-1).unsqueeze(-1)
x = x.__sub__(mean).__div__(std)
x[torch.isnan(x)] = 0
x[torch.isinf(x)] = 0
x[np.isnan(x)] = 0
x[np.isinf(x)] = 0
return x