-
Notifications
You must be signed in to change notification settings - Fork 816
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚀 ✍ Update Config for FastSpeech2_v2 small, add test multi-speaker fo…
…r FastSpeech2.
- Loading branch information
1 parent
f65547c
commit a9a48a7
Showing
3 changed files
with
95 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2020 Minh Nguyen (@dathudeptrai) | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
import os | ||
import pytest | ||
import tensorflow as tf | ||
|
||
from tensorflow_tts.models import TFFastSpeech2 | ||
from tensorflow_tts.configs import FastSpeech2Config | ||
|
||
os.environ["CUDA_VISIBLE_DEVICES"] = "" | ||
|
||
logging.basicConfig( | ||
level=logging.DEBUG, | ||
format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s", | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("num_hidden_layers,n_speakers", [(2, 1), (3, 2), (4, 3)]) | ||
def test_fastspeech_trainable(num_hidden_layers, n_speakers): | ||
config = FastSpeech2Config( | ||
encoder_num_hidden_layers=num_hidden_layers, | ||
decoder_num_hidden_layers=num_hidden_layers + 1, | ||
n_speakers=n_speakers | ||
) | ||
|
||
fastspeech2 = TFFastSpeech2(config, name="fastspeech") | ||
optimizer = tf.keras.optimizers.Adam(lr=0.001) | ||
|
||
# fake inputs | ||
input_ids = tf.convert_to_tensor([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], tf.int32) | ||
attention_mask = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], tf.int32) | ||
speaker_ids = tf.convert_to_tensor([0], tf.int32) | ||
duration_gts = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], tf.int32) | ||
f0_gts = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], tf.float32) | ||
energy_gts = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], tf.float32) | ||
|
||
mel_gts = tf.random.uniform(shape=[1, 10, 80], dtype=tf.float32) | ||
|
||
@tf.function | ||
def one_step_training(): | ||
with tf.GradientTape() as tape: | ||
mel_outputs_before, _, duration_outputs, _, _ = fastspeech2( | ||
input_ids, attention_mask, speaker_ids, duration_gts, f0_gts, energy_gts, training=True | ||
) | ||
duration_loss = tf.keras.losses.MeanSquaredError()( | ||
duration_gts, duration_outputs | ||
) | ||
mel_loss = tf.keras.losses.MeanSquaredError()(mel_gts, mel_outputs_before) | ||
loss = duration_loss + mel_loss | ||
gradients = tape.gradient(loss, fastspeech2.trainable_variables) | ||
optimizer.apply_gradients(zip(gradients, fastspeech2.trainable_variables)) | ||
|
||
tf.print(loss) | ||
|
||
import time | ||
|
||
for i in range(2): | ||
if i == 1: | ||
start = time.time() | ||
one_step_training() | ||
print(time.time() - start) |