Urban 8k Train running with newest Lightning and pytorch

This commit is contained in:
Si11ium
2021-01-04 11:22:34 +01:00
parent 93103aba01
commit f6156c6cde
7 changed files with 217 additions and 19 deletions

View File

@ -1,6 +1,6 @@
from typing import Union
import torch
import numpy as np
try:
import librosa
@ -53,7 +53,7 @@ class NormalizeLocal(object):
def __repr__(self):
return f'{self.__class__.__name__}({self.__dict__})'
def __call__(self, x: torch.Tensor):
def __call__(self, x: np.ndarray):
mean = x.mean()
std = x.std() + 0.0001
@ -62,8 +62,8 @@ class NormalizeLocal(object):
# Numpy Version
x = (x - mean) / std
x[torch.isnan(x)] = 0
x[torch.isinf(x)] = 0
x[np.isnan(x)] = 0
x[np.isinf(x)] = 0
return x
@ -76,13 +76,13 @@ class NormalizeMelband(object):
def __repr__(self):
return f'{self.__class__.__name__}({self.__dict__})'
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
@ -100,8 +100,6 @@ class LibrosaAudioToMel(object):
self.power_to_db = power_to_db
def __call__(self, y):
import numpy as np
mel = librosa.feature.melspectrogram(y, **self.mel_kwargs)
if self.amplitude_to_db:
mel = librosa.amplitude_to_db(mel, ref=np.max)
@ -121,7 +119,6 @@ class PowerToDB(object):
return f'{self.__class__.__name__}({self.__dict__})'
def __call__(self, x):
import numpy as np
if self.running_max is not None:
self.running_max = max(np.max(x), self.running_max)
return librosa.power_to_db(x, ref=self.running_max)
@ -148,11 +145,11 @@ class MelToImage(object):
def __call__(self, x):
# Source to Solution: https://stackoverflow.com/a/57204349
mels = torch.log(x + 1e-9) # add small number to avoid log(0)
mels = np.log(x + 1e-9) # add small number to avoid log(0)
# min-max scale to fit inside 8-bit range
img = scale_minmax(mels, 0, 255).int()
img = torch.flip(img, dims=(0,)) # put low frequencies at the bottom in image
img = torch.as_tensor(255) - img # invert. make black==more energy
img = img.float()
img = scale_minmax(mels, 0, 255)
img = np.flip(img) # put low frequencies at the bottom in image
img = 255 - img # invert. make black==more energy
img = img.astype(np.float)
return img

View File

@ -35,7 +35,7 @@ class _AudioToMelDataset(Dataset, ABC):
self.audio_augmentations = audio_augmentations
self.dataset = TorchMelDataset(self.mel_file_path, sample_segment_len, sample_hop_len, label,
self.audio_file_duration, mel_kwargs['sample_rate'], mel_kwargs['hop_length'],
self.audio_file_duration, mel_kwargs['sr'], mel_kwargs['hop_length'],
mel_kwargs['n_mels'], transform=mel_augmentations)
def _build_mel(self):
@ -70,7 +70,7 @@ class LibrosaAudioToMelDataset(_AudioToMelDataset):
audio_file_path = Path(audio_file_path)
# audio_file, sampling_rate = librosa.load(self.audio_path, sr=sampling_rate)
mel_kwargs = kwargs.get('mel_kwargs', dict())
mel_kwargs.update(sr=mel_kwargs.get('sr', None) or librosa.get_samplerate(self.audio_path))
mel_kwargs.update(sr=mel_kwargs.get('sr', None) or librosa.get_samplerate(audio_file_path))
kwargs.update(mel_kwargs=mel_kwargs)
super(LibrosaAudioToMelDataset, self).__init__(audio_file_path, *args, **kwargs)
@ -84,11 +84,14 @@ class LibrosaAudioToMelDataset(_AudioToMelDataset):
if self.reset:
self.mel_file_path.unlink(missing_ok=True)
if not self.mel_file_path.exists():
lockfile = Path(str(self.mel_file_path).replace(self.mel_file_path.suffix, '.lock'))
self.mel_file_path.parent.mkdir(parents=True, exist_ok=True)
lockfile.touch(exist_ok=False)
raw_sample, _ = librosa.core.load(self.audio_path, sr=self.sampling_rate)
mel_sample = self._mel_transform(raw_sample)
with self.mel_file_path.open('wb') as mel_file:
pickle.dump(mel_sample, mel_file, protocol=pickle.HIGHEST_PROTOCOL)
lockfile.unlink(missing_ok=False)
else:
pass

View File

@ -13,7 +13,7 @@ class TorchMelDataset(Dataset):
super(TorchMelDataset, self).__init__()
self.sampling_rate = sampling_rate
self.audio_file_len = audio_file_len
self.padding = AutoPadToShape((1, n_mels , sub_segment_len)) if auto_pad_to_shape else None
self.padding = AutoPadToShape((n_mels , sub_segment_len)) if auto_pad_to_shape else None
self.path = Path(mel_path)
self.sub_segment_len = sub_segment_len
self.mel_hop_len = mel_hop_len
@ -29,7 +29,7 @@ class TorchMelDataset(Dataset):
with self.path.open('rb') as mel_file:
mel_spec = pickle.load(mel_file, fix_imports=True)
start = self.offsets[item]
snippet = mel_spec[:, : , start: start + self.sub_segment_len]
snippet = mel_spec[: , start: start + self.sub_segment_len]
if self.transform:
snippet = self.transform(snippet)
if self.padding: