Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/notebooks/fastspeech_inference.ipynb
1558 views
Kernel: Python 3
import yaml import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from tensorflow_tts.inference import AutoConfig from tensorflow_tts.inference import TFAutoModel from tensorflow_tts.inference import AutoProcessor
processor = AutoProcessor.from_pretrained("tensorspeech/tts-fastspeech-ljspeech-en")
HBox(children=(FloatProgress(value=0.0, description='Downloading', max=3568.0, style=ProgressStyle(description…
input_text = "i love you so much." input_ids = processor.text_to_sequence(input_text)
fastspeech = TFAutoModel.from_pretrained("tensorspeech/tts-fastspeech-ljspeech-en")
HBox(children=(FloatProgress(value=0.0, description='Downloading', max=120784120.0, style=ProgressStyle(descri…
HBox(children=(FloatProgress(value=0.0, description='Downloading', max=3710.0, style=ProgressStyle(description…

Save to Pb

# save model into pb and do inference. Note that signatures should be a tf.function with input_signatures. tf.saved_model.save(fastspeech, "./test_saved", signatures=fastspeech.inference)
WARNING:tensorflow:From /home/lap13548/anaconda3/envs/tensorflow-tts/lib/python3.7/site-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version. Instructions for updating: This property should not be used in TensorFlow 2.0, as updates are applied automatically. WARNING:tensorflow:From /home/lap13548/anaconda3/envs/tensorflow-tts/lib/python3.7/site-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version. Instructions for updating: This property should not be used in TensorFlow 2.0, as updates are applied automatically. INFO:tensorflow:Assets written to: ./test_saved/assets

Load and Inference

fastspeech = tf.saved_model.load("./test_saved")
input_text = "There’s a way to measure the acute emotional intelligence that has never gone out of style." input_ids = processor.text_to_sequence(input_text)
mel_before, mel_after, duration_outputs = fastspeech.inference( input_ids=tf.expand_dims(tf.convert_to_tensor(input_ids, dtype=tf.int32), 0), speaker_ids=tf.convert_to_tensor([0], dtype=tf.int32), speed_ratios=tf.convert_to_tensor([1.0], dtype=tf.float32), )
mel_after = tf.reshape(mel_after, [-1, 80]).numpy() fig = plt.figure(figsize=(10, 8)) ax1 = fig.add_subplot(311) ax1.set_title(f'Predicted Mel-after-Spectrogram') im = ax1.imshow(np.rot90(mel_after), aspect='auto', interpolation='none') fig.colorbar(mappable=im, shrink=0.65, orientation='horizontal', ax=ax1) plt.show() plt.close()
Image in a Jupyter notebook

Let inference other input to check dynamic shape

input_text = "The Commission further recommends that the Secret Service coordinate its planning as closely as possible with all of the Federal agencies from which it receives information." input_ids = processor.text_to_sequence(input_text)
mel_before, mel_after, duration_outputs = fastspeech.inference( input_ids=tf.expand_dims(tf.convert_to_tensor(input_ids, dtype=tf.int32), 0), speaker_ids=tf.convert_to_tensor([0], dtype=tf.int32), speed_ratios=tf.convert_to_tensor([1.0], dtype=tf.float32), )
mel_after = tf.reshape(mel_after, [-1, 80]).numpy() fig = plt.figure(figsize=(10, 8)) ax1 = fig.add_subplot(311) ax1.set_title(f'Predicted Mel-after-Spectrogram') im = ax1.imshow(np.rot90(mel_after), aspect='auto', interpolation='none') fig.colorbar(mappable=im, shrink=0.65, orientation='horizontal', ax=ax1) plt.show() plt.close()
Image in a Jupyter notebook

Let check speed control

mel_before, mel_after, duration_outputs = fastspeech.inference( input_ids=tf.expand_dims(tf.convert_to_tensor(input_ids, dtype=tf.int32), 0), speaker_ids=tf.convert_to_tensor([0], dtype=tf.int32), speed_ratios=tf.convert_to_tensor([1.5], dtype=tf.float32), )
mel_after = tf.reshape(mel_after, [-1, 80]).numpy() fig = plt.figure(figsize=(10, 8)) ax1 = fig.add_subplot(311) ax1.set_title(f'Predicted Mel-after-Spectrogram') im = ax1.imshow(np.rot90(mel_after), aspect='auto', interpolation='none') fig.colorbar(mappable=im, shrink=0.65, orientation='horizontal', ax=ax1) plt.show() plt.close()
Image in a Jupyter notebook