Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/test/test_melgan.py
1558 views
1
# -*- coding: utf-8 -*-
2
# Copyright 2020 Minh Nguyen (@dathudeptrai)
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import logging
17
import os
18
19
import pytest
20
import tensorflow as tf
21
22
from tensorflow_tts.configs import MelGANDiscriminatorConfig, MelGANGeneratorConfig
23
from tensorflow_tts.models import TFMelGANGenerator, TFMelGANMultiScaleDiscriminator
24
25
os.environ["CUDA_VISIBLE_DEVICES"] = ""
26
27
logging.basicConfig(
28
level=logging.DEBUG,
29
format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s",
30
)
31
32
33
def make_melgan_generator_args(**kwargs):
34
defaults = dict(
35
out_channels=1,
36
kernel_size=7,
37
filters=512,
38
use_bias=True,
39
upsample_scales=[8, 8, 2, 2],
40
stack_kernel_size=3,
41
stacks=3,
42
nonlinear_activation="LeakyReLU",
43
nonlinear_activation_params={"alpha": 0.2},
44
padding_type="REFLECT",
45
)
46
defaults.update(kwargs)
47
return defaults
48
49
50
def make_melgan_discriminator_args(**kwargs):
51
defaults = dict(
52
out_channels=1,
53
scales=3,
54
downsample_pooling="AveragePooling1D",
55
downsample_pooling_params={"pool_size": 4, "strides": 2,},
56
kernel_sizes=[5, 3],
57
filters=16,
58
max_downsample_filters=1024,
59
use_bias=True,
60
downsample_scales=[4, 4, 4, 4],
61
nonlinear_activation="LeakyReLU",
62
nonlinear_activation_params={"alpha": 0.2},
63
padding_type="REFLECT",
64
)
65
defaults.update(kwargs)
66
return defaults
67
68
69
@pytest.mark.parametrize(
70
"dict_g, dict_d, dict_loss",
71
[
72
({}, {}, {}),
73
({"kernel_size": 3}, {}, {}),
74
({"filters": 1024}, {}, {}),
75
({"stack_kernel_size": 5}, {}, {}),
76
({"stack_kernel_size": 5, "stacks": 2}, {}, {}),
77
({"upsample_scales": [4, 4, 4, 4]}, {}, {}),
78
({"upsample_scales": [8, 8, 2, 2]}, {}, {}),
79
({"filters": 1024, "upsample_scales": [8, 8, 2, 2]}, {}, {}),
80
],
81
)
82
def test_melgan_trainable(dict_g, dict_d, dict_loss):
83
batch_size = 4
84
batch_length = 4096
85
args_g = make_melgan_generator_args(**dict_g)
86
args_d = make_melgan_discriminator_args(**dict_d)
87
88
args_g = MelGANGeneratorConfig(**args_g)
89
args_d = MelGANDiscriminatorConfig(**args_d)
90
91
generator = TFMelGANGenerator(args_g)
92
discriminator = TFMelGANMultiScaleDiscriminator(args_d)
93
94