File size: 10,234 Bytes
ae8e1dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import os.path
import random
import numpy as np
import torch
import re
import torch.utils.data
import json

import kaldiio
from tqdm import tqdm
from text import text_to_sequence

class BaseLoader(torch.utils.data.Dataset):
    def __init__(self, utts: str, hparams, feats_scp: str, utt2text:str):
        """
        :param utts: file path. A list of utts for this loader. These are the only utts that this loader has access.
        This loader only deals with text, duration and feats. Other files despite `utts` can be larger.
        """
        self.n_mel_channels = hparams.n_mel_channels
        self.sampling_rate = hparams.sampling_rate
        self.utts = self.get_utts(utts)
        self.utt2feat = self.get_utt2feat(feats_scp)
        self.utt2text = self.get_utt2text(utt2text)

    def get_utts(self, utts: str) -> list:
        with open(utts, 'r') as f:
            L = f.readlines()
            L = list(map(lambda x: x.strip(), L))
            random.seed(1234)
            random.shuffle(L)
        return L


    def get_utt2feat(self, feats_scp: str):
        utt2feat = kaldiio.load_scp(feats_scp)  # lazy load mode
        print(f"Succeed reading feats from {feats_scp}")
        return utt2feat
    
    def get_utt2text(self, utt2text: str):
        with open(utt2text, 'r') as f:
            L = f.readlines()  
            utt2text = {line.split()[0]: line.strip().split(" ", 1)[1] for line in L}
        return utt2text

    def get_mel_from_kaldi(self, utt):
        feat = self.utt2feat[utt]
        feat = torch.FloatTensor(feat).squeeze()
        assert self.n_mel_channels in feat.shape
        if feat.shape[0] == self.n_mel_channels:
            return feat
        else:
            return feat.T
    
    def get_text(self, utt):
        text = self.utt2text[utt]
        text_norm = text_to_sequence(text)
        text_norm = torch.IntTensor(text_norm)
        return text_norm

    def __getitem__(self, index):
        res = self.get_mel_text_pair(self.utts[index])
        return res

    def __len__(self):
        return len(self.utts)

    def sample_test_batch(self, size):
        idx = np.random.choice(range(len(self)), size=size, replace=False)
        test_batch = []
        for index in idx:
            test_batch.append(self.__getitem__(index))
        return test_batch


class SpkIDLoader(BaseLoader):
    def __init__(self, utts: str, hparams, feats_scp: str, utt2phns: str, phn2id: str,
                 utt2phn_duration: str, utt2spk: str):
        """
        :param utt2spk: json file path (utt name -> spk id)
        This loader loads speaker as a speaker ID for embedding table
        """
        super(SpkIDLoader, self).__init__(utts, hparams, feats_scp, utt2phns, phn2id, utt2phn_duration)
        self.utt2spk = self.get_utt2spk(utt2spk)

    def get_utt2spk(self, utt2spk: str) -> dict:
        with open(utt2spk, 'r') as f:
            res = json.load(f)
        return res

    def get_mel_text_pair(self, utt):
        # separate filename and text
        spkid = self.utt2spk[utt]
        phn_ids = self.get_text(utt)
        mel = self.get_mel_from_kaldi(utt)
        dur = self.get_dur_from_kaldi(utt)

        assert sum(dur) == mel.shape[1], f"Frame length mismatch: utt {utt}, dur: {sum(dur)}, mel: {mel.shape[1]}"
        res = {
            "utt": utt,
            "mel": mel,
            "spk_ids": spkid
        }
        return res

    def __getitem__(self, index):
        res = self.get_mel_text_pair(self.utts[index])
        return res

    def __len__(self):
        return len(self.utts)


class SpkIDLoaderWithEmo(BaseLoader):
    def __init__(self, utts: str, hparams, feats_scp: str, utt2text:str, utt2spk: str, utt2emo: str):
        """
        :param utt2spk: json file path (utt name -> spk id)
        This loader loads speaker as a speaker ID for embedding table
        """
        super(SpkIDLoaderWithEmo, self).__init__(utts, hparams, feats_scp, utt2text)
        self.utt2spk = self.get_utt2spk(utt2spk)
        self.utt2emo = self.get_utt2emo(utt2emo)

    def get_utt2spk(self, utt2spk: str) -> dict:
        with open(utt2spk, 'r') as f:
            res = json.load(f)
        return res   

    def get_utt2emo(self, utt2emo: str) -> dict:
        with open(utt2emo, 'r') as f:
            res = json.load(f)
        return res

    def get_mel_text_pair(self, utt):
        # separate filename and text
        spkid = int(self.utt2spk[utt])
        emoid = int(self.utt2emo[utt])
        text = self.get_text(utt)
        mel = self.get_mel_from_kaldi(utt)

        res = {
            "utt": utt,
            "text": text,
            "mel": mel,
            "spk_ids": spkid,
            "emo_ids": emoid
        }
        return res

    def __getitem__(self, index):
        res = self.get_mel_text_pair(self.utts[index])
        return res

    def __len__(self):
        return len(self.utts)


class SpkIDLoaderWithPE(SpkIDLoader):
    def __init__(self, utts: str, hparams, feats_scp: str, utt2phns: str, phn2id: str,
                 utt2phn_duration: str, utt2spk: str, var_scp: str):
        """
        This loader loads speaker ID together with variance (4-dim pitch, 1-dim energy)
        """
        super(SpkIDLoaderWithPE, self).__init__(utts, hparams, feats_scp, utt2phns, phn2id, utt2phn_duration, utt2spk)
        self.utt2var = self.get_utt2var(var_scp)

    def get_utt2var(self, utt2var: str) -> dict:
        res = kaldiio.load_scp(utt2var)
        print(f"Succeed reading feats from {utt2var}")
        return res

    def get_var_from_kaldi(self, utt):
        var = self.utt2var[utt]
        var = torch.FloatTensor(var).squeeze()
        assert 5 in var.shape
        if var.shape[0] == 5:
            return var
        else:
            return var.T

    def get_mel_text_pair(self, utt):
        # separate filename and text
        spkid = self.utt2spk[utt]
        phn_ids = self.get_text(utt)
        mel = self.get_mel_from_kaldi(utt)
        dur = self.get_dur_from_kaldi(utt)
        var = self.get_var_from_kaldi(utt)

        assert sum(dur) == mel.shape[1] == var.shape[1], \
            f"Frame length mismatch: utt {utt}, dur: {sum(dur)}, mel: {mel.shape[1]}, var: {var.shape[1]}"

        res = {
            "utt": utt,
            "phn_ids": phn_ids,
            "mel": mel,
            "dur": dur,
            "spk_ids": spkid,
            "var": var
        }
        return res


class XvectorLoader(BaseLoader):
    def __init__(self, utts: str, hparams, feats_scp: str, utt2phns: str, phn2id: str,
                 utt2phn_duration: str, utt2spk_name: str, spk_xvector_scp: str):
        """
        :param utt2spk_name: like kaldi-style utt2spk
        :param spk_xvector_scp: kaldi-style speaker-level xvector.scp
        """
        super(XvectorLoader, self).__init__(utts, hparams, feats_scp, utt2phns, phn2id, utt2phn_duration)
        self.utt2spk = self.get_utt2spk(utt2spk_name)
        self.spk2xvector = self.get_spk2xvector(spk_xvector_scp)

    def get_utt2spk(self, utt2spk):
        res = dict()
        with open(utt2spk, 'r') as f:
            for l in f.readlines():
                res[l.split()[0]] = l.split()[1]
        return res

    def get_spk2xvector(self, spk_xvector_scp: str) -> dict:
        res = kaldiio.load_scp(spk_xvector_scp)
        print(f"Succeed reading xvector from {spk_xvector_scp}")
        return res

    def get_xvector(self, utt):
        xv = self.spk2xvector[self.utt2spk[utt]]
        xv = torch.FloatTensor(xv).squeeze()
        return xv

    def get_mel_text_pair(self, utt):
        phn_ids = self.get_text(utt)
        mel = self.get_mel_from_kaldi(utt)
        dur = self.get_dur_from_kaldi(utt)
        xvector = self.get_xvector(utt)

        assert sum(dur) == mel.shape[1], \
            f"Frame length mismatch: utt {utt}, dur: {sum(dur)}, mel: {mel.shape[1]}"

        res = {
            "utt": utt,
            "phn_ids": phn_ids,
            "mel": mel,
            "dur": dur,
            "xvector": xvector,
        }
        return res


class XvectorLoaderWithPE(BaseLoader):
    def __init__(self, utts: str, hparams, feats_scp: str, utt2phns: str, phn2id: str,
                 utt2phn_duration: str, utt2spk_name: str, spk_xvector_scp: str, var_scp: str):
        super(XvectorLoaderWithPE, self).__init__(utts, hparams, feats_scp, utt2phns, phn2id, utt2phn_duration)
        self.utt2spk = self.get_utt2spk(utt2spk_name)
        self.spk2xvector = self.get_spk2xvector(spk_xvector_scp)
        self.utt2var = self.get_utt2var(var_scp)

    def get_spk2xvector(self, spk_xvector_scp: str) -> dict:
        res = kaldiio.load_scp(spk_xvector_scp)
        print(f"Succeed reading xvector from {spk_xvector_scp}")
        return res

    def get_utt2spk(self, utt2spk):
        res = dict()
        with open(utt2spk, 'r') as f:
            for l in f.readlines():
                res[l.split()[0]] = l.split()[1]
        return res

    def get_utt2var(self, utt2var: str) -> dict:
        res = kaldiio.load_scp(utt2var)
        print(f"Succeed reading feats from {utt2var}")
        return res

    def get_var_from_kaldi(self, utt):
        var = self.utt2var[utt]
        var = torch.FloatTensor(var).squeeze()
        assert 5 in var.shape
        if var.shape[0] == 5:
            return var
        else:
            return var.T

    def get_xvector(self, utt):
        xv = self.spk2xvector[self.utt2spk[utt]]
        xv = torch.FloatTensor(xv).squeeze()
        return xv

    def get_mel_text_pair(self, utt):
        # separate filename and text
        spkid = self.utt2spk[utt]
        phn_ids = self.get_text(utt)
        mel = self.get_mel_from_kaldi(utt)
        dur = self.get_dur_from_kaldi(utt)
        var = self.get_var_from_kaldi(utt)
        xvector = self.get_xvector(utt)

        assert sum(dur) == mel.shape[1] == var.shape[1], \
            f"Frame length mismatch: utt {utt}, dur: {sum(dur)}, mel: {mel.shape[1]}, var: {var.shape[1]}"

        res = {
            "utt": utt,
            "phn_ids": phn_ids,
            "mel": mel,
            "dur": dur,
            "spk_ids": spkid,
            "var": var,
            "xvector": xvector
        }
        return res