Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/test/test_melgan_layers.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 numpy as np
20
import pytest
21
import tensorflow as tf
22
23
from tensorflow_tts.models.melgan import (
24
TFConvTranspose1d,
25
TFReflectionPad1d,
26
TFResidualStack,
27
)
28
29
os.environ["CUDA_VISIBLE_DEVICES"] = ""
30
31
logging.basicConfig(
32
level=logging.DEBUG,
33
format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s",
34
)
35
36
37
@pytest.mark.parametrize("padding_size", [(3), (5)])
38
def test_padding(padding_size):
39
fake_input_1d = tf.random.normal(shape=[4, 8000, 256], dtype=tf.float32)
40
out = TFReflectionPad1d(padding_size=padding_size)(fake_input_1d)
41
assert np.array_equal(
42
tf.keras.backend.int_shape(out), [4, 8000 + 2 * padding_size, 256]
43
)
44
45
46
@pytest.mark.parametrize(
47
"filters,kernel_size,strides,padding,is_weight_norm",
48
[(512, 40, 8, "same", False), (768, 15, 8, "same", True)],
49
)
50
def test_convtranpose1d(filters, kernel_size, strides, padding, is_weight_norm):
51
fake_input_1d = tf.random.normal(shape=[4, 8000, 256], dtype=tf.float32)
52
conv1d_transpose = TFConvTranspose1d(
53
filters=filters,
54
kernel_size=kernel_size,
55
strides=strides,
56
padding=padding,
57
is_weight_norm=is_weight_norm,
58
initializer_seed=42,
59
)
60
out = conv1d_transpose(fake_input_1d)
61
assert np.array_equal(tf.keras.backend.int_shape(out), [4, 8000 * strides, filters])
62
63
64
@pytest.mark.parametrize(
65
"kernel_size,filters,dilation_rate,use_bias,nonlinear_activation,nonlinear_activation_params,is_weight_norm",
66
[
67
(3, 256, 1, True, "LeakyReLU", {"alpha": 0.3}, True),
68
(3, 256, 3, True, "ReLU", {}, False),
69
],
70
)
71
def test_residualblock(
72
kernel_size,
73
filters,
74
dilation_rate,
75
use_bias,
76
nonlinear_activation,
77
nonlinear_activation_params,
78
is_weight_norm,
79
):
80
fake_input_1d = tf.random.normal(shape=[4, 8000, 256], dtype=tf.float32)
81
residual_block = TFResidualStack(
82
kernel_size=kernel_size,
83
filters=filters,
84
dilation_rate=dilation_rate,
85
use_bias=use_bias,
86
nonlinear_activation=nonlinear_activation,
87
nonlinear_activation_params=nonlinear_activation_params,
88
is_weight_norm=is_weight_norm,
89
initializer_seed=42,
90
)
91
out = residual_block(fake_input_1d)
92
assert np.array_equal(tf.keras.backend.int_shape(out), [4, 8000, filters])
93
94