File size: 17,240 Bytes
45ee559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import copy
import os
import unittest

import torch
from torch import optim
from trainer.logging.tensorboard_logger import TensorboardLogger

from tests import get_tests_data_path, get_tests_input_path, get_tests_output_path
from TTS.tts.configs.glow_tts_config import GlowTTSConfig
from TTS.tts.layers.losses import GlowTTSLoss
from TTS.tts.models.glow_tts import GlowTTS
from TTS.tts.utils.speakers import SpeakerManager
from TTS.utils.audio import AudioProcessor

# pylint: disable=unused-variable

torch.manual_seed(1)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

c = GlowTTSConfig()

ap = AudioProcessor(**c.audio)
WAV_FILE = os.path.join(get_tests_input_path(), "example_1.wav")
BATCH_SIZE = 3


def count_parameters(model):
    r"""Count number of trainable parameters in a network"""
    return sum(p.numel() for p in model.parameters() if p.requires_grad)


class TestGlowTTS(unittest.TestCase):
    @staticmethod
    def _create_inputs(batch_size=8):
        input_dummy = torch.randint(0, 24, (batch_size, 128)).long().to(device)
        input_lengths = torch.randint(100, 129, (batch_size,)).long().to(device)
        input_lengths[-1] = 128
        mel_spec = torch.rand(batch_size, 30, c.audio["num_mels"]).to(device)
        mel_lengths = torch.randint(20, 30, (batch_size,)).long().to(device)
        speaker_ids = torch.randint(0, 5, (batch_size,)).long().to(device)
        return input_dummy, input_lengths, mel_spec, mel_lengths, speaker_ids

    @staticmethod
    def _check_parameter_changes(model, model_ref):
        count = 0
        for param, param_ref in zip(model.parameters(), model_ref.parameters()):
            assert (param != param_ref).any(), "param {} with shape {} not updated!! \n{}\n{}".format(
                count, param.shape, param, param_ref
            )
            count += 1

    def test_init_multispeaker(self):
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS(config)
        # speaker embedding with default speaker_embedding_dim
        config.use_speaker_embedding = True
        config.num_speakers = 5
        config.d_vector_dim = None
        model.init_multispeaker(config)
        self.assertEqual(model.c_in_channels, model.hidden_channels_enc)
        # use external speaker embeddings with speaker_embedding_dim = 301
        config = GlowTTSConfig(num_chars=32)
        config.use_d_vector_file = True
        config.d_vector_dim = 301
        model = GlowTTS(config)
        model.init_multispeaker(config)
        self.assertEqual(model.c_in_channels, 301)
        # use speaker embedddings by the provided speaker_manager
        config = GlowTTSConfig(num_chars=32)
        config.use_speaker_embedding = True
        config.speakers_file = os.path.join(get_tests_data_path(), "ljspeech", "speakers.json")
        speaker_manager = SpeakerManager.init_from_config(config)
        model = GlowTTS(config)
        model.speaker_manager = speaker_manager
        model.init_multispeaker(config)
        self.assertEqual(model.c_in_channels, model.hidden_channels_enc)
        self.assertEqual(model.num_speakers, speaker_manager.num_speakers)
        # use external speaker embeddings by the provided speaker_manager
        config = GlowTTSConfig(num_chars=32)
        config.use_d_vector_file = True
        config.d_vector_dim = 256
        config.d_vector_file = os.path.join(get_tests_data_path(), "dummy_speakers.json")
        speaker_manager = SpeakerManager.init_from_config(config)
        model = GlowTTS(config)
        model.speaker_manager = speaker_manager
        model.init_multispeaker(config)
        self.assertEqual(model.c_in_channels, speaker_manager.embedding_dim)
        self.assertEqual(model.num_speakers, speaker_manager.num_speakers)

    def test_unlock_act_norm_layers(self):
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS(config).to(device)
        model.unlock_act_norm_layers()
        for f in model.decoder.flows:
            if getattr(f, "set_ddi", False):
                self.assertFalse(f.initialized)

    def test_lock_act_norm_layers(self):
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS(config).to(device)
        model.lock_act_norm_layers()
        for f in model.decoder.flows:
            if getattr(f, "set_ddi", False):
                self.assertTrue(f.initialized)

    def _test_forward(self, batch_size):
        input_dummy, input_lengths, mel_spec, mel_lengths, speaker_ids = self._create_inputs(batch_size)
        # create model
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS(config).to(device)
        model.train()
        print(" > Num parameters for GlowTTS model:%s" % (count_parameters(model)))
        # inference encoder and decoder with MAS
        y = model.forward(input_dummy, input_lengths, mel_spec, mel_lengths)
        self.assertEqual(y["z"].shape, mel_spec.shape)
        self.assertEqual(y["logdet"].shape, torch.Size([batch_size]))
        self.assertEqual(y["y_mean"].shape, mel_spec.shape)
        self.assertEqual(y["y_log_scale"].shape, mel_spec.shape)
        self.assertEqual(y["alignments"].shape, mel_spec.shape[:2] + (input_dummy.shape[1],))
        self.assertEqual(y["durations_log"].shape, input_dummy.shape + (1,))
        self.assertEqual(y["total_durations_log"].shape, input_dummy.shape + (1,))

    def test_forward(self):
        self._test_forward(1)
        self._test_forward(3)

    def _test_forward_with_d_vector(self, batch_size):
        input_dummy, input_lengths, mel_spec, mel_lengths, speaker_ids = self._create_inputs(batch_size)
        d_vector = torch.rand(batch_size, 256).to(device)
        # create model
        config = GlowTTSConfig(
            num_chars=32,
            use_d_vector_file=True,
            d_vector_dim=256,
            d_vector_file=os.path.join(get_tests_data_path(), "dummy_speakers.json"),
        )
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        model.train()
        print(" > Num parameters for GlowTTS model:%s" % (count_parameters(model)))
        # inference encoder and decoder with MAS
        y = model.forward(input_dummy, input_lengths, mel_spec, mel_lengths, {"d_vectors": d_vector})
        self.assertEqual(y["z"].shape, mel_spec.shape)
        self.assertEqual(y["logdet"].shape, torch.Size([batch_size]))
        self.assertEqual(y["y_mean"].shape, mel_spec.shape)
        self.assertEqual(y["y_log_scale"].shape, mel_spec.shape)
        self.assertEqual(y["alignments"].shape, mel_spec.shape[:2] + (input_dummy.shape[1],))
        self.assertEqual(y["durations_log"].shape, input_dummy.shape + (1,))
        self.assertEqual(y["total_durations_log"].shape, input_dummy.shape + (1,))

    def test_forward_with_d_vector(self):
        self._test_forward_with_d_vector(1)
        self._test_forward_with_d_vector(3)

    def _test_forward_with_speaker_id(self, batch_size):
        input_dummy, input_lengths, mel_spec, mel_lengths, speaker_ids = self._create_inputs(batch_size)
        speaker_ids = torch.randint(0, 24, (batch_size,)).long().to(device)
        # create model
        config = GlowTTSConfig(
            num_chars=32,
            use_speaker_embedding=True,
            num_speakers=24,
        )
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        model.train()
        print(" > Num parameters for GlowTTS model:%s" % (count_parameters(model)))
        # inference encoder and decoder with MAS
        y = model.forward(input_dummy, input_lengths, mel_spec, mel_lengths, {"speaker_ids": speaker_ids})
        self.assertEqual(y["z"].shape, mel_spec.shape)
        self.assertEqual(y["logdet"].shape, torch.Size([batch_size]))
        self.assertEqual(y["y_mean"].shape, mel_spec.shape)
        self.assertEqual(y["y_log_scale"].shape, mel_spec.shape)
        self.assertEqual(y["alignments"].shape, mel_spec.shape[:2] + (input_dummy.shape[1],))
        self.assertEqual(y["durations_log"].shape, input_dummy.shape + (1,))
        self.assertEqual(y["total_durations_log"].shape, input_dummy.shape + (1,))

    def test_forward_with_speaker_id(self):
        self._test_forward_with_speaker_id(1)
        self._test_forward_with_speaker_id(3)

    def _assert_inference_outputs(self, outputs, input_dummy, mel_spec):
        output_shape = outputs["model_outputs"].shape
        self.assertEqual(outputs["model_outputs"].shape[::2], mel_spec.shape[::2])
        self.assertEqual(outputs["logdet"], None)
        self.assertEqual(outputs["y_mean"].shape, output_shape)
        self.assertEqual(outputs["y_log_scale"].shape, output_shape)
        self.assertEqual(outputs["alignments"].shape, output_shape[:2] + (input_dummy.shape[1],))
        self.assertEqual(outputs["durations_log"].shape, input_dummy.shape + (1,))
        self.assertEqual(outputs["total_durations_log"].shape, input_dummy.shape + (1,))

    def _test_inference(self, batch_size):
        input_dummy, input_lengths, mel_spec, mel_lengths, speaker_ids = self._create_inputs(batch_size)
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS(config).to(device)
        model.eval()
        outputs = model.inference(input_dummy, {"x_lengths": input_lengths})
        self._assert_inference_outputs(outputs, input_dummy, mel_spec)

    def test_inference(self):
        self._test_inference(1)
        self._test_inference(3)

    def _test_inference_with_d_vector(self, batch_size):
        input_dummy, input_lengths, mel_spec, mel_lengths, speaker_ids = self._create_inputs(batch_size)
        d_vector = torch.rand(batch_size, 256).to(device)
        config = GlowTTSConfig(
            num_chars=32,
            use_d_vector_file=True,
            d_vector_dim=256,
            d_vector_file=os.path.join(get_tests_data_path(), "dummy_speakers.json"),
        )
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        model.eval()
        outputs = model.inference(input_dummy, {"x_lengths": input_lengths, "d_vectors": d_vector})
        self._assert_inference_outputs(outputs, input_dummy, mel_spec)

    def test_inference_with_d_vector(self):
        self._test_inference_with_d_vector(1)
        self._test_inference_with_d_vector(3)

    def _test_inference_with_speaker_ids(self, batch_size):
        input_dummy, input_lengths, mel_spec, mel_lengths, speaker_ids = self._create_inputs(batch_size)
        speaker_ids = torch.randint(0, 24, (batch_size,)).long().to(device)
        # create model
        config = GlowTTSConfig(
            num_chars=32,
            use_speaker_embedding=True,
            num_speakers=24,
        )
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        outputs = model.inference(input_dummy, {"x_lengths": input_lengths, "speaker_ids": speaker_ids})
        self._assert_inference_outputs(outputs, input_dummy, mel_spec)

    def test_inference_with_speaker_ids(self):
        self._test_inference_with_speaker_ids(1)
        self._test_inference_with_speaker_ids(3)

    def _test_inference_with_MAS(self, batch_size):
        input_dummy, input_lengths, mel_spec, mel_lengths, speaker_ids = self._create_inputs(batch_size)
        # create model
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS(config).to(device)
        model.eval()
        # inference encoder and decoder with MAS
        y = model.inference_with_MAS(input_dummy, input_lengths, mel_spec, mel_lengths)
        y2 = model.decoder_inference(mel_spec, mel_lengths)
        assert (
            y2["model_outputs"].shape == y["model_outputs"].shape
        ), "Difference between the shapes of the glowTTS inference with MAS ({}) and the inference using only the decoder ({}) !!".format(
            y["model_outputs"].shape, y2["model_outputs"].shape
        )

    def test_inference_with_MAS(self):
        self._test_inference_with_MAS(1)
        self._test_inference_with_MAS(3)

    def test_train_step(self):
        batch_size = BATCH_SIZE
        input_dummy, input_lengths, mel_spec, mel_lengths, speaker_ids = self._create_inputs(batch_size)
        criterion = GlowTTSLoss()
        # model to train
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS(config).to(device)
        # reference model to compare model weights
        model_ref = GlowTTS(config).to(device)
        model.train()
        print(" > Num parameters for GlowTTS model:%s" % (count_parameters(model)))
        # pass the state to ref model
        model_ref.load_state_dict(copy.deepcopy(model.state_dict()))
        count = 0
        for param, param_ref in zip(model.parameters(), model_ref.parameters()):
            assert (param - param_ref).sum() == 0, param
            count += 1
        optimizer = optim.Adam(model.parameters(), lr=0.001)
        for _ in range(5):
            optimizer.zero_grad()
            outputs = model.forward(input_dummy, input_lengths, mel_spec, mel_lengths, None)
            loss_dict = criterion(
                outputs["z"],
                outputs["y_mean"],
                outputs["y_log_scale"],
                outputs["logdet"],
                mel_lengths,
                outputs["durations_log"],
                outputs["total_durations_log"],
                input_lengths,
            )
            loss = loss_dict["loss"]
            loss.backward()
            optimizer.step()
        # check parameter changes
        self._check_parameter_changes(model, model_ref)

    def test_train_eval_log(self):
        batch_size = BATCH_SIZE
        input_dummy, input_lengths, mel_spec, mel_lengths, _ = self._create_inputs(batch_size)
        batch = {}
        batch["text_input"] = input_dummy
        batch["text_lengths"] = input_lengths
        batch["mel_lengths"] = mel_lengths
        batch["mel_input"] = mel_spec
        batch["d_vectors"] = None
        batch["speaker_ids"] = None
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        model.run_data_dep_init = False
        model.train()
        logger = TensorboardLogger(
            log_dir=os.path.join(get_tests_output_path(), "dummy_glow_tts_logs"), model_name="glow_tts_test_train_log"
        )
        criterion = model.get_criterion()
        outputs, _ = model.train_step(batch, criterion)
        model.train_log(batch, outputs, logger, None, 1)
        model.eval_log(batch, outputs, logger, None, 1)
        logger.finish()

    def test_test_run(self):
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        model.run_data_dep_init = False
        model.eval()
        test_figures, test_audios = model.test_run(None)
        self.assertTrue(test_figures is not None)
        self.assertTrue(test_audios is not None)

    def test_load_checkpoint(self):
        chkp_path = os.path.join(get_tests_output_path(), "dummy_glow_tts_checkpoint.pth")
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        chkp = {}
        chkp["model"] = model.state_dict()
        torch.save(chkp, chkp_path)
        model.load_checkpoint(config, chkp_path)
        self.assertTrue(model.training)
        model.load_checkpoint(config, chkp_path, eval=True)
        self.assertFalse(model.training)

    def test_get_criterion(self):
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        criterion = model.get_criterion()
        self.assertTrue(criterion is not None)

    def test_init_from_config(self):
        config = GlowTTSConfig(num_chars=32)
        model = GlowTTS.init_from_config(config, verbose=False).to(device)

        config = GlowTTSConfig(num_chars=32, num_speakers=2)
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        self.assertTrue(model.num_speakers == 2)
        self.assertTrue(not hasattr(model, "emb_g"))

        config = GlowTTSConfig(num_chars=32, num_speakers=2, use_speaker_embedding=True)
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        self.assertTrue(model.num_speakers == 2)
        self.assertTrue(hasattr(model, "emb_g"))

        config = GlowTTSConfig(
            num_chars=32,
            num_speakers=2,
            use_speaker_embedding=True,
            speakers_file=os.path.join(get_tests_data_path(), "ljspeech", "speakers.json"),
        )
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        self.assertTrue(model.num_speakers == 10)
        self.assertTrue(hasattr(model, "emb_g"))

        config = GlowTTSConfig(
            num_chars=32,
            use_d_vector_file=True,
            d_vector_dim=256,
            d_vector_file=os.path.join(get_tests_data_path(), "dummy_speakers.json"),
        )
        model = GlowTTS.init_from_config(config, verbose=False).to(device)
        self.assertTrue(model.num_speakers == 1)
        self.assertTrue(not hasattr(model, "emb_g"))
        self.assertTrue(model.c_in_channels == config.d_vector_dim)