A PyTorch implementation of FastSpeech 2, a fast, robust, and controllable neural text-to-speech (TTS) model, specifically adapted for Japanese speech synthesis. This implementation uses the Kokoro dataset and supports multi-speaker TTS with controllable pitch, energy, and duration.
Datasets: https://drive.google.com/drive/folders/1oklNXKZJgAjQm12OB8PNXuArHaMAaJx3?usp=sharing
- For any raw_data zip, you will need to create a raw_data folder at the root of the repository, and then extract it within the raw_data folder.
- For any preprocessed_data zip files, you will need to create a preprocessed_data folder, and then extract it within the preprocessed_data folder.
Models: https://drive.google.com/file/d/1B3UDk4EhAqPBc5cyiFb2Gi1q0B4dR5Ih/view?usp=drive_link
- Simply extract this zip file in the root of your file project. It should contain all necessary trained models to synthesize.
FastSpeech 2 is a non-autoregressive TTS model that generates mel-spectrograms directly from text, enabling fast and parallel synthesis. This implementation includes:
- Japanese language support with IPA phoneme representation
- Multi-speaker TTS with speaker embeddings
- Controllable synthesis with adjustable pitch, energy, and duration
- HiFi-GAN vocoder for high-quality waveform generation
- Montreal Forced Aligner (MFA) integration for phoneme alignment
- β‘ Fast inference: Non-autoregressive architecture enables parallel generation
- π― High quality: Transformer-based encoder-decoder with variance adaptor
- ποΈ Controllable: Adjust pitch, energy, and speaking rate during synthesis
- π₯ Multi-speaker: Support for multiple speakers with speaker embeddings
- π―π΅ Japanese optimized: Custom Japanese text preprocessing with pyopenjtalk
fast-speech-2/
βββ audio/ # Audio processing utilities (STFT, mel-spectrogram)
βββ config/ # Configuration files (preprocess, model, train)
β βββ kokoro/ # Kokoro dataset configurations
βββ hifigan/ # HiFi-GAN vocoder implementation
βββ lexicon/ # Phoneme dictionaries (Japanese MFA dictionary)
βββ model/ # FastSpeech 2 model implementation
β βββ fastspeech2.py # Main model architecture
β βββ loss.py # Loss functions
β βββ modules.py # Model components (VarianceAdaptor)
βββ output/ # Training outputs (checkpoints, logs, results)
βββ preprocessed_data/ # Preprocessed dataset (mel, pitch, energy, duration)
βββ preprocessor/ # Data preprocessing scripts
βββ raw_data/ # Raw audio dataset
βββ text/ # Text processing (cleaners, symbols, phoneme conversion)
βββ transformer/ # Transformer encoder/decoder implementation
βββ utils/ # Utility functions
βββ dataset.py # PyTorch dataset classes
βββ evaluate.py # Model evaluation script
βββ preprocess.py # Data preprocessing pipeline
βββ prepare_align.py # MFA alignment preparation
βββ synthesize.py # Text-to-speech synthesis script
βββ train.py # Training script
- Python 3.10
- CUDA-capable GPU (recommended) or CPU-only setup
- Conda (for environment management)
I have provided a script to setup the environment for both GPU and CPU environments. If you are going to manually setup the environment, you MUST use conda. If you do not use conda you will have issues with MFA (Montreal Force Aligner).
The automatic setup will also install the required models for running the trained model, and the required data to train a new model.
For CPU Only installation, the documentation is found in docs/CPU_INSTALLATION.md.
For GPU Only installation, the documentation is found in docs/GPU_INSTALLATION.md
Place your audio files and transcripts in the raw_data/ directory following the expected structure.
Audio files must be in .wav format with a 22050 Hz smaple rate. and the transcripts must be .lab files. Both the .wav and .lab files need to be named the same for each audio/text pair.
Prepare data for Montreal Forced Aligner (MFA):
python prepare_align.py config/kokoro/preprocess.yamlYou will need to modify the preprocess.yaml file to specify where the files are, and where you want the files output.
Use Montreal Forced Aligner to generate phoneme alignments:
mfa align raw_data/kokoro lexicon/japanese_mfa.dict japanese_mfa preprocessed_data/kokoro/TextGridExtract features (mel-spectrograms, pitch, energy, duration):
Ensure you modify the preprocess.yaml to modify the locations of the data, and output.
python preprocess.py config/kokoro/preprocess.yamlThis will generate:
- Mel-spectrograms
- Pitch contours
- Energy features
- Duration labels
- Train/validation splits
Train the FastSpeech 2 model:
python train.py \
-p config/kokoro/preprocess.yaml \
-m config/kokoro/model.yaml \
-t config/kokoro/train.yaml \
--restore_step 0To resume from a checkpoint:
python train.py \
-p config/kokoro/preprocess.yaml \
-m config/kokoro/model.yaml \
-t config/kokoro/train.yaml \
--restore_step 30000Training progress is logged to TensorBoard:
tensorboard --logdir output/log/kokoroEvaluate the model on the validation set:
python evaluate.py \
-p config/kokoro/preprocess.yaml \
-m config/kokoro/model.yaml \
-t config/kokoro/train.yaml \
--restore_step 30000Synthesize a single Japanese sentence:
python synthesize.py --speaker_id 0 --restore_step 30000 --mode single --text "η§γγγΌγγ£γΌγ«ζ₯γγΉγγ γγ¨γε½Όγ―γ€γε γγγ" -p config/male_and_female/preprocess.yaml -m config/male_and_female/model.yaml -t config/male_and_female/train.yamlSynthesize from a text file (format: basename|speaker|phonemes|raw_text):
python synthesize.py \
--mode batch \
--source path/to/text_file.txt \
--restore_step 30000 \
-p config/kokoro/preprocess.yaml \
-m config/kokoro/model.yaml \
-t config/kokoro/train.yamlAdjust pitch, energy, and duration:
python synthesize.py \
--mode single \
--text "γγγ«γ‘γ―" \
--restore_step 30000 \
-p config/kokoro/preprocess.yaml \
-m config/kokoro/model.yaml \
-t config/kokoro/train.yaml \
--pitch_control 1.2 \
--energy_control 1.1 \
--duration_control 0.9--pitch_control: Higher values = higher pitch (default: 1.0)--energy_control: Higher values = louder (default: 1.0)--duration_control: Higher values = slower speech (default: 1.0)
Configuration files are located in config/kokoro/:
preprocess.yaml: Data preprocessing settings (audio parameters, feature extraction)model.yaml: Model architecture (transformer layers, variance predictors)train.yaml: Training hyperparameters (batch size, learning rate, steps)
See config/README.md for detailed explanations of configuration options.
-
preprocess.yaml:sampling_rate: Audio sample rate (22050 Hz)n_mel_channels: Number of mel-spectrogram channels (80)pitch.feature:phoneme_levelorframe_levelenergy.feature:phoneme_levelorframe_level
-
model.yaml:transformer.encoder_layer: Number of encoder layers (4)transformer.decoder_layer: Number of decoder layers (6)multi_speaker: Enable multi-speaker TTS (True/False)vocoder.model: Vocoder type (HiFi-GANorMelGAN)
-
train.yaml:optimizer.batch_size: Training batch size (16)optimizer.grad_acc_step: Gradient accumulation steps (1)step.total_step: Total training steps (900000)
FastSpeech 2 consists of:
- Encoder: Transformer encoder that processes phoneme sequences
- Variance Adaptor: Predicts and adapts pitch, energy, and duration
- Decoder: Transformer decoder that generates mel-spectrograms
- PostNet: Convolutional post-processing network
- Speaker Embedding: Optional speaker embeddings for multi-speaker TTS
The model predicts:
- Mel-spectrograms (main output)
- Pitch contours (phoneme or frame level)
- Energy values (phoneme or frame level)
- Duration labels (phoneme level)
Key dependencies (see requirements.txt for complete list):
- PyTorch
- NumPy, SciPy
- librosa (audio processing)
- pyopenjtalk (Japanese text-to-phoneme)
- Montreal Forced Aligner (MFA)
- HiFi-GAN vocoder
- TensorBoard (logging)
- PyYAML (configuration)
- Checkpoints: Saved in
output/ckpt/kokoro/(everysave_stepsteps) - Logs: TensorBoard logs in
output/log/kokoro/ - Synthesized audio: WAV files in
output/result/kokoro/ - Mel-spectrogram plots: PNG files in
output/result/kokoro/
- This implementation uses phoneme-level pitch and energy features (as opposed to frame-level in the original paper) for more natural prosody
- Japanese text preprocessing uses pyopenjtalk for G2P conversion and maps to IPA phonemes
- The model supports normalized pitch and energy values with linear quantization
- HiFi-GAN universal vocoder is used for waveform generation
See LICENSE file for details.
- FastSpeech 2: Fast, Robust and Controllable Text to Speech (Ren et al., NeurIPS 2020)
- HiFi-GAN: Generative Adversarial Networks for Efficient and High Fidelity Speech Synthesis (Kong et al., NeurIPS 2020)
- Montreal Forced Aligner: https://montreal-forced-alignment.readthedocs.io/
This implementation is adapted for Japanese TTS using the Kokoro dataset and includes custom preprocessing for Japanese phoneme representation.