File size: 2,961 Bytes
11b4f27 | 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 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
|