Download data.py from Kremon96/NGAN: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/Kremon96/NGAN/resolve/main/data.py
- Command line
-
hf download hf://Kremon96/NGAN/data.py
-
curl -L -o data.py https://huggingface.co/Kremon96/NGAN/resolve/main/data.py
2.86 kB
| """Training data pipeline shared by model.py and load_balanced_model.py. | |
| Simulates a low-resolution input for a random target sample rate: lowpass | |
| filter the 48kHz ground truth, downsample, then cubic-spline upsample it | |
| back to 48kHz so the generator sees fixed-size input/target pairs. | |
| """ | |
| import os | |
| import random | |
| import librosa | |
| import torch | |
| import torch.nn.functional as F | |
| import torchaudio | |
| import torchaudio.transforms as T | |
| from ssr_eval.lowpass import lowpass_filter | |
| from layers import cubic_upsampler | |
| FILTER_TYPES = ["butter", "cheby1", "ellip", "bessel"] | |
| INPUT_SAMPLE_RATES = [8000, 16000, 24000] | |
| LOWPASS_FILTER_ORDER = 8 | |
| def _get_flist(folder): | |
| wav_files = [] | |
| for root, dirs, files in os.walk(folder): | |
| for filename in files: | |
| if filename.endswith(('.wav', '.flac', '.mp4')): | |
| wav_files.append(os.path.join(root, filename)) | |
| return wav_files | |
| class VoiceDataset(torch.utils.data.Dataset): | |
| def __init__(self, root: str, segment_length: int, target_sample_rate: int) -> None: | |
| self._flist = _get_flist(root) | |
| self.segment_length = segment_length | |
| self.target_sample_rate = target_sample_rate | |
| def __getitem__(self, n: int): | |
| x, sr = torchaudio.load(self._flist[n]) | |
| if sr > self.target_sample_rate: | |
| resample_transform_x = T.Resample(orig_freq=sr, new_freq=self.target_sample_rate) | |
| x = resample_transform_x(x) | |
| if x.size()[0] == 2: | |
| x = x[random.randint(0, 1)].unsqueeze(dim=0) | |
| assert x.shape[0] == 1 | |
| x = torch.squeeze(x) | |
| assert x.dim() == 1 | |
| if x.shape[0] < self.segment_length: | |
| x = F.pad(x, [0, self.segment_length - x.shape[0]], "constant") | |
| pos = random.randint(0, x.shape[0] - self.segment_length) | |
| x = x[pos:pos + self.segment_length] | |
| freq = random.choice(INPUT_SAMPLE_RATES) | |
| cutoff = freq * 0.5 | |
| z = lowpass_filter(x, cutoff, self.target_sample_rate, LOWPASS_FILTER_ORDER, ftype=random.choice(FILTER_TYPES)) | |
| z = librosa.resample(z, orig_sr=self.target_sample_rate, target_sr=freq) | |
| v = torch.tensor(z.copy(), dtype=torch.float32) | |
| v_ = cubic_upsampler(v, freq, self.target_sample_rate) | |
| y = torch.tensor(v_, dtype=torch.float32) | |
| return [x, y] | |
| def __len__(self) -> int: | |
| return len(self._flist) | |
| def _collate(examples): | |
| x_stack = torch.stack([example[0] for example in examples]) | |
| y_stack = torch.stack([example[1] for example in examples]) | |
| return [x_stack, y_stack] | |
| def make_dataloader(root: str, segment_length: int, target_sample_rate: int, batch_size: int) -> torch.utils.data.DataLoader: | |
| ds = VoiceDataset(root, segment_length, target_sample_rate) | |
| return torch.utils.data.DataLoader( | |
| ds, batch_size=batch_size, shuffle=True, collate_fn=_collate) | |