File size: 2,942 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
91
92
93
94
95
96
97
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
        }