Download code/s1/QuantizeDataset.py from rcell233/magvits_data: direct link, hf CLI and curl.
- Browser
- Download file 2.94 kB
-
https://huggingface.co/datasets/rcell233/magvits_data/resolve/main/code/s1/QuantizeDataset.py
- Command line
-
hf download hf://datasets/rcell233/magvits_data/code/s1/QuantizeDataset.py
-
curl -L -o QuantizeDataset.py https://huggingface.co/datasets/rcell233/magvits_data/resolve/main/code/s1/QuantizeDataset.py
2.94 kB
| import os | |
| from torch.utils import data | |
| import torch | |
| import numpy as np | |
| import random | |
| import torch.nn.functional as F | |
| from text import cleaned_text_to_sequence | |
| class QuantizeDataset(data.Dataset): | |
| def __init__(self, hp, dataset): | |
| self.hp = hp | |
| self.dataset = dataset | |
| l = len(self.dataset) | |
| print(f'Total {l} examples') | |
| self.lengths = dataset['length'] | |
| avglen = sum(self.lengths) / len(self.lengths) | |
| maxlen = max(self.lengths) | |
| minlen = min(self.lengths) | |
| print( | |
| f"Average phoneme length of audio: {avglen}, Maximum phoneme length: {maxlen}, Minimum phoneme length: {minlen}") | |
| def __len__(self): | |
| return len(self.dataset) | |
| def __getitem__(self, i): | |
| i = int(i) | |
| row = self.dataset[i] | |
| dataname = row['item_name'] | |
| phoneme = row['phoneme'] | |
| phoneme_ids = cleaned_text_to_sequence(phoneme.split(' ')) | |
| phonemes = [0] * (len(phoneme_ids) * 3) | |
| phonemes[1::3] = phoneme_ids | |
| spk_emb = row['spk_emb'] | |
| latent_path = dataname.replace('.wav', '.latent.pt') | |
| latent = torch.load(latent_path, map_location='cpu').squeeze(0) | |
| length = latent.shape[-1] | |
| bert_path = dataname.replace('.wav', '.bert.pt') | |
| phones_length = len(phonemes) | |
| bert_feature = torch.load(bert_path, map_location='cpu') | |
| bert_feature = F.interpolate(bert_feature.unsqueeze(0), scale_factor=3, mode='nearest') | |
| assert bert_feature.shape[-1] == phones_length | |
| assert length == len(phonemes) | |
| return latent, phonemes, dataname, spk_emb, bert_feature, length | |
| def seqCollate(self, batch): | |
| # Get the max length of everything | |
| max_len = 0 | |
| for _, _, _, spk_emb, bert,l in batch: | |
| if l > max_len: | |
| max_len = l | |
| bert_padded = torch.FloatTensor(len(batch), 1024, max_len) | |
| latent_padded = torch.FloatTensor(len(batch), 4, max_len) | |
| phone_padded = torch.LongTensor(len(batch), max_len) | |
| length_padded = torch.LongTensor(len(batch)) | |
| spk_emb_padded = torch.FloatTensor(len(batch), 256) | |
| bert_padded.zero_() | |
| latent_padded.zero_() | |
| phone_padded.zero_() | |
| length_padded.zero_() | |
| spk_emb_padded.zero_() | |
| # Pad each element, create mask | |
| for idx, (latent, phone, _, spk_emb, bert, l) in enumerate(batch): | |
| phone_padded[idx, :l] = torch.LongTensor(phone) | |
| length_padded[idx] = l | |
| spk_emb_padded[idx] = torch.FloatTensor(spk_emb) | |
| bert_padded[idx, :, :bert.shape[-1]] = bert | |
| latent_padded[idx, :, :latent.shape[-1]] = latent | |
| return { | |
| 'phone': phone_padded, | |
| 'length': length_padded, | |
| 'spk_emb': spk_emb_padded, | |
| 'bert_feature': bert_padded, | |
| 'latent': latent_padded | |
| } | |