Download code/s1/QuantizeDataset copy.py from rcell233/magvits_data: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/datasets/rcell233/magvits_data/resolve/main/code/s1/QuantizeDataset%20copy.py
- Command line
-
hf download 'hf://datasets/rcell233/magvits_data/code/s1/QuantizeDataset copy.py'
-
curl -L -o 'QuantizeDataset copy.py' https://huggingface.co/datasets/rcell233/magvits_data/resolve/main/code/s1/QuantizeDataset%20copy.py
2.96 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 | |
| from module import commons | |
| 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'] | |
| phonemes = cleaned_text_to_sequence(phoneme.split(' ')) | |
| phonemes = commons.intersperse(phonemes, 0) | |
| spk_emb = row['spk_emb'] | |
| quantization = np.array(row['codes'])[:,-1] | |
| length = quantization.shape[0] | |
| bert_path = dataname.replace('.wav', '.bert.pt') | |
| phones_length = len(phonemes) | |
| bert_feature = torch.load(bert_path) | |
| bert_feature = F.interpolate(bert_feature.unsqueeze(0), scale_factor=2, mode='nearest') | |
| bert_feature = F.pad(bert_feature, (0, 1), value=0).squeeze(0) | |
| assert bert_feature.shape[-1] == phones_length | |
| assert length == len(phonemes) | |
| return quantization, phonemes, dataname, spk_emb, bert_feature, length | |
| def seqCollate(self, batch): | |
| output = { | |
| 'phone': [], | |
| 'length': [], | |
| 'quantization': [], | |
| 'spk_emb': [], | |
| } | |
| # Get the max length of everything | |
| max_len = 0 | |
| for q_s, ph, _, spk_emb, bert,l in batch: | |
| if l > max_len: | |
| max_len = l | |
| output['spk_emb'].append(spk_emb) | |
| bert_padded = torch.FloatTensor(len(batch), 1024, max_len) | |
| bert_padded.zero_() | |
| # Pad each element, create mask | |
| for idx, (q, phone, _, _, bert, l) in enumerate(batch): | |
| phone = np.pad(phone, [0, max_len - len(phone)], constant_values=0) | |
| q = np.pad(q, [0, max_len - len(q)], constant_values=0) | |
| output['phone'].append(phone) | |
| output['length'].append(l) | |
| output['quantization'].append(q) | |
| bert_padded[idx, :, :bert.shape[-1]] = bert | |
| for k in output.keys(): | |
| output[k] = np.array(output[k]) | |
| if k in ['phone', 'length', 'quantization']: | |
| output[k] = torch.LongTensor(output[k]) | |
| elif k in ['spk_emb']: | |
| output[k] = torch.FloatTensor(output[k]) | |
| else: | |
| raise ValueError(f"Unknown key {k}") | |
| output['bert_feature'] = bert_padded | |
| return output | |