Spaces:
Runtime error
Runtime error
File size: 1,544 Bytes
bd0a813 9ff4511 bd0a813 20c7778 bd0a813 9ff4511 bd0a813 9ff4511 bd0a813 9ff4511 bd0a813 9ff4511 bd0a813 20c7778 bd0a813 3f8f152 |
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 |
import torch
from torch.utils.data import Dataset
from pathlib import Path
import torchaudio
MAX_RANDOM_SEED = 1000
class Valentini(Dataset):
def __init__(self, dataset_path, val_fraction, transform=None, valid=False, *args, **kwargs):
clean_path = Path(dataset_path) / 'clean_trainset_56spk_wav'
noisy_path = Path(dataset_path) / 'noisy_trainset_56spk_wav'
clean_wavs = list(clean_path.glob("*"))
noisy_wavs = list(noisy_path.glob("*"))
valid_threshold = int(len(clean_wavs) * val_fraction)
if valid:
self.clean_wavs = clean_wavs[:valid_threshold]
self.noisy_wavs = noisy_wavs[:valid_threshold]
else:
self.clean_wavs = clean_wavs[valid_threshold:]
self.noisy_wavs = noisy_wavs[valid_threshold:]
assert len(self.clean_wavs) == len(self.noisy_wavs)
self.transform = transform
self.valid = valid
def __len__(self):
return len(self.clean_wavs)
def __getitem__(self, idx):
noisy_wav, noisy_sr = torchaudio.load(self.noisy_wavs[idx])
clean_wav, clean_sr = torchaudio.load(self.clean_wavs[idx])
if self.transform:
random_seed = 0 if self.valid else torch.randint(MAX_RANDOM_SEED, (1,))[0]
torch.manual_seed(random_seed)
noisy_wav = self.transform(noisy_wav)
torch.manual_seed(random_seed)
clean_wav = self.transform(clean_wav)
return noisy_wav, clean_wav
|