Path: blob/master/test/test_melgan_layers.py
1558 views
# -*- coding: utf-8 -*-1# Copyright 2020 Minh Nguyen (@dathudeptrai)2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.1415import logging16import os1718import numpy as np19import pytest20import tensorflow as tf2122from tensorflow_tts.models.melgan import (23TFConvTranspose1d,24TFReflectionPad1d,25TFResidualStack,26)2728os.environ["CUDA_VISIBLE_DEVICES"] = ""2930logging.basicConfig(31level=logging.DEBUG,32format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s",33)343536@pytest.mark.parametrize("padding_size", [(3), (5)])37def test_padding(padding_size):38fake_input_1d = tf.random.normal(shape=[4, 8000, 256], dtype=tf.float32)39out = TFReflectionPad1d(padding_size=padding_size)(fake_input_1d)40assert np.array_equal(41tf.keras.backend.int_shape(out), [4, 8000 + 2 * padding_size, 256]42)434445@pytest.mark.parametrize(46"filters,kernel_size,strides,padding,is_weight_norm",47[(512, 40, 8, "same", False), (768, 15, 8, "same", True)],48)49def test_convtranpose1d(filters, kernel_size, strides, padding, is_weight_norm):50fake_input_1d = tf.random.normal(shape=[4, 8000, 256], dtype=tf.float32)51conv1d_transpose = TFConvTranspose1d(52filters=filters,53kernel_size=kernel_size,54strides=strides,55padding=padding,56is_weight_norm=is_weight_norm,57initializer_seed=42,58)59out = conv1d_transpose(fake_input_1d)60assert np.array_equal(tf.keras.backend.int_shape(out), [4, 8000 * strides, filters])616263@pytest.mark.parametrize(64"kernel_size,filters,dilation_rate,use_bias,nonlinear_activation,nonlinear_activation_params,is_weight_norm",65[66(3, 256, 1, True, "LeakyReLU", {"alpha": 0.3}, True),67(3, 256, 3, True, "ReLU", {}, False),68],69)70def test_residualblock(71kernel_size,72filters,73dilation_rate,74use_bias,75nonlinear_activation,76nonlinear_activation_params,77is_weight_norm,78):79fake_input_1d = tf.random.normal(shape=[4, 8000, 256], dtype=tf.float32)80residual_block = TFResidualStack(81kernel_size=kernel_size,82filters=filters,83dilation_rate=dilation_rate,84use_bias=use_bias,85nonlinear_activation=nonlinear_activation,86nonlinear_activation_params=nonlinear_activation_params,87is_weight_norm=is_weight_norm,88initializer_seed=42,89)90out = residual_block(fake_input_1d)91assert np.array_equal(tf.keras.backend.int_shape(out), [4, 8000, filters])929394