chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: RayeRen # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||
patreon: # Replace with a single Patreon username
|
||||
open_collective: # Replace with a single Open Collective username
|
||||
ko_fi: # Replace with a single Ko-fi username
|
||||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||
liberapay: # Replace with a single Liberapay username
|
||||
issuehunt: # Replace with a single IssueHunt username
|
||||
otechie: # Replace with a single Otechie username
|
||||
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
|
||||
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||
@@ -0,0 +1,5 @@
|
||||
.idea
|
||||
*.pyc
|
||||
__pycache__/
|
||||
*.sh
|
||||
local_tools/
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Jinglin Liu
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,97 @@
|
||||
# DiffSinger: Singing Voice Synthesis via Shallow Diffusion Mechanism
|
||||
[](https://arxiv.org/abs/2105.02446)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger/releases)
|
||||
[](https://huggingface.co/spaces/NATSpeech/DiffSpeech)
|
||||
[](https://huggingface.co/spaces/Silentlin/DiffSinger)
|
||||
|
||||
|
||||
This repository is the official PyTorch implementation of our AAAI-2022 [paper](https://arxiv.org/abs/2105.02446), in which we propose DiffSinger (for Singing-Voice-Synthesis) and DiffSpeech (for Text-to-Speech).
|
||||
|
||||
|
||||
:tada: :tada: :tada: **Updates**:
|
||||
- Sep.11, 2022: :electric_plug: [DiffSinger-PN](docs/README-SVS-opencpop-pndm.md). Add plug-in [PNDM](https://arxiv.org/abs/2202.09778), ICLR 2022 in our laboratory, to accelerate DiffSinger freely.
|
||||
- Jul.27, 2022: Update documents for [SVS](docs/README-SVS.md). Add easy inference [A](docs/README-SVS-opencpop-cascade.md#4-inference-from-raw-inputs) & [B](docs/README-SVS-opencpop-e2e.md#4-inference-from-raw-inputs); Add Interactive SVS running on [HuggingFace🤗 SVS](https://huggingface.co/spaces/Silentlin/DiffSinger).
|
||||
- Mar.2, 2022: MIDI-B-version.
|
||||
- Mar.1, 2022: [NeuralSVB](https://github.com/MoonInTheRiver/NeuralSVB), for singing voice beautifying, has been released.
|
||||
- Feb.13, 2022: [NATSpeech](https://github.com/NATSpeech/NATSpeech), the improved code framework, which contains the implementations of DiffSpeech and our NeurIPS-2021 work [PortaSpeech](https://openreview.net/forum?id=xmJsuh8xlq) has been released.
|
||||
- Jan.29, 2022: support MIDI-A-version SVS.
|
||||
- Jan.13, 2022: support SVS, release PopCS dataset.
|
||||
- Dec.19, 2021: support TTS. [HuggingFace🤗 TTS](https://huggingface.co/spaces/NATSpeech/DiffSpeech)
|
||||
|
||||
:rocket: **News**:
|
||||
- Feb.24, 2022: Our new work, NeuralSVB was accepted by ACL-2022 [](https://arxiv.org/abs/2202.13277). [Demo Page](https://neuralsvb.github.io).
|
||||
- Dec.01, 2021: DiffSinger was accepted by AAAI-2022.
|
||||
- Sep.29, 2021: Our recent work `PortaSpeech: Portable and High-Quality Generative Text-to-Speech` was accepted by NeurIPS-2021 [](https://arxiv.org/abs/2109.15166) .
|
||||
- May.06, 2021: We submitted DiffSinger to Arxiv [](https://arxiv.org/abs/2105.02446).
|
||||
|
||||
## Environments
|
||||
1. If you want to use env of anaconda:
|
||||
```sh
|
||||
conda create -n your_env_name python=3.8
|
||||
source activate your_env_name
|
||||
pip install -r requirements_2080.txt (GPU 2080Ti, CUDA 10.2)
|
||||
or pip install -r requirements_3090.txt (GPU 3090, CUDA 11.4)
|
||||
```
|
||||
|
||||
2. Or, if you want to use virtual env of python:
|
||||
```sh
|
||||
## Install Python 3.8 first.
|
||||
python -m venv venv
|
||||
source venv/bin/activate
|
||||
# install requirements.
|
||||
pip install -U pip
|
||||
pip install Cython numpy==1.19.1
|
||||
pip install torch==1.9.0
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Documents
|
||||
- [Run DiffSpeech (TTS version)](docs/README-TTS.md).
|
||||
- [Run DiffSinger (SVS version)](docs/README-SVS.md).
|
||||
|
||||
## Overview
|
||||
| Mel Pipeline | Dataset | Pitch Input | F0 Prediction | Acceleration Method | Vocoder |
|
||||
| ------------------------------------------------------------------------------------------- | ---------------------------------------------------------| ----------------- | ------------- | --------------------------- | ----------------------------- |
|
||||
| [DiffSpeech (Text->F0, Text+F0->Mel, Mel->Wav)](docs/README-TTS.md) | [Ljspeech](https://keithito.com/LJ-Speech-Dataset/) | None | Explicit | Shallow Diffusion | HiFiGAN |
|
||||
| [DiffSinger (Lyric+F0->Mel, Mel->Wav)](docs/README-SVS-popcs.md) | [PopCS](https://github.com/MoonInTheRiver/DiffSinger) | Ground-Truth F0 | None | Shallow Diffusion | NSF-HiFiGAN |
|
||||
| [DiffSinger (Lyric+MIDI->F0, Lyric+F0->Mel, Mel->Wav)](docs/README-SVS-opencpop-cascade.md) | [OpenCpop](https://wenet.org.cn/opencpop/) | MIDI | Explicit | Shallow Diffusion | NSF-HiFiGAN |
|
||||
| [FFT-Singer (Lyric+MIDI->F0, Lyric+F0->Mel, Mel->Wav)](docs/README-SVS-opencpop-cascade.md) | [OpenCpop](https://wenet.org.cn/opencpop/) | MIDI | Explicit | Invalid | NSF-HiFiGAN |
|
||||
| [DiffSinger (Lyric+MIDI->Mel, Mel->Wav)](docs/README-SVS-opencpop-e2e.md) | [OpenCpop](https://wenet.org.cn/opencpop/) | MIDI | Implicit | None | Pitch-Extractor + NSF-HiFiGAN |
|
||||
| [DiffSinger+PNDM (Lyric+MIDI->Mel, Mel->Wav)](docs/README-SVS-opencpop-pndm.md) | [OpenCpop](https://wenet.org.cn/opencpop/) | MIDI | Implicit | PLMS | Pitch-Extractor + NSF-HiFiGAN |
|
||||
| [DiffSpeech+PNDM (Text->Mel, Mel->Wav)](docs/README-TTS-pndm.md) | [Ljspeech](https://keithito.com/LJ-Speech-Dataset/) | None | Implicit | PLMS | HiFiGAN |
|
||||
|
||||
|
||||
## Tensorboard
|
||||
```sh
|
||||
tensorboard --logdir_spec exp_name
|
||||
```
|
||||
<table style="width:100%">
|
||||
<tr>
|
||||
<td><img src="resources/tfb.png" alt="Tensorboard" height="250"></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
## Citation
|
||||
@article{liu2021diffsinger,
|
||||
title={Diffsinger: Singing voice synthesis via shallow diffusion mechanism},
|
||||
author={Liu, Jinglin and Li, Chengxi and Ren, Yi and Chen, Feiyang and Liu, Peng and Zhao, Zhou},
|
||||
journal={arXiv preprint arXiv:2105.02446},
|
||||
volume={2},
|
||||
year={2021}}
|
||||
|
||||
|
||||
## Acknowledgements
|
||||
* lucidrains' [denoising-diffusion-pytorch](https://github.com/lucidrains/denoising-diffusion-pytorch)
|
||||
* Official [PyTorch Lightning](https://github.com/PyTorchLightning/pytorch-lightning)
|
||||
* kan-bayashi's [ParallelWaveGAN](https://github.com/kan-bayashi/ParallelWaveGAN)
|
||||
* jik876's [HifiGAN](https://github.com/jik876/hifi-gan)
|
||||
* Official [espnet](https://github.com/espnet/espnet)
|
||||
* lmnt-com's [DiffWave](https://github.com/lmnt-com/diffwave)
|
||||
* keonlee9420's [Implementation](https://github.com/keonlee9420/DiffSinger).
|
||||
|
||||
Especially thanks to:
|
||||
|
||||
* Team Openvpi's maintenance: [DiffSinger](https://github.com/openvpi/DiffSinger).
|
||||
* Your re-creation and sharing.
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# WeHub 来源说明
|
||||
|
||||
- 原始项目:`MoonInTheRiver/DiffSinger`
|
||||
- 原始仓库:https://github.com/MoonInTheRiver/DiffSinger
|
||||
- 导入方式:上游默认分支的最新快照
|
||||
- 原作者、版权和许可证信息以原始仓库及本仓库 LICENSE 为准
|
||||
- 本文件仅用于记录来源,不代表 WeHub 是原项目作者
|
||||
@@ -0,0 +1,42 @@
|
||||
# task
|
||||
binary_data_dir: ''
|
||||
work_dir: '' # experiment directory.
|
||||
infer: false # infer
|
||||
seed: 1234
|
||||
debug: false
|
||||
save_codes:
|
||||
- configs
|
||||
- modules
|
||||
- tasks
|
||||
- utils
|
||||
- usr
|
||||
|
||||
#############
|
||||
# dataset
|
||||
#############
|
||||
ds_workers: 1
|
||||
test_num: 100
|
||||
valid_num: 100
|
||||
endless_ds: false
|
||||
sort_by_len: true
|
||||
|
||||
#########
|
||||
# train and eval
|
||||
#########
|
||||
load_ckpt: ''
|
||||
save_ckpt: true
|
||||
save_best: false
|
||||
num_ckpt_keep: 3
|
||||
clip_grad_norm: 0
|
||||
accumulate_grad_batches: 1
|
||||
log_interval: 100
|
||||
num_sanity_val_steps: 5 # steps of validation at the beginning
|
||||
check_val_every_n_epoch: 10
|
||||
val_check_interval: 2000
|
||||
max_epochs: 1000
|
||||
max_updates: 160000
|
||||
max_tokens: 31250
|
||||
max_sentences: 100000
|
||||
max_eval_tokens: -1
|
||||
max_eval_sentences: -1
|
||||
test_input_dir: ''
|
||||
@@ -0,0 +1,42 @@
|
||||
base_config:
|
||||
- configs/tts/base.yaml
|
||||
- configs/tts/base_zh.yaml
|
||||
|
||||
|
||||
datasets: []
|
||||
test_prefixes: []
|
||||
test_num: 0
|
||||
valid_num: 0
|
||||
|
||||
pre_align_cls: data_gen.singing.pre_align.SingingPreAlign
|
||||
binarizer_cls: data_gen.singing.binarize.SingingBinarizer
|
||||
pre_align_args:
|
||||
use_tone: false # for ZH
|
||||
forced_align: mfa
|
||||
use_sox: true
|
||||
hop_size: 128 # Hop size.
|
||||
fft_size: 512 # FFT size.
|
||||
win_size: 512 # FFT size.
|
||||
max_frames: 8000
|
||||
fmin: 50 # Minimum freq in mel basis calculation.
|
||||
fmax: 11025 # Maximum frequency in mel basis calculation.
|
||||
pitch_type: frame
|
||||
|
||||
hidden_size: 256
|
||||
mel_loss: "ssim:0.5|l1:0.5"
|
||||
lambda_f0: 0.0
|
||||
lambda_uv: 0.0
|
||||
lambda_energy: 0.0
|
||||
lambda_ph_dur: 0.0
|
||||
lambda_sent_dur: 0.0
|
||||
lambda_word_dur: 0.0
|
||||
predictor_grad: 0.0
|
||||
use_spk_embed: true
|
||||
use_spk_id: false
|
||||
|
||||
max_tokens: 20000
|
||||
max_updates: 400000
|
||||
num_spk: 100
|
||||
save_f0: true
|
||||
use_gt_dur: true
|
||||
use_gt_f0: true
|
||||
@@ -0,0 +1,3 @@
|
||||
base_config:
|
||||
- configs/tts/fs2.yaml
|
||||
- configs/singing/base.yaml
|
||||
@@ -0,0 +1,95 @@
|
||||
# task
|
||||
base_config: configs/config_base.yaml
|
||||
task_cls: ''
|
||||
#############
|
||||
# dataset
|
||||
#############
|
||||
raw_data_dir: ''
|
||||
processed_data_dir: ''
|
||||
binary_data_dir: ''
|
||||
dict_dir: ''
|
||||
pre_align_cls: ''
|
||||
binarizer_cls: data_gen.tts.base_binarizer.BaseBinarizer
|
||||
pre_align_args:
|
||||
use_tone: true # for ZH
|
||||
forced_align: mfa
|
||||
use_sox: false
|
||||
txt_processor: en
|
||||
allow_no_txt: false
|
||||
denoise: false
|
||||
binarization_args:
|
||||
shuffle: false
|
||||
with_txt: true
|
||||
with_wav: false
|
||||
with_align: true
|
||||
with_spk_embed: true
|
||||
with_f0: true
|
||||
with_f0cwt: true
|
||||
|
||||
loud_norm: false
|
||||
endless_ds: true
|
||||
reset_phone_dict: true
|
||||
|
||||
test_num: 100
|
||||
valid_num: 100
|
||||
max_frames: 1550
|
||||
max_input_tokens: 1550
|
||||
audio_num_mel_bins: 80
|
||||
audio_sample_rate: 22050
|
||||
hop_size: 256 # For 22050Hz, 275 ~= 12.5 ms (0.0125 * sample_rate)
|
||||
win_size: 1024 # For 22050Hz, 1100 ~= 50 ms (If None, win_size: fft_size) (0.05 * sample_rate)
|
||||
fmin: 80 # Set this to 55 if your speaker is male! if female, 95 should help taking off noise. (To test depending on dataset. Pitch info: male~[65, 260], female~[100, 525])
|
||||
fmax: 7600 # To be increased/reduced depending on data.
|
||||
fft_size: 1024 # Extra window size is filled with 0 paddings to match this parameter
|
||||
min_level_db: -100
|
||||
num_spk: 1
|
||||
mel_vmin: -6
|
||||
mel_vmax: 1.5
|
||||
ds_workers: 4
|
||||
|
||||
#########
|
||||
# model
|
||||
#########
|
||||
dropout: 0.1
|
||||
enc_layers: 4
|
||||
dec_layers: 4
|
||||
hidden_size: 384
|
||||
num_heads: 2
|
||||
prenet_dropout: 0.5
|
||||
prenet_hidden_size: 256
|
||||
stop_token_weight: 5.0
|
||||
enc_ffn_kernel_size: 9
|
||||
dec_ffn_kernel_size: 9
|
||||
ffn_act: gelu
|
||||
ffn_padding: 'SAME'
|
||||
|
||||
|
||||
###########
|
||||
# optimization
|
||||
###########
|
||||
lr: 2.0
|
||||
warmup_updates: 8000
|
||||
optimizer_adam_beta1: 0.9
|
||||
optimizer_adam_beta2: 0.98
|
||||
weight_decay: 0
|
||||
clip_grad_norm: 1
|
||||
|
||||
|
||||
###########
|
||||
# train and eval
|
||||
###########
|
||||
max_tokens: 30000
|
||||
max_sentences: 100000
|
||||
max_eval_sentences: 1
|
||||
max_eval_tokens: 60000
|
||||
train_set_name: 'train'
|
||||
valid_set_name: 'valid'
|
||||
test_set_name: 'test'
|
||||
vocoder: pwg
|
||||
vocoder_ckpt: ''
|
||||
profile_infer: false
|
||||
out_wav_norm: false
|
||||
save_gt: false
|
||||
save_f0: false
|
||||
gen_dir_name: ''
|
||||
use_denoise: false
|
||||
@@ -0,0 +1,3 @@
|
||||
pre_align_args:
|
||||
txt_processor: zh_g2pM
|
||||
binarizer_cls: data_gen.tts.binarizer_zh.ZhBinarizer
|
||||
@@ -0,0 +1,80 @@
|
||||
base_config: configs/tts/base.yaml
|
||||
task_cls: tasks.tts.fs2.FastSpeech2Task
|
||||
|
||||
# model
|
||||
hidden_size: 256
|
||||
dropout: 0.1
|
||||
encoder_type: fft # fft|tacotron|tacotron2|conformer
|
||||
encoder_K: 8 # for tacotron encoder
|
||||
decoder_type: fft # fft|rnn|conv|conformer
|
||||
use_pos_embed: true
|
||||
|
||||
# duration
|
||||
predictor_hidden: -1
|
||||
predictor_kernel: 5
|
||||
predictor_layers: 2
|
||||
dur_predictor_kernel: 3
|
||||
dur_predictor_layers: 2
|
||||
predictor_dropout: 0.5
|
||||
|
||||
# pitch and energy
|
||||
use_pitch_embed: true
|
||||
pitch_type: ph # frame|ph|cwt
|
||||
use_uv: true
|
||||
cwt_hidden_size: 128
|
||||
cwt_layers: 2
|
||||
cwt_loss: l1
|
||||
cwt_add_f0_loss: false
|
||||
cwt_std_scale: 0.8
|
||||
|
||||
pitch_ar: false
|
||||
#pitch_embed_type: 0q
|
||||
pitch_loss: 'l1' # l1|l2|ssim
|
||||
pitch_norm: log
|
||||
use_energy_embed: false
|
||||
|
||||
# reference encoder and speaker embedding
|
||||
use_spk_id: false
|
||||
use_split_spk_id: false
|
||||
use_spk_embed: false
|
||||
use_var_enc: false
|
||||
lambda_commit: 0.25
|
||||
ref_norm_layer: bn
|
||||
pitch_enc_hidden_stride_kernel:
|
||||
- 0,2,5 # conv_hidden_size, conv_stride, conv_kernel_size. conv_hidden_size=0: use hidden_size
|
||||
- 0,2,5
|
||||
- 0,2,5
|
||||
dur_enc_hidden_stride_kernel:
|
||||
- 0,2,3 # conv_hidden_size, conv_stride, conv_kernel_size. conv_hidden_size=0: use hidden_size
|
||||
- 0,2,3
|
||||
- 0,1,3
|
||||
|
||||
|
||||
# mel
|
||||
mel_loss: l1:0.5|ssim:0.5 # l1|l2|gdl|ssim or l1:0.5|ssim:0.5
|
||||
|
||||
# loss lambda
|
||||
lambda_f0: 1.0
|
||||
lambda_uv: 1.0
|
||||
lambda_energy: 0.1
|
||||
lambda_ph_dur: 1.0
|
||||
lambda_sent_dur: 1.0
|
||||
lambda_word_dur: 1.0
|
||||
predictor_grad: 0.1
|
||||
|
||||
# train and eval
|
||||
pretrain_fs_ckpt: ''
|
||||
warmup_updates: 2000
|
||||
max_tokens: 32000
|
||||
max_sentences: 100000
|
||||
max_eval_sentences: 1
|
||||
max_updates: 120000
|
||||
num_valid_plots: 5
|
||||
num_test_samples: 0
|
||||
test_ids: []
|
||||
use_gt_dur: false
|
||||
use_gt_f0: false
|
||||
|
||||
# exp
|
||||
dur_loss: mse # huber|mol
|
||||
norm_type: gn
|
||||
@@ -0,0 +1,21 @@
|
||||
base_config: configs/tts/pwg.yaml
|
||||
task_cls: tasks.vocoder.hifigan.HifiGanTask
|
||||
resblock: "1"
|
||||
adam_b1: 0.8
|
||||
adam_b2: 0.99
|
||||
upsample_rates: [ 8,8,2,2 ]
|
||||
upsample_kernel_sizes: [ 16,16,4,4 ]
|
||||
upsample_initial_channel: 128
|
||||
resblock_kernel_sizes: [ 3,7,11 ]
|
||||
resblock_dilation_sizes: [ [ 1,3,5 ], [ 1,3,5 ], [ 1,3,5 ] ]
|
||||
|
||||
lambda_mel: 45.0
|
||||
|
||||
max_samples: 8192
|
||||
max_sentences: 16
|
||||
|
||||
generator_params:
|
||||
lr: 0.0002 # Generator's learning rate.
|
||||
aux_context_window: 0 # Context window size for auxiliary feature.
|
||||
discriminator_optimizer_params:
|
||||
lr: 0.0002 # Discriminator's learning rate.
|
||||
@@ -0,0 +1,3 @@
|
||||
raw_data_dir: 'data/raw/LJSpeech-1.1'
|
||||
processed_data_dir: 'data/processed/ljspeech'
|
||||
binary_data_dir: 'data/binary/ljspeech_wav'
|
||||
@@ -0,0 +1,13 @@
|
||||
raw_data_dir: 'data/raw/LJSpeech-1.1'
|
||||
processed_data_dir: 'data/processed/ljspeech'
|
||||
binary_data_dir: 'data/binary/ljspeech'
|
||||
pre_align_cls: data_gen.tts.lj.pre_align.LJPreAlign
|
||||
|
||||
pitch_type: cwt
|
||||
mel_loss: l1
|
||||
num_test_samples: 20
|
||||
test_ids: [ 68, 70, 74, 87, 110, 172, 190, 215, 231, 294,
|
||||
316, 324, 402, 422, 485, 500, 505, 508, 509, 519 ]
|
||||
use_energy_embed: false
|
||||
test_num: 523
|
||||
valid_num: 348
|
||||
@@ -0,0 +1,3 @@
|
||||
base_config:
|
||||
- configs/tts/fs2.yaml
|
||||
- configs/tts/lj/base_text2mel.yaml
|
||||
@@ -0,0 +1,3 @@
|
||||
base_config:
|
||||
- configs/tts/hifigan.yaml
|
||||
- configs/tts/lj/base_mel2wav.yaml
|
||||
@@ -0,0 +1,3 @@
|
||||
base_config:
|
||||
- configs/tts/pwg.yaml
|
||||
- configs/tts/lj/base_mel2wav.yaml
|
||||
@@ -0,0 +1,110 @@
|
||||
base_config: configs/tts/base.yaml
|
||||
task_cls: tasks.vocoder.pwg.PwgTask
|
||||
|
||||
binarization_args:
|
||||
with_wav: true
|
||||
with_spk_embed: false
|
||||
with_align: false
|
||||
test_input_dir: ''
|
||||
|
||||
###########
|
||||
# train and eval
|
||||
###########
|
||||
max_samples: 25600
|
||||
max_sentences: 5
|
||||
max_eval_sentences: 1
|
||||
max_updates: 1000000
|
||||
val_check_interval: 2000
|
||||
|
||||
|
||||
###########################################################
|
||||
# FEATURE EXTRACTION SETTING #
|
||||
###########################################################
|
||||
sampling_rate: 22050 # Sampling rate.
|
||||
fft_size: 1024 # FFT size.
|
||||
hop_size: 256 # Hop size.
|
||||
win_length: null # Window length.
|
||||
# If set to null, it will be the same as fft_size.
|
||||
window: "hann" # Window function.
|
||||
num_mels: 80 # Number of mel basis.
|
||||
fmin: 80 # Minimum freq in mel basis calculation.
|
||||
fmax: 7600 # Maximum frequency in mel basis calculation.
|
||||
format: "hdf5" # Feature file format. "npy" or "hdf5" is supported.
|
||||
|
||||
###########################################################
|
||||
# GENERATOR NETWORK ARCHITECTURE SETTING #
|
||||
###########################################################
|
||||
generator_params:
|
||||
in_channels: 1 # Number of input channels.
|
||||
out_channels: 1 # Number of output channels.
|
||||
kernel_size: 3 # Kernel size of dilated convolution.
|
||||
layers: 30 # Number of residual block layers.
|
||||
stacks: 3 # Number of stacks i.e., dilation cycles.
|
||||
residual_channels: 64 # Number of channels in residual conv.
|
||||
gate_channels: 128 # Number of channels in gated conv.
|
||||
skip_channels: 64 # Number of channels in skip conv.
|
||||
aux_channels: 80 # Number of channels for auxiliary feature conv.
|
||||
# Must be the same as num_mels.
|
||||
aux_context_window: 2 # Context window size for auxiliary feature.
|
||||
# If set to 2, previous 2 and future 2 frames will be considered.
|
||||
dropout: 0.0 # Dropout rate. 0.0 means no dropout applied.
|
||||
use_weight_norm: true # Whether to use weight norm.
|
||||
# If set to true, it will be applied to all of the conv layers.
|
||||
upsample_net: "ConvInUpsampleNetwork" # Upsampling network architecture.
|
||||
upsample_params: # Upsampling network parameters.
|
||||
upsample_scales: [4, 4, 4, 4] # Upsampling scales. Prodcut of these must be the same as hop size.
|
||||
use_pitch_embed: false
|
||||
|
||||
###########################################################
|
||||
# DISCRIMINATOR NETWORK ARCHITECTURE SETTING #
|
||||
###########################################################
|
||||
discriminator_params:
|
||||
in_channels: 1 # Number of input channels.
|
||||
out_channels: 1 # Number of output channels.
|
||||
kernel_size: 3 # Number of output channels.
|
||||
layers: 10 # Number of conv layers.
|
||||
conv_channels: 64 # Number of chnn layers.
|
||||
bias: true # Whether to use bias parameter in conv.
|
||||
use_weight_norm: true # Whether to use weight norm.
|
||||
# If set to true, it will be applied to all of the conv layers.
|
||||
nonlinear_activation: "LeakyReLU" # Nonlinear function after each conv.
|
||||
nonlinear_activation_params: # Nonlinear function parameters
|
||||
negative_slope: 0.2 # Alpha in LeakyReLU.
|
||||
|
||||
###########################################################
|
||||
# STFT LOSS SETTING #
|
||||
###########################################################
|
||||
stft_loss_params:
|
||||
fft_sizes: [1024, 2048, 512] # List of FFT size for STFT-based loss.
|
||||
hop_sizes: [120, 240, 50] # List of hop size for STFT-based loss
|
||||
win_lengths: [600, 1200, 240] # List of window length for STFT-based loss.
|
||||
window: "hann_window" # Window function for STFT-based loss
|
||||
use_mel_loss: false
|
||||
|
||||
###########################################################
|
||||
# ADVERSARIAL LOSS SETTING #
|
||||
###########################################################
|
||||
lambda_adv: 4.0 # Loss balancing coefficient.
|
||||
|
||||
###########################################################
|
||||
# OPTIMIZER & SCHEDULER SETTING #
|
||||
###########################################################
|
||||
generator_optimizer_params:
|
||||
lr: 0.0001 # Generator's learning rate.
|
||||
eps: 1.0e-6 # Generator's epsilon.
|
||||
weight_decay: 0.0 # Generator's weight decay coefficient.
|
||||
generator_scheduler_params:
|
||||
step_size: 200000 # Generator's scheduler step size.
|
||||
gamma: 0.5 # Generator's scheduler gamma.
|
||||
# At each step size, lr will be multiplied by this parameter.
|
||||
generator_grad_norm: 10 # Generator's gradient norm.
|
||||
discriminator_optimizer_params:
|
||||
lr: 0.00005 # Discriminator's learning rate.
|
||||
eps: 1.0e-6 # Discriminator's epsilon.
|
||||
weight_decay: 0.0 # Discriminator's weight decay coefficient.
|
||||
discriminator_scheduler_params:
|
||||
step_size: 200000 # Discriminator's scheduler step size.
|
||||
gamma: 0.5 # Discriminator's scheduler gamma.
|
||||
# At each step size, lr will be multiplied by this parameter.
|
||||
discriminator_grad_norm: 1 # Discriminator's gradient norm.
|
||||
disc_start_steps: 40000 # Number of steps to start to train discriminator.
|
||||
@@ -0,0 +1,77 @@
|
||||
! !
|
||||
, ,
|
||||
. .
|
||||
; ;
|
||||
<BOS> <BOS>
|
||||
<EOS> <EOS>
|
||||
? ?
|
||||
AA0 AA0
|
||||
AA1 AA1
|
||||
AA2 AA2
|
||||
AE0 AE0
|
||||
AE1 AE1
|
||||
AE2 AE2
|
||||
AH0 AH0
|
||||
AH1 AH1
|
||||
AH2 AH2
|
||||
AO0 AO0
|
||||
AO1 AO1
|
||||
AO2 AO2
|
||||
AW0 AW0
|
||||
AW1 AW1
|
||||
AW2 AW2
|
||||
AY0 AY0
|
||||
AY1 AY1
|
||||
AY2 AY2
|
||||
B B
|
||||
CH CH
|
||||
D D
|
||||
DH DH
|
||||
EH0 EH0
|
||||
EH1 EH1
|
||||
EH2 EH2
|
||||
ER0 ER0
|
||||
ER1 ER1
|
||||
ER2 ER2
|
||||
EY0 EY0
|
||||
EY1 EY1
|
||||
EY2 EY2
|
||||
F F
|
||||
G G
|
||||
HH HH
|
||||
IH0 IH0
|
||||
IH1 IH1
|
||||
IH2 IH2
|
||||
IY0 IY0
|
||||
IY1 IY1
|
||||
IY2 IY2
|
||||
JH JH
|
||||
K K
|
||||
L L
|
||||
M M
|
||||
N N
|
||||
NG NG
|
||||
OW0 OW0
|
||||
OW1 OW1
|
||||
OW2 OW2
|
||||
OY0 OY0
|
||||
OY1 OY1
|
||||
OY2 OY2
|
||||
P P
|
||||
R R
|
||||
S S
|
||||
SH SH
|
||||
T T
|
||||
TH TH
|
||||
UH0 UH0
|
||||
UH1 UH1
|
||||
UH2 UH2
|
||||
UW0 UW0
|
||||
UW1 UW1
|
||||
UW2 UW2
|
||||
V V
|
||||
W W
|
||||
Y Y
|
||||
Z Z
|
||||
ZH ZH
|
||||
| |
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1 @@
|
||||
["!", ",", ".", ";", "<BOS>", "<EOS>", "?", "AA0", "AA1", "AA2", "AE0", "AE1", "AE2", "AH0", "AH1", "AH2", "AO0", "AO1", "AO2", "AW0", "AW1", "AW2", "AY0", "AY1", "AY2", "B", "CH", "D", "DH", "EH0", "EH1", "EH2", "ER0", "ER1", "ER2", "EY0", "EY1", "EY2", "F", "G", "HH", "IH0", "IH1", "IH2", "IY0", "IY1", "IY2", "JH", "K", "L", "M", "N", "NG", "OW0", "OW1", "OW2", "OY0", "OY1", "OY2", "P", "R", "S", "SH", "T", "TH", "UH0", "UH1", "UH2", "UW0", "UW1", "UW2", "V", "W", "Y", "Z", "ZH", "|"]
|
||||
@@ -0,0 +1,398 @@
|
||||
import os
|
||||
import random
|
||||
from copy import deepcopy
|
||||
import pandas as pd
|
||||
import logging
|
||||
from tqdm import tqdm
|
||||
import json
|
||||
import glob
|
||||
import re
|
||||
from resemblyzer import VoiceEncoder
|
||||
import traceback
|
||||
import numpy as np
|
||||
import pretty_midi
|
||||
import librosa
|
||||
from scipy.interpolate import interp1d
|
||||
import torch
|
||||
from textgrid import TextGrid
|
||||
|
||||
from utils.hparams import hparams
|
||||
from data_gen.tts.data_gen_utils import build_phone_encoder, get_pitch
|
||||
from utils.pitch_utils import f0_to_coarse
|
||||
from data_gen.tts.base_binarizer import BaseBinarizer, BinarizationError
|
||||
from data_gen.tts.binarizer_zh import ZhBinarizer
|
||||
from data_gen.tts.txt_processors.zh_g2pM import ALL_YUNMU
|
||||
from vocoders.base_vocoder import VOCODERS
|
||||
|
||||
|
||||
class SingingBinarizer(BaseBinarizer):
|
||||
def __init__(self, processed_data_dir=None):
|
||||
if processed_data_dir is None:
|
||||
processed_data_dir = hparams['processed_data_dir']
|
||||
self.processed_data_dirs = processed_data_dir.split(",")
|
||||
self.binarization_args = hparams['binarization_args']
|
||||
self.pre_align_args = hparams['pre_align_args']
|
||||
self.item2txt = {}
|
||||
self.item2ph = {}
|
||||
self.item2wavfn = {}
|
||||
self.item2f0fn = {}
|
||||
self.item2tgfn = {}
|
||||
self.item2spk = {}
|
||||
|
||||
def split_train_test_set(self, item_names):
|
||||
item_names = deepcopy(item_names)
|
||||
test_item_names = [x for x in item_names if any([ts in x for ts in hparams['test_prefixes']])]
|
||||
train_item_names = [x for x in item_names if x not in set(test_item_names)]
|
||||
logging.info("train {}".format(len(train_item_names)))
|
||||
logging.info("test {}".format(len(test_item_names)))
|
||||
return train_item_names, test_item_names
|
||||
|
||||
def load_meta_data(self):
|
||||
for ds_id, processed_data_dir in enumerate(self.processed_data_dirs):
|
||||
wav_suffix = '_wf0.wav'
|
||||
txt_suffix = '.txt'
|
||||
ph_suffix = '_ph.txt'
|
||||
tg_suffix = '.TextGrid'
|
||||
all_wav_pieces = glob.glob(f'{processed_data_dir}/*/*{wav_suffix}')
|
||||
|
||||
for piece_path in all_wav_pieces:
|
||||
item_name = raw_item_name = piece_path[len(processed_data_dir)+1:].replace('/', '-')[:-len(wav_suffix)]
|
||||
if len(self.processed_data_dirs) > 1:
|
||||
item_name = f'ds{ds_id}_{item_name}'
|
||||
self.item2txt[item_name] = open(f'{piece_path.replace(wav_suffix, txt_suffix)}').readline()
|
||||
self.item2ph[item_name] = open(f'{piece_path.replace(wav_suffix, ph_suffix)}').readline()
|
||||
self.item2wavfn[item_name] = piece_path
|
||||
|
||||
self.item2spk[item_name] = re.split('-|#', piece_path.split('/')[-2])[0]
|
||||
if len(self.processed_data_dirs) > 1:
|
||||
self.item2spk[item_name] = f"ds{ds_id}_{self.item2spk[item_name]}"
|
||||
self.item2tgfn[item_name] = piece_path.replace(wav_suffix, tg_suffix)
|
||||
print('spkers: ', set(self.item2spk.values()))
|
||||
self.item_names = sorted(list(self.item2txt.keys()))
|
||||
if self.binarization_args['shuffle']:
|
||||
random.seed(1234)
|
||||
random.shuffle(self.item_names)
|
||||
self._train_item_names, self._test_item_names = self.split_train_test_set(self.item_names)
|
||||
|
||||
@property
|
||||
def train_item_names(self):
|
||||
return self._train_item_names
|
||||
|
||||
@property
|
||||
def valid_item_names(self):
|
||||
return self._test_item_names
|
||||
|
||||
@property
|
||||
def test_item_names(self):
|
||||
return self._test_item_names
|
||||
|
||||
def process(self):
|
||||
self.load_meta_data()
|
||||
os.makedirs(hparams['binary_data_dir'], exist_ok=True)
|
||||
self.spk_map = self.build_spk_map()
|
||||
print("| spk_map: ", self.spk_map)
|
||||
spk_map_fn = f"{hparams['binary_data_dir']}/spk_map.json"
|
||||
json.dump(self.spk_map, open(spk_map_fn, 'w'))
|
||||
|
||||
self.phone_encoder = self._phone_encoder()
|
||||
self.process_data('valid')
|
||||
self.process_data('test')
|
||||
self.process_data('train')
|
||||
|
||||
def _phone_encoder(self):
|
||||
ph_set_fn = f"{hparams['binary_data_dir']}/phone_set.json"
|
||||
ph_set = []
|
||||
if hparams['reset_phone_dict'] or not os.path.exists(ph_set_fn):
|
||||
for ph_sent in self.item2ph.values():
|
||||
ph_set += ph_sent.split(' ')
|
||||
ph_set = sorted(set(ph_set))
|
||||
json.dump(ph_set, open(ph_set_fn, 'w'))
|
||||
print("| Build phone set: ", ph_set)
|
||||
else:
|
||||
ph_set = json.load(open(ph_set_fn, 'r'))
|
||||
print("| Load phone set: ", ph_set)
|
||||
return build_phone_encoder(hparams['binary_data_dir'])
|
||||
|
||||
# @staticmethod
|
||||
# def get_pitch(wav_fn, spec, res):
|
||||
# wav_suffix = '_wf0.wav'
|
||||
# f0_suffix = '_f0.npy'
|
||||
# f0fn = wav_fn.replace(wav_suffix, f0_suffix)
|
||||
# pitch_info = np.load(f0fn)
|
||||
# f0 = [x[1] for x in pitch_info]
|
||||
# spec_x_coor = np.arange(0, 1, 1 / len(spec))[:len(spec)]
|
||||
# f0_x_coor = np.arange(0, 1, 1 / len(f0))[:len(f0)]
|
||||
# f0 = interp1d(f0_x_coor, f0, 'nearest', fill_value='extrapolate')(spec_x_coor)[:len(spec)]
|
||||
# # f0_x_coor = np.arange(0, 1, 1 / len(f0))
|
||||
# # f0_x_coor[-1] = 1
|
||||
# # f0 = interp1d(f0_x_coor, f0, 'nearest')(spec_x_coor)[:len(spec)]
|
||||
# if sum(f0) == 0:
|
||||
# raise BinarizationError("Empty f0")
|
||||
# assert len(f0) == len(spec), (len(f0), len(spec))
|
||||
# pitch_coarse = f0_to_coarse(f0)
|
||||
#
|
||||
# # vis f0
|
||||
# # import matplotlib.pyplot as plt
|
||||
# # from textgrid import TextGrid
|
||||
# # tg_fn = wav_fn.replace(wav_suffix, '.TextGrid')
|
||||
# # fig = plt.figure(figsize=(12, 6))
|
||||
# # plt.pcolor(spec.T, vmin=-5, vmax=0)
|
||||
# # ax = plt.gca()
|
||||
# # ax2 = ax.twinx()
|
||||
# # ax2.plot(f0, color='red')
|
||||
# # ax2.set_ylim(0, 800)
|
||||
# # itvs = TextGrid.fromFile(tg_fn)[0]
|
||||
# # for itv in itvs:
|
||||
# # x = itv.maxTime * hparams['audio_sample_rate'] / hparams['hop_size']
|
||||
# # plt.vlines(x=x, ymin=0, ymax=80, color='black')
|
||||
# # plt.text(x=x, y=20, s=itv.mark, color='black')
|
||||
# # plt.savefig('tmp/20211229_singing_plots_test.png')
|
||||
#
|
||||
# res['f0'] = f0
|
||||
# res['pitch'] = pitch_coarse
|
||||
|
||||
@classmethod
|
||||
def process_item(cls, item_name, ph, txt, tg_fn, wav_fn, spk_id, encoder, binarization_args):
|
||||
if hparams['vocoder'] in VOCODERS:
|
||||
wav, mel = VOCODERS[hparams['vocoder']].wav2spec(wav_fn)
|
||||
else:
|
||||
wav, mel = VOCODERS[hparams['vocoder'].split('.')[-1]].wav2spec(wav_fn)
|
||||
res = {
|
||||
'item_name': item_name, 'txt': txt, 'ph': ph, 'mel': mel, 'wav': wav, 'wav_fn': wav_fn,
|
||||
'sec': len(wav) / hparams['audio_sample_rate'], 'len': mel.shape[0], 'spk_id': spk_id
|
||||
}
|
||||
try:
|
||||
if binarization_args['with_f0']:
|
||||
# cls.get_pitch(wav_fn, mel, res)
|
||||
cls.get_pitch(wav, mel, res)
|
||||
if binarization_args['with_txt']:
|
||||
try:
|
||||
# print(ph)
|
||||
phone_encoded = res['phone'] = encoder.encode(ph)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
raise BinarizationError(f"Empty phoneme")
|
||||
if binarization_args['with_align']:
|
||||
cls.get_align(tg_fn, ph, mel, phone_encoded, res)
|
||||
except BinarizationError as e:
|
||||
print(f"| Skip item ({e}). item_name: {item_name}, wav_fn: {wav_fn}")
|
||||
return None
|
||||
return res
|
||||
|
||||
|
||||
class MidiSingingBinarizer(SingingBinarizer):
|
||||
item2midi = {}
|
||||
item2midi_dur = {}
|
||||
item2is_slur = {}
|
||||
item2ph_durs = {}
|
||||
item2wdb = {}
|
||||
|
||||
def load_meta_data(self):
|
||||
for ds_id, processed_data_dir in enumerate(self.processed_data_dirs):
|
||||
meta_midi = json.load(open(os.path.join(processed_data_dir, 'meta.json'))) # [list of dict]
|
||||
|
||||
for song_item in meta_midi:
|
||||
item_name = raw_item_name = song_item['item_name']
|
||||
if len(self.processed_data_dirs) > 1:
|
||||
item_name = f'ds{ds_id}_{item_name}'
|
||||
self.item2wavfn[item_name] = song_item['wav_fn']
|
||||
self.item2txt[item_name] = song_item['txt']
|
||||
|
||||
self.item2ph[item_name] = ' '.join(song_item['phs'])
|
||||
self.item2wdb[item_name] = [1 if x in ALL_YUNMU + ['AP', 'SP', '<SIL>'] else 0 for x in song_item['phs']]
|
||||
self.item2ph_durs[item_name] = song_item['ph_dur']
|
||||
|
||||
self.item2midi[item_name] = song_item['notes']
|
||||
self.item2midi_dur[item_name] = song_item['notes_dur']
|
||||
self.item2is_slur[item_name] = song_item['is_slur']
|
||||
self.item2spk[item_name] = 'pop-cs'
|
||||
if len(self.processed_data_dirs) > 1:
|
||||
self.item2spk[item_name] = f"ds{ds_id}_{self.item2spk[item_name]}"
|
||||
|
||||
print('spkers: ', set(self.item2spk.values()))
|
||||
self.item_names = sorted(list(self.item2txt.keys()))
|
||||
if self.binarization_args['shuffle']:
|
||||
random.seed(1234)
|
||||
random.shuffle(self.item_names)
|
||||
self._train_item_names, self._test_item_names = self.split_train_test_set(self.item_names)
|
||||
|
||||
@staticmethod
|
||||
def get_pitch(wav_fn, wav, spec, ph, res):
|
||||
wav_suffix = '.wav'
|
||||
# midi_suffix = '.mid'
|
||||
wav_dir = 'wavs'
|
||||
f0_dir = 'f0'
|
||||
|
||||
item_name = '/'.join(os.path.splitext(wav_fn)[0].split('/')[-2:]).replace('_wf0', '')
|
||||
res['pitch_midi'] = np.asarray(MidiSingingBinarizer.item2midi[item_name])
|
||||
res['midi_dur'] = np.asarray(MidiSingingBinarizer.item2midi_dur[item_name])
|
||||
res['is_slur'] = np.asarray(MidiSingingBinarizer.item2is_slur[item_name])
|
||||
res['word_boundary'] = np.asarray(MidiSingingBinarizer.item2wdb[item_name])
|
||||
assert res['pitch_midi'].shape == res['midi_dur'].shape == res['is_slur'].shape, (
|
||||
res['pitch_midi'].shape, res['midi_dur'].shape, res['is_slur'].shape)
|
||||
|
||||
# gt f0.
|
||||
gt_f0, gt_pitch_coarse = get_pitch(wav, spec, hparams)
|
||||
if sum(gt_f0) == 0:
|
||||
raise BinarizationError("Empty **gt** f0")
|
||||
res['f0'] = gt_f0
|
||||
res['pitch'] = gt_pitch_coarse
|
||||
|
||||
@staticmethod
|
||||
def get_align(ph_durs, mel, phone_encoded, res, hop_size=hparams['hop_size'], audio_sample_rate=hparams['audio_sample_rate']):
|
||||
mel2ph = np.zeros([mel.shape[0]], int)
|
||||
startTime = 0
|
||||
|
||||
for i_ph in range(len(ph_durs)):
|
||||
start_frame = int(startTime * audio_sample_rate / hop_size + 0.5)
|
||||
end_frame = int((startTime + ph_durs[i_ph]) * audio_sample_rate / hop_size + 0.5)
|
||||
mel2ph[start_frame:end_frame] = i_ph + 1
|
||||
startTime = startTime + ph_durs[i_ph]
|
||||
|
||||
# print('ph durs: ', ph_durs)
|
||||
# print('mel2ph: ', mel2ph, len(mel2ph))
|
||||
res['mel2ph'] = mel2ph
|
||||
# res['dur'] = None
|
||||
|
||||
@classmethod
|
||||
def process_item(cls, item_name, ph, txt, tg_fn, wav_fn, spk_id, encoder, binarization_args):
|
||||
if hparams['vocoder'] in VOCODERS:
|
||||
wav, mel = VOCODERS[hparams['vocoder']].wav2spec(wav_fn)
|
||||
else:
|
||||
wav, mel = VOCODERS[hparams['vocoder'].split('.')[-1]].wav2spec(wav_fn)
|
||||
res = {
|
||||
'item_name': item_name, 'txt': txt, 'ph': ph, 'mel': mel, 'wav': wav, 'wav_fn': wav_fn,
|
||||
'sec': len(wav) / hparams['audio_sample_rate'], 'len': mel.shape[0], 'spk_id': spk_id
|
||||
}
|
||||
try:
|
||||
if binarization_args['with_f0']:
|
||||
cls.get_pitch(wav_fn, wav, mel, ph, res)
|
||||
if binarization_args['with_txt']:
|
||||
try:
|
||||
phone_encoded = res['phone'] = encoder.encode(ph)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
raise BinarizationError(f"Empty phoneme")
|
||||
if binarization_args['with_align']:
|
||||
cls.get_align(MidiSingingBinarizer.item2ph_durs[item_name], mel, phone_encoded, res)
|
||||
except BinarizationError as e:
|
||||
print(f"| Skip item ({e}). item_name: {item_name}, wav_fn: {wav_fn}")
|
||||
return None
|
||||
return res
|
||||
|
||||
|
||||
class ZhSingingBinarizer(ZhBinarizer, SingingBinarizer):
|
||||
pass
|
||||
|
||||
|
||||
class OpencpopBinarizer(MidiSingingBinarizer):
|
||||
item2midi = {}
|
||||
item2midi_dur = {}
|
||||
item2is_slur = {}
|
||||
item2ph_durs = {}
|
||||
item2wdb = {}
|
||||
|
||||
def split_train_test_set(self, item_names):
|
||||
item_names = deepcopy(item_names)
|
||||
test_item_names = [x for x in item_names if any([x.startswith(ts) for ts in hparams['test_prefixes']])]
|
||||
train_item_names = [x for x in item_names if x not in set(test_item_names)]
|
||||
logging.info("train {}".format(len(train_item_names)))
|
||||
logging.info("test {}".format(len(test_item_names)))
|
||||
return train_item_names, test_item_names
|
||||
|
||||
def load_meta_data(self):
|
||||
raw_data_dir = hparams['raw_data_dir']
|
||||
# meta_midi = json.load(open(os.path.join(raw_data_dir, 'meta.json'))) # [list of dict]
|
||||
utterance_labels = open(os.path.join(raw_data_dir, 'transcriptions.txt')).readlines()
|
||||
|
||||
for utterance_label in utterance_labels:
|
||||
song_info = utterance_label.split('|')
|
||||
item_name = raw_item_name = song_info[0]
|
||||
self.item2wavfn[item_name] = f'{raw_data_dir}/wavs/{item_name}.wav'
|
||||
self.item2txt[item_name] = song_info[1]
|
||||
|
||||
self.item2ph[item_name] = song_info[2]
|
||||
# self.item2wdb[item_name] = list(np.nonzero([1 if x in ALL_YUNMU + ['AP', 'SP'] else 0 for x in song_info[2].split()])[0])
|
||||
self.item2wdb[item_name] = [1 if x in ALL_YUNMU + ['AP', 'SP'] else 0 for x in song_info[2].split()]
|
||||
self.item2ph_durs[item_name] = [float(x) for x in song_info[5].split(" ")]
|
||||
|
||||
self.item2midi[item_name] = [librosa.note_to_midi(x.split("/")[0]) if x != 'rest' else 0
|
||||
for x in song_info[3].split(" ")]
|
||||
self.item2midi_dur[item_name] = [float(x) for x in song_info[4].split(" ")]
|
||||
self.item2is_slur[item_name] = [int(x) for x in song_info[6].split(" ")]
|
||||
self.item2spk[item_name] = 'opencpop'
|
||||
|
||||
print('spkers: ', set(self.item2spk.values()))
|
||||
self.item_names = sorted(list(self.item2txt.keys()))
|
||||
if self.binarization_args['shuffle']:
|
||||
random.seed(1234)
|
||||
random.shuffle(self.item_names)
|
||||
self._train_item_names, self._test_item_names = self.split_train_test_set(self.item_names)
|
||||
|
||||
@staticmethod
|
||||
def get_pitch(wav_fn, wav, spec, ph, res):
|
||||
wav_suffix = '.wav'
|
||||
# midi_suffix = '.mid'
|
||||
wav_dir = 'wavs'
|
||||
f0_dir = 'text_f0_align'
|
||||
|
||||
item_name = os.path.splitext(os.path.basename(wav_fn))[0]
|
||||
res['pitch_midi'] = np.asarray(OpencpopBinarizer.item2midi[item_name])
|
||||
res['midi_dur'] = np.asarray(OpencpopBinarizer.item2midi_dur[item_name])
|
||||
res['is_slur'] = np.asarray(OpencpopBinarizer.item2is_slur[item_name])
|
||||
res['word_boundary'] = np.asarray(OpencpopBinarizer.item2wdb[item_name])
|
||||
assert res['pitch_midi'].shape == res['midi_dur'].shape == res['is_slur'].shape, (res['pitch_midi'].shape, res['midi_dur'].shape, res['is_slur'].shape)
|
||||
|
||||
# gt f0.
|
||||
# f0 = None
|
||||
# f0_suffix = '_f0.npy'
|
||||
# f0fn = wav_fn.replace(wav_suffix, f0_suffix).replace(wav_dir, f0_dir)
|
||||
# pitch_info = np.load(f0fn)
|
||||
# f0 = [x[1] for x in pitch_info]
|
||||
# spec_x_coor = np.arange(0, 1, 1 / len(spec))[:len(spec)]
|
||||
#
|
||||
# f0_x_coor = np.arange(0, 1, 1 / len(f0))[:len(f0)]
|
||||
# f0 = interp1d(f0_x_coor, f0, 'nearest', fill_value='extrapolate')(spec_x_coor)[:len(spec)]
|
||||
# if sum(f0) == 0:
|
||||
# raise BinarizationError("Empty **gt** f0")
|
||||
#
|
||||
# pitch_coarse = f0_to_coarse(f0)
|
||||
# res['f0'] = f0
|
||||
# res['pitch'] = pitch_coarse
|
||||
|
||||
# gt f0.
|
||||
gt_f0, gt_pitch_coarse = get_pitch(wav, spec, hparams)
|
||||
if sum(gt_f0) == 0:
|
||||
raise BinarizationError("Empty **gt** f0")
|
||||
res['f0'] = gt_f0
|
||||
res['pitch'] = gt_pitch_coarse
|
||||
|
||||
@classmethod
|
||||
def process_item(cls, item_name, ph, txt, tg_fn, wav_fn, spk_id, encoder, binarization_args):
|
||||
if hparams['vocoder'] in VOCODERS:
|
||||
wav, mel = VOCODERS[hparams['vocoder']].wav2spec(wav_fn)
|
||||
else:
|
||||
wav, mel = VOCODERS[hparams['vocoder'].split('.')[-1]].wav2spec(wav_fn)
|
||||
res = {
|
||||
'item_name': item_name, 'txt': txt, 'ph': ph, 'mel': mel, 'wav': wav, 'wav_fn': wav_fn,
|
||||
'sec': len(wav) / hparams['audio_sample_rate'], 'len': mel.shape[0], 'spk_id': spk_id
|
||||
}
|
||||
try:
|
||||
if binarization_args['with_f0']:
|
||||
cls.get_pitch(wav_fn, wav, mel, ph, res)
|
||||
if binarization_args['with_txt']:
|
||||
try:
|
||||
phone_encoded = res['phone'] = encoder.encode(ph)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
raise BinarizationError(f"Empty phoneme")
|
||||
if binarization_args['with_align']:
|
||||
cls.get_align(OpencpopBinarizer.item2ph_durs[item_name], mel, phone_encoded, res)
|
||||
except BinarizationError as e:
|
||||
print(f"| Skip item ({e}). item_name: {item_name}, wav_fn: {wav_fn}")
|
||||
return None
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
SingingBinarizer().process()
|
||||
@@ -0,0 +1,224 @@
|
||||
import os
|
||||
os.environ["OMP_NUM_THREADS"] = "1"
|
||||
|
||||
from utils.multiprocess_utils import chunked_multiprocess_run
|
||||
import random
|
||||
import traceback
|
||||
import json
|
||||
from resemblyzer import VoiceEncoder
|
||||
from tqdm import tqdm
|
||||
from data_gen.tts.data_gen_utils import get_mel2ph, get_pitch, build_phone_encoder
|
||||
from utils.hparams import set_hparams, hparams
|
||||
import numpy as np
|
||||
from utils.indexed_datasets import IndexedDatasetBuilder
|
||||
from vocoders.base_vocoder import VOCODERS
|
||||
import pandas as pd
|
||||
|
||||
|
||||
class BinarizationError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class BaseBinarizer:
|
||||
def __init__(self, processed_data_dir=None):
|
||||
if processed_data_dir is None:
|
||||
processed_data_dir = hparams['processed_data_dir']
|
||||
self.processed_data_dirs = processed_data_dir.split(",")
|
||||
self.binarization_args = hparams['binarization_args']
|
||||
self.pre_align_args = hparams['pre_align_args']
|
||||
self.forced_align = self.pre_align_args['forced_align']
|
||||
tg_dir = None
|
||||
if self.forced_align == 'mfa':
|
||||
tg_dir = 'mfa_outputs'
|
||||
if self.forced_align == 'kaldi':
|
||||
tg_dir = 'kaldi_outputs'
|
||||
self.item2txt = {}
|
||||
self.item2ph = {}
|
||||
self.item2wavfn = {}
|
||||
self.item2tgfn = {}
|
||||
self.item2spk = {}
|
||||
for ds_id, processed_data_dir in enumerate(self.processed_data_dirs):
|
||||
self.meta_df = pd.read_csv(f"{processed_data_dir}/metadata_phone.csv", dtype=str)
|
||||
for r_idx, r in self.meta_df.iterrows():
|
||||
item_name = raw_item_name = r['item_name']
|
||||
if len(self.processed_data_dirs) > 1:
|
||||
item_name = f'ds{ds_id}_{item_name}'
|
||||
self.item2txt[item_name] = r['txt']
|
||||
self.item2ph[item_name] = r['ph']
|
||||
self.item2wavfn[item_name] = os.path.join(hparams['raw_data_dir'], 'wavs', os.path.basename(r['wav_fn']).split('_')[1])
|
||||
self.item2spk[item_name] = r.get('spk', 'SPK1')
|
||||
if len(self.processed_data_dirs) > 1:
|
||||
self.item2spk[item_name] = f"ds{ds_id}_{self.item2spk[item_name]}"
|
||||
if tg_dir is not None:
|
||||
self.item2tgfn[item_name] = f"{processed_data_dir}/{tg_dir}/{raw_item_name}.TextGrid"
|
||||
self.item_names = sorted(list(self.item2txt.keys()))
|
||||
if self.binarization_args['shuffle']:
|
||||
random.seed(1234)
|
||||
random.shuffle(self.item_names)
|
||||
|
||||
@property
|
||||
def train_item_names(self):
|
||||
return self.item_names[hparams['test_num']+hparams['valid_num']:]
|
||||
|
||||
@property
|
||||
def valid_item_names(self):
|
||||
return self.item_names[0: hparams['test_num']+hparams['valid_num']] #
|
||||
|
||||
@property
|
||||
def test_item_names(self):
|
||||
return self.item_names[0: hparams['test_num']] # Audios for MOS testing are in 'test_ids'
|
||||
|
||||
def build_spk_map(self):
|
||||
spk_map = set()
|
||||
for item_name in self.item_names:
|
||||
spk_name = self.item2spk[item_name]
|
||||
spk_map.add(spk_name)
|
||||
spk_map = {x: i for i, x in enumerate(sorted(list(spk_map)))}
|
||||
assert len(spk_map) == 0 or len(spk_map) <= hparams['num_spk'], len(spk_map)
|
||||
return spk_map
|
||||
|
||||
def item_name2spk_id(self, item_name):
|
||||
return self.spk_map[self.item2spk[item_name]]
|
||||
|
||||
def _phone_encoder(self):
|
||||
ph_set_fn = f"{hparams['binary_data_dir']}/phone_set.json"
|
||||
ph_set = []
|
||||
if hparams['reset_phone_dict'] or not os.path.exists(ph_set_fn):
|
||||
for processed_data_dir in self.processed_data_dirs:
|
||||
ph_set += [x.split(' ')[0] for x in open(f'{processed_data_dir}/dict.txt').readlines()]
|
||||
ph_set = sorted(set(ph_set))
|
||||
json.dump(ph_set, open(ph_set_fn, 'w'))
|
||||
else:
|
||||
ph_set = json.load(open(ph_set_fn, 'r'))
|
||||
print("| phone set: ", ph_set)
|
||||
return build_phone_encoder(hparams['binary_data_dir'])
|
||||
|
||||
def meta_data(self, prefix):
|
||||
if prefix == 'valid':
|
||||
item_names = self.valid_item_names
|
||||
elif prefix == 'test':
|
||||
item_names = self.test_item_names
|
||||
else:
|
||||
item_names = self.train_item_names
|
||||
for item_name in item_names:
|
||||
ph = self.item2ph[item_name]
|
||||
txt = self.item2txt[item_name]
|
||||
tg_fn = self.item2tgfn.get(item_name)
|
||||
wav_fn = self.item2wavfn[item_name]
|
||||
spk_id = self.item_name2spk_id(item_name)
|
||||
yield item_name, ph, txt, tg_fn, wav_fn, spk_id
|
||||
|
||||
def process(self):
|
||||
os.makedirs(hparams['binary_data_dir'], exist_ok=True)
|
||||
self.spk_map = self.build_spk_map()
|
||||
print("| spk_map: ", self.spk_map)
|
||||
spk_map_fn = f"{hparams['binary_data_dir']}/spk_map.json"
|
||||
json.dump(self.spk_map, open(spk_map_fn, 'w'))
|
||||
|
||||
self.phone_encoder = self._phone_encoder()
|
||||
self.process_data('valid')
|
||||
self.process_data('test')
|
||||
self.process_data('train')
|
||||
|
||||
def process_data(self, prefix):
|
||||
data_dir = hparams['binary_data_dir']
|
||||
args = []
|
||||
builder = IndexedDatasetBuilder(f'{data_dir}/{prefix}')
|
||||
lengths = []
|
||||
f0s = []
|
||||
total_sec = 0
|
||||
if self.binarization_args['with_spk_embed']:
|
||||
voice_encoder = VoiceEncoder().cuda()
|
||||
|
||||
meta_data = list(self.meta_data(prefix))
|
||||
for m in meta_data:
|
||||
args.append(list(m) + [self.phone_encoder, self.binarization_args])
|
||||
num_workers = int(os.getenv('N_PROC', os.cpu_count() // 3))
|
||||
for f_id, (_, item) in enumerate(
|
||||
zip(tqdm(meta_data), chunked_multiprocess_run(self.process_item, args, num_workers=num_workers))):
|
||||
if item is None:
|
||||
continue
|
||||
item['spk_embed'] = voice_encoder.embed_utterance(item['wav']) \
|
||||
if self.binarization_args['with_spk_embed'] else None
|
||||
if not self.binarization_args['with_wav'] and 'wav' in item:
|
||||
print("del wav")
|
||||
del item['wav']
|
||||
builder.add_item(item)
|
||||
lengths.append(item['len'])
|
||||
total_sec += item['sec']
|
||||
if item.get('f0') is not None:
|
||||
f0s.append(item['f0'])
|
||||
builder.finalize()
|
||||
np.save(f'{data_dir}/{prefix}_lengths.npy', lengths)
|
||||
if len(f0s) > 0:
|
||||
f0s = np.concatenate(f0s, 0)
|
||||
f0s = f0s[f0s != 0]
|
||||
np.save(f'{data_dir}/{prefix}_f0s_mean_std.npy', [np.mean(f0s).item(), np.std(f0s).item()])
|
||||
print(f"| {prefix} total duration: {total_sec:.3f}s")
|
||||
|
||||
@classmethod
|
||||
def process_item(cls, item_name, ph, txt, tg_fn, wav_fn, spk_id, encoder, binarization_args):
|
||||
if hparams['vocoder'] in VOCODERS:
|
||||
wav, mel = VOCODERS[hparams['vocoder']].wav2spec(wav_fn)
|
||||
else:
|
||||
wav, mel = VOCODERS[hparams['vocoder'].split('.')[-1]].wav2spec(wav_fn)
|
||||
res = {
|
||||
'item_name': item_name, 'txt': txt, 'ph': ph, 'mel': mel, 'wav': wav, 'wav_fn': wav_fn,
|
||||
'sec': len(wav) / hparams['audio_sample_rate'], 'len': mel.shape[0], 'spk_id': spk_id
|
||||
}
|
||||
try:
|
||||
if binarization_args['with_f0']:
|
||||
cls.get_pitch(wav, mel, res)
|
||||
if binarization_args['with_f0cwt']:
|
||||
cls.get_f0cwt(res['f0'], res)
|
||||
if binarization_args['with_txt']:
|
||||
try:
|
||||
phone_encoded = res['phone'] = encoder.encode(ph)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
raise BinarizationError(f"Empty phoneme")
|
||||
if binarization_args['with_align']:
|
||||
cls.get_align(tg_fn, ph, mel, phone_encoded, res)
|
||||
except BinarizationError as e:
|
||||
print(f"| Skip item ({e}). item_name: {item_name}, wav_fn: {wav_fn}")
|
||||
return None
|
||||
return res
|
||||
|
||||
@staticmethod
|
||||
def get_align(tg_fn, ph, mel, phone_encoded, res):
|
||||
if tg_fn is not None and os.path.exists(tg_fn):
|
||||
mel2ph, dur = get_mel2ph(tg_fn, ph, mel, hparams)
|
||||
else:
|
||||
raise BinarizationError(f"Align not found")
|
||||
if mel2ph.max() - 1 >= len(phone_encoded):
|
||||
raise BinarizationError(
|
||||
f"Align does not match: mel2ph.max() - 1: {mel2ph.max() - 1}, len(phone_encoded): {len(phone_encoded)}")
|
||||
res['mel2ph'] = mel2ph
|
||||
res['dur'] = dur
|
||||
|
||||
@staticmethod
|
||||
def get_pitch(wav, mel, res):
|
||||
f0, pitch_coarse = get_pitch(wav, mel, hparams)
|
||||
if sum(f0) == 0:
|
||||
raise BinarizationError("Empty f0")
|
||||
res['f0'] = f0
|
||||
res['pitch'] = pitch_coarse
|
||||
|
||||
@staticmethod
|
||||
def get_f0cwt(f0, res):
|
||||
from utils.cwt import get_cont_lf0, get_lf0_cwt
|
||||
uv, cont_lf0_lpf = get_cont_lf0(f0)
|
||||
logf0s_mean_org, logf0s_std_org = np.mean(cont_lf0_lpf), np.std(cont_lf0_lpf)
|
||||
cont_lf0_lpf_norm = (cont_lf0_lpf - logf0s_mean_org) / logf0s_std_org
|
||||
Wavelet_lf0, scales = get_lf0_cwt(cont_lf0_lpf_norm)
|
||||
if np.any(np.isnan(Wavelet_lf0)):
|
||||
raise BinarizationError("NaN CWT")
|
||||
res['cwt_spec'] = Wavelet_lf0
|
||||
res['cwt_scales'] = scales
|
||||
res['f0_mean'] = logf0s_mean_org
|
||||
res['f0_std'] = logf0s_std_org
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
set_hparams()
|
||||
BaseBinarizer().process()
|
||||
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
|
||||
os.environ["OMP_NUM_THREADS"] = "1"
|
||||
|
||||
import importlib
|
||||
from utils.hparams import set_hparams, hparams
|
||||
|
||||
|
||||
def binarize():
|
||||
binarizer_cls = hparams.get("binarizer_cls", 'data_gen.tts.base_binarizer.BaseBinarizer')
|
||||
pkg = ".".join(binarizer_cls.split(".")[:-1])
|
||||
cls_name = binarizer_cls.split(".")[-1]
|
||||
binarizer_cls = getattr(importlib.import_module(pkg), cls_name)
|
||||
print("| Binarizer: ", binarizer_cls)
|
||||
binarizer_cls().process()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
set_hparams()
|
||||
binarize()
|
||||
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
|
||||
os.environ["OMP_NUM_THREADS"] = "1"
|
||||
|
||||
from data_gen.tts.txt_processors.zh_g2pM import ALL_SHENMU
|
||||
from data_gen.tts.base_binarizer import BaseBinarizer, BinarizationError
|
||||
from data_gen.tts.data_gen_utils import get_mel2ph
|
||||
from utils.hparams import set_hparams, hparams
|
||||
import numpy as np
|
||||
|
||||
|
||||
class ZhBinarizer(BaseBinarizer):
|
||||
@staticmethod
|
||||
def get_align(tg_fn, ph, mel, phone_encoded, res):
|
||||
if tg_fn is not None and os.path.exists(tg_fn):
|
||||
_, dur = get_mel2ph(tg_fn, ph, mel, hparams)
|
||||
else:
|
||||
raise BinarizationError(f"Align not found")
|
||||
ph_list = ph.split(" ")
|
||||
assert len(dur) == len(ph_list)
|
||||
mel2ph = []
|
||||
# 分隔符的时长分配给韵母
|
||||
dur_cumsum = np.pad(np.cumsum(dur), [1, 0], mode='constant', constant_values=0)
|
||||
for i in range(len(dur)):
|
||||
p = ph_list[i]
|
||||
if p[0] != '<' and not p[0].isalpha():
|
||||
uv_ = res['f0'][dur_cumsum[i]:dur_cumsum[i + 1]] == 0
|
||||
j = 0
|
||||
while j < len(uv_) and not uv_[j]:
|
||||
j += 1
|
||||
dur[i - 1] += j
|
||||
dur[i] -= j
|
||||
if dur[i] < 100:
|
||||
dur[i - 1] += dur[i]
|
||||
dur[i] = 0
|
||||
# 声母和韵母等长
|
||||
for i in range(len(dur)):
|
||||
p = ph_list[i]
|
||||
if p in ALL_SHENMU:
|
||||
p_next = ph_list[i + 1]
|
||||
if not (dur[i] > 0 and p_next[0].isalpha() and p_next not in ALL_SHENMU):
|
||||
print(f"assert dur[i] > 0 and p_next[0].isalpha() and p_next not in ALL_SHENMU, "
|
||||
f"dur[i]: {dur[i]}, p: {p}, p_next: {p_next}.")
|
||||
continue
|
||||
total = dur[i + 1] + dur[i]
|
||||
dur[i] = total // 2
|
||||
dur[i + 1] = total - dur[i]
|
||||
for i in range(len(dur)):
|
||||
mel2ph += [i + 1] * dur[i]
|
||||
mel2ph = np.array(mel2ph)
|
||||
if mel2ph.max() - 1 >= len(phone_encoded):
|
||||
raise BinarizationError(f"| Align does not match: {(mel2ph.max() - 1, len(phone_encoded))}")
|
||||
res['mel2ph'] = mel2ph
|
||||
res['dur'] = dur
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
set_hparams()
|
||||
ZhBinarizer().process()
|
||||
@@ -0,0 +1,347 @@
|
||||
import warnings
|
||||
|
||||
warnings.filterwarnings("ignore")
|
||||
|
||||
import parselmouth
|
||||
import os
|
||||
import torch
|
||||
from skimage.transform import resize
|
||||
from utils.text_encoder import TokenTextEncoder
|
||||
from utils.pitch_utils import f0_to_coarse
|
||||
import struct
|
||||
import webrtcvad
|
||||
from scipy.ndimage.morphology import binary_dilation
|
||||
import librosa
|
||||
import numpy as np
|
||||
from utils import audio
|
||||
import pyloudnorm as pyln
|
||||
import re
|
||||
import json
|
||||
from collections import OrderedDict
|
||||
|
||||
PUNCS = '!,.?;:'
|
||||
|
||||
int16_max = (2 ** 15) - 1
|
||||
|
||||
|
||||
def trim_long_silences(path, sr=None, return_raw_wav=False, norm=True, vad_max_silence_length=12):
|
||||
"""
|
||||
Ensures that segments without voice in the waveform remain no longer than a
|
||||
threshold determined by the VAD parameters in params.py.
|
||||
:param wav: the raw waveform as a numpy array of floats
|
||||
:param vad_max_silence_length: Maximum number of consecutive silent frames a segment can have.
|
||||
:return: the same waveform with silences trimmed away (length <= original wav length)
|
||||
"""
|
||||
|
||||
## Voice Activation Detection
|
||||
# Window size of the VAD. Must be either 10, 20 or 30 milliseconds.
|
||||
# This sets the granularity of the VAD. Should not need to be changed.
|
||||
sampling_rate = 16000
|
||||
wav_raw, sr = librosa.core.load(path, sr=sr)
|
||||
|
||||
if norm:
|
||||
meter = pyln.Meter(sr) # create BS.1770 meter
|
||||
loudness = meter.integrated_loudness(wav_raw)
|
||||
wav_raw = pyln.normalize.loudness(wav_raw, loudness, -20.0)
|
||||
if np.abs(wav_raw).max() > 1.0:
|
||||
wav_raw = wav_raw / np.abs(wav_raw).max()
|
||||
|
||||
wav = librosa.resample(wav_raw, sr, sampling_rate, res_type='kaiser_best')
|
||||
|
||||
vad_window_length = 30 # In milliseconds
|
||||
# Number of frames to average together when performing the moving average smoothing.
|
||||
# The larger this value, the larger the VAD variations must be to not get smoothed out.
|
||||
vad_moving_average_width = 8
|
||||
|
||||
# Compute the voice detection window size
|
||||
samples_per_window = (vad_window_length * sampling_rate) // 1000
|
||||
|
||||
# Trim the end of the audio to have a multiple of the window size
|
||||
wav = wav[:len(wav) - (len(wav) % samples_per_window)]
|
||||
|
||||
# Convert the float waveform to 16-bit mono PCM
|
||||
pcm_wave = struct.pack("%dh" % len(wav), *(np.round(wav * int16_max)).astype(np.int16))
|
||||
|
||||
# Perform voice activation detection
|
||||
voice_flags = []
|
||||
vad = webrtcvad.Vad(mode=3)
|
||||
for window_start in range(0, len(wav), samples_per_window):
|
||||
window_end = window_start + samples_per_window
|
||||
voice_flags.append(vad.is_speech(pcm_wave[window_start * 2:window_end * 2],
|
||||
sample_rate=sampling_rate))
|
||||
voice_flags = np.array(voice_flags)
|
||||
|
||||
# Smooth the voice detection with a moving average
|
||||
def moving_average(array, width):
|
||||
array_padded = np.concatenate((np.zeros((width - 1) // 2), array, np.zeros(width // 2)))
|
||||
ret = np.cumsum(array_padded, dtype=float)
|
||||
ret[width:] = ret[width:] - ret[:-width]
|
||||
return ret[width - 1:] / width
|
||||
|
||||
audio_mask = moving_average(voice_flags, vad_moving_average_width)
|
||||
audio_mask = np.round(audio_mask).astype(np.bool)
|
||||
|
||||
# Dilate the voiced regions
|
||||
audio_mask = binary_dilation(audio_mask, np.ones(vad_max_silence_length + 1))
|
||||
audio_mask = np.repeat(audio_mask, samples_per_window)
|
||||
audio_mask = resize(audio_mask, (len(wav_raw),)) > 0
|
||||
if return_raw_wav:
|
||||
return wav_raw, audio_mask, sr
|
||||
return wav_raw[audio_mask], audio_mask, sr
|
||||
|
||||
|
||||
def process_utterance(wav_path,
|
||||
fft_size=1024,
|
||||
hop_size=256,
|
||||
win_length=1024,
|
||||
window="hann",
|
||||
num_mels=80,
|
||||
fmin=80,
|
||||
fmax=7600,
|
||||
eps=1e-6,
|
||||
sample_rate=22050,
|
||||
loud_norm=False,
|
||||
min_level_db=-100,
|
||||
return_linear=False,
|
||||
trim_long_sil=False, vocoder='pwg'):
|
||||
if isinstance(wav_path, str):
|
||||
if trim_long_sil:
|
||||
wav, _, _ = trim_long_silences(wav_path, sample_rate)
|
||||
else:
|
||||
wav, _ = librosa.core.load(wav_path, sr=sample_rate)
|
||||
else:
|
||||
wav = wav_path
|
||||
|
||||
if loud_norm:
|
||||
meter = pyln.Meter(sample_rate) # create BS.1770 meter
|
||||
loudness = meter.integrated_loudness(wav)
|
||||
wav = pyln.normalize.loudness(wav, loudness, -22.0)
|
||||
if np.abs(wav).max() > 1:
|
||||
wav = wav / np.abs(wav).max()
|
||||
|
||||
# get amplitude spectrogram
|
||||
x_stft = librosa.stft(wav, n_fft=fft_size, hop_length=hop_size,
|
||||
win_length=win_length, window=window, pad_mode="constant")
|
||||
spc = np.abs(x_stft) # (n_bins, T)
|
||||
|
||||
# get mel basis
|
||||
fmin = 0 if fmin == -1 else fmin
|
||||
fmax = sample_rate / 2 if fmax == -1 else fmax
|
||||
mel_basis = librosa.filters.mel(sample_rate, fft_size, num_mels, fmin, fmax)
|
||||
mel = mel_basis @ spc
|
||||
|
||||
if vocoder == 'pwg':
|
||||
mel = np.log10(np.maximum(eps, mel)) # (n_mel_bins, T)
|
||||
else:
|
||||
assert False, f'"{vocoder}" is not in ["pwg"].'
|
||||
|
||||
l_pad, r_pad = audio.librosa_pad_lr(wav, fft_size, hop_size, 1)
|
||||
wav = np.pad(wav, (l_pad, r_pad), mode='constant', constant_values=0.0)
|
||||
wav = wav[:mel.shape[1] * hop_size]
|
||||
|
||||
if not return_linear:
|
||||
return wav, mel
|
||||
else:
|
||||
spc = audio.amp_to_db(spc)
|
||||
spc = audio.normalize(spc, {'min_level_db': min_level_db})
|
||||
return wav, mel, spc
|
||||
|
||||
|
||||
def get_pitch(wav_data, mel, hparams):
|
||||
"""
|
||||
|
||||
:param wav_data: [T]
|
||||
:param mel: [T, 80]
|
||||
:param hparams:
|
||||
:return:
|
||||
"""
|
||||
time_step = hparams['hop_size'] / hparams['audio_sample_rate'] * 1000
|
||||
f0_min = 80
|
||||
f0_max = 750
|
||||
|
||||
if hparams['hop_size'] == 128:
|
||||
pad_size = 4
|
||||
elif hparams['hop_size'] == 256:
|
||||
pad_size = 2
|
||||
else:
|
||||
assert False
|
||||
|
||||
f0 = parselmouth.Sound(wav_data, hparams['audio_sample_rate']).to_pitch_ac(
|
||||
time_step=time_step / 1000, voicing_threshold=0.6,
|
||||
pitch_floor=f0_min, pitch_ceiling=f0_max).selected_array['frequency']
|
||||
lpad = pad_size * 2
|
||||
rpad = len(mel) - len(f0) - lpad
|
||||
f0 = np.pad(f0, [[lpad, rpad]], mode='constant')
|
||||
# mel and f0 are extracted by 2 different libraries. we should force them to have the same length.
|
||||
# Attention: we find that new version of some libraries could cause ``rpad'' to be a negetive value...
|
||||
# Just to be sure, we recommend users to set up the same environments as them in requirements_auto.txt (by Anaconda)
|
||||
delta_l = len(mel) - len(f0)
|
||||
assert np.abs(delta_l) <= 8
|
||||
if delta_l > 0:
|
||||
f0 = np.concatenate([f0, [f0[-1]] * delta_l], 0)
|
||||
f0 = f0[:len(mel)]
|
||||
pitch_coarse = f0_to_coarse(f0)
|
||||
return f0, pitch_coarse
|
||||
|
||||
|
||||
def remove_empty_lines(text):
|
||||
"""remove empty lines"""
|
||||
assert (len(text) > 0)
|
||||
assert (isinstance(text, list))
|
||||
text = [t.strip() for t in text]
|
||||
if "" in text:
|
||||
text.remove("")
|
||||
return text
|
||||
|
||||
|
||||
class TextGrid(object):
|
||||
def __init__(self, text):
|
||||
text = remove_empty_lines(text)
|
||||
self.text = text
|
||||
self.line_count = 0
|
||||
self._get_type()
|
||||
self._get_time_intval()
|
||||
self._get_size()
|
||||
self.tier_list = []
|
||||
self._get_item_list()
|
||||
|
||||
def _extract_pattern(self, pattern, inc):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
pattern : regex to extract pattern
|
||||
inc : increment of line count after extraction
|
||||
Returns
|
||||
-------
|
||||
group : extracted info
|
||||
"""
|
||||
try:
|
||||
group = re.match(pattern, self.text[self.line_count]).group(1)
|
||||
self.line_count += inc
|
||||
except AttributeError:
|
||||
raise ValueError("File format error at line %d:%s" % (self.line_count, self.text[self.line_count]))
|
||||
return group
|
||||
|
||||
def _get_type(self):
|
||||
self.file_type = self._extract_pattern(r"File type = \"(.*)\"", 2)
|
||||
|
||||
def _get_time_intval(self):
|
||||
self.xmin = self._extract_pattern(r"xmin = (.*)", 1)
|
||||
self.xmax = self._extract_pattern(r"xmax = (.*)", 2)
|
||||
|
||||
def _get_size(self):
|
||||
self.size = int(self._extract_pattern(r"size = (.*)", 2))
|
||||
|
||||
def _get_item_list(self):
|
||||
"""Only supports IntervalTier currently"""
|
||||
for itemIdx in range(1, self.size + 1):
|
||||
tier = OrderedDict()
|
||||
item_list = []
|
||||
tier_idx = self._extract_pattern(r"item \[(.*)\]:", 1)
|
||||
tier_class = self._extract_pattern(r"class = \"(.*)\"", 1)
|
||||
if tier_class != "IntervalTier":
|
||||
raise NotImplementedError("Only IntervalTier class is supported currently")
|
||||
tier_name = self._extract_pattern(r"name = \"(.*)\"", 1)
|
||||
tier_xmin = self._extract_pattern(r"xmin = (.*)", 1)
|
||||
tier_xmax = self._extract_pattern(r"xmax = (.*)", 1)
|
||||
tier_size = self._extract_pattern(r"intervals: size = (.*)", 1)
|
||||
for i in range(int(tier_size)):
|
||||
item = OrderedDict()
|
||||
item["idx"] = self._extract_pattern(r"intervals \[(.*)\]", 1)
|
||||
item["xmin"] = self._extract_pattern(r"xmin = (.*)", 1)
|
||||
item["xmax"] = self._extract_pattern(r"xmax = (.*)", 1)
|
||||
item["text"] = self._extract_pattern(r"text = \"(.*)\"", 1)
|
||||
item_list.append(item)
|
||||
tier["idx"] = tier_idx
|
||||
tier["class"] = tier_class
|
||||
tier["name"] = tier_name
|
||||
tier["xmin"] = tier_xmin
|
||||
tier["xmax"] = tier_xmax
|
||||
tier["size"] = tier_size
|
||||
tier["items"] = item_list
|
||||
self.tier_list.append(tier)
|
||||
|
||||
def toJson(self):
|
||||
_json = OrderedDict()
|
||||
_json["file_type"] = self.file_type
|
||||
_json["xmin"] = self.xmin
|
||||
_json["xmax"] = self.xmax
|
||||
_json["size"] = self.size
|
||||
_json["tiers"] = self.tier_list
|
||||
return json.dumps(_json, ensure_ascii=False, indent=2)
|
||||
|
||||
|
||||
def get_mel2ph(tg_fn, ph, mel, hparams):
|
||||
ph_list = ph.split(" ")
|
||||
with open(tg_fn, "r") as f:
|
||||
tg = f.readlines()
|
||||
tg = remove_empty_lines(tg)
|
||||
tg = TextGrid(tg)
|
||||
tg = json.loads(tg.toJson())
|
||||
split = np.ones(len(ph_list) + 1, np.float) * -1
|
||||
tg_idx = 0
|
||||
ph_idx = 0
|
||||
tg_align = [x for x in tg['tiers'][-1]['items']]
|
||||
tg_align_ = []
|
||||
for x in tg_align:
|
||||
x['xmin'] = float(x['xmin'])
|
||||
x['xmax'] = float(x['xmax'])
|
||||
if x['text'] in ['sil', 'sp', '', 'SIL', 'PUNC']:
|
||||
x['text'] = ''
|
||||
if len(tg_align_) > 0 and tg_align_[-1]['text'] == '':
|
||||
tg_align_[-1]['xmax'] = x['xmax']
|
||||
continue
|
||||
tg_align_.append(x)
|
||||
tg_align = tg_align_
|
||||
tg_len = len([x for x in tg_align if x['text'] != ''])
|
||||
ph_len = len([x for x in ph_list if not is_sil_phoneme(x)])
|
||||
assert tg_len == ph_len, (tg_len, ph_len, tg_align, ph_list, tg_fn)
|
||||
while tg_idx < len(tg_align) or ph_idx < len(ph_list):
|
||||
if tg_idx == len(tg_align) and is_sil_phoneme(ph_list[ph_idx]):
|
||||
split[ph_idx] = 1e8
|
||||
ph_idx += 1
|
||||
continue
|
||||
x = tg_align[tg_idx]
|
||||
if x['text'] == '' and ph_idx == len(ph_list):
|
||||
tg_idx += 1
|
||||
continue
|
||||
assert ph_idx < len(ph_list), (tg_len, ph_len, tg_align, ph_list, tg_fn)
|
||||
ph = ph_list[ph_idx]
|
||||
if x['text'] == '' and not is_sil_phoneme(ph):
|
||||
assert False, (ph_list, tg_align)
|
||||
if x['text'] != '' and is_sil_phoneme(ph):
|
||||
ph_idx += 1
|
||||
else:
|
||||
assert (x['text'] == '' and is_sil_phoneme(ph)) \
|
||||
or x['text'].lower() == ph.lower() \
|
||||
or x['text'].lower() == 'sil', (x['text'], ph)
|
||||
split[ph_idx] = x['xmin']
|
||||
if ph_idx > 0 and split[ph_idx - 1] == -1 and is_sil_phoneme(ph_list[ph_idx - 1]):
|
||||
split[ph_idx - 1] = split[ph_idx]
|
||||
ph_idx += 1
|
||||
tg_idx += 1
|
||||
assert tg_idx == len(tg_align), (tg_idx, [x['text'] for x in tg_align])
|
||||
assert ph_idx >= len(ph_list) - 1, (ph_idx, ph_list, len(ph_list), [x['text'] for x in tg_align], tg_fn)
|
||||
mel2ph = np.zeros([mel.shape[0]], np.int)
|
||||
split[0] = 0
|
||||
split[-1] = 1e8
|
||||
for i in range(len(split) - 1):
|
||||
assert split[i] != -1 and split[i] <= split[i + 1], (split[:-1],)
|
||||
split = [int(s * hparams['audio_sample_rate'] / hparams['hop_size'] + 0.5) for s in split]
|
||||
for ph_idx in range(len(ph_list)):
|
||||
mel2ph[split[ph_idx]:split[ph_idx + 1]] = ph_idx + 1
|
||||
mel2ph_torch = torch.from_numpy(mel2ph)
|
||||
T_t = len(ph_list)
|
||||
dur = mel2ph_torch.new_zeros([T_t + 1]).scatter_add(0, mel2ph_torch, torch.ones_like(mel2ph_torch))
|
||||
dur = dur[1:].numpy()
|
||||
return mel2ph, dur
|
||||
|
||||
|
||||
def build_phone_encoder(data_dir):
|
||||
phone_list_file = os.path.join(data_dir, 'phone_set.json')
|
||||
phone_list = json.load(open(phone_list_file))
|
||||
return TokenTextEncoder(None, vocab_list=phone_list, replace_oov=',')
|
||||
|
||||
|
||||
def is_sil_phoneme(p):
|
||||
return not p[0].isalpha()
|
||||
@@ -0,0 +1,8 @@
|
||||
class BaseTxtProcessor:
|
||||
@staticmethod
|
||||
def sp_phonemes():
|
||||
return ['|']
|
||||
|
||||
@classmethod
|
||||
def process(cls, txt, pre_align_args):
|
||||
raise NotImplementedError
|
||||
@@ -0,0 +1,78 @@
|
||||
import re
|
||||
from data_gen.tts.data_gen_utils import PUNCS
|
||||
from g2p_en import G2p
|
||||
import unicodedata
|
||||
from g2p_en.expand import normalize_numbers
|
||||
from nltk import pos_tag
|
||||
from nltk.tokenize import TweetTokenizer
|
||||
|
||||
from data_gen.tts.txt_processors.base_text_processor import BaseTxtProcessor
|
||||
|
||||
|
||||
class EnG2p(G2p):
|
||||
word_tokenize = TweetTokenizer().tokenize
|
||||
|
||||
def __call__(self, text):
|
||||
# preprocessing
|
||||
words = EnG2p.word_tokenize(text)
|
||||
tokens = pos_tag(words) # tuples of (word, tag)
|
||||
|
||||
# steps
|
||||
prons = []
|
||||
for word, pos in tokens:
|
||||
if re.search("[a-z]", word) is None:
|
||||
pron = [word]
|
||||
|
||||
elif word in self.homograph2features: # Check homograph
|
||||
pron1, pron2, pos1 = self.homograph2features[word]
|
||||
if pos.startswith(pos1):
|
||||
pron = pron1
|
||||
else:
|
||||
pron = pron2
|
||||
elif word in self.cmu: # lookup CMU dict
|
||||
pron = self.cmu[word][0]
|
||||
else: # predict for oov
|
||||
pron = self.predict(word)
|
||||
|
||||
prons.extend(pron)
|
||||
prons.extend([" "])
|
||||
|
||||
return prons[:-1]
|
||||
|
||||
|
||||
class TxtProcessor(BaseTxtProcessor):
|
||||
g2p = EnG2p()
|
||||
|
||||
@staticmethod
|
||||
def preprocess_text(text):
|
||||
text = normalize_numbers(text)
|
||||
text = ''.join(char for char in unicodedata.normalize('NFD', text)
|
||||
if unicodedata.category(char) != 'Mn') # Strip accents
|
||||
text = text.lower()
|
||||
text = re.sub("[\'\"()]+", "", text)
|
||||
text = re.sub("[-]+", " ", text)
|
||||
text = re.sub(f"[^ a-z{PUNCS}]", "", text)
|
||||
text = re.sub(f" ?([{PUNCS}]) ?", r"\1", text) # !! -> !
|
||||
text = re.sub(f"([{PUNCS}])+", r"\1", text) # !! -> !
|
||||
text = text.replace("i.e.", "that is")
|
||||
text = text.replace("i.e.", "that is")
|
||||
text = text.replace("etc.", "etc")
|
||||
text = re.sub(f"([{PUNCS}])", r" \1 ", text)
|
||||
text = re.sub(rf"\s+", r" ", text)
|
||||
return text
|
||||
|
||||
@classmethod
|
||||
def process(cls, txt, pre_align_args):
|
||||
txt = cls.preprocess_text(txt).strip()
|
||||
phs = cls.g2p(txt)
|
||||
phs_ = []
|
||||
n_word_sep = 0
|
||||
for p in phs:
|
||||
if p.strip() == '':
|
||||
phs_ += ['|']
|
||||
n_word_sep += 1
|
||||
else:
|
||||
phs_ += p.split(" ")
|
||||
phs = phs_
|
||||
assert n_word_sep + 1 == len(txt.split(" ")), (phs, f"\"{txt}\"")
|
||||
return phs, txt
|
||||
@@ -0,0 +1,41 @@
|
||||
import re
|
||||
from pypinyin import pinyin, Style
|
||||
from data_gen.tts.data_gen_utils import PUNCS
|
||||
from data_gen.tts.txt_processors.base_text_processor import BaseTxtProcessor
|
||||
from utils.text_norm import NSWNormalizer
|
||||
|
||||
|
||||
class TxtProcessor(BaseTxtProcessor):
|
||||
table = {ord(f): ord(t) for f, t in zip(
|
||||
u':,。!?【】()%#@&1234567890',
|
||||
u':,.!?[]()%#@&1234567890')}
|
||||
|
||||
@staticmethod
|
||||
def preprocess_text(text):
|
||||
text = text.translate(TxtProcessor.table)
|
||||
text = NSWNormalizer(text).normalize(remove_punc=False)
|
||||
text = re.sub("[\'\"()]+", "", text)
|
||||
text = re.sub("[-]+", " ", text)
|
||||
text = re.sub(f"[^ A-Za-z\u4e00-\u9fff{PUNCS}]", "", text)
|
||||
text = re.sub(f"([{PUNCS}])+", r"\1", text) # !! -> !
|
||||
text = re.sub(f"([{PUNCS}])", r" \1 ", text)
|
||||
text = re.sub(rf"\s+", r"", text)
|
||||
return text
|
||||
|
||||
@classmethod
|
||||
def process(cls, txt, pre_align_args):
|
||||
txt = cls.preprocess_text(txt)
|
||||
shengmu = pinyin(txt, style=Style.INITIALS) # https://blog.csdn.net/zhoulei124/article/details/89055403
|
||||
yunmu_finals = pinyin(txt, style=Style.FINALS)
|
||||
yunmu_tone3 = pinyin(txt, style=Style.FINALS_TONE3)
|
||||
yunmu = [[t[0] + '5'] if t[0] == f[0] else t for f, t in zip(yunmu_finals, yunmu_tone3)] \
|
||||
if pre_align_args['use_tone'] else yunmu_finals
|
||||
|
||||
assert len(shengmu) == len(yunmu)
|
||||
phs = ["|"]
|
||||
for a, b, c in zip(shengmu, yunmu, yunmu_finals):
|
||||
if a[0] == c[0]:
|
||||
phs += [a[0], "|"]
|
||||
else:
|
||||
phs += [a[0], b[0], "|"]
|
||||
return phs, txt
|
||||
@@ -0,0 +1,72 @@
|
||||
import re
|
||||
import jieba
|
||||
from pypinyin import pinyin, Style
|
||||
from data_gen.tts.data_gen_utils import PUNCS
|
||||
from data_gen.tts.txt_processors import zh
|
||||
from g2pM import G2pM
|
||||
|
||||
ALL_SHENMU = ['zh', 'ch', 'sh', 'b', 'p', 'm', 'f', 'd', 't', 'n', 'l', 'g', 'k', 'h', 'j',
|
||||
'q', 'x', 'r', 'z', 'c', 's', 'y', 'w']
|
||||
ALL_YUNMU = ['a', 'ai', 'an', 'ang', 'ao', 'e', 'ei', 'en', 'eng', 'er', 'i', 'ia', 'ian',
|
||||
'iang', 'iao', 'ie', 'in', 'ing', 'iong', 'iu', 'ng', 'o', 'ong', 'ou',
|
||||
'u', 'ua', 'uai', 'uan', 'uang', 'ui', 'un', 'uo', 'v', 'van', 've', 'vn']
|
||||
|
||||
|
||||
class TxtProcessor(zh.TxtProcessor):
|
||||
model = G2pM()
|
||||
|
||||
@staticmethod
|
||||
def sp_phonemes():
|
||||
return ['|', '#']
|
||||
|
||||
@classmethod
|
||||
def process(cls, txt, pre_align_args):
|
||||
txt = cls.preprocess_text(txt)
|
||||
ph_list = cls.model(txt, tone=pre_align_args['use_tone'], char_split=True)
|
||||
seg_list = '#'.join(jieba.cut(txt))
|
||||
assert len(ph_list) == len([s for s in seg_list if s != '#']), (ph_list, seg_list)
|
||||
|
||||
# 加入词边界'#'
|
||||
ph_list_ = []
|
||||
seg_idx = 0
|
||||
for p in ph_list:
|
||||
p = p.replace("u:", "v")
|
||||
if seg_list[seg_idx] == '#':
|
||||
ph_list_.append('#')
|
||||
seg_idx += 1
|
||||
else:
|
||||
ph_list_.append("|")
|
||||
seg_idx += 1
|
||||
if re.findall('[\u4e00-\u9fff]', p):
|
||||
if pre_align_args['use_tone']:
|
||||
p = pinyin(p, style=Style.TONE3, strict=True)[0][0]
|
||||
if p[-1] not in ['1', '2', '3', '4', '5']:
|
||||
p = p + '5'
|
||||
else:
|
||||
p = pinyin(p, style=Style.NORMAL, strict=True)[0][0]
|
||||
|
||||
finished = False
|
||||
if len([c.isalpha() for c in p]) > 1:
|
||||
for shenmu in ALL_SHENMU:
|
||||
if p.startswith(shenmu) and not p.lstrip(shenmu).isnumeric():
|
||||
ph_list_ += [shenmu, p.lstrip(shenmu)]
|
||||
finished = True
|
||||
break
|
||||
if not finished:
|
||||
ph_list_.append(p)
|
||||
|
||||
ph_list = ph_list_
|
||||
|
||||
# 去除静音符号周围的词边界标记 [..., '#', ',', '#', ...]
|
||||
sil_phonemes = list(PUNCS) + TxtProcessor.sp_phonemes()
|
||||
ph_list_ = []
|
||||
for i in range(0, len(ph_list), 1):
|
||||
if ph_list[i] != '#' or (ph_list[i - 1] not in sil_phonemes and ph_list[i + 1] not in sil_phonemes):
|
||||
ph_list_.append(ph_list[i])
|
||||
ph_list = ph_list_
|
||||
return ph_list, txt
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
phs, txt = TxtProcessor.process('他来到了,网易杭研大厦', {'use_tone': True})
|
||||
print(phs)
|
||||
@@ -0,0 +1,113 @@
|
||||
# DiffSinger: Singing Voice Synthesis via Shallow Diffusion Mechanism
|
||||
[](https://arxiv.org/abs/2105.02446)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger/releases)
|
||||
|
||||
## DiffSinger (MIDI SVS | A version)
|
||||
### 0. Data Acquirement
|
||||
For Opencpop dataset: Please strictly follow the instructions of [Opencpop](https://wenet.org.cn/opencpop/). We have no right to give you the access to Opencpop.
|
||||
|
||||
The pipeline below is designed for Opencpop dataset:
|
||||
|
||||
### 1. Preparation
|
||||
|
||||
#### Data Preparation
|
||||
a) Download and extract Opencpop, then create a link to the dataset folder: `ln -s /xxx/opencpop data/raw/`
|
||||
|
||||
b) Run the following scripts to pack the dataset for training/inference.
|
||||
|
||||
```sh
|
||||
export PYTHONPATH=.
|
||||
CUDA_VISIBLE_DEVICES=0 python data_gen/tts/bin/binarize.py --config usr/configs/midi/cascade/opencs/aux_rel.yaml
|
||||
|
||||
# `data/binary/opencpop-midi-dp` will be generated.
|
||||
```
|
||||
|
||||
#### Vocoder Preparation
|
||||
We provide the pre-trained model of [HifiGAN-Singing](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0109_hifigan_bigpopcs_hop128.zip) which is specially designed for SVS with NSF mechanism.
|
||||
Please unzip this file into `checkpoints` before training your acoustic model.
|
||||
|
||||
(Update: You can also move [a ckpt with more training steps](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/model_ckpt_steps_1512000.ckpt) into this vocoder directory)
|
||||
|
||||
This singing vocoder is trained on ~70 hours singing data, which can be viewed as a universal vocoder.
|
||||
|
||||
#### Exp Name Preparation
|
||||
```bash
|
||||
export MY_FS_EXP_NAME=0302_opencpop_fs_midi
|
||||
export MY_DS_EXP_NAME=0303_opencpop_ds58_midi
|
||||
```
|
||||
|
||||
```
|
||||
.
|
||||
|--data
|
||||
|--raw
|
||||
|--opencpop
|
||||
|--segments
|
||||
|--transcriptions.txt
|
||||
|--wavs
|
||||
|--checkpoints
|
||||
|--MY_FS_EXP_NAME (optional)
|
||||
|--MY_DS_EXP_NAME (optional)
|
||||
|--0109_hifigan_bigpopcs_hop128
|
||||
|--model_ckpt_steps_1512000.ckpt
|
||||
|--config.yaml
|
||||
```
|
||||
|
||||
### 2. Training Example
|
||||
First, you need a pre-trained FFT-Singer checkpoint. You can use the pre-trained model, or train FFT-Singer from scratch, run:
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/midi/cascade/opencs/aux_rel.yaml --exp_name $MY_FS_EXP_NAME --reset
|
||||
```
|
||||
|
||||
Then, to train DiffSinger, run:
|
||||
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/midi/cascade/opencs/ds60_rel.yaml --exp_name $MY_DS_EXP_NAME --reset
|
||||
```
|
||||
|
||||
Remember to adjust the "fs2_ckpt" parameter in `usr/configs/midi/cascade/opencs/ds60_rel.yaml` to fit your path.
|
||||
|
||||
### 3. Inference from packed test set
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/midi/cascade/opencs/ds60_rel.yaml --exp_name $MY_DS_EXP_NAME --reset --infer
|
||||
```
|
||||
Inference results will be saved in `./checkpoints/MY_DS_EXP_NAME/generated_` by default.
|
||||
|
||||
We also provide:
|
||||
- the pre-trained model of DiffSinger;
|
||||
- the pre-trained model of FFT-Singer;
|
||||
|
||||
They can be found in [here](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/adjust-receptive-field.zip).
|
||||
|
||||
Remember to put the pre-trained models in `checkpoints` directory.
|
||||
|
||||
### 4. Inference from raw inputs
|
||||
```sh
|
||||
python inference/svs/ds_cascade.py --config usr/configs/midi/cascade/opencs/ds60_rel.yaml --exp_name $MY_DS_EXP_NAME
|
||||
```
|
||||
Raw inputs:
|
||||
```
|
||||
inp = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'notes': 'C#4/Db4 | F#4/Gb4 | G#4/Ab4 | A#4/Bb4 F#4/Gb4 | F#4/Gb4 C#4/Db4 | C#4/Db4 | rest | C#4/Db4 | A#4/Bb4 | G#4/Ab4 | A#4/Bb4 | G#4/Ab4 | F4 | C#4/Db4',
|
||||
'notes_duration': '0.407140 | 0.376190 | 0.242180 | 0.509550 0.183420 | 0.315400 0.235020 | 0.361660 | 0.223070 | 0.377270 | 0.340550 | 0.299620 | 0.344510 | 0.283770 | 0.323390 | 0.360340',
|
||||
'input_type': 'word'
|
||||
} # user input: Chinese characters
|
||||
or,
|
||||
inp = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'ph_seq': 'x iao j iu w o ch ang ang j ie ie m ao AP sh i n i z ui m ei d e j i h ao',
|
||||
'note_seq': 'C#4/Db4 C#4/Db4 F#4/Gb4 F#4/Gb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 F#4/Gb4 F#4/Gb4 F#4/Gb4 C#4/Db4 C#4/Db4 C#4/Db4 rest C#4/Db4 C#4/Db4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 F4 F4 C#4/Db4 C#4/Db4',
|
||||
'note_dur_seq': '0.407140 0.407140 0.376190 0.376190 0.242180 0.242180 0.509550 0.509550 0.183420 0.315400 0.315400 0.235020 0.361660 0.361660 0.223070 0.377270 0.377270 0.340550 0.340550 0.299620 0.299620 0.344510 0.344510 0.283770 0.283770 0.323390 0.323390 0.360340 0.360340',
|
||||
'is_slur_seq': '0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
|
||||
'input_type': 'phoneme'
|
||||
} # input like Opencpop dataset.
|
||||
```
|
||||
Here the inference results will be saved in `./infer_out` by default.
|
||||
|
||||
### 5. Some issues.
|
||||
a) the HifiGAN-Singing is trained on our [vocoder dataset](https://dl.acm.org/doi/abs/10.1145/3474085.3475437) and the training set of [PopCS](https://arxiv.org/abs/2105.02446). Opencpop is the out-of-domain dataset (unseen speaker). This may cause the deterioration of audio quality, and we are considering fine-tuning this vocoder on the training set of Opencpop.
|
||||
|
||||
b) in this version of codes, we used the melody frontend ([lyric + MIDI]->[F0+ph_dur]) to predict F0 contour and phoneme duration.
|
||||
|
||||
c) generated audio demos can be found in [MY_DS_EXP_NAME](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/adjust-receptive-field.zip).
|
||||
@@ -0,0 +1,106 @@
|
||||
# DiffSinger: Singing Voice Synthesis via Shallow Diffusion Mechanism
|
||||
[](https://arxiv.org/abs/2105.02446)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger/releases)
|
||||
[](https://huggingface.co/spaces/Silentlin/DiffSinger)
|
||||
|
||||
Substantial update: We 1) **abandon** the explicit prediction of the F0 curve; 2) increase the receptive field of the denoiser; 3) make the linguistic encoder more robust.
|
||||
**By doing so, 1) the synthesized recordings are more natural in terms of pitch; 2) the pipeline is simpler.**
|
||||
|
||||
简而言之,把F0曲线的动态性交给生成式模型去捕捉,而不再是以前那样用MSE约束对数域F0。
|
||||
|
||||
## DiffSinger (MIDI SVS | B version)
|
||||
### 0. Data Acquirement
|
||||
For Opencpop dataset: Please strictly follow the instructions of [Opencpop](https://wenet.org.cn/opencpop/). We have no right to give you the access to Opencpop.
|
||||
|
||||
The pipeline below is designed for Opencpop dataset:
|
||||
|
||||
### 1. Preparation
|
||||
|
||||
#### Data Preparation
|
||||
a) Download and extract Opencpop, then create a link to the dataset folder: `ln -s /xxx/opencpop data/raw/`
|
||||
|
||||
b) Run the following scripts to pack the dataset for training/inference.
|
||||
|
||||
```sh
|
||||
export PYTHONPATH=.
|
||||
CUDA_VISIBLE_DEVICES=0 python data_gen/tts/bin/binarize.py --config usr/configs/midi/cascade/opencs/aux_rel.yaml
|
||||
|
||||
# `data/binary/opencpop-midi-dp` will be generated.
|
||||
```
|
||||
|
||||
#### Vocoder Preparation
|
||||
We provide the pre-trained model of [HifiGAN-Singing](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0109_hifigan_bigpopcs_hop128.zip) which is specially designed for SVS with NSF mechanism.
|
||||
|
||||
Also, please unzip pre-trained vocoder and [this pendant for vocoder](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0102_xiaoma_pe.zip) into `checkpoints` before training your acoustic model.
|
||||
|
||||
(Update: You can also move [a ckpt with more training steps](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/model_ckpt_steps_1512000.ckpt) into this vocoder directory)
|
||||
|
||||
This singing vocoder is trained on ~70 hours singing data, which can be viewed as a universal vocoder.
|
||||
|
||||
#### Exp Name Preparation
|
||||
```bash
|
||||
export MY_DS_EXP_NAME=0228_opencpop_ds100_rel
|
||||
```
|
||||
|
||||
```
|
||||
.
|
||||
|--data
|
||||
|--raw
|
||||
|--opencpop
|
||||
|--segments
|
||||
|--transcriptions.txt
|
||||
|--wavs
|
||||
|--checkpoints
|
||||
|--MY_DS_EXP_NAME (optional)
|
||||
|--0109_hifigan_bigpopcs_hop128 (vocoder)
|
||||
|--model_ckpt_steps_1512000.ckpt
|
||||
|--config.yaml
|
||||
```
|
||||
|
||||
### 2. Training Example
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/midi/e2e/opencpop/ds100_adj_rel.yaml --exp_name $MY_DS_EXP_NAME --reset
|
||||
```
|
||||
|
||||
### 3. Inference from packed test set
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/midi/e2e/opencpop/ds100_adj_rel.yaml --exp_name $MY_DS_EXP_NAME --reset --infer
|
||||
```
|
||||
Inference results will be saved in `./checkpoints/MY_DS_EXP_NAME/generated_` by default.
|
||||
|
||||
We also provide:
|
||||
- the pre-trained model of DiffSinger;
|
||||
|
||||
They can be found in [here](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0228_opencpop_ds100_rel.zip).
|
||||
|
||||
Remember to put the pre-trained models in `checkpoints` directory.
|
||||
|
||||
### 4. Inference from raw inputs
|
||||
```sh
|
||||
python inference/svs/ds_e2e.py --config usr/configs/midi/e2e/opencpop/ds100_adj_rel.yaml --exp_name $MY_DS_EXP_NAME
|
||||
```
|
||||
Raw inputs:
|
||||
```
|
||||
inp = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'notes': 'C#4/Db4 | F#4/Gb4 | G#4/Ab4 | A#4/Bb4 F#4/Gb4 | F#4/Gb4 C#4/Db4 | C#4/Db4 | rest | C#4/Db4 | A#4/Bb4 | G#4/Ab4 | A#4/Bb4 | G#4/Ab4 | F4 | C#4/Db4',
|
||||
'notes_duration': '0.407140 | 0.376190 | 0.242180 | 0.509550 0.183420 | 0.315400 0.235020 | 0.361660 | 0.223070 | 0.377270 | 0.340550 | 0.299620 | 0.344510 | 0.283770 | 0.323390 | 0.360340',
|
||||
'input_type': 'word'
|
||||
} # user input: Chinese characters
|
||||
or,
|
||||
inp = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'ph_seq': 'x iao j iu w o ch ang ang j ie ie m ao AP sh i n i z ui m ei d e j i h ao',
|
||||
'note_seq': 'C#4/Db4 C#4/Db4 F#4/Gb4 F#4/Gb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 F#4/Gb4 F#4/Gb4 F#4/Gb4 C#4/Db4 C#4/Db4 C#4/Db4 rest C#4/Db4 C#4/Db4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 F4 F4 C#4/Db4 C#4/Db4',
|
||||
'note_dur_seq': '0.407140 0.407140 0.376190 0.376190 0.242180 0.242180 0.509550 0.509550 0.183420 0.315400 0.315400 0.235020 0.361660 0.361660 0.223070 0.377270 0.377270 0.340550 0.340550 0.299620 0.299620 0.344510 0.344510 0.283770 0.283770 0.323390 0.323390 0.360340 0.360340',
|
||||
'is_slur_seq': '0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
|
||||
'input_type': 'phoneme'
|
||||
} # input like Opencpop dataset.
|
||||
```
|
||||
Here the inference results will be saved in `./infer_out` by default.
|
||||
### 5. Some issues.
|
||||
a) the HifiGAN-Singing is trained on our [vocoder dataset](https://dl.acm.org/doi/abs/10.1145/3474085.3475437) and the training set of [PopCS](https://arxiv.org/abs/2105.02446). Opencpop is the out-of-domain dataset (unseen speaker). This may cause the deterioration of audio quality, and we are considering fine-tuning this vocoder on the training set of Opencpop.
|
||||
|
||||
b) in this version of codes, we used the melody frontend ([lyric + MIDI]->[ph_dur]) to predict phoneme duration. F0 curve is implicitly predicted together with mel-spectrogram.
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
# DiffSinger-PNDM
|
||||
[](https://arxiv.org/abs/2105.02446)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger/releases)
|
||||
|
||||
Highlights:
|
||||
|
||||
Training diffusion model: 1000 steps
|
||||
|
||||
Default pndm_speedup: 40
|
||||
|
||||
Inference diffusion model: (1000 / pndm_speedup) steps = 25 steps
|
||||
|
||||
You can freely control the inference steps, by adding these arguments in your experiment scripts :
|
||||
--hparams="pndm_speedup=40" or --hparams="pndm_speedup=20" or --hparams="pndm_speedup=10".
|
||||
|
||||
Contributed by @luping-liu .
|
||||
|
||||
## DiffSinger (MIDI SVS | B version | +PNDM)
|
||||
### 0. Data Acquirement
|
||||
For Opencpop dataset: Please strictly follow the instructions of [Opencpop](https://wenet.org.cn/opencpop/). We have no right to give you the access to Opencpop.
|
||||
|
||||
The pipeline below is designed for Opencpop dataset:
|
||||
|
||||
### 1. Preparation
|
||||
|
||||
#### Data Preparation
|
||||
a) Download and extract Opencpop, then create a link to the dataset folder: `ln -s /xxx/opencpop data/raw/`
|
||||
|
||||
b) Run the following scripts to pack the dataset for training/inference.
|
||||
|
||||
```sh
|
||||
export PYTHONPATH=.
|
||||
CUDA_VISIBLE_DEVICES=0 python data_gen/tts/bin/binarize.py --config usr/configs/midi/cascade/opencs/aux_rel.yaml
|
||||
|
||||
# `data/binary/opencpop-midi-dp` will be generated.
|
||||
```
|
||||
|
||||
#### Vocoder Preparation
|
||||
We provide the pre-trained model of [HifiGAN-Singing](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0109_hifigan_bigpopcs_hop128.zip) which is specially designed for SVS with NSF mechanism.
|
||||
|
||||
Also, please unzip pre-trained vocoder and [this pendant for vocoder](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0102_xiaoma_pe.zip) into `checkpoints` before training your acoustic model.
|
||||
|
||||
(Update: You can also move [a ckpt with more training steps](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/model_ckpt_steps_1512000.ckpt) into this vocoder directory)
|
||||
|
||||
This singing vocoder is trained on ~70 hours singing data, which can be viewed as a universal vocoder.
|
||||
|
||||
#### Exp Name Preparation
|
||||
```bash
|
||||
export MY_DS_EXP_NAME=0831_opencpop_ds1000
|
||||
```
|
||||
|
||||
```
|
||||
.
|
||||
|--data
|
||||
|--raw
|
||||
|--opencpop
|
||||
|--segments
|
||||
|--transcriptions.txt
|
||||
|--wavs
|
||||
|--checkpoints
|
||||
|--MY_DS_EXP_NAME (optional)
|
||||
|--0109_hifigan_bigpopcs_hop128 (vocoder)
|
||||
|--model_ckpt_steps_1512000.ckpt
|
||||
|--config.yaml
|
||||
```
|
||||
|
||||
### 2. Training Example
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/midi/e2e/opencpop/ds1000.yaml --exp_name $MY_DS_EXP_NAME --reset
|
||||
```
|
||||
|
||||
### 3. Inference from packed test set
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/midi/e2e/opencpop/ds1000.yaml --exp_name $MY_DS_EXP_NAME --reset --infer
|
||||
```
|
||||
Inference results will be saved in `./checkpoints/MY_DS_EXP_NAME/generated_` by default.
|
||||
|
||||
We also provide:
|
||||
- the pre-trained model of DiffSinger;
|
||||
|
||||
They can be found in [here](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0831_opencpop_ds1000.zip).
|
||||
|
||||
Remember to put the pre-trained models in `checkpoints` directory.
|
||||
|
||||
### 4. Inference from raw inputs
|
||||
```sh
|
||||
python inference/svs/ds_e2e.py --config usr/configs/midi/e2e/opencpop/ds1000.yaml --exp_name $MY_DS_EXP_NAME
|
||||
```
|
||||
Raw inputs:
|
||||
```
|
||||
inp = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'notes': 'C#4/Db4 | F#4/Gb4 | G#4/Ab4 | A#4/Bb4 F#4/Gb4 | F#4/Gb4 C#4/Db4 | C#4/Db4 | rest | C#4/Db4 | A#4/Bb4 | G#4/Ab4 | A#4/Bb4 | G#4/Ab4 | F4 | C#4/Db4',
|
||||
'notes_duration': '0.407140 | 0.376190 | 0.242180 | 0.509550 0.183420 | 0.315400 0.235020 | 0.361660 | 0.223070 | 0.377270 | 0.340550 | 0.299620 | 0.344510 | 0.283770 | 0.323390 | 0.360340',
|
||||
'input_type': 'word'
|
||||
} # user input: Chinese characters
|
||||
or,
|
||||
inp = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'ph_seq': 'x iao j iu w o ch ang ang j ie ie m ao AP sh i n i z ui m ei d e j i h ao',
|
||||
'note_seq': 'C#4/Db4 C#4/Db4 F#4/Gb4 F#4/Gb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 F#4/Gb4 F#4/Gb4 F#4/Gb4 C#4/Db4 C#4/Db4 C#4/Db4 rest C#4/Db4 C#4/Db4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 F4 F4 C#4/Db4 C#4/Db4',
|
||||
'note_dur_seq': '0.407140 0.407140 0.376190 0.376190 0.242180 0.242180 0.509550 0.509550 0.183420 0.315400 0.315400 0.235020 0.361660 0.361660 0.223070 0.377270 0.377270 0.340550 0.340550 0.299620 0.299620 0.344510 0.344510 0.283770 0.283770 0.323390 0.323390 0.360340 0.360340',
|
||||
'is_slur_seq': '0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
|
||||
'input_type': 'phoneme'
|
||||
} # input like Opencpop dataset.
|
||||
```
|
||||
Here the inference results will be saved in `./infer_out` by default.
|
||||
### 5. Some issues.
|
||||
a) the HifiGAN-Singing is trained on our [vocoder dataset](https://dl.acm.org/doi/abs/10.1145/3474085.3475437) and the training set of [PopCS](https://arxiv.org/abs/2105.02446). Opencpop is the out-of-domain dataset (unseen speaker). This may cause the deterioration of audio quality, and we are considering fine-tuning this vocoder on the training set of Opencpop.
|
||||
|
||||
b) in this version of codes, we used the melody frontend ([lyric + MIDI]->[ph_dur]) to predict phoneme duration. F0 curve is implicitly predicted together with mel-spectrogram.
|
||||
@@ -0,0 +1,63 @@
|
||||
## DiffSinger (SVS version)
|
||||
|
||||
### 0. Data Acquirement
|
||||
- [Download link](https://drive.google.com/file/d/1uFJmPEUWbzguGBdiuupYvYbBEjopN-Xq/view?usp=sharing).
|
||||
- Please note that, if you are using PopCS, it means that you have accepted the terms in [apply_form](https://github.com/MoonInTheRiver/DiffSinger/blob/master/resources/apply_form.md).
|
||||
|
||||
### 1. Preparation
|
||||
#### Data Preparation
|
||||
a) Download and extract PopCS, then create a link to the dataset folder: `ln -s /xxx/popcs/ data/processed/popcs`
|
||||
|
||||
b) Run the following scripts to pack the dataset for training/inference.
|
||||
```sh
|
||||
export PYTHONPATH=.
|
||||
CUDA_VISIBLE_DEVICES=0 python data_gen/tts/bin/binarize.py --config usr/configs/popcs_ds_beta6.yaml
|
||||
# `data/binary/popcs-pmf0` will be generated.
|
||||
```
|
||||
|
||||
#### Vocoder Preparation
|
||||
We provide the pre-trained model of [HifiGAN-Singing](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0109_hifigan_bigpopcs_hop128.zip) which is specially designed for SVS with NSF mechanism.
|
||||
Please unzip this file into `checkpoints` before training your acoustic model.
|
||||
|
||||
(Update: You can also move [a ckpt with more training steps](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/model_ckpt_steps_1512000.ckpt) into this vocoder directory)
|
||||
|
||||
This singing vocoder is trained on ~70 hours singing data, which can be viewed as a universal vocoder.
|
||||
|
||||
### 2. Training Example
|
||||
First, you need a pre-trained FFT-Singer checkpoint. You can use the [pre-trained model](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/popcs_fs2_pmf0_1230.zip), or train FFT-Singer from scratch, run:
|
||||
|
||||
```sh
|
||||
# First, train fft-singer;
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/popcs_fs2.yaml --exp_name popcs_fs2_pmf0_1230 --reset
|
||||
# Then, infer fft-singer;
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/popcs_fs2.yaml --exp_name popcs_fs2_pmf0_1230 --reset --infer
|
||||
```
|
||||
|
||||
Then, to train DiffSinger, run:
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/popcs_ds_beta6_offline.yaml --exp_name popcs_ds_beta6_offline_pmf0_1230 --reset
|
||||
```
|
||||
|
||||
Remember to adjust the "fs2_ckpt" parameter in `usr/configs/popcs_ds_beta6_offline.yaml` to fit your path.
|
||||
|
||||
### 3. Inference Example
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/popcs_ds_beta6_offline.yaml --exp_name popcs_ds_beta6_offline_pmf0_1230 --reset --infer
|
||||
```
|
||||
|
||||
We also provide:
|
||||
- the pre-trained model of [DiffSinger](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/popcs_ds_beta6_offline_pmf0_1230.zip);
|
||||
- the pre-trained model of [FFT-Singer](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/popcs_fs2_pmf0_1230.zip) for the shallow diffusion mechanism in DiffSinger;
|
||||
|
||||
Remember to put the pre-trained models in `checkpoints` directory.
|
||||
|
||||
*Note that:*
|
||||
|
||||
- *the original PWG version vocoder in the paper we used has been put into commercial use, so we provide this HifiGAN version vocoder as a substitute.*
|
||||
- *we assume the ground-truth F0 to be given as the pitch information following [1][2][3]. If you want to conduct experiments on MIDI data, you need an external F0 predictor (like [MIDI-A-version](README-SVS-opencpop-cascade.md)) or a joint prediction with spectrograms(like [MIDI-B-version](README-SVS-opencpop-e2e.md)).*
|
||||
|
||||
[1] Adversarially trained multi-singer sequence-to-sequence singing synthesizer. Interspeech 2020.
|
||||
|
||||
[2] SEQUENCE-TO-SEQUENCE SINGING SYNTHESIS USING THE FEED-FORWARD TRANSFORMER. ICASSP 2020.
|
||||
|
||||
[3] DeepSinger : Singing Voice Synthesis with Data Mined From the Web. KDD 2020.
|
||||
@@ -0,0 +1,76 @@
|
||||
# DiffSinger: Singing Voice Synthesis via Shallow Diffusion Mechanism
|
||||
[](https://arxiv.org/abs/2105.02446)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger/releases)
|
||||
[](https://huggingface.co/spaces/Silentlin/DiffSinger)
|
||||
|
||||
## DiffSinger (SVS)
|
||||
|
||||
### PART1. [Run DiffSinger on PopCS](README-SVS-popcs.md)
|
||||
In PART1, we only focus on spectrum modeling (acoustic model) and assume the ground-truth (GT) F0 to be given as the pitch information following these papers [1][2][3]. If you want to conduct experiments with F0 prediction, please move to PART2.
|
||||
|
||||
Thus, the pipeline of this part can be summarized as:
|
||||
|
||||
```
|
||||
[lyrics] -> [linguistic representation] (Frontend)
|
||||
[linguistic representation] + [GT F0] + [GT phoneme duration] -> [mel-spectrogram] (Acoustic model)
|
||||
[mel-spectrogram] + [GT F0] -> [waveform] (Vocoder)
|
||||
```
|
||||
|
||||
|
||||
[1] Adversarially trained multi-singer sequence-to-sequence singing synthesizer. Interspeech 2020.
|
||||
|
||||
[2] SEQUENCE-TO-SEQUENCE SINGING SYNTHESIS USING THE FEED-FORWARD TRANSFORMER. ICASSP 2020.
|
||||
|
||||
[3] DeepSinger : Singing Voice Synthesis with Data Mined From the Web. KDD 2020.
|
||||
|
||||
Click here for detailed instructions: [link](README-SVS-popcs.md).
|
||||
|
||||
|
||||
### PART2. [Run DiffSinger on Opencpop](README-SVS-opencpop-cascade.md)
|
||||
Thanks [Opencpop team](https://wenet.org.cn/opencpop/) for releasing their SVS dataset with MIDI label, **Jan.20, 2022** (after we published our paper).
|
||||
|
||||
Since there are elaborately annotated MIDI labels, we are able to supplement the pipeline in PART 1 by adding a naive melody frontend.
|
||||
|
||||
#### 2.A
|
||||
Thus, the pipeline of [2.A](README-SVS-opencpop-cascade.md) can be summarized as:
|
||||
|
||||
```
|
||||
[lyrics] + [MIDI] -> [linguistic representation (with MIDI information)] + [predicted F0] + [predicted phoneme duration] (Melody frontend)
|
||||
[linguistic representation] + [predicted F0] + [predicted phoneme duration] -> [mel-spectrogram] (Acoustic model)
|
||||
[mel-spectrogram] + [predicted F0] -> [waveform] (Vocoder)
|
||||
```
|
||||
|
||||
Click here for detailed instructions: [link](README-SVS-opencpop-cascade.md).
|
||||
|
||||
#### 2.B
|
||||
In 2.1, we find that if we predict F0 explicitly in the melody frontend, there will be many bad cases of uv/v prediction. Then, we abandon the explicit prediction of the F0 curve in the melody frontend and make a joint prediction with spectrograms.
|
||||
|
||||
Thus, the pipeline of [2.B](README-SVS-opencpop-e2e.md) can be summarized as:
|
||||
```
|
||||
[lyrics] + [MIDI] -> [linguistic representation] + [predicted phoneme duration] (Melody frontend)
|
||||
[linguistic representation (with MIDI information)] + [predicted phoneme duration] -> [mel-spectrogram] (Acoustic model)
|
||||
[mel-spectrogram] -> [predicted F0] (Pitch extractor)
|
||||
[mel-spectrogram] + [predicted F0] -> [waveform] (Vocoder)
|
||||
```
|
||||
|
||||
Click here for detailed instructions: [link](README-SVS-opencpop-e2e.md).
|
||||
|
||||
### FAQ
|
||||
Q1: Why do I need F0 in Vocoders?
|
||||
|
||||
A1: See vocoder parts in HiFiSinger, DiffSinger or SingGAN. This is a common practice now.
|
||||
|
||||
Q2: Why not run MIDI version SVS on PopCS dataset? or Why not release MIDI labels for PopCS dataset?
|
||||
|
||||
A2: Our laboratory has no funds to label PopCS dataset. But there are funds for labeling other singing dataset, which is coming soon.
|
||||
|
||||
Q3: Why " 'HifiGAN' object has no attribute 'model' "?
|
||||
|
||||
A3: Please put the pretrained vocoders in your `checkpoints` dictionary.
|
||||
|
||||
Q4: How to check whether I use GT information or predicted information during inference from packed test set?
|
||||
|
||||
A4: Please see codes [here](https://github.com/MoonInTheRiver/DiffSinger/blob/55e2f46068af6e69940a9f8f02d306c24a940cab/tasks/tts/fs2.py#L343).
|
||||
|
||||
...
|
||||
@@ -0,0 +1,38 @@
|
||||
# DiffSinger: Singing Voice Synthesis via Shallow Diffusion Mechanism
|
||||
[](https://arxiv.org/abs/2105.02446)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger/releases)
|
||||
[](https://huggingface.co/spaces/NATSpeech/DiffSpeech)
|
||||
|
||||
## DiffSpeech (TTS)
|
||||
### 1. Preparation
|
||||
|
||||
#### Data Preparation
|
||||
a) Download and extract the [LJ Speech dataset](https://keithito.com/LJ-Speech-Dataset/), then create a link to the dataset folder: `ln -s /xxx/LJSpeech-1.1/ data/raw/`
|
||||
|
||||
b) Download and Unzip the [ground-truth duration](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/mfa_outputs.tar) extracted by [MFA](https://github.com/MontrealCorpusTools/Montreal-Forced-Aligner/releases/download/v1.0.1/montreal-forced-aligner_linux.tar.gz): `tar -xvf mfa_outputs.tar; mv mfa_outputs data/processed/ljspeech/`
|
||||
|
||||
c) Run the following scripts to pack the dataset for training/inference.
|
||||
|
||||
```sh
|
||||
export PYTHONPATH=.
|
||||
CUDA_VISIBLE_DEVICES=0 python data_gen/tts/bin/binarize.py --config configs/tts/lj/fs2.yaml
|
||||
|
||||
# `data/binary/ljspeech` will be generated.
|
||||
```
|
||||
|
||||
#### Vocoder Preparation
|
||||
We provide the pre-trained model of [HifiGAN](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0414_hifi_lj_1.zip) vocoder.
|
||||
Please unzip this file into `checkpoints` before training your acoustic model.
|
||||
|
||||
### 2. Training Example
|
||||
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/lj_ds_pndm.yaml --exp_name ds_pndm_lj_1 --reset
|
||||
```
|
||||
|
||||
### 3. Inference Example
|
||||
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/lj_ds_pndm.yaml --exp_name ds_pndm_lj_1 --reset --infer
|
||||
```
|
||||
@@ -0,0 +1,69 @@
|
||||
# DiffSinger: Singing Voice Synthesis via Shallow Diffusion Mechanism
|
||||
[](https://arxiv.org/abs/2105.02446)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger)
|
||||
[](https://github.com/MoonInTheRiver/DiffSinger/releases)
|
||||
[](https://huggingface.co/spaces/NATSpeech/DiffSpeech)
|
||||
|
||||
## DiffSpeech (TTS)
|
||||
### 1. Preparation
|
||||
|
||||
#### Data Preparation
|
||||
a) Download and extract the [LJ Speech dataset](https://keithito.com/LJ-Speech-Dataset/), then create a link to the dataset folder: `ln -s /xxx/LJSpeech-1.1/ data/raw/`
|
||||
|
||||
b) Download and Unzip the [ground-truth duration](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/mfa_outputs.tar) extracted by [MFA](https://github.com/MontrealCorpusTools/Montreal-Forced-Aligner/releases/download/v1.0.1/montreal-forced-aligner_linux.tar.gz): `tar -xvf mfa_outputs.tar; mv mfa_outputs data/processed/ljspeech/`
|
||||
|
||||
c) Run the following scripts to pack the dataset for training/inference.
|
||||
|
||||
```sh
|
||||
export PYTHONPATH=.
|
||||
CUDA_VISIBLE_DEVICES=0 python data_gen/tts/bin/binarize.py --config configs/tts/lj/fs2.yaml
|
||||
|
||||
# `data/binary/ljspeech` will be generated.
|
||||
```
|
||||
|
||||
#### Vocoder Preparation
|
||||
We provide the pre-trained model of [HifiGAN](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/0414_hifi_lj_1.zip) vocoder.
|
||||
Please unzip this file into `checkpoints` before training your acoustic model.
|
||||
|
||||
### 2. Training Example
|
||||
|
||||
First, you need a pre-trained FastSpeech2 checkpoint. You can use the [pre-trained model](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/fs2_lj_1.zip), or train FastSpeech2 from scratch, run:
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config configs/tts/lj/fs2.yaml --exp_name fs2_lj_1 --reset
|
||||
```
|
||||
Then, to train DiffSpeech, run:
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/lj_ds_beta6.yaml --exp_name lj_ds_beta6_1213 --reset
|
||||
```
|
||||
|
||||
Remember to adjust the "fs2_ckpt" parameter in `usr/configs/lj_ds_beta6.yaml` to fit your path.
|
||||
|
||||
### 3. Inference Example
|
||||
|
||||
```sh
|
||||
CUDA_VISIBLE_DEVICES=0 python tasks/run.py --config usr/configs/lj_ds_beta6.yaml --exp_name lj_ds_beta6_1213 --reset --infer
|
||||
```
|
||||
|
||||
We also provide:
|
||||
- the pre-trained model of [DiffSpeech](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/lj_ds_beta6_1213.zip);
|
||||
- the individual pre-trained model of [FastSpeech 2](https://github.com/MoonInTheRiver/DiffSinger/releases/download/pretrain-model/fs2_lj_1.zip) for the shallow diffusion mechanism in DiffSpeech;
|
||||
|
||||
Remember to put the pre-trained models in `checkpoints` directory.
|
||||
|
||||
## Mel Visualization
|
||||
Along vertical axis, DiffSpeech: [0-80]; FastSpeech2: [80-160].
|
||||
|
||||
<table style="width:100%">
|
||||
<tr>
|
||||
<th>DiffSpeech vs. FastSpeech 2</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><img src="resources/diffspeech-fs2.png" alt="DiffSpeech-vs-FastSpeech2" height="250"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><img src="resources/diffspeech-fs2-1.png" alt="DiffSpeech-vs-FastSpeech2" height="250"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><img src="resources/diffspeech-fs2-2.png" alt="DiffSpeech-vs-FastSpeech2" height="250"></td>
|
||||
</tr>
|
||||
</table>
|
||||
@@ -0,0 +1,265 @@
|
||||
import os
|
||||
|
||||
import torch
|
||||
import numpy as np
|
||||
from modules.hifigan.hifigan import HifiGanGenerator
|
||||
from vocoders.hifigan import HifiGAN
|
||||
from inference.svs.opencpop.map import cpop_pinyin2ph_func
|
||||
|
||||
from utils import load_ckpt
|
||||
from utils.hparams import set_hparams, hparams
|
||||
from utils.text_encoder import TokenTextEncoder
|
||||
from pypinyin import pinyin, lazy_pinyin, Style
|
||||
import librosa
|
||||
import glob
|
||||
import re
|
||||
|
||||
|
||||
class BaseSVSInfer:
|
||||
def __init__(self, hparams, device=None):
|
||||
if device is None:
|
||||
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
||||
self.hparams = hparams
|
||||
self.device = device
|
||||
|
||||
phone_list = ["AP", "SP", "a", "ai", "an", "ang", "ao", "b", "c", "ch", "d", "e", "ei", "en", "eng", "er", "f", "g",
|
||||
"h", "i", "ia", "ian", "iang", "iao", "ie", "in", "ing", "iong", "iu", "j", "k", "l", "m", "n", "o",
|
||||
"ong", "ou", "p", "q", "r", "s", "sh", "t", "u", "ua", "uai", "uan", "uang", "ui", "un", "uo", "v",
|
||||
"van", "ve", "vn", "w", "x", "y", "z", "zh"]
|
||||
self.ph_encoder = TokenTextEncoder(None, vocab_list=phone_list, replace_oov=',')
|
||||
self.pinyin2phs = cpop_pinyin2ph_func()
|
||||
self.spk_map = {'opencpop': 0}
|
||||
|
||||
self.model = self.build_model()
|
||||
self.model.eval()
|
||||
self.model.to(self.device)
|
||||
self.vocoder = self.build_vocoder()
|
||||
self.vocoder.eval()
|
||||
self.vocoder.to(self.device)
|
||||
|
||||
def build_model(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def forward_model(self, inp):
|
||||
raise NotImplementedError
|
||||
|
||||
def build_vocoder(self):
|
||||
base_dir = hparams['vocoder_ckpt']
|
||||
config_path = f'{base_dir}/config.yaml'
|
||||
ckpt = sorted(glob.glob(f'{base_dir}/model_ckpt_steps_*.ckpt'), key=
|
||||
lambda x: int(re.findall(f'{base_dir}/model_ckpt_steps_(\d+).ckpt', x)[0]))[-1]
|
||||
print('| load HifiGAN: ', ckpt)
|
||||
ckpt_dict = torch.load(ckpt, map_location="cpu")
|
||||
config = set_hparams(config_path, global_hparams=False)
|
||||
state = ckpt_dict["state_dict"]["model_gen"]
|
||||
vocoder = HifiGanGenerator(config)
|
||||
vocoder.load_state_dict(state, strict=True)
|
||||
vocoder.remove_weight_norm()
|
||||
vocoder = vocoder.eval().to(self.device)
|
||||
return vocoder
|
||||
|
||||
def run_vocoder(self, c, **kwargs):
|
||||
c = c.transpose(2, 1) # [B, 80, T]
|
||||
f0 = kwargs.get('f0') # [B, T]
|
||||
if f0 is not None and hparams.get('use_nsf'):
|
||||
# f0 = torch.FloatTensor(f0).to(self.device)
|
||||
y = self.vocoder(c, f0).view(-1)
|
||||
else:
|
||||
y = self.vocoder(c).view(-1)
|
||||
# [T]
|
||||
return y[None]
|
||||
|
||||
def preprocess_word_level_input(self, inp):
|
||||
# Pypinyin can't solve polyphonic words
|
||||
text_raw = inp['text'].replace('最长', '最常').replace('长睫毛', '常睫毛') \
|
||||
.replace('那么长', '那么常').replace('多长', '多常') \
|
||||
.replace('很长', '很常') # We hope someone could provide a better g2p module for us by opening pull requests.
|
||||
|
||||
# lyric
|
||||
pinyins = lazy_pinyin(text_raw, strict=False)
|
||||
ph_per_word_lst = [self.pinyin2phs[pinyin.strip()] for pinyin in pinyins if pinyin.strip() in self.pinyin2phs]
|
||||
|
||||
# Note
|
||||
note_per_word_lst = [x.strip() for x in inp['notes'].split('|') if x.strip() != '']
|
||||
mididur_per_word_lst = [x.strip() for x in inp['notes_duration'].split('|') if x.strip() != '']
|
||||
|
||||
if len(note_per_word_lst) == len(ph_per_word_lst) == len(mididur_per_word_lst):
|
||||
print('Pass word-notes check.')
|
||||
else:
|
||||
print('The number of words does\'t match the number of notes\' windows. ',
|
||||
'You should split the note(s) for each word by | mark.')
|
||||
print(ph_per_word_lst, note_per_word_lst, mididur_per_word_lst)
|
||||
print(len(ph_per_word_lst), len(note_per_word_lst), len(mididur_per_word_lst))
|
||||
return None
|
||||
|
||||
note_lst = []
|
||||
ph_lst = []
|
||||
midi_dur_lst = []
|
||||
is_slur = []
|
||||
for idx, ph_per_word in enumerate(ph_per_word_lst):
|
||||
# for phs in one word:
|
||||
# single ph like ['ai'] or multiple phs like ['n', 'i']
|
||||
ph_in_this_word = ph_per_word.split()
|
||||
|
||||
# for notes in one word:
|
||||
# single note like ['D4'] or multiple notes like ['D4', 'E4'] which means a 'slur' here.
|
||||
note_in_this_word = note_per_word_lst[idx].split()
|
||||
midi_dur_in_this_word = mididur_per_word_lst[idx].split()
|
||||
# process for the model input
|
||||
# Step 1.
|
||||
# Deal with note of 'not slur' case or the first note of 'slur' case
|
||||
# j ie
|
||||
# F#4/Gb4 F#4/Gb4
|
||||
# 0 0
|
||||
for ph in ph_in_this_word:
|
||||
ph_lst.append(ph)
|
||||
note_lst.append(note_in_this_word[0])
|
||||
midi_dur_lst.append(midi_dur_in_this_word[0])
|
||||
is_slur.append(0)
|
||||
# step 2.
|
||||
# Deal with the 2nd, 3rd... notes of 'slur' case
|
||||
# j ie ie
|
||||
# F#4/Gb4 F#4/Gb4 C#4/Db4
|
||||
# 0 0 1
|
||||
if len(note_in_this_word) > 1: # is_slur = True, we should repeat the YUNMU to match the 2nd, 3rd... notes.
|
||||
for idx in range(1, len(note_in_this_word)):
|
||||
ph_lst.append(ph_in_this_word[-1])
|
||||
note_lst.append(note_in_this_word[idx])
|
||||
midi_dur_lst.append(midi_dur_in_this_word[idx])
|
||||
is_slur.append(1)
|
||||
ph_seq = ' '.join(ph_lst)
|
||||
|
||||
if len(ph_lst) == len(note_lst) == len(midi_dur_lst):
|
||||
print(len(ph_lst), len(note_lst), len(midi_dur_lst))
|
||||
print('Pass word-notes check.')
|
||||
else:
|
||||
print('The number of words does\'t match the number of notes\' windows. ',
|
||||
'You should split the note(s) for each word by | mark.')
|
||||
return None
|
||||
return ph_seq, note_lst, midi_dur_lst, is_slur
|
||||
|
||||
def preprocess_phoneme_level_input(self, inp):
|
||||
ph_seq = inp['ph_seq']
|
||||
note_lst = inp['note_seq'].split()
|
||||
midi_dur_lst = inp['note_dur_seq'].split()
|
||||
is_slur = [float(x) for x in inp['is_slur_seq'].split()]
|
||||
print(len(note_lst), len(ph_seq.split()), len(midi_dur_lst))
|
||||
if len(note_lst) == len(ph_seq.split()) == len(midi_dur_lst):
|
||||
print('Pass word-notes check.')
|
||||
else:
|
||||
print('The number of words does\'t match the number of notes\' windows. ',
|
||||
'You should split the note(s) for each word by | mark.')
|
||||
return None
|
||||
return ph_seq, note_lst, midi_dur_lst, is_slur
|
||||
|
||||
def preprocess_input(self, inp, input_type='word'):
|
||||
"""
|
||||
|
||||
:param inp: {'text': str, 'item_name': (str, optional), 'spk_name': (str, optional)}
|
||||
:return:
|
||||
"""
|
||||
|
||||
item_name = inp.get('item_name', '<ITEM_NAME>')
|
||||
spk_name = inp.get('spk_name', 'opencpop')
|
||||
|
||||
# single spk
|
||||
spk_id = self.spk_map[spk_name]
|
||||
|
||||
# get ph seq, note lst, midi dur lst, is slur lst.
|
||||
if input_type == 'word':
|
||||
ret = self.preprocess_word_level_input(inp)
|
||||
elif input_type == 'phoneme': # like transcriptions.txt in Opencpop dataset.
|
||||
ret = self.preprocess_phoneme_level_input(inp)
|
||||
else:
|
||||
print('Invalid input type.')
|
||||
return None
|
||||
|
||||
if ret:
|
||||
ph_seq, note_lst, midi_dur_lst, is_slur = ret
|
||||
else:
|
||||
print('==========> Preprocess_word_level or phone_level input wrong.')
|
||||
return None
|
||||
|
||||
# convert note lst to midi id; convert note dur lst to midi duration
|
||||
try:
|
||||
midis = [librosa.note_to_midi(x.split("/")[0]) if x != 'rest' else 0
|
||||
for x in note_lst]
|
||||
midi_dur_lst = [float(x) for x in midi_dur_lst]
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print('Invalid Input Type.')
|
||||
return None
|
||||
|
||||
ph_token = self.ph_encoder.encode(ph_seq)
|
||||
item = {'item_name': item_name, 'text': inp['text'], 'ph': ph_seq, 'spk_id': spk_id,
|
||||
'ph_token': ph_token, 'pitch_midi': np.asarray(midis), 'midi_dur': np.asarray(midi_dur_lst),
|
||||
'is_slur': np.asarray(is_slur), }
|
||||
item['ph_len'] = len(item['ph_token'])
|
||||
return item
|
||||
|
||||
def input_to_batch(self, item):
|
||||
item_names = [item['item_name']]
|
||||
text = [item['text']]
|
||||
ph = [item['ph']]
|
||||
txt_tokens = torch.LongTensor(item['ph_token'])[None, :].to(self.device)
|
||||
txt_lengths = torch.LongTensor([txt_tokens.shape[1]]).to(self.device)
|
||||
spk_ids = torch.LongTensor(item['spk_id'])[None, :].to(self.device)
|
||||
|
||||
pitch_midi = torch.LongTensor(item['pitch_midi'])[None, :hparams['max_frames']].to(self.device)
|
||||
midi_dur = torch.FloatTensor(item['midi_dur'])[None, :hparams['max_frames']].to(self.device)
|
||||
is_slur = torch.LongTensor(item['is_slur'])[None, :hparams['max_frames']].to(self.device)
|
||||
|
||||
batch = {
|
||||
'item_name': item_names,
|
||||
'text': text,
|
||||
'ph': ph,
|
||||
'txt_tokens': txt_tokens,
|
||||
'txt_lengths': txt_lengths,
|
||||
'spk_ids': spk_ids,
|
||||
'pitch_midi': pitch_midi,
|
||||
'midi_dur': midi_dur,
|
||||
'is_slur': is_slur
|
||||
}
|
||||
return batch
|
||||
|
||||
def postprocess_output(self, output):
|
||||
return output
|
||||
|
||||
def infer_once(self, inp):
|
||||
inp = self.preprocess_input(inp, input_type=inp['input_type'] if inp.get('input_type') else 'word')
|
||||
output = self.forward_model(inp)
|
||||
output = self.postprocess_output(output)
|
||||
return output
|
||||
|
||||
@classmethod
|
||||
def example_run(cls, inp):
|
||||
from utils.audio import save_wav
|
||||
set_hparams(print_hparams=False)
|
||||
infer_ins = cls(hparams)
|
||||
out = infer_ins.infer_once(inp)
|
||||
os.makedirs('infer_out', exist_ok=True)
|
||||
save_wav(out, f'infer_out/example_out.wav', hparams['audio_sample_rate'])
|
||||
|
||||
|
||||
# if __name__ == '__main__':
|
||||
# debug
|
||||
# a = BaseSVSInfer(hparams)
|
||||
# a.preprocess_input({'text': '你 说 你 不 SP 懂 为 何 在 这 时 牵 手 AP',
|
||||
# 'notes': 'D#4/Eb4 | D#4/Eb4 | D#4/Eb4 | D#4/Eb4 | rest | D#4/Eb4 | D4 | D4 | D4 | D#4/Eb4 | F4 | D#4/Eb4 | D4 | rest',
|
||||
# 'notes_duration': '0.113740 | 0.329060 | 0.287950 | 0.133480 | 0.150900 | 0.484730 | 0.242010 | 0.180820 | 0.343570 | 0.152050 | 0.266720 | 0.280310 | 0.633300 | 0.444590'
|
||||
# })
|
||||
|
||||
# b = {
|
||||
# 'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
# 'notes': 'C#4/Db4 | F#4/Gb4 | G#4/Ab4 | A#4/Bb4 F#4/Gb4 | F#4/Gb4 C#4/Db4 | C#4/Db4 | rest | C#4/Db4 | A#4/Bb4 | G#4/Ab4 | A#4/Bb4 | G#4/Ab4 | F4 | C#4/Db4',
|
||||
# 'notes_duration': '0.407140 | 0.376190 | 0.242180 | 0.509550 0.183420 | 0.315400 0.235020 | 0.361660 | 0.223070 | 0.377270 | 0.340550 | 0.299620 | 0.344510 | 0.283770 | 0.323390 | 0.360340'
|
||||
# }
|
||||
# c = {
|
||||
# 'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
# 'ph_seq': 'x iao j iu w o ch ang ang j ie ie m ao AP sh i n i z ui m ei d e j i h ao',
|
||||
# 'note_seq': 'C#4/Db4 C#4/Db4 F#4/Gb4 F#4/Gb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 F#4/Gb4 F#4/Gb4 F#4/Gb4 C#4/Db4 C#4/Db4 C#4/Db4 rest C#4/Db4 C#4/Db4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 F4 F4 C#4/Db4 C#4/Db4',
|
||||
# 'note_dur_seq': '0.407140 0.407140 0.376190 0.376190 0.242180 0.242180 0.509550 0.509550 0.183420 0.315400 0.315400 0.235020 0.361660 0.361660 0.223070 0.377270 0.377270 0.340550 0.340550 0.299620 0.299620 0.344510 0.344510 0.283770 0.283770 0.323390 0.323390 0.360340 0.360340',
|
||||
# 'is_slur_seq': '0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
|
||||
# } # input like Opencpop dataset.
|
||||
# a.preprocess_input(b)
|
||||
# a.preprocess_input(c, input_type='phoneme')
|
||||
@@ -0,0 +1,54 @@
|
||||
import torch
|
||||
# from inference.tts.fs import FastSpeechInfer
|
||||
# from modules.tts.fs2_orig import FastSpeech2Orig
|
||||
from inference.svs.base_svs_infer import BaseSVSInfer
|
||||
from utils import load_ckpt
|
||||
from utils.hparams import hparams
|
||||
from usr.diff.shallow_diffusion_tts import GaussianDiffusion
|
||||
from usr.diffsinger_task import DIFF_DECODERS
|
||||
|
||||
class DiffSingerCascadeInfer(BaseSVSInfer):
|
||||
def build_model(self):
|
||||
model = GaussianDiffusion(
|
||||
phone_encoder=self.ph_encoder,
|
||||
out_dims=hparams['audio_num_mel_bins'], denoise_fn=DIFF_DECODERS[hparams['diff_decoder_type']](hparams),
|
||||
timesteps=hparams['timesteps'],
|
||||
K_step=hparams['K_step'],
|
||||
loss_type=hparams['diff_loss_type'],
|
||||
spec_min=hparams['spec_min'], spec_max=hparams['spec_max'],
|
||||
)
|
||||
model.eval()
|
||||
load_ckpt(model, hparams['work_dir'], 'model')
|
||||
return model
|
||||
|
||||
def forward_model(self, inp):
|
||||
sample = self.input_to_batch(inp)
|
||||
txt_tokens = sample['txt_tokens'] # [B, T_t]
|
||||
spk_id = sample.get('spk_ids')
|
||||
with torch.no_grad():
|
||||
output = self.model(txt_tokens, spk_id=spk_id, ref_mels=None, infer=True,
|
||||
pitch_midi=sample['pitch_midi'], midi_dur=sample['midi_dur'],
|
||||
is_slur=sample['is_slur'])
|
||||
mel_out = output['mel_out'] # [B, T,80]
|
||||
f0_pred = output['f0_denorm']
|
||||
wav_out = self.run_vocoder(mel_out, f0=f0_pred)
|
||||
wav_out = wav_out.cpu().numpy()
|
||||
return wav_out[0]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
inp = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'notes': 'C#4/Db4 | F#4/Gb4 | G#4/Ab4 | A#4/Bb4 F#4/Gb4 | F#4/Gb4 C#4/Db4 | C#4/Db4 | rest | C#4/Db4 | A#4/Bb4 | G#4/Ab4 | A#4/Bb4 | G#4/Ab4 | F4 | C#4/Db4',
|
||||
'notes_duration': '0.407140 | 0.376190 | 0.242180 | 0.509550 0.183420 | 0.315400 0.235020 | 0.361660 | 0.223070 | 0.377270 | 0.340550 | 0.299620 | 0.344510 | 0.283770 | 0.323390 | 0.360340',
|
||||
'input_type': 'word'
|
||||
} # user input: Chinese characters
|
||||
c = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'ph_seq': 'x iao j iu w o ch ang ang j ie ie m ao AP sh i n i z ui m ei d e j i h ao',
|
||||
'note_seq': 'C#4/Db4 C#4/Db4 F#4/Gb4 F#4/Gb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 F#4/Gb4 F#4/Gb4 F#4/Gb4 C#4/Db4 C#4/Db4 C#4/Db4 rest C#4/Db4 C#4/Db4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 F4 F4 C#4/Db4 C#4/Db4',
|
||||
'note_dur_seq': '0.407140 0.407140 0.376190 0.376190 0.242180 0.242180 0.509550 0.509550 0.183420 0.315400 0.315400 0.235020 0.361660 0.361660 0.223070 0.377270 0.377270 0.340550 0.340550 0.299620 0.299620 0.344510 0.344510 0.283770 0.283770 0.323390 0.323390 0.360340 0.360340',
|
||||
'is_slur_seq': '0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
|
||||
'input_type': 'phoneme'
|
||||
} # input like Opencpop dataset.
|
||||
DiffSingerCascadeInfer.example_run(inp)
|
||||
@@ -0,0 +1,67 @@
|
||||
import torch
|
||||
# from inference.tts.fs import FastSpeechInfer
|
||||
# from modules.tts.fs2_orig import FastSpeech2Orig
|
||||
from inference.svs.base_svs_infer import BaseSVSInfer
|
||||
from utils import load_ckpt
|
||||
from utils.hparams import hparams
|
||||
from usr.diff.shallow_diffusion_tts import GaussianDiffusion
|
||||
from usr.diffsinger_task import DIFF_DECODERS
|
||||
from modules.fastspeech.pe import PitchExtractor
|
||||
import utils
|
||||
|
||||
|
||||
class DiffSingerE2EInfer(BaseSVSInfer):
|
||||
def build_model(self):
|
||||
model = GaussianDiffusion(
|
||||
phone_encoder=self.ph_encoder,
|
||||
out_dims=hparams['audio_num_mel_bins'], denoise_fn=DIFF_DECODERS[hparams['diff_decoder_type']](hparams),
|
||||
timesteps=hparams['timesteps'],
|
||||
K_step=hparams['K_step'],
|
||||
loss_type=hparams['diff_loss_type'],
|
||||
spec_min=hparams['spec_min'], spec_max=hparams['spec_max'],
|
||||
)
|
||||
model.eval()
|
||||
load_ckpt(model, hparams['work_dir'], 'model')
|
||||
|
||||
if hparams.get('pe_enable') is not None and hparams['pe_enable']:
|
||||
self.pe = PitchExtractor().to(self.device)
|
||||
utils.load_ckpt(self.pe, hparams['pe_ckpt'], 'model', strict=True)
|
||||
self.pe.eval()
|
||||
return model
|
||||
|
||||
def forward_model(self, inp):
|
||||
sample = self.input_to_batch(inp)
|
||||
txt_tokens = sample['txt_tokens'] # [B, T_t]
|
||||
spk_id = sample.get('spk_ids')
|
||||
with torch.no_grad():
|
||||
output = self.model(txt_tokens, spk_id=spk_id, ref_mels=None, infer=True,
|
||||
pitch_midi=sample['pitch_midi'], midi_dur=sample['midi_dur'],
|
||||
is_slur=sample['is_slur'])
|
||||
mel_out = output['mel_out'] # [B, T,80]
|
||||
if hparams.get('pe_enable') is not None and hparams['pe_enable']:
|
||||
f0_pred = self.pe(mel_out)['f0_denorm_pred'] # pe predict from Pred mel
|
||||
else:
|
||||
f0_pred = output['f0_denorm']
|
||||
wav_out = self.run_vocoder(mel_out, f0=f0_pred)
|
||||
wav_out = wav_out.cpu().numpy()
|
||||
return wav_out[0]
|
||||
|
||||
if __name__ == '__main__':
|
||||
inp = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'notes': 'C#4/Db4 | F#4/Gb4 | G#4/Ab4 | A#4/Bb4 F#4/Gb4 | F#4/Gb4 C#4/Db4 | C#4/Db4 | rest | C#4/Db4 | A#4/Bb4 | G#4/Ab4 | A#4/Bb4 | G#4/Ab4 | F4 | C#4/Db4',
|
||||
'notes_duration': '0.407140 | 0.376190 | 0.242180 | 0.509550 0.183420 | 0.315400 0.235020 | 0.361660 | 0.223070 | 0.377270 | 0.340550 | 0.299620 | 0.344510 | 0.283770 | 0.323390 | 0.360340',
|
||||
'input_type': 'word'
|
||||
} # user input: Chinese characters
|
||||
c = {
|
||||
'text': '小酒窝长睫毛AP是你最美的记号',
|
||||
'ph_seq': 'x iao j iu w o ch ang ang j ie ie m ao AP sh i n i z ui m ei d e j i h ao',
|
||||
'note_seq': 'C#4/Db4 C#4/Db4 F#4/Gb4 F#4/Gb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 F#4/Gb4 F#4/Gb4 F#4/Gb4 C#4/Db4 C#4/Db4 C#4/Db4 rest C#4/Db4 C#4/Db4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 A#4/Bb4 A#4/Bb4 G#4/Ab4 G#4/Ab4 F4 F4 C#4/Db4 C#4/Db4',
|
||||
'note_dur_seq': '0.407140 0.407140 0.376190 0.376190 0.242180 0.242180 0.509550 0.509550 0.183420 0.315400 0.315400 0.235020 0.361660 0.361660 0.223070 0.377270 0.377270 0.340550 0.340550 0.299620 0.299620 0.344510 0.344510 0.283770 0.283770 0.323390 0.323390 0.360340 0.360340',
|
||||
'is_slur_seq': '0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
|
||||
'input_type': 'phoneme'
|
||||
} # input like Opencpop dataset.
|
||||
DiffSingerE2EInfer.example_run(inp)
|
||||
|
||||
|
||||
# python inference/svs/ds_e2e.py --config usr/configs/midi/e2e/opencpop/ds100_adj_rel.yaml --exp_name 0228_opencpop_ds100_rel
|
||||
@@ -0,0 +1,27 @@
|
||||
title: 'DiffSinger'
|
||||
description: |
|
||||
This model is trained on 5 hours single female singing voice samples of Opencpop dataset. (该模型在开源数据集Opencpop的5小时单人歌声上训练。)
|
||||
|
||||
Please assign pitch and duration values to each Chinese character. The corresponding pitch and duration value of each character should be separated by a | separator. It is necessary to ensure that the note window separated by the separator is consistent with the number of Chinese characters (AP or SP is also viewed as a Chinese character). (请给每个汉字分配音高和时值, 每个字对应的音高和时值需要用|分隔符隔开。需要保证分隔符分割出来的音符窗口与汉字个数(AP或SP也算一个汉字)一致。)
|
||||
|
||||
You can click one of the examples to load them. (你可以点击下方示例,加载示例曲谱。)
|
||||
|
||||
Note: This space is running on CPU. (该Demo是在Huggingface提供的CPU上运行的, 其推理速度在本地会更快一些。)
|
||||
|
||||
article: |
|
||||
Link to <a href='https://github.com/MoonInTheRiver/DiffSinger' style='color:blue;' target='_blank\'>Github REPO</a>
|
||||
example_inputs:
|
||||
- |-
|
||||
你 说 你 不 SP 懂 为 何 在 这 时 牵 手 AP<sep>D#4/Eb4 | D#4/Eb4 | D#4/Eb4 | D#4/Eb4 | rest | D#4/Eb4 | D4 | D4 | D4 | D#4/Eb4 | F4 | D#4/Eb4 | D4 | rest<sep>0.113740 | 0.329060 | 0.287950 | 0.133480 | 0.150900 | 0.484730 | 0.242010 | 0.180820 | 0.343570 | 0.152050 | 0.266720 | 0.280310 | 0.633300 | 0.444590
|
||||
- |-
|
||||
小酒窝长睫毛AP是你最美的记号<sep>C#4/Db4 | F#4/Gb4 | G#4/Ab4 | A#4/Bb4 F#4/Gb4 | F#4/Gb4 C#4/Db4 | C#4/Db4 | rest | C#4/Db4 | A#4/Bb4 | G#4/Ab4 | A#4/Bb4 | G#4/Ab4 | F4 | C#4/Db4<sep>0.407140 | 0.376190 | 0.242180 | 0.509550 0.183420 | 0.315400 0.235020 | 0.361660 | 0.223070 | 0.377270 | 0.340550 | 0.299620 | 0.344510 | 0.283770 | 0.323390 | 0.360340
|
||||
- |-
|
||||
我真的SP爱你SP句句不轻易<sep>D4 | A4 | F#4 | rest | A4 | D4 | rest | B4 | A4 F#4 | F#4 | A4 | A4<sep>0.8 | 0.4 | 0.967 | 0.3 | 0.4 | 0.967 | 0.4 | 0.8 | 0.4 0.4 | 0.25 | 0.967 | 0.9
|
||||
- |-
|
||||
好冷啊 AP 我在东北玩泥巴<sep>F4 | F4 | D4 | rest | D4 | D4 | C4 | C4 | B3 | C4 | D4<sep>0.5 | 0.3 | 0.3 | 0.3 | 0.2 | 0.2 | 0.2 | 0.2 | 0.25 | 0.25 | 0.4
|
||||
|
||||
#inference_cls: inference.svs.ds_cascade.DiffSingerCascadeInfer
|
||||
#exp_name: 0303_opencpop_ds58_midi
|
||||
|
||||
inference_cls: inference.svs.ds_e2e.DiffSingerE2EInfer
|
||||
exp_name: 0228_opencpop_ds100_rel
|
||||
@@ -0,0 +1,91 @@
|
||||
import importlib
|
||||
import re
|
||||
|
||||
import gradio as gr
|
||||
import yaml
|
||||
from gradio.inputs import Textbox
|
||||
|
||||
from inference.svs.base_svs_infer import BaseSVSInfer
|
||||
from utils.hparams import set_hparams
|
||||
from utils.hparams import hparams as hp
|
||||
import numpy as np
|
||||
|
||||
|
||||
class GradioInfer:
|
||||
def __init__(self, exp_name, inference_cls, title, description, article, example_inputs):
|
||||
self.exp_name = exp_name
|
||||
self.title = title
|
||||
self.description = description
|
||||
self.article = article
|
||||
self.example_inputs = example_inputs
|
||||
pkg = ".".join(inference_cls.split(".")[:-1])
|
||||
cls_name = inference_cls.split(".")[-1]
|
||||
self.inference_cls = getattr(importlib.import_module(pkg), cls_name)
|
||||
|
||||
def greet(self, text, notes, notes_duration):
|
||||
PUNCS = '。?;:'
|
||||
sents = re.split(rf'([{PUNCS}])', text.replace('\n', ','))
|
||||
sents_notes = re.split(rf'([{PUNCS}])', notes.replace('\n', ','))
|
||||
sents_notes_dur = re.split(rf'([{PUNCS}])', notes_duration.replace('\n', ','))
|
||||
|
||||
if sents[-1] not in list(PUNCS):
|
||||
sents = sents + ['']
|
||||
sents_notes = sents_notes + ['']
|
||||
sents_notes_dur = sents_notes_dur + ['']
|
||||
|
||||
audio_outs = []
|
||||
s, n, n_dur = "", "", ""
|
||||
for i in range(0, len(sents), 2):
|
||||
if len(sents[i]) > 0:
|
||||
s += sents[i] + sents[i + 1]
|
||||
n += sents_notes[i] + sents_notes[i+1]
|
||||
n_dur += sents_notes_dur[i] + sents_notes_dur[i+1]
|
||||
if len(s) >= 400 or (i >= len(sents) - 2 and len(s) > 0):
|
||||
audio_out = self.infer_ins.infer_once({
|
||||
'text': s,
|
||||
'notes': n,
|
||||
'notes_duration': n_dur,
|
||||
})
|
||||
audio_out = audio_out * 32767
|
||||
audio_out = audio_out.astype(np.int16)
|
||||
audio_outs.append(audio_out)
|
||||
audio_outs.append(np.zeros(int(hp['audio_sample_rate'] * 0.3)).astype(np.int16))
|
||||
s = ""
|
||||
n = ""
|
||||
audio_outs = np.concatenate(audio_outs)
|
||||
return hp['audio_sample_rate'], audio_outs
|
||||
|
||||
def run(self):
|
||||
set_hparams(exp_name=self.exp_name, print_hparams=False)
|
||||
infer_cls = self.inference_cls
|
||||
self.infer_ins: BaseSVSInfer = infer_cls(hp)
|
||||
example_inputs = self.example_inputs
|
||||
for i in range(len(example_inputs)):
|
||||
text, notes, notes_dur = example_inputs[i].split('<sep>')
|
||||
example_inputs[i] = [text, notes, notes_dur]
|
||||
|
||||
iface = gr.Interface(fn=self.greet,
|
||||
inputs=[
|
||||
Textbox(lines=2, placeholder=None, default=example_inputs[0][0], label="input text"),
|
||||
Textbox(lines=2, placeholder=None, default=example_inputs[0][1], label="input note"),
|
||||
Textbox(lines=2, placeholder=None, default=example_inputs[0][2], label="input duration")]
|
||||
,
|
||||
outputs="audio",
|
||||
allow_flagging="never",
|
||||
title=self.title,
|
||||
description=self.description,
|
||||
article=self.article,
|
||||
examples=example_inputs,
|
||||
enable_queue=True)
|
||||
iface.launch(share=True,)# cache_examples=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
gradio_config = yaml.safe_load(open('inference/svs/gradio/gradio_settings.yaml'))
|
||||
g = GradioInfer(**gradio_config)
|
||||
g.run()
|
||||
|
||||
|
||||
# python inference/svs/gradio/infer.py --config usr/configs/midi/cascade/opencs/ds60_rel.yaml --exp_name 0303_opencpop_ds58_midi
|
||||
# python inference/svs/ds_cascade.py --config usr/configs/midi/cascade/opencs/ds60_rel.yaml --exp_name 0303_opencpop_ds58_midi
|
||||
# CUDA_VISIBLE_DEVICES=3 python inference/svs/gradio/infer.py --config usr/configs/midi/e2e/opencpop/ds100_adj_rel.yaml --exp_name 0228_opencpop_ds100_rel
|
||||
@@ -0,0 +1,418 @@
|
||||
| a | a |
|
||||
| ai | ai |
|
||||
| an | an |
|
||||
| ang | ang |
|
||||
| ao | ao |
|
||||
| ba | b a |
|
||||
| bai | b ai |
|
||||
| ban | b an |
|
||||
| bang | b ang |
|
||||
| bao | b ao |
|
||||
| bei | b ei |
|
||||
| ben | b en |
|
||||
| beng | b eng |
|
||||
| bi | b i |
|
||||
| bian | b ian |
|
||||
| biao | b iao |
|
||||
| bie | b ie |
|
||||
| bin | b in |
|
||||
| bing | b ing |
|
||||
| bo | b o |
|
||||
| bu | b u |
|
||||
| ca | c a |
|
||||
| cai | c ai |
|
||||
| can | c an |
|
||||
| cang | c ang |
|
||||
| cao | c ao |
|
||||
| ce | c e |
|
||||
| cei | c ei |
|
||||
| cen | c en |
|
||||
| ceng | c eng |
|
||||
| cha | ch a |
|
||||
| chai | ch ai |
|
||||
| chan | ch an |
|
||||
| chang | ch ang |
|
||||
| chao | ch ao |
|
||||
| che | ch e |
|
||||
| chen | ch en |
|
||||
| cheng | ch eng |
|
||||
| chi | ch i |
|
||||
| chong | ch ong |
|
||||
| chou | ch ou |
|
||||
| chu | ch u |
|
||||
| chua | ch ua |
|
||||
| chuai | ch uai |
|
||||
| chuan | ch uan |
|
||||
| chuang | ch uang |
|
||||
| chui | ch ui |
|
||||
| chun | ch un |
|
||||
| chuo | ch uo |
|
||||
| ci | c i |
|
||||
| cong | c ong |
|
||||
| cou | c ou |
|
||||
| cu | c u |
|
||||
| cuan | c uan |
|
||||
| cui | c ui |
|
||||
| cun | c un |
|
||||
| cuo | c uo |
|
||||
| da | d a |
|
||||
| dai | d ai |
|
||||
| dan | d an |
|
||||
| dang | d ang |
|
||||
| dao | d ao |
|
||||
| de | d e |
|
||||
| dei | d ei |
|
||||
| den | d en |
|
||||
| deng | d eng |
|
||||
| di | d i |
|
||||
| dia | d ia |
|
||||
| dian | d ian |
|
||||
| diao | d iao |
|
||||
| die | d ie |
|
||||
| ding | d ing |
|
||||
| diu | d iu |
|
||||
| dong | d ong |
|
||||
| dou | d ou |
|
||||
| du | d u |
|
||||
| duan | d uan |
|
||||
| dui | d ui |
|
||||
| dun | d un |
|
||||
| duo | d uo |
|
||||
| e | e |
|
||||
| ei | ei |
|
||||
| en | en |
|
||||
| eng | eng |
|
||||
| er | er |
|
||||
| fa | f a |
|
||||
| fan | f an |
|
||||
| fang | f ang |
|
||||
| fei | f ei |
|
||||
| fen | f en |
|
||||
| feng | f eng |
|
||||
| fo | f o |
|
||||
| fou | f ou |
|
||||
| fu | f u |
|
||||
| ga | g a |
|
||||
| gai | g ai |
|
||||
| gan | g an |
|
||||
| gang | g ang |
|
||||
| gao | g ao |
|
||||
| ge | g e |
|
||||
| gei | g ei |
|
||||
| gen | g en |
|
||||
| geng | g eng |
|
||||
| gong | g ong |
|
||||
| gou | g ou |
|
||||
| gu | g u |
|
||||
| gua | g ua |
|
||||
| guai | g uai |
|
||||
| guan | g uan |
|
||||
| guang | g uang |
|
||||
| gui | g ui |
|
||||
| gun | g un |
|
||||
| guo | g uo |
|
||||
| ha | h a |
|
||||
| hai | h ai |
|
||||
| han | h an |
|
||||
| hang | h ang |
|
||||
| hao | h ao |
|
||||
| he | h e |
|
||||
| hei | h ei |
|
||||
| hen | h en |
|
||||
| heng | h eng |
|
||||
| hm | h m |
|
||||
| hng | h ng |
|
||||
| hong | h ong |
|
||||
| hou | h ou |
|
||||
| hu | h u |
|
||||
| hua | h ua |
|
||||
| huai | h uai |
|
||||
| huan | h uan |
|
||||
| huang | h uang |
|
||||
| hui | h ui |
|
||||
| hun | h un |
|
||||
| huo | h uo |
|
||||
| ji | j i |
|
||||
| jia | j ia |
|
||||
| jian | j ian |
|
||||
| jiang | j iang |
|
||||
| jiao | j iao |
|
||||
| jie | j ie |
|
||||
| jin | j in |
|
||||
| jing | j ing |
|
||||
| jiong | j iong |
|
||||
| jiu | j iu |
|
||||
| ju | j v |
|
||||
| juan | j van |
|
||||
| jue | j ve |
|
||||
| jun | j vn |
|
||||
| ka | k a |
|
||||
| kai | k ai |
|
||||
| kan | k an |
|
||||
| kang | k ang |
|
||||
| kao | k ao |
|
||||
| ke | k e |
|
||||
| kei | k ei |
|
||||
| ken | k en |
|
||||
| keng | k eng |
|
||||
| kong | k ong |
|
||||
| kou | k ou |
|
||||
| ku | k u |
|
||||
| kua | k ua |
|
||||
| kuai | k uai |
|
||||
| kuan | k uan |
|
||||
| kuang | k uang |
|
||||
| kui | k ui |
|
||||
| kun | k un |
|
||||
| kuo | k uo |
|
||||
| la | l a |
|
||||
| lai | l ai |
|
||||
| lan | l an |
|
||||
| lang | l ang |
|
||||
| lao | l ao |
|
||||
| le | l e |
|
||||
| lei | l ei |
|
||||
| leng | l eng |
|
||||
| li | l i |
|
||||
| lia | l ia |
|
||||
| lian | l ian |
|
||||
| liang | l iang |
|
||||
| liao | l iao |
|
||||
| lie | l ie |
|
||||
| lin | l in |
|
||||
| ling | l ing |
|
||||
| liu | l iu |
|
||||
| lo | l o |
|
||||
| long | l ong |
|
||||
| lou | l ou |
|
||||
| lu | l u |
|
||||
| luan | l uan |
|
||||
| lun | l un |
|
||||
| luo | l uo |
|
||||
| lv | l v |
|
||||
| lve | l ve |
|
||||
| m | m |
|
||||
| ma | m a |
|
||||
| mai | m ai |
|
||||
| man | m an |
|
||||
| mang | m ang |
|
||||
| mao | m ao |
|
||||
| me | m e |
|
||||
| mei | m ei |
|
||||
| men | m en |
|
||||
| meng | m eng |
|
||||
| mi | m i |
|
||||
| mian | m ian |
|
||||
| miao | m iao |
|
||||
| mie | m ie |
|
||||
| min | m in |
|
||||
| ming | m ing |
|
||||
| miu | m iu |
|
||||
| mo | m o |
|
||||
| mou | m ou |
|
||||
| mu | m u |
|
||||
| n | n |
|
||||
| na | n a |
|
||||
| nai | n ai |
|
||||
| nan | n an |
|
||||
| nang | n ang |
|
||||
| nao | n ao |
|
||||
| ne | n e |
|
||||
| nei | n ei |
|
||||
| nen | n en |
|
||||
| neng | n eng |
|
||||
| ng | n g |
|
||||
| ni | n i |
|
||||
| nian | n ian |
|
||||
| niang | n iang |
|
||||
| niao | n iao |
|
||||
| nie | n ie |
|
||||
| nin | n in |
|
||||
| ning | n ing |
|
||||
| niu | n iu |
|
||||
| nong | n ong |
|
||||
| nou | n ou |
|
||||
| nu | n u |
|
||||
| nuan | n uan |
|
||||
| nun | n un |
|
||||
| nuo | n uo |
|
||||
| nv | n v |
|
||||
| nve | n ve |
|
||||
| o | o |
|
||||
| ou | ou |
|
||||
| pa | p a |
|
||||
| pai | p ai |
|
||||
| pan | p an |
|
||||
| pang | p ang |
|
||||
| pao | p ao |
|
||||
| pei | p ei |
|
||||
| pen | p en |
|
||||
| peng | p eng |
|
||||
| pi | p i |
|
||||
| pian | p ian |
|
||||
| piao | p iao |
|
||||
| pie | p ie |
|
||||
| pin | p in |
|
||||
| ping | p ing |
|
||||
| po | p o |
|
||||
| pou | p ou |
|
||||
| pu | p u |
|
||||
| qi | q i |
|
||||
| qia | q ia |
|
||||
| qian | q ian |
|
||||
| qiang | q iang |
|
||||
| qiao | q iao |
|
||||
| qie | q ie |
|
||||
| qin | q in |
|
||||
| qing | q ing |
|
||||
| qiong | q iong |
|
||||
| qiu | q iu |
|
||||
| qu | q v |
|
||||
| quan | q van |
|
||||
| que | q ve |
|
||||
| qun | q vn |
|
||||
| ran | r an |
|
||||
| rang | r ang |
|
||||
| rao | r ao |
|
||||
| re | r e |
|
||||
| ren | r en |
|
||||
| reng | r eng |
|
||||
| ri | r i |
|
||||
| rong | r ong |
|
||||
| rou | r ou |
|
||||
| ru | r u |
|
||||
| rua | r ua |
|
||||
| ruan | r uan |
|
||||
| rui | r ui |
|
||||
| run | r un |
|
||||
| ruo | r uo |
|
||||
| sa | s a |
|
||||
| sai | s ai |
|
||||
| san | s an |
|
||||
| sang | s ang |
|
||||
| sao | s ao |
|
||||
| se | s e |
|
||||
| sen | s en |
|
||||
| seng | s eng |
|
||||
| sha | sh a |
|
||||
| shai | sh ai |
|
||||
| shan | sh an |
|
||||
| shang | sh ang |
|
||||
| shao | sh ao |
|
||||
| she | sh e |
|
||||
| shei | sh ei |
|
||||
| shen | sh en |
|
||||
| sheng | sh eng |
|
||||
| shi | sh i |
|
||||
| shou | sh ou |
|
||||
| shu | sh u |
|
||||
| shua | sh ua |
|
||||
| shuai | sh uai |
|
||||
| shuan | sh uan |
|
||||
| shuang | sh uang |
|
||||
| shui | sh ui |
|
||||
| shun | sh un |
|
||||
| shuo | sh uo |
|
||||
| si | s i |
|
||||
| song | s ong |
|
||||
| sou | s ou |
|
||||
| su | s u |
|
||||
| suan | s uan |
|
||||
| sui | s ui |
|
||||
| sun | s un |
|
||||
| suo | s uo |
|
||||
| ta | t a |
|
||||
| tai | t ai |
|
||||
| tan | t an |
|
||||
| tang | t ang |
|
||||
| tao | t ao |
|
||||
| te | t e |
|
||||
| tei | t ei |
|
||||
| teng | t eng |
|
||||
| ti | t i |
|
||||
| tian | t ian |
|
||||
| tiao | t iao |
|
||||
| tie | t ie |
|
||||
| ting | t ing |
|
||||
| tong | t ong |
|
||||
| tou | t ou |
|
||||
| tu | t u |
|
||||
| tuan | t uan |
|
||||
| tui | t ui |
|
||||
| tun | t un |
|
||||
| tuo | t uo |
|
||||
| wa | w a |
|
||||
| wai | w ai |
|
||||
| wan | w an |
|
||||
| wang | w ang |
|
||||
| wei | w ei |
|
||||
| wen | w en |
|
||||
| weng | w eng |
|
||||
| wo | w o |
|
||||
| wu | w u |
|
||||
| xi | x i |
|
||||
| xia | x ia |
|
||||
| xian | x ian |
|
||||
| xiang | x iang |
|
||||
| xiao | x iao |
|
||||
| xie | x ie |
|
||||
| xin | x in |
|
||||
| xing | x ing |
|
||||
| xiong | x iong |
|
||||
| xiu | x iu |
|
||||
| xu | x v |
|
||||
| xuan | x van |
|
||||
| xue | x ve |
|
||||
| xun | x vn |
|
||||
| ya | y a |
|
||||
| yan | y an |
|
||||
| yang | y ang |
|
||||
| yao | y ao |
|
||||
| ye | y e |
|
||||
| yi | y i |
|
||||
| yin | y in |
|
||||
| ying | y ing |
|
||||
| yo | y o |
|
||||
| yong | y ong |
|
||||
| you | y ou |
|
||||
| yu | y v |
|
||||
| yuan | y van |
|
||||
| yue | y ve |
|
||||
| yun | y vn |
|
||||
| za | z a |
|
||||
| zai | z ai |
|
||||
| zan | z an |
|
||||
| zang | z ang |
|
||||
| zao | z ao |
|
||||
| ze | z e |
|
||||
| zei | z ei |
|
||||
| zen | z en |
|
||||
| zeng | z eng |
|
||||
| zha | zh a |
|
||||
| zhai | zh ai |
|
||||
| zhan | zh an |
|
||||
| zhang | zh ang |
|
||||
| zhao | zh ao |
|
||||
| zhe | zh e |
|
||||
| zhei | zh ei |
|
||||
| zhen | zh en |
|
||||
| zheng | zh eng |
|
||||
| zhi | zh i |
|
||||
| zhong | zh ong |
|
||||
| zhou | zh ou |
|
||||
| zhu | zh u |
|
||||
| zhua | zh ua |
|
||||
| zhuai | zh uai |
|
||||
| zhuan | zh uan |
|
||||
| zhuang | zh uang |
|
||||
| zhui | zh ui |
|
||||
| zhun | zh un |
|
||||
| zhuo | zh uo |
|
||||
| zi | z i |
|
||||
| zong | z ong |
|
||||
| zou | z ou |
|
||||
| zu | z u |
|
||||
| zuan | z uan |
|
||||
| zui | z ui |
|
||||
| zun | z un |
|
||||
| zuo | z uo |
|
||||
@@ -0,0 +1,8 @@
|
||||
def cpop_pinyin2ph_func():
|
||||
# In the README file of opencpop dataset, they defined a "pinyin to phoneme mapping table"
|
||||
pinyin2phs = {'AP': 'AP', 'SP': 'SP'}
|
||||
with open('inference/svs/opencpop/cpop_pinyin2ph.txt') as rf:
|
||||
for line in rf.readlines():
|
||||
elements = [x.strip() for x in line.split('|') if x.strip() != '']
|
||||
pinyin2phs[elements[0]] = elements[1]
|
||||
return pinyin2phs
|
||||
@@ -0,0 +1,668 @@
|
||||
import math
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import Parameter
|
||||
import torch.onnx.operators
|
||||
import torch.nn.functional as F
|
||||
import utils
|
||||
|
||||
|
||||
class Reshape(nn.Module):
|
||||
def __init__(self, *args):
|
||||
super(Reshape, self).__init__()
|
||||
self.shape = args
|
||||
|
||||
def forward(self, x):
|
||||
return x.view(self.shape)
|
||||
|
||||
|
||||
class Permute(nn.Module):
|
||||
def __init__(self, *args):
|
||||
super(Permute, self).__init__()
|
||||
self.args = args
|
||||
|
||||
def forward(self, x):
|
||||
return x.permute(self.args)
|
||||
|
||||
|
||||
class LinearNorm(torch.nn.Module):
|
||||
def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'):
|
||||
super(LinearNorm, self).__init__()
|
||||
self.linear_layer = torch.nn.Linear(in_dim, out_dim, bias=bias)
|
||||
|
||||
torch.nn.init.xavier_uniform_(
|
||||
self.linear_layer.weight,
|
||||
gain=torch.nn.init.calculate_gain(w_init_gain))
|
||||
|
||||
def forward(self, x):
|
||||
return self.linear_layer(x)
|
||||
|
||||
|
||||
class ConvNorm(torch.nn.Module):
|
||||
def __init__(self, in_channels, out_channels, kernel_size=1, stride=1,
|
||||
padding=None, dilation=1, bias=True, w_init_gain='linear'):
|
||||
super(ConvNorm, self).__init__()
|
||||
if padding is None:
|
||||
assert (kernel_size % 2 == 1)
|
||||
padding = int(dilation * (kernel_size - 1) / 2)
|
||||
|
||||
self.conv = torch.nn.Conv1d(in_channels, out_channels,
|
||||
kernel_size=kernel_size, stride=stride,
|
||||
padding=padding, dilation=dilation,
|
||||
bias=bias)
|
||||
|
||||
torch.nn.init.xavier_uniform_(
|
||||
self.conv.weight, gain=torch.nn.init.calculate_gain(w_init_gain))
|
||||
|
||||
def forward(self, signal):
|
||||
conv_signal = self.conv(signal)
|
||||
return conv_signal
|
||||
|
||||
|
||||
def Embedding(num_embeddings, embedding_dim, padding_idx=None):
|
||||
m = nn.Embedding(num_embeddings, embedding_dim, padding_idx=padding_idx)
|
||||
nn.init.normal_(m.weight, mean=0, std=embedding_dim ** -0.5)
|
||||
if padding_idx is not None:
|
||||
nn.init.constant_(m.weight[padding_idx], 0)
|
||||
return m
|
||||
|
||||
|
||||
def LayerNorm(normalized_shape, eps=1e-5, elementwise_affine=True, export=False):
|
||||
if not export and torch.cuda.is_available():
|
||||
try:
|
||||
from apex.normalization import FusedLayerNorm
|
||||
return FusedLayerNorm(normalized_shape, eps, elementwise_affine)
|
||||
except ImportError:
|
||||
pass
|
||||
return torch.nn.LayerNorm(normalized_shape, eps, elementwise_affine)
|
||||
|
||||
|
||||
def Linear(in_features, out_features, bias=True):
|
||||
m = nn.Linear(in_features, out_features, bias)
|
||||
nn.init.xavier_uniform_(m.weight)
|
||||
if bias:
|
||||
nn.init.constant_(m.bias, 0.)
|
||||
return m
|
||||
|
||||
|
||||
class SinusoidalPositionalEmbedding(nn.Module):
|
||||
"""This module produces sinusoidal positional embeddings of any length.
|
||||
|
||||
Padding symbols are ignored.
|
||||
"""
|
||||
|
||||
def __init__(self, embedding_dim, padding_idx, init_size=1024):
|
||||
super().__init__()
|
||||
self.embedding_dim = embedding_dim
|
||||
self.padding_idx = padding_idx
|
||||
self.weights = SinusoidalPositionalEmbedding.get_embedding(
|
||||
init_size,
|
||||
embedding_dim,
|
||||
padding_idx,
|
||||
)
|
||||
self.register_buffer('_float_tensor', torch.FloatTensor(1))
|
||||
|
||||
@staticmethod
|
||||
def get_embedding(num_embeddings, embedding_dim, padding_idx=None):
|
||||
"""Build sinusoidal embeddings.
|
||||
|
||||
This matches the implementation in tensor2tensor, but differs slightly
|
||||
from the description in Section 3.5 of "Attention Is All You Need".
|
||||
"""
|
||||
half_dim = embedding_dim // 2
|
||||
emb = math.log(10000) / (half_dim - 1)
|
||||
emb = torch.exp(torch.arange(half_dim, dtype=torch.float) * -emb)
|
||||
emb = torch.arange(num_embeddings, dtype=torch.float).unsqueeze(1) * emb.unsqueeze(0)
|
||||
emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1).view(num_embeddings, -1)
|
||||
if embedding_dim % 2 == 1:
|
||||
# zero pad
|
||||
emb = torch.cat([emb, torch.zeros(num_embeddings, 1)], dim=1)
|
||||
if padding_idx is not None:
|
||||
emb[padding_idx, :] = 0
|
||||
return emb
|
||||
|
||||
def forward(self, input, incremental_state=None, timestep=None, positions=None, **kwargs):
|
||||
"""Input is expected to be of size [bsz x seqlen]."""
|
||||
bsz, seq_len = input.shape[:2]
|
||||
max_pos = self.padding_idx + 1 + seq_len
|
||||
if self.weights is None or max_pos > self.weights.size(0):
|
||||
# recompute/expand embeddings if needed
|
||||
self.weights = SinusoidalPositionalEmbedding.get_embedding(
|
||||
max_pos,
|
||||
self.embedding_dim,
|
||||
self.padding_idx,
|
||||
)
|
||||
self.weights = self.weights.to(self._float_tensor)
|
||||
|
||||
if incremental_state is not None:
|
||||
# positions is the same for every token when decoding a single step
|
||||
pos = timestep.view(-1)[0] + 1 if timestep is not None else seq_len
|
||||
return self.weights[self.padding_idx + pos, :].expand(bsz, 1, -1)
|
||||
|
||||
positions = utils.make_positions(input, self.padding_idx) if positions is None else positions
|
||||
return self.weights.index_select(0, positions.view(-1)).view(bsz, seq_len, -1).detach()
|
||||
|
||||
def max_positions(self):
|
||||
"""Maximum number of supported positions."""
|
||||
return int(1e5) # an arbitrary large number
|
||||
|
||||
|
||||
class ConvTBC(nn.Module):
|
||||
def __init__(self, in_channels, out_channels, kernel_size, padding=0):
|
||||
super(ConvTBC, self).__init__()
|
||||
self.in_channels = in_channels
|
||||
self.out_channels = out_channels
|
||||
self.kernel_size = kernel_size
|
||||
self.padding = padding
|
||||
|
||||
self.weight = torch.nn.Parameter(torch.Tensor(
|
||||
self.kernel_size, in_channels, out_channels))
|
||||
self.bias = torch.nn.Parameter(torch.Tensor(out_channels))
|
||||
|
||||
def forward(self, input):
|
||||
return torch.conv_tbc(input.contiguous(), self.weight, self.bias, self.padding)
|
||||
|
||||
|
||||
class MultiheadAttention(nn.Module):
|
||||
def __init__(self, embed_dim, num_heads, kdim=None, vdim=None, dropout=0., bias=True,
|
||||
add_bias_kv=False, add_zero_attn=False, self_attention=False,
|
||||
encoder_decoder_attention=False):
|
||||
super().__init__()
|
||||
self.embed_dim = embed_dim
|
||||
self.kdim = kdim if kdim is not None else embed_dim
|
||||
self.vdim = vdim if vdim is not None else embed_dim
|
||||
self.qkv_same_dim = self.kdim == embed_dim and self.vdim == embed_dim
|
||||
|
||||
self.num_heads = num_heads
|
||||
self.dropout = dropout
|
||||
self.head_dim = embed_dim // num_heads
|
||||
assert self.head_dim * num_heads == self.embed_dim, "embed_dim must be divisible by num_heads"
|
||||
self.scaling = self.head_dim ** -0.5
|
||||
|
||||
self.self_attention = self_attention
|
||||
self.encoder_decoder_attention = encoder_decoder_attention
|
||||
|
||||
assert not self.self_attention or self.qkv_same_dim, 'Self-attention requires query, key and ' \
|
||||
'value to be of the same size'
|
||||
|
||||
if self.qkv_same_dim:
|
||||
self.in_proj_weight = Parameter(torch.Tensor(3 * embed_dim, embed_dim))
|
||||
else:
|
||||
self.k_proj_weight = Parameter(torch.Tensor(embed_dim, self.kdim))
|
||||
self.v_proj_weight = Parameter(torch.Tensor(embed_dim, self.vdim))
|
||||
self.q_proj_weight = Parameter(torch.Tensor(embed_dim, embed_dim))
|
||||
|
||||
if bias:
|
||||
self.in_proj_bias = Parameter(torch.Tensor(3 * embed_dim))
|
||||
else:
|
||||
self.register_parameter('in_proj_bias', None)
|
||||
|
||||
self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
||||
|
||||
if add_bias_kv:
|
||||
self.bias_k = Parameter(torch.Tensor(1, 1, embed_dim))
|
||||
self.bias_v = Parameter(torch.Tensor(1, 1, embed_dim))
|
||||
else:
|
||||
self.bias_k = self.bias_v = None
|
||||
|
||||
self.add_zero_attn = add_zero_attn
|
||||
|
||||
self.reset_parameters()
|
||||
|
||||
self.enable_torch_version = False
|
||||
if hasattr(F, "multi_head_attention_forward"):
|
||||
self.enable_torch_version = True
|
||||
else:
|
||||
self.enable_torch_version = False
|
||||
self.last_attn_probs = None
|
||||
|
||||
def reset_parameters(self):
|
||||
if self.qkv_same_dim:
|
||||
nn.init.xavier_uniform_(self.in_proj_weight)
|
||||
else:
|
||||
nn.init.xavier_uniform_(self.k_proj_weight)
|
||||
nn.init.xavier_uniform_(self.v_proj_weight)
|
||||
nn.init.xavier_uniform_(self.q_proj_weight)
|
||||
|
||||
nn.init.xavier_uniform_(self.out_proj.weight)
|
||||
if self.in_proj_bias is not None:
|
||||
nn.init.constant_(self.in_proj_bias, 0.)
|
||||
nn.init.constant_(self.out_proj.bias, 0.)
|
||||
if self.bias_k is not None:
|
||||
nn.init.xavier_normal_(self.bias_k)
|
||||
if self.bias_v is not None:
|
||||
nn.init.xavier_normal_(self.bias_v)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
query, key, value,
|
||||
key_padding_mask=None,
|
||||
incremental_state=None,
|
||||
need_weights=True,
|
||||
static_kv=False,
|
||||
attn_mask=None,
|
||||
before_softmax=False,
|
||||
need_head_weights=False,
|
||||
enc_dec_attn_constraint_mask=None,
|
||||
reset_attn_weight=None
|
||||
):
|
||||
"""Input shape: Time x Batch x Channel
|
||||
|
||||
Args:
|
||||
key_padding_mask (ByteTensor, optional): mask to exclude
|
||||
keys that are pads, of shape `(batch, src_len)`, where
|
||||
padding elements are indicated by 1s.
|
||||
need_weights (bool, optional): return the attention weights,
|
||||
averaged over heads (default: False).
|
||||
attn_mask (ByteTensor, optional): typically used to
|
||||
implement causal attention, where the mask prevents the
|
||||
attention from looking forward in time (default: None).
|
||||
before_softmax (bool, optional): return the raw attention
|
||||
weights and values before the attention softmax.
|
||||
need_head_weights (bool, optional): return the attention
|
||||
weights for each head. Implies *need_weights*. Default:
|
||||
return the average attention weights over all heads.
|
||||
"""
|
||||
if need_head_weights:
|
||||
need_weights = True
|
||||
|
||||
tgt_len, bsz, embed_dim = query.size()
|
||||
assert embed_dim == self.embed_dim
|
||||
assert list(query.size()) == [tgt_len, bsz, embed_dim]
|
||||
|
||||
if self.enable_torch_version and incremental_state is None and not static_kv and reset_attn_weight is None:
|
||||
if self.qkv_same_dim:
|
||||
return F.multi_head_attention_forward(query, key, value,
|
||||
self.embed_dim, self.num_heads,
|
||||
self.in_proj_weight,
|
||||
self.in_proj_bias, self.bias_k, self.bias_v,
|
||||
self.add_zero_attn, self.dropout,
|
||||
self.out_proj.weight, self.out_proj.bias,
|
||||
self.training, key_padding_mask, need_weights,
|
||||
attn_mask)
|
||||
else:
|
||||
return F.multi_head_attention_forward(query, key, value,
|
||||
self.embed_dim, self.num_heads,
|
||||
torch.empty([0]),
|
||||
self.in_proj_bias, self.bias_k, self.bias_v,
|
||||
self.add_zero_attn, self.dropout,
|
||||
self.out_proj.weight, self.out_proj.bias,
|
||||
self.training, key_padding_mask, need_weights,
|
||||
attn_mask, use_separate_proj_weight=True,
|
||||
q_proj_weight=self.q_proj_weight,
|
||||
k_proj_weight=self.k_proj_weight,
|
||||
v_proj_weight=self.v_proj_weight)
|
||||
|
||||
if incremental_state is not None:
|
||||
print('Not implemented error.')
|
||||
exit()
|
||||
else:
|
||||
saved_state = None
|
||||
|
||||
if self.self_attention:
|
||||
# self-attention
|
||||
q, k, v = self.in_proj_qkv(query)
|
||||
elif self.encoder_decoder_attention:
|
||||
# encoder-decoder attention
|
||||
q = self.in_proj_q(query)
|
||||
if key is None:
|
||||
assert value is None
|
||||
k = v = None
|
||||
else:
|
||||
k = self.in_proj_k(key)
|
||||
v = self.in_proj_v(key)
|
||||
|
||||
else:
|
||||
q = self.in_proj_q(query)
|
||||
k = self.in_proj_k(key)
|
||||
v = self.in_proj_v(value)
|
||||
q *= self.scaling
|
||||
|
||||
if self.bias_k is not None:
|
||||
assert self.bias_v is not None
|
||||
k = torch.cat([k, self.bias_k.repeat(1, bsz, 1)])
|
||||
v = torch.cat([v, self.bias_v.repeat(1, bsz, 1)])
|
||||
if attn_mask is not None:
|
||||
attn_mask = torch.cat([attn_mask, attn_mask.new_zeros(attn_mask.size(0), 1)], dim=1)
|
||||
if key_padding_mask is not None:
|
||||
key_padding_mask = torch.cat(
|
||||
[key_padding_mask, key_padding_mask.new_zeros(key_padding_mask.size(0), 1)], dim=1)
|
||||
|
||||
q = q.contiguous().view(tgt_len, bsz * self.num_heads, self.head_dim).transpose(0, 1)
|
||||
if k is not None:
|
||||
k = k.contiguous().view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1)
|
||||
if v is not None:
|
||||
v = v.contiguous().view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1)
|
||||
|
||||
if saved_state is not None:
|
||||
print('Not implemented error.')
|
||||
exit()
|
||||
|
||||
src_len = k.size(1)
|
||||
|
||||
# This is part of a workaround to get around fork/join parallelism
|
||||
# not supporting Optional types.
|
||||
if key_padding_mask is not None and key_padding_mask.shape == torch.Size([]):
|
||||
key_padding_mask = None
|
||||
|
||||
if key_padding_mask is not None:
|
||||
assert key_padding_mask.size(0) == bsz
|
||||
assert key_padding_mask.size(1) == src_len
|
||||
|
||||
if self.add_zero_attn:
|
||||
src_len += 1
|
||||
k = torch.cat([k, k.new_zeros((k.size(0), 1) + k.size()[2:])], dim=1)
|
||||
v = torch.cat([v, v.new_zeros((v.size(0), 1) + v.size()[2:])], dim=1)
|
||||
if attn_mask is not None:
|
||||
attn_mask = torch.cat([attn_mask, attn_mask.new_zeros(attn_mask.size(0), 1)], dim=1)
|
||||
if key_padding_mask is not None:
|
||||
key_padding_mask = torch.cat(
|
||||
[key_padding_mask, torch.zeros(key_padding_mask.size(0), 1).type_as(key_padding_mask)], dim=1)
|
||||
|
||||
attn_weights = torch.bmm(q, k.transpose(1, 2))
|
||||
attn_weights = self.apply_sparse_mask(attn_weights, tgt_len, src_len, bsz)
|
||||
|
||||
assert list(attn_weights.size()) == [bsz * self.num_heads, tgt_len, src_len]
|
||||
|
||||
if attn_mask is not None:
|
||||
if len(attn_mask.shape) == 2:
|
||||
attn_mask = attn_mask.unsqueeze(0)
|
||||
elif len(attn_mask.shape) == 3:
|
||||
attn_mask = attn_mask[:, None].repeat([1, self.num_heads, 1, 1]).reshape(
|
||||
bsz * self.num_heads, tgt_len, src_len)
|
||||
attn_weights = attn_weights + attn_mask
|
||||
|
||||
if enc_dec_attn_constraint_mask is not None: # bs x head x L_kv
|
||||
attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
|
||||
attn_weights = attn_weights.masked_fill(
|
||||
enc_dec_attn_constraint_mask.unsqueeze(2).bool(),
|
||||
-1e9,
|
||||
)
|
||||
attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)
|
||||
|
||||
if key_padding_mask is not None:
|
||||
# don't attend to padding symbols
|
||||
attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
|
||||
attn_weights = attn_weights.masked_fill(
|
||||
key_padding_mask.unsqueeze(1).unsqueeze(2),
|
||||
-1e9,
|
||||
)
|
||||
attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)
|
||||
|
||||
attn_logits = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
|
||||
|
||||
if before_softmax:
|
||||
return attn_weights, v
|
||||
|
||||
attn_weights_float = utils.softmax(attn_weights, dim=-1)
|
||||
attn_weights = attn_weights_float.type_as(attn_weights)
|
||||
attn_probs = F.dropout(attn_weights_float.type_as(attn_weights), p=self.dropout, training=self.training)
|
||||
|
||||
if reset_attn_weight is not None:
|
||||
if reset_attn_weight:
|
||||
self.last_attn_probs = attn_probs.detach()
|
||||
else:
|
||||
assert self.last_attn_probs is not None
|
||||
attn_probs = self.last_attn_probs
|
||||
attn = torch.bmm(attn_probs, v)
|
||||
assert list(attn.size()) == [bsz * self.num_heads, tgt_len, self.head_dim]
|
||||
attn = attn.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim)
|
||||
attn = self.out_proj(attn)
|
||||
|
||||
if need_weights:
|
||||
attn_weights = attn_weights_float.view(bsz, self.num_heads, tgt_len, src_len).transpose(1, 0)
|
||||
if not need_head_weights:
|
||||
# average attention weights over heads
|
||||
attn_weights = attn_weights.mean(dim=0)
|
||||
else:
|
||||
attn_weights = None
|
||||
|
||||
return attn, (attn_weights, attn_logits)
|
||||
|
||||
def in_proj_qkv(self, query):
|
||||
return self._in_proj(query).chunk(3, dim=-1)
|
||||
|
||||
def in_proj_q(self, query):
|
||||
if self.qkv_same_dim:
|
||||
return self._in_proj(query, end=self.embed_dim)
|
||||
else:
|
||||
bias = self.in_proj_bias
|
||||
if bias is not None:
|
||||
bias = bias[:self.embed_dim]
|
||||
return F.linear(query, self.q_proj_weight, bias)
|
||||
|
||||
def in_proj_k(self, key):
|
||||
if self.qkv_same_dim:
|
||||
return self._in_proj(key, start=self.embed_dim, end=2 * self.embed_dim)
|
||||
else:
|
||||
weight = self.k_proj_weight
|
||||
bias = self.in_proj_bias
|
||||
if bias is not None:
|
||||
bias = bias[self.embed_dim:2 * self.embed_dim]
|
||||
return F.linear(key, weight, bias)
|
||||
|
||||
def in_proj_v(self, value):
|
||||
if self.qkv_same_dim:
|
||||
return self._in_proj(value, start=2 * self.embed_dim)
|
||||
else:
|
||||
weight = self.v_proj_weight
|
||||
bias = self.in_proj_bias
|
||||
if bias is not None:
|
||||
bias = bias[2 * self.embed_dim:]
|
||||
return F.linear(value, weight, bias)
|
||||
|
||||
def _in_proj(self, input, start=0, end=None):
|
||||
weight = self.in_proj_weight
|
||||
bias = self.in_proj_bias
|
||||
weight = weight[start:end, :]
|
||||
if bias is not None:
|
||||
bias = bias[start:end]
|
||||
return F.linear(input, weight, bias)
|
||||
|
||||
|
||||
def apply_sparse_mask(self, attn_weights, tgt_len, src_len, bsz):
|
||||
return attn_weights
|
||||
|
||||
|
||||
class Swish(torch.autograd.Function):
|
||||
@staticmethod
|
||||
def forward(ctx, i):
|
||||
result = i * torch.sigmoid(i)
|
||||
ctx.save_for_backward(i)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def backward(ctx, grad_output):
|
||||
i = ctx.saved_variables[0]
|
||||
sigmoid_i = torch.sigmoid(i)
|
||||
return grad_output * (sigmoid_i * (1 + i * (1 - sigmoid_i)))
|
||||
|
||||
|
||||
class CustomSwish(nn.Module):
|
||||
def forward(self, input_tensor):
|
||||
return Swish.apply(input_tensor)
|
||||
|
||||
|
||||
class TransformerFFNLayer(nn.Module):
|
||||
def __init__(self, hidden_size, filter_size, padding="SAME", kernel_size=1, dropout=0., act='gelu'):
|
||||
super().__init__()
|
||||
self.kernel_size = kernel_size
|
||||
self.dropout = dropout
|
||||
self.act = act
|
||||
if padding == 'SAME':
|
||||
self.ffn_1 = nn.Conv1d(hidden_size, filter_size, kernel_size, padding=kernel_size // 2)
|
||||
elif padding == 'LEFT':
|
||||
self.ffn_1 = nn.Sequential(
|
||||
nn.ConstantPad1d((kernel_size - 1, 0), 0.0),
|
||||
nn.Conv1d(hidden_size, filter_size, kernel_size)
|
||||
)
|
||||
self.ffn_2 = Linear(filter_size, hidden_size)
|
||||
if self.act == 'swish':
|
||||
self.swish_fn = CustomSwish()
|
||||
|
||||
def forward(self, x, incremental_state=None):
|
||||
# x: T x B x C
|
||||
if incremental_state is not None:
|
||||
assert incremental_state is None, 'Nar-generation does not allow this.'
|
||||
exit(1)
|
||||
|
||||
x = self.ffn_1(x.permute(1, 2, 0)).permute(2, 0, 1)
|
||||
x = x * self.kernel_size ** -0.5
|
||||
|
||||
if incremental_state is not None:
|
||||
x = x[-1:]
|
||||
if self.act == 'gelu':
|
||||
x = F.gelu(x)
|
||||
if self.act == 'relu':
|
||||
x = F.relu(x)
|
||||
if self.act == 'swish':
|
||||
x = self.swish_fn(x)
|
||||
x = F.dropout(x, self.dropout, training=self.training)
|
||||
x = self.ffn_2(x)
|
||||
return x
|
||||
|
||||
|
||||
class BatchNorm1dTBC(nn.Module):
|
||||
def __init__(self, c):
|
||||
super(BatchNorm1dTBC, self).__init__()
|
||||
self.bn = nn.BatchNorm1d(c)
|
||||
|
||||
def forward(self, x):
|
||||
"""
|
||||
|
||||
:param x: [T, B, C]
|
||||
:return: [T, B, C]
|
||||
"""
|
||||
x = x.permute(1, 2, 0) # [B, C, T]
|
||||
x = self.bn(x) # [B, C, T]
|
||||
x = x.permute(2, 0, 1) # [T, B, C]
|
||||
return x
|
||||
|
||||
|
||||
class EncSALayer(nn.Module):
|
||||
def __init__(self, c, num_heads, dropout, attention_dropout=0.1,
|
||||
relu_dropout=0.1, kernel_size=9, padding='SAME', norm='ln', act='gelu'):
|
||||
super().__init__()
|
||||
self.c = c
|
||||
self.dropout = dropout
|
||||
self.num_heads = num_heads
|
||||
if num_heads > 0:
|
||||
if norm == 'ln':
|
||||
self.layer_norm1 = LayerNorm(c)
|
||||
elif norm == 'bn':
|
||||
self.layer_norm1 = BatchNorm1dTBC(c)
|
||||
self.self_attn = MultiheadAttention(
|
||||
self.c, num_heads, self_attention=True, dropout=attention_dropout, bias=False,
|
||||
)
|
||||
if norm == 'ln':
|
||||
self.layer_norm2 = LayerNorm(c)
|
||||
elif norm == 'bn':
|
||||
self.layer_norm2 = BatchNorm1dTBC(c)
|
||||
self.ffn = TransformerFFNLayer(
|
||||
c, 4 * c, kernel_size=kernel_size, dropout=relu_dropout, padding=padding, act=act)
|
||||
|
||||
def forward(self, x, encoder_padding_mask=None, **kwargs):
|
||||
layer_norm_training = kwargs.get('layer_norm_training', None)
|
||||
if layer_norm_training is not None:
|
||||
self.layer_norm1.training = layer_norm_training
|
||||
self.layer_norm2.training = layer_norm_training
|
||||
if self.num_heads > 0:
|
||||
residual = x
|
||||
x = self.layer_norm1(x)
|
||||
x, _, = self.self_attn(
|
||||
query=x,
|
||||
key=x,
|
||||
value=x,
|
||||
key_padding_mask=encoder_padding_mask
|
||||
)
|
||||
x = F.dropout(x, self.dropout, training=self.training)
|
||||
x = residual + x
|
||||
x = x * (1 - encoder_padding_mask.float()).transpose(0, 1)[..., None]
|
||||
|
||||
residual = x
|
||||
x = self.layer_norm2(x)
|
||||
x = self.ffn(x)
|
||||
x = F.dropout(x, self.dropout, training=self.training)
|
||||
x = residual + x
|
||||
x = x * (1 - encoder_padding_mask.float()).transpose(0, 1)[..., None]
|
||||
return x
|
||||
|
||||
|
||||
class DecSALayer(nn.Module):
|
||||
def __init__(self, c, num_heads, dropout, attention_dropout=0.1, relu_dropout=0.1, kernel_size=9, act='gelu'):
|
||||
super().__init__()
|
||||
self.c = c
|
||||
self.dropout = dropout
|
||||
self.layer_norm1 = LayerNorm(c)
|
||||
self.self_attn = MultiheadAttention(
|
||||
c, num_heads, self_attention=True, dropout=attention_dropout, bias=False
|
||||
)
|
||||
self.layer_norm2 = LayerNorm(c)
|
||||
self.encoder_attn = MultiheadAttention(
|
||||
c, num_heads, encoder_decoder_attention=True, dropout=attention_dropout, bias=False,
|
||||
)
|
||||
self.layer_norm3 = LayerNorm(c)
|
||||
self.ffn = TransformerFFNLayer(
|
||||
c, 4 * c, padding='LEFT', kernel_size=kernel_size, dropout=relu_dropout, act=act)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
x,
|
||||
encoder_out=None,
|
||||
encoder_padding_mask=None,
|
||||
incremental_state=None,
|
||||
self_attn_mask=None,
|
||||
self_attn_padding_mask=None,
|
||||
attn_out=None,
|
||||
reset_attn_weight=None,
|
||||
**kwargs,
|
||||
):
|
||||
layer_norm_training = kwargs.get('layer_norm_training', None)
|
||||
if layer_norm_training is not None:
|
||||
self.layer_norm1.training = layer_norm_training
|
||||
self.layer_norm2.training = layer_norm_training
|
||||
self.layer_norm3.training = layer_norm_training
|
||||
residual = x
|
||||
x = self.layer_norm1(x)
|
||||
x, _ = self.self_attn(
|
||||
query=x,
|
||||
key=x,
|
||||
value=x,
|
||||
key_padding_mask=self_attn_padding_mask,
|
||||
incremental_state=incremental_state,
|
||||
attn_mask=self_attn_mask
|
||||
)
|
||||
x = F.dropout(x, self.dropout, training=self.training)
|
||||
x = residual + x
|
||||
|
||||
residual = x
|
||||
x = self.layer_norm2(x)
|
||||
if encoder_out is not None:
|
||||
x, attn = self.encoder_attn(
|
||||
query=x,
|
||||
key=encoder_out,
|
||||
value=encoder_out,
|
||||
key_padding_mask=encoder_padding_mask,
|
||||
incremental_state=incremental_state,
|
||||
static_kv=True,
|
||||
enc_dec_attn_constraint_mask=None, #utils.get_incremental_state(self, incremental_state, 'enc_dec_attn_constraint_mask'),
|
||||
reset_attn_weight=reset_attn_weight
|
||||
)
|
||||
attn_logits = attn[1]
|
||||
else:
|
||||
assert attn_out is not None
|
||||
x = self.encoder_attn.in_proj_v(attn_out.transpose(0, 1))
|
||||
attn_logits = None
|
||||
x = F.dropout(x, self.dropout, training=self.training)
|
||||
x = residual + x
|
||||
|
||||
residual = x
|
||||
x = self.layer_norm3(x)
|
||||
x = self.ffn(x, incremental_state=incremental_state)
|
||||
x = F.dropout(x, self.dropout, training=self.training)
|
||||
x = residual + x
|
||||
# if len(attn_logits.size()) > 3:
|
||||
# indices = attn_logits.softmax(-1).max(-1).values.sum(-1).argmax(-1)
|
||||
# attn_logits = attn_logits.gather(1,
|
||||
# indices[:, None, None, None].repeat(1, 1, attn_logits.size(-2), attn_logits.size(-1))).squeeze(1)
|
||||
return x, attn_logits
|
||||
@@ -0,0 +1,113 @@
|
||||
import math
|
||||
import torch
|
||||
|
||||
|
||||
class PositionalEncoding(torch.nn.Module):
|
||||
"""Positional encoding.
|
||||
Args:
|
||||
d_model (int): Embedding dimension.
|
||||
dropout_rate (float): Dropout rate.
|
||||
max_len (int): Maximum input length.
|
||||
reverse (bool): Whether to reverse the input position.
|
||||
"""
|
||||
|
||||
def __init__(self, d_model, dropout_rate, max_len=5000, reverse=False):
|
||||
"""Construct an PositionalEncoding object."""
|
||||
super(PositionalEncoding, self).__init__()
|
||||
self.d_model = d_model
|
||||
self.reverse = reverse
|
||||
self.xscale = math.sqrt(self.d_model)
|
||||
self.dropout = torch.nn.Dropout(p=dropout_rate)
|
||||
self.pe = None
|
||||
self.extend_pe(torch.tensor(0.0).expand(1, max_len))
|
||||
|
||||
def extend_pe(self, x):
|
||||
"""Reset the positional encodings."""
|
||||
if self.pe is not None:
|
||||
if self.pe.size(1) >= x.size(1):
|
||||
if self.pe.dtype != x.dtype or self.pe.device != x.device:
|
||||
self.pe = self.pe.to(dtype=x.dtype, device=x.device)
|
||||
return
|
||||
pe = torch.zeros(x.size(1), self.d_model)
|
||||
if self.reverse:
|
||||
position = torch.arange(
|
||||
x.size(1) - 1, -1, -1.0, dtype=torch.float32
|
||||
).unsqueeze(1)
|
||||
else:
|
||||
position = torch.arange(0, x.size(1), dtype=torch.float32).unsqueeze(1)
|
||||
div_term = torch.exp(
|
||||
torch.arange(0, self.d_model, 2, dtype=torch.float32)
|
||||
* -(math.log(10000.0) / self.d_model)
|
||||
)
|
||||
pe[:, 0::2] = torch.sin(position * div_term)
|
||||
pe[:, 1::2] = torch.cos(position * div_term)
|
||||
pe = pe.unsqueeze(0)
|
||||
self.pe = pe.to(device=x.device, dtype=x.dtype)
|
||||
|
||||
def forward(self, x: torch.Tensor):
|
||||
"""Add positional encoding.
|
||||
Args:
|
||||
x (torch.Tensor): Input tensor (batch, time, `*`).
|
||||
Returns:
|
||||
torch.Tensor: Encoded tensor (batch, time, `*`).
|
||||
"""
|
||||
self.extend_pe(x)
|
||||
x = x * self.xscale + self.pe[:, : x.size(1)]
|
||||
return self.dropout(x)
|
||||
|
||||
|
||||
class ScaledPositionalEncoding(PositionalEncoding):
|
||||
"""Scaled positional encoding module.
|
||||
See Sec. 3.2 https://arxiv.org/abs/1809.08895
|
||||
Args:
|
||||
d_model (int): Embedding dimension.
|
||||
dropout_rate (float): Dropout rate.
|
||||
max_len (int): Maximum input length.
|
||||
"""
|
||||
|
||||
def __init__(self, d_model, dropout_rate, max_len=5000):
|
||||
"""Initialize class."""
|
||||
super().__init__(d_model=d_model, dropout_rate=dropout_rate, max_len=max_len)
|
||||
self.alpha = torch.nn.Parameter(torch.tensor(1.0))
|
||||
|
||||
def reset_parameters(self):
|
||||
"""Reset parameters."""
|
||||
self.alpha.data = torch.tensor(1.0)
|
||||
|
||||
def forward(self, x):
|
||||
"""Add positional encoding.
|
||||
Args:
|
||||
x (torch.Tensor): Input tensor (batch, time, `*`).
|
||||
Returns:
|
||||
torch.Tensor: Encoded tensor (batch, time, `*`).
|
||||
"""
|
||||
self.extend_pe(x)
|
||||
x = x + self.alpha * self.pe[:, : x.size(1)]
|
||||
return self.dropout(x)
|
||||
|
||||
|
||||
class RelPositionalEncoding(PositionalEncoding):
|
||||
"""Relative positional encoding module.
|
||||
See : Appendix B in https://arxiv.org/abs/1901.02860
|
||||
Args:
|
||||
d_model (int): Embedding dimension.
|
||||
dropout_rate (float): Dropout rate.
|
||||
max_len (int): Maximum input length.
|
||||
"""
|
||||
|
||||
def __init__(self, d_model, dropout_rate, max_len=5000):
|
||||
"""Initialize class."""
|
||||
super().__init__(d_model, dropout_rate, max_len, reverse=True)
|
||||
|
||||
def forward(self, x):
|
||||
"""Compute positional encoding.
|
||||
Args:
|
||||
x (torch.Tensor): Input tensor (batch, time, `*`).
|
||||
Returns:
|
||||
torch.Tensor: Encoded tensor (batch, time, `*`).
|
||||
torch.Tensor: Positional embedding tensor (1, time, `*`).
|
||||
"""
|
||||
self.extend_pe(x)
|
||||
x = x * self.xscale
|
||||
pos_emb = self.pe[:, : x.size(1)]
|
||||
return self.dropout(x) + self.dropout(pos_emb)
|
||||
@@ -0,0 +1,391 @@
|
||||
# '''
|
||||
# https://github.com/One-sixth/ms_ssim_pytorch/blob/master/ssim.py
|
||||
# '''
|
||||
#
|
||||
# import torch
|
||||
# import torch.jit
|
||||
# import torch.nn.functional as F
|
||||
#
|
||||
#
|
||||
# @torch.jit.script
|
||||
# def create_window(window_size: int, sigma: float, channel: int):
|
||||
# '''
|
||||
# Create 1-D gauss kernel
|
||||
# :param window_size: the size of gauss kernel
|
||||
# :param sigma: sigma of normal distribution
|
||||
# :param channel: input channel
|
||||
# :return: 1D kernel
|
||||
# '''
|
||||
# coords = torch.arange(window_size, dtype=torch.float)
|
||||
# coords -= window_size // 2
|
||||
#
|
||||
# g = torch.exp(-(coords ** 2) / (2 * sigma ** 2))
|
||||
# g /= g.sum()
|
||||
#
|
||||
# g = g.reshape(1, 1, 1, -1).repeat(channel, 1, 1, 1)
|
||||
# return g
|
||||
#
|
||||
#
|
||||
# @torch.jit.script
|
||||
# def _gaussian_filter(x, window_1d, use_padding: bool):
|
||||
# '''
|
||||
# Blur input with 1-D kernel
|
||||
# :param x: batch of tensors to be blured
|
||||
# :param window_1d: 1-D gauss kernel
|
||||
# :param use_padding: padding image before conv
|
||||
# :return: blured tensors
|
||||
# '''
|
||||
# C = x.shape[1]
|
||||
# padding = 0
|
||||
# if use_padding:
|
||||
# window_size = window_1d.shape[3]
|
||||
# padding = window_size // 2
|
||||
# out = F.conv2d(x, window_1d, stride=1, padding=(0, padding), groups=C)
|
||||
# out = F.conv2d(out, window_1d.transpose(2, 3), stride=1, padding=(padding, 0), groups=C)
|
||||
# return out
|
||||
#
|
||||
#
|
||||
# @torch.jit.script
|
||||
# def ssim(X, Y, window, data_range: float, use_padding: bool = False):
|
||||
# '''
|
||||
# Calculate ssim index for X and Y
|
||||
# :param X: images [B, C, H, N_bins]
|
||||
# :param Y: images [B, C, H, N_bins]
|
||||
# :param window: 1-D gauss kernel
|
||||
# :param data_range: value range of input images. (usually 1.0 or 255)
|
||||
# :param use_padding: padding image before conv
|
||||
# :return:
|
||||
# '''
|
||||
#
|
||||
# K1 = 0.01
|
||||
# K2 = 0.03
|
||||
# compensation = 1.0
|
||||
#
|
||||
# C1 = (K1 * data_range) ** 2
|
||||
# C2 = (K2 * data_range) ** 2
|
||||
#
|
||||
# mu1 = _gaussian_filter(X, window, use_padding)
|
||||
# mu2 = _gaussian_filter(Y, window, use_padding)
|
||||
# sigma1_sq = _gaussian_filter(X * X, window, use_padding)
|
||||
# sigma2_sq = _gaussian_filter(Y * Y, window, use_padding)
|
||||
# sigma12 = _gaussian_filter(X * Y, window, use_padding)
|
||||
#
|
||||
# mu1_sq = mu1.pow(2)
|
||||
# mu2_sq = mu2.pow(2)
|
||||
# mu1_mu2 = mu1 * mu2
|
||||
#
|
||||
# sigma1_sq = compensation * (sigma1_sq - mu1_sq)
|
||||
# sigma2_sq = compensation * (sigma2_sq - mu2_sq)
|
||||
# sigma12 = compensation * (sigma12 - mu1_mu2)
|
||||
#
|
||||
# cs_map = (2 * sigma12 + C2) / (sigma1_sq + sigma2_sq + C2)
|
||||
# # Fixed the issue that the negative value of cs_map caused ms_ssim to output Nan.
|
||||
# cs_map = cs_map.clamp_min(0.)
|
||||
# ssim_map = ((2 * mu1_mu2 + C1) / (mu1_sq + mu2_sq + C1)) * cs_map
|
||||
#
|
||||
# ssim_val = ssim_map.mean(dim=(1, 2, 3)) # reduce along CHW
|
||||
# cs = cs_map.mean(dim=(1, 2, 3))
|
||||
#
|
||||
# return ssim_val, cs
|
||||
#
|
||||
#
|
||||
# @torch.jit.script
|
||||
# def ms_ssim(X, Y, window, data_range: float, weights, use_padding: bool = False, eps: float = 1e-8):
|
||||
# '''
|
||||
# interface of ms-ssim
|
||||
# :param X: a batch of images, (N,C,H,W)
|
||||
# :param Y: a batch of images, (N,C,H,W)
|
||||
# :param window: 1-D gauss kernel
|
||||
# :param data_range: value range of input images. (usually 1.0 or 255)
|
||||
# :param weights: weights for different levels
|
||||
# :param use_padding: padding image before conv
|
||||
# :param eps: use for avoid grad nan.
|
||||
# :return:
|
||||
# '''
|
||||
# levels = weights.shape[0]
|
||||
# cs_vals = []
|
||||
# ssim_vals = []
|
||||
# for _ in range(levels):
|
||||
# ssim_val, cs = ssim(X, Y, window=window, data_range=data_range, use_padding=use_padding)
|
||||
# # Use for fix a issue. When c = a ** b and a is 0, c.backward() will cause the a.grad become inf.
|
||||
# ssim_val = ssim_val.clamp_min(eps)
|
||||
# cs = cs.clamp_min(eps)
|
||||
# cs_vals.append(cs)
|
||||
#
|
||||
# ssim_vals.append(ssim_val)
|
||||
# padding = (X.shape[2] % 2, X.shape[3] % 2)
|
||||
# X = F.avg_pool2d(X, kernel_size=2, stride=2, padding=padding)
|
||||
# Y = F.avg_pool2d(Y, kernel_size=2, stride=2, padding=padding)
|
||||
#
|
||||
# cs_vals = torch.stack(cs_vals, dim=0)
|
||||
# ms_ssim_val = torch.prod((cs_vals[:-1] ** weights[:-1].unsqueeze(1)) * (ssim_vals[-1] ** weights[-1]), dim=0)
|
||||
# return ms_ssim_val
|
||||
#
|
||||
#
|
||||
# class SSIM(torch.jit.ScriptModule):
|
||||
# __constants__ = ['data_range', 'use_padding']
|
||||
#
|
||||
# def __init__(self, window_size=11, window_sigma=1.5, data_range=255., channel=3, use_padding=False):
|
||||
# '''
|
||||
# :param window_size: the size of gauss kernel
|
||||
# :param window_sigma: sigma of normal distribution
|
||||
# :param data_range: value range of input images. (usually 1.0 or 255)
|
||||
# :param channel: input channels (default: 3)
|
||||
# :param use_padding: padding image before conv
|
||||
# '''
|
||||
# super().__init__()
|
||||
# assert window_size % 2 == 1, 'Window size must be odd.'
|
||||
# window = create_window(window_size, window_sigma, channel)
|
||||
# self.register_buffer('window', window)
|
||||
# self.data_range = data_range
|
||||
# self.use_padding = use_padding
|
||||
#
|
||||
# @torch.jit.script_method
|
||||
# def forward(self, X, Y):
|
||||
# r = ssim(X, Y, window=self.window, data_range=self.data_range, use_padding=self.use_padding)
|
||||
# return r[0]
|
||||
#
|
||||
#
|
||||
# class MS_SSIM(torch.jit.ScriptModule):
|
||||
# __constants__ = ['data_range', 'use_padding', 'eps']
|
||||
#
|
||||
# def __init__(self, window_size=11, window_sigma=1.5, data_range=255., channel=3, use_padding=False, weights=None,
|
||||
# levels=None, eps=1e-8):
|
||||
# '''
|
||||
# class for ms-ssim
|
||||
# :param window_size: the size of gauss kernel
|
||||
# :param window_sigma: sigma of normal distribution
|
||||
# :param data_range: value range of input images. (usually 1.0 or 255)
|
||||
# :param channel: input channels
|
||||
# :param use_padding: padding image before conv
|
||||
# :param weights: weights for different levels. (default [0.0448, 0.2856, 0.3001, 0.2363, 0.1333])
|
||||
# :param levels: number of downsampling
|
||||
# :param eps: Use for fix a issue. When c = a ** b and a is 0, c.backward() will cause the a.grad become inf.
|
||||
# '''
|
||||
# super().__init__()
|
||||
# assert window_size % 2 == 1, 'Window size must be odd.'
|
||||
# self.data_range = data_range
|
||||
# self.use_padding = use_padding
|
||||
# self.eps = eps
|
||||
#
|
||||
# window = create_window(window_size, window_sigma, channel)
|
||||
# self.register_buffer('window', window)
|
||||
#
|
||||
# if weights is None:
|
||||
# weights = [0.0448, 0.2856, 0.3001, 0.2363, 0.1333]
|
||||
# weights = torch.tensor(weights, dtype=torch.float)
|
||||
#
|
||||
# if levels is not None:
|
||||
# weights = weights[:levels]
|
||||
# weights = weights / weights.sum()
|
||||
#
|
||||
# self.register_buffer('weights', weights)
|
||||
#
|
||||
# @torch.jit.script_method
|
||||
# def forward(self, X, Y):
|
||||
# return ms_ssim(X, Y, window=self.window, data_range=self.data_range, weights=self.weights,
|
||||
# use_padding=self.use_padding, eps=self.eps)
|
||||
#
|
||||
#
|
||||
# if __name__ == '__main__':
|
||||
# print('Simple Test')
|
||||
# im = torch.randint(0, 255, (5, 3, 256, 256), dtype=torch.float, device='cuda')
|
||||
# img1 = im / 255
|
||||
# img2 = img1 * 0.5
|
||||
#
|
||||
# losser = SSIM(data_range=1.).cuda()
|
||||
# loss = losser(img1, img2).mean()
|
||||
#
|
||||
# losser2 = MS_SSIM(data_range=1.).cuda()
|
||||
# loss2 = losser2(img1, img2).mean()
|
||||
#
|
||||
# print(loss.item())
|
||||
# print(loss2.item())
|
||||
#
|
||||
# if __name__ == '__main__':
|
||||
# print('Training Test')
|
||||
# import cv2
|
||||
# import torch.optim
|
||||
# import numpy as np
|
||||
# import imageio
|
||||
# import time
|
||||
#
|
||||
# out_test_video = False
|
||||
# # 最好不要直接输出gif图,会非常大,最好先输出mkv文件后用ffmpeg转换到GIF
|
||||
# video_use_gif = False
|
||||
#
|
||||
# im = cv2.imread('test_img1.jpg', 1)
|
||||
# t_im = torch.from_numpy(im).cuda().permute(2, 0, 1).float()[None] / 255.
|
||||
#
|
||||
# if out_test_video:
|
||||
# if video_use_gif:
|
||||
# fps = 0.5
|
||||
# out_wh = (im.shape[1] // 2, im.shape[0] // 2)
|
||||
# suffix = '.gif'
|
||||
# else:
|
||||
# fps = 5
|
||||
# out_wh = (im.shape[1], im.shape[0])
|
||||
# suffix = '.mkv'
|
||||
# video_last_time = time.perf_counter()
|
||||
# video = imageio.get_writer('ssim_test' + suffix, fps=fps)
|
||||
#
|
||||
# # 测试ssim
|
||||
# print('Training SSIM')
|
||||
# rand_im = torch.randint_like(t_im, 0, 255, dtype=torch.float32) / 255.
|
||||
# rand_im.requires_grad = True
|
||||
# optim = torch.optim.Adam([rand_im], 0.003, eps=1e-8)
|
||||
# losser = SSIM(data_range=1., channel=t_im.shape[1]).cuda()
|
||||
# ssim_score = 0
|
||||
# while ssim_score < 0.999:
|
||||
# optim.zero_grad()
|
||||
# loss = losser(rand_im, t_im)
|
||||
# (-loss).sum().backward()
|
||||
# ssim_score = loss.item()
|
||||
# optim.step()
|
||||
# r_im = np.transpose(rand_im.detach().cpu().numpy().clip(0, 1) * 255, [0, 2, 3, 1]).astype(np.uint8)[0]
|
||||
# r_im = cv2.putText(r_im, 'ssim %f' % ssim_score, (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
|
||||
#
|
||||
# if out_test_video:
|
||||
# if time.perf_counter() - video_last_time > 1. / fps:
|
||||
# video_last_time = time.perf_counter()
|
||||
# out_frame = cv2.cvtColor(r_im, cv2.COLOR_BGR2RGB)
|
||||
# out_frame = cv2.resize(out_frame, out_wh, interpolation=cv2.INTER_AREA)
|
||||
# if isinstance(out_frame, cv2.UMat):
|
||||
# out_frame = out_frame.get()
|
||||
# video.append_data(out_frame)
|
||||
#
|
||||
# cv2.imshow('ssim', r_im)
|
||||
# cv2.setWindowTitle('ssim', 'ssim %f' % ssim_score)
|
||||
# cv2.waitKey(1)
|
||||
#
|
||||
# if out_test_video:
|
||||
# video.close()
|
||||
#
|
||||
# # 测试ms_ssim
|
||||
# if out_test_video:
|
||||
# if video_use_gif:
|
||||
# fps = 0.5
|
||||
# out_wh = (im.shape[1] // 2, im.shape[0] // 2)
|
||||
# suffix = '.gif'
|
||||
# else:
|
||||
# fps = 5
|
||||
# out_wh = (im.shape[1], im.shape[0])
|
||||
# suffix = '.mkv'
|
||||
# video_last_time = time.perf_counter()
|
||||
# video = imageio.get_writer('ms_ssim_test' + suffix, fps=fps)
|
||||
#
|
||||
# print('Training MS_SSIM')
|
||||
# rand_im = torch.randint_like(t_im, 0, 255, dtype=torch.float32) / 255.
|
||||
# rand_im.requires_grad = True
|
||||
# optim = torch.optim.Adam([rand_im], 0.003, eps=1e-8)
|
||||
# losser = MS_SSIM(data_range=1., channel=t_im.shape[1]).cuda()
|
||||
# ssim_score = 0
|
||||
# while ssim_score < 0.999:
|
||||
# optim.zero_grad()
|
||||
# loss = losser(rand_im, t_im)
|
||||
# (-loss).sum().backward()
|
||||
# ssim_score = loss.item()
|
||||
# optim.step()
|
||||
# r_im = np.transpose(rand_im.detach().cpu().numpy().clip(0, 1) * 255, [0, 2, 3, 1]).astype(np.uint8)[0]
|
||||
# r_im = cv2.putText(r_im, 'ms_ssim %f' % ssim_score, (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
|
||||
#
|
||||
# if out_test_video:
|
||||
# if time.perf_counter() - video_last_time > 1. / fps:
|
||||
# video_last_time = time.perf_counter()
|
||||
# out_frame = cv2.cvtColor(r_im, cv2.COLOR_BGR2RGB)
|
||||
# out_frame = cv2.resize(out_frame, out_wh, interpolation=cv2.INTER_AREA)
|
||||
# if isinstance(out_frame, cv2.UMat):
|
||||
# out_frame = out_frame.get()
|
||||
# video.append_data(out_frame)
|
||||
#
|
||||
# cv2.imshow('ms_ssim', r_im)
|
||||
# cv2.setWindowTitle('ms_ssim', 'ms_ssim %f' % ssim_score)
|
||||
# cv2.waitKey(1)
|
||||
#
|
||||
# if out_test_video:
|
||||
# video.close()
|
||||
|
||||
"""
|
||||
Adapted from https://github.com/Po-Hsun-Su/pytorch-ssim
|
||||
"""
|
||||
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from torch.autograd import Variable
|
||||
import numpy as np
|
||||
from math import exp
|
||||
|
||||
|
||||
def gaussian(window_size, sigma):
|
||||
gauss = torch.Tensor([exp(-(x - window_size // 2) ** 2 / float(2 * sigma ** 2)) for x in range(window_size)])
|
||||
return gauss / gauss.sum()
|
||||
|
||||
|
||||
def create_window(window_size, channel):
|
||||
_1D_window = gaussian(window_size, 1.5).unsqueeze(1)
|
||||
_2D_window = _1D_window.mm(_1D_window.t()).float().unsqueeze(0).unsqueeze(0)
|
||||
window = Variable(_2D_window.expand(channel, 1, window_size, window_size).contiguous())
|
||||
return window
|
||||
|
||||
|
||||
def _ssim(img1, img2, window, window_size, channel, size_average=True):
|
||||
mu1 = F.conv2d(img1, window, padding=window_size // 2, groups=channel)
|
||||
mu2 = F.conv2d(img2, window, padding=window_size // 2, groups=channel)
|
||||
|
||||
mu1_sq = mu1.pow(2)
|
||||
mu2_sq = mu2.pow(2)
|
||||
mu1_mu2 = mu1 * mu2
|
||||
|
||||
sigma1_sq = F.conv2d(img1 * img1, window, padding=window_size // 2, groups=channel) - mu1_sq
|
||||
sigma2_sq = F.conv2d(img2 * img2, window, padding=window_size // 2, groups=channel) - mu2_sq
|
||||
sigma12 = F.conv2d(img1 * img2, window, padding=window_size // 2, groups=channel) - mu1_mu2
|
||||
|
||||
C1 = 0.01 ** 2
|
||||
C2 = 0.03 ** 2
|
||||
|
||||
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2))
|
||||
|
||||
if size_average:
|
||||
return ssim_map.mean()
|
||||
else:
|
||||
return ssim_map.mean(1)
|
||||
|
||||
|
||||
class SSIM(torch.nn.Module):
|
||||
def __init__(self, window_size=11, size_average=True):
|
||||
super(SSIM, self).__init__()
|
||||
self.window_size = window_size
|
||||
self.size_average = size_average
|
||||
self.channel = 1
|
||||
self.window = create_window(window_size, self.channel)
|
||||
|
||||
def forward(self, img1, img2):
|
||||
(_, channel, _, _) = img1.size()
|
||||
|
||||
if channel == self.channel and self.window.data.type() == img1.data.type():
|
||||
window = self.window
|
||||
else:
|
||||
window = create_window(self.window_size, channel)
|
||||
|
||||
if img1.is_cuda:
|
||||
window = window.cuda(img1.get_device())
|
||||
window = window.type_as(img1)
|
||||
|
||||
self.window = window
|
||||
self.channel = channel
|
||||
|
||||
return _ssim(img1, img2, window, self.window_size, channel, self.size_average)
|
||||
|
||||
|
||||
window = None
|
||||
|
||||
|
||||
def ssim(img1, img2, window_size=11, size_average=True):
|
||||
(_, channel, _, _) = img1.size()
|
||||
global window
|
||||
if window is None:
|
||||
window = create_window(window_size, channel)
|
||||
if img1.is_cuda:
|
||||
window = window.cuda(img1.get_device())
|
||||
window = window.type_as(img1)
|
||||
return _ssim(img1, img2, window, window_size, channel, size_average)
|
||||
@@ -0,0 +1,118 @@
|
||||
from modules.commons.common_layers import *
|
||||
from modules.commons.common_layers import Embedding
|
||||
from modules.fastspeech.tts_modules import FastspeechDecoder, DurationPredictor, LengthRegulator, PitchPredictor, \
|
||||
EnergyPredictor, FastspeechEncoder
|
||||
from utils.cwt import cwt2f0
|
||||
from utils.hparams import hparams
|
||||
from utils.pitch_utils import f0_to_coarse, denorm_f0, norm_f0
|
||||
from modules.fastspeech.fs2 import FastSpeech2
|
||||
|
||||
|
||||
class FastspeechMIDIEncoder(FastspeechEncoder):
|
||||
def forward_embedding(self, txt_tokens, midi_embedding, midi_dur_embedding, slur_embedding):
|
||||
# embed tokens and positions
|
||||
x = self.embed_scale * self.embed_tokens(txt_tokens)
|
||||
x = x + midi_embedding + midi_dur_embedding + slur_embedding
|
||||
if hparams['use_pos_embed']:
|
||||
if hparams.get('rel_pos') is not None and hparams['rel_pos']:
|
||||
x = self.embed_positions(x)
|
||||
else:
|
||||
positions = self.embed_positions(txt_tokens)
|
||||
x = x + positions
|
||||
x = F.dropout(x, p=self.dropout, training=self.training)
|
||||
return x
|
||||
|
||||
def forward(self, txt_tokens, midi_embedding, midi_dur_embedding, slur_embedding):
|
||||
"""
|
||||
|
||||
:param txt_tokens: [B, T]
|
||||
:return: {
|
||||
'encoder_out': [T x B x C]
|
||||
}
|
||||
"""
|
||||
encoder_padding_mask = txt_tokens.eq(self.padding_idx).data
|
||||
x = self.forward_embedding(txt_tokens, midi_embedding, midi_dur_embedding, slur_embedding) # [B, T, H]
|
||||
x = super(FastspeechEncoder, self).forward(x, encoder_padding_mask)
|
||||
return x
|
||||
|
||||
|
||||
FS_ENCODERS = {
|
||||
'fft': lambda hp, embed_tokens, d: FastspeechMIDIEncoder(
|
||||
embed_tokens, hp['hidden_size'], hp['enc_layers'], hp['enc_ffn_kernel_size'],
|
||||
num_heads=hp['num_heads']),
|
||||
}
|
||||
|
||||
|
||||
class FastSpeech2MIDI(FastSpeech2):
|
||||
def __init__(self, dictionary, out_dims=None):
|
||||
super().__init__(dictionary, out_dims)
|
||||
del self.encoder
|
||||
self.encoder = FS_ENCODERS[hparams['encoder_type']](hparams, self.encoder_embed_tokens, self.dictionary)
|
||||
self.midi_embed = Embedding(300, self.hidden_size, self.padding_idx)
|
||||
self.midi_dur_layer = Linear(1, self.hidden_size)
|
||||
self.is_slur_embed = Embedding(2, self.hidden_size)
|
||||
|
||||
def forward(self, txt_tokens, mel2ph=None, spk_embed=None,
|
||||
ref_mels=None, f0=None, uv=None, energy=None, skip_decoder=False,
|
||||
spk_embed_dur_id=None, spk_embed_f0_id=None, infer=False, **kwargs):
|
||||
ret = {}
|
||||
|
||||
midi_embedding = self.midi_embed(kwargs['pitch_midi'])
|
||||
midi_dur_embedding, slur_embedding = 0, 0
|
||||
if kwargs.get('midi_dur') is not None:
|
||||
midi_dur_embedding = self.midi_dur_layer(kwargs['midi_dur'][:, :, None]) # [B, T, 1] -> [B, T, H]
|
||||
if kwargs.get('is_slur') is not None:
|
||||
slur_embedding = self.is_slur_embed(kwargs['is_slur'])
|
||||
encoder_out = self.encoder(txt_tokens, midi_embedding, midi_dur_embedding, slur_embedding) # [B, T, C]
|
||||
src_nonpadding = (txt_tokens > 0).float()[:, :, None]
|
||||
|
||||
# add ref style embed
|
||||
# Not implemented
|
||||
# variance encoder
|
||||
var_embed = 0
|
||||
|
||||
# encoder_out_dur denotes encoder outputs for duration predictor
|
||||
# in speech adaptation, duration predictor use old speaker embedding
|
||||
if hparams['use_spk_embed']:
|
||||
spk_embed_dur = spk_embed_f0 = spk_embed = self.spk_embed_proj(spk_embed)[:, None, :]
|
||||
elif hparams['use_spk_id']:
|
||||
spk_embed_id = spk_embed
|
||||
if spk_embed_dur_id is None:
|
||||
spk_embed_dur_id = spk_embed_id
|
||||
if spk_embed_f0_id is None:
|
||||
spk_embed_f0_id = spk_embed_id
|
||||
spk_embed = self.spk_embed_proj(spk_embed_id)[:, None, :]
|
||||
spk_embed_dur = spk_embed_f0 = spk_embed
|
||||
if hparams['use_split_spk_id']:
|
||||
spk_embed_dur = self.spk_embed_dur(spk_embed_dur_id)[:, None, :]
|
||||
spk_embed_f0 = self.spk_embed_f0(spk_embed_f0_id)[:, None, :]
|
||||
else:
|
||||
spk_embed_dur = spk_embed_f0 = spk_embed = 0
|
||||
|
||||
# add dur
|
||||
dur_inp = (encoder_out + var_embed + spk_embed_dur) * src_nonpadding
|
||||
|
||||
mel2ph = self.add_dur(dur_inp, mel2ph, txt_tokens, ret)
|
||||
|
||||
decoder_inp = F.pad(encoder_out, [0, 0, 1, 0])
|
||||
|
||||
mel2ph_ = mel2ph[..., None].repeat([1, 1, encoder_out.shape[-1]])
|
||||
decoder_inp_origin = decoder_inp = torch.gather(decoder_inp, 1, mel2ph_) # [B, T, H]
|
||||
|
||||
tgt_nonpadding = (mel2ph > 0).float()[:, :, None]
|
||||
|
||||
# add pitch and energy embed
|
||||
pitch_inp = (decoder_inp_origin + var_embed + spk_embed_f0) * tgt_nonpadding
|
||||
if hparams['use_pitch_embed']:
|
||||
pitch_inp_ph = (encoder_out + var_embed + spk_embed_f0) * src_nonpadding
|
||||
decoder_inp = decoder_inp + self.add_pitch(pitch_inp, f0, uv, mel2ph, ret, encoder_out=pitch_inp_ph)
|
||||
if hparams['use_energy_embed']:
|
||||
decoder_inp = decoder_inp + self.add_energy(pitch_inp, energy, ret)
|
||||
|
||||
ret['decoder_inp'] = decoder_inp = (decoder_inp + spk_embed) * tgt_nonpadding
|
||||
|
||||
if skip_decoder:
|
||||
return ret
|
||||
ret['mel_out'] = self.run_decoder(decoder_inp, tgt_nonpadding, ret, infer=infer, **kwargs)
|
||||
|
||||
return ret
|
||||
@@ -0,0 +1,255 @@
|
||||
from modules.commons.common_layers import *
|
||||
from modules.commons.common_layers import Embedding
|
||||
from modules.fastspeech.tts_modules import FastspeechDecoder, DurationPredictor, LengthRegulator, PitchPredictor, \
|
||||
EnergyPredictor, FastspeechEncoder
|
||||
from utils.cwt import cwt2f0
|
||||
from utils.hparams import hparams
|
||||
from utils.pitch_utils import f0_to_coarse, denorm_f0, norm_f0
|
||||
|
||||
FS_ENCODERS = {
|
||||
'fft': lambda hp, embed_tokens, d: FastspeechEncoder(
|
||||
embed_tokens, hp['hidden_size'], hp['enc_layers'], hp['enc_ffn_kernel_size'],
|
||||
num_heads=hp['num_heads']),
|
||||
}
|
||||
|
||||
FS_DECODERS = {
|
||||
'fft': lambda hp: FastspeechDecoder(
|
||||
hp['hidden_size'], hp['dec_layers'], hp['dec_ffn_kernel_size'], hp['num_heads']),
|
||||
}
|
||||
|
||||
|
||||
class FastSpeech2(nn.Module):
|
||||
def __init__(self, dictionary, out_dims=None):
|
||||
super().__init__()
|
||||
self.dictionary = dictionary
|
||||
self.padding_idx = dictionary.pad()
|
||||
self.enc_layers = hparams['enc_layers']
|
||||
self.dec_layers = hparams['dec_layers']
|
||||
self.hidden_size = hparams['hidden_size']
|
||||
self.encoder_embed_tokens = self.build_embedding(self.dictionary, self.hidden_size)
|
||||
self.encoder = FS_ENCODERS[hparams['encoder_type']](hparams, self.encoder_embed_tokens, self.dictionary)
|
||||
self.decoder = FS_DECODERS[hparams['decoder_type']](hparams)
|
||||
self.out_dims = out_dims
|
||||
if out_dims is None:
|
||||
self.out_dims = hparams['audio_num_mel_bins']
|
||||
self.mel_out = Linear(self.hidden_size, self.out_dims, bias=True)
|
||||
|
||||
if hparams['use_spk_id']:
|
||||
self.spk_embed_proj = Embedding(hparams['num_spk'] + 1, self.hidden_size)
|
||||
if hparams['use_split_spk_id']:
|
||||
self.spk_embed_f0 = Embedding(hparams['num_spk'] + 1, self.hidden_size)
|
||||
self.spk_embed_dur = Embedding(hparams['num_spk'] + 1, self.hidden_size)
|
||||
elif hparams['use_spk_embed']:
|
||||
self.spk_embed_proj = Linear(256, self.hidden_size, bias=True)
|
||||
predictor_hidden = hparams['predictor_hidden'] if hparams['predictor_hidden'] > 0 else self.hidden_size
|
||||
self.dur_predictor = DurationPredictor(
|
||||
self.hidden_size,
|
||||
n_chans=predictor_hidden,
|
||||
n_layers=hparams['dur_predictor_layers'],
|
||||
dropout_rate=hparams['predictor_dropout'], padding=hparams['ffn_padding'],
|
||||
kernel_size=hparams['dur_predictor_kernel'])
|
||||
self.length_regulator = LengthRegulator()
|
||||
if hparams['use_pitch_embed']:
|
||||
self.pitch_embed = Embedding(300, self.hidden_size, self.padding_idx)
|
||||
if hparams['pitch_type'] == 'cwt':
|
||||
h = hparams['cwt_hidden_size']
|
||||
cwt_out_dims = 10
|
||||
if hparams['use_uv']:
|
||||
cwt_out_dims = cwt_out_dims + 1
|
||||
self.cwt_predictor = nn.Sequential(
|
||||
nn.Linear(self.hidden_size, h),
|
||||
PitchPredictor(
|
||||
h,
|
||||
n_chans=predictor_hidden,
|
||||
n_layers=hparams['predictor_layers'],
|
||||
dropout_rate=hparams['predictor_dropout'], odim=cwt_out_dims,
|
||||
padding=hparams['ffn_padding'], kernel_size=hparams['predictor_kernel']))
|
||||
self.cwt_stats_layers = nn.Sequential(
|
||||
nn.Linear(self.hidden_size, h), nn.ReLU(),
|
||||
nn.Linear(h, h), nn.ReLU(), nn.Linear(h, 2)
|
||||
)
|
||||
else:
|
||||
self.pitch_predictor = PitchPredictor(
|
||||
self.hidden_size,
|
||||
n_chans=predictor_hidden,
|
||||
n_layers=hparams['predictor_layers'],
|
||||
dropout_rate=hparams['predictor_dropout'],
|
||||
odim=2 if hparams['pitch_type'] == 'frame' else 1,
|
||||
padding=hparams['ffn_padding'], kernel_size=hparams['predictor_kernel'])
|
||||
if hparams['use_energy_embed']:
|
||||
self.energy_embed = Embedding(256, self.hidden_size, self.padding_idx)
|
||||
self.energy_predictor = EnergyPredictor(
|
||||
self.hidden_size,
|
||||
n_chans=predictor_hidden,
|
||||
n_layers=hparams['predictor_layers'],
|
||||
dropout_rate=hparams['predictor_dropout'], odim=1,
|
||||
padding=hparams['ffn_padding'], kernel_size=hparams['predictor_kernel'])
|
||||
|
||||
def build_embedding(self, dictionary, embed_dim):
|
||||
num_embeddings = len(dictionary)
|
||||
emb = Embedding(num_embeddings, embed_dim, self.padding_idx)
|
||||
return emb
|
||||
|
||||
def forward(self, txt_tokens, mel2ph=None, spk_embed=None,
|
||||
ref_mels=None, f0=None, uv=None, energy=None, skip_decoder=False,
|
||||
spk_embed_dur_id=None, spk_embed_f0_id=None, infer=False, **kwargs):
|
||||
ret = {}
|
||||
encoder_out = self.encoder(txt_tokens) # [B, T, C]
|
||||
src_nonpadding = (txt_tokens > 0).float()[:, :, None]
|
||||
|
||||
# add ref style embed
|
||||
# Not implemented
|
||||
# variance encoder
|
||||
var_embed = 0
|
||||
|
||||
# encoder_out_dur denotes encoder outputs for duration predictor
|
||||
# in speech adaptation, duration predictor use old speaker embedding
|
||||
if hparams['use_spk_embed']:
|
||||
spk_embed_dur = spk_embed_f0 = spk_embed = self.spk_embed_proj(spk_embed)[:, None, :]
|
||||
elif hparams['use_spk_id']:
|
||||
spk_embed_id = spk_embed
|
||||
if spk_embed_dur_id is None:
|
||||
spk_embed_dur_id = spk_embed_id
|
||||
if spk_embed_f0_id is None:
|
||||
spk_embed_f0_id = spk_embed_id
|
||||
spk_embed = self.spk_embed_proj(spk_embed_id)[:, None, :]
|
||||
spk_embed_dur = spk_embed_f0 = spk_embed
|
||||
if hparams['use_split_spk_id']:
|
||||
spk_embed_dur = self.spk_embed_dur(spk_embed_dur_id)[:, None, :]
|
||||
spk_embed_f0 = self.spk_embed_f0(spk_embed_f0_id)[:, None, :]
|
||||
else:
|
||||
spk_embed_dur = spk_embed_f0 = spk_embed = 0
|
||||
|
||||
# add dur
|
||||
dur_inp = (encoder_out + var_embed + spk_embed_dur) * src_nonpadding
|
||||
|
||||
mel2ph = self.add_dur(dur_inp, mel2ph, txt_tokens, ret)
|
||||
|
||||
decoder_inp = F.pad(encoder_out, [0, 0, 1, 0])
|
||||
|
||||
mel2ph_ = mel2ph[..., None].repeat([1, 1, encoder_out.shape[-1]])
|
||||
decoder_inp_origin = decoder_inp = torch.gather(decoder_inp, 1, mel2ph_) # [B, T, H]
|
||||
|
||||
tgt_nonpadding = (mel2ph > 0).float()[:, :, None]
|
||||
|
||||
# add pitch and energy embed
|
||||
pitch_inp = (decoder_inp_origin + var_embed + spk_embed_f0) * tgt_nonpadding
|
||||
if hparams['use_pitch_embed']:
|
||||
pitch_inp_ph = (encoder_out + var_embed + spk_embed_f0) * src_nonpadding
|
||||
decoder_inp = decoder_inp + self.add_pitch(pitch_inp, f0, uv, mel2ph, ret, encoder_out=pitch_inp_ph)
|
||||
if hparams['use_energy_embed']:
|
||||
decoder_inp = decoder_inp + self.add_energy(pitch_inp, energy, ret)
|
||||
|
||||
ret['decoder_inp'] = decoder_inp = (decoder_inp + spk_embed) * tgt_nonpadding
|
||||
|
||||
if skip_decoder:
|
||||
return ret
|
||||
ret['mel_out'] = self.run_decoder(decoder_inp, tgt_nonpadding, ret, infer=infer, **kwargs)
|
||||
|
||||
return ret
|
||||
|
||||
def add_dur(self, dur_input, mel2ph, txt_tokens, ret):
|
||||
"""
|
||||
|
||||
:param dur_input: [B, T_txt, H]
|
||||
:param mel2ph: [B, T_mel]
|
||||
:param txt_tokens: [B, T_txt]
|
||||
:param ret:
|
||||
:return:
|
||||
"""
|
||||
src_padding = txt_tokens == 0
|
||||
dur_input = dur_input.detach() + hparams['predictor_grad'] * (dur_input - dur_input.detach())
|
||||
if mel2ph is None:
|
||||
dur, xs = self.dur_predictor.inference(dur_input, src_padding)
|
||||
ret['dur'] = xs
|
||||
ret['dur_choice'] = dur
|
||||
mel2ph = self.length_regulator(dur, src_padding).detach()
|
||||
# from modules.fastspeech.fake_modules import FakeLengthRegulator
|
||||
# fake_lr = FakeLengthRegulator()
|
||||
# fake_mel2ph = fake_lr(dur, (1 - src_padding.long()).sum(-1))[..., 0].detach()
|
||||
# print(mel2ph == fake_mel2ph)
|
||||
else:
|
||||
ret['dur'] = self.dur_predictor(dur_input, src_padding)
|
||||
ret['mel2ph'] = mel2ph
|
||||
return mel2ph
|
||||
|
||||
def add_energy(self, decoder_inp, energy, ret):
|
||||
decoder_inp = decoder_inp.detach() + hparams['predictor_grad'] * (decoder_inp - decoder_inp.detach())
|
||||
ret['energy_pred'] = energy_pred = self.energy_predictor(decoder_inp)[:, :, 0]
|
||||
if energy is None:
|
||||
energy = energy_pred
|
||||
energy = torch.clamp(energy * 256 // 4, max=255).long()
|
||||
energy_embed = self.energy_embed(energy)
|
||||
return energy_embed
|
||||
|
||||
def add_pitch(self, decoder_inp, f0, uv, mel2ph, ret, encoder_out=None):
|
||||
if hparams['pitch_type'] == 'ph':
|
||||
pitch_pred_inp = encoder_out.detach() + hparams['predictor_grad'] * (encoder_out - encoder_out.detach())
|
||||
pitch_padding = encoder_out.sum().abs() == 0
|
||||
ret['pitch_pred'] = pitch_pred = self.pitch_predictor(pitch_pred_inp)
|
||||
if f0 is None:
|
||||
f0 = pitch_pred[:, :, 0]
|
||||
ret['f0_denorm'] = f0_denorm = denorm_f0(f0, None, hparams, pitch_padding=pitch_padding)
|
||||
pitch = f0_to_coarse(f0_denorm) # start from 0 [B, T_txt]
|
||||
pitch = F.pad(pitch, [1, 0])
|
||||
pitch = torch.gather(pitch, 1, mel2ph) # [B, T_mel]
|
||||
pitch_embed = self.pitch_embed(pitch)
|
||||
return pitch_embed
|
||||
decoder_inp = decoder_inp.detach() + hparams['predictor_grad'] * (decoder_inp - decoder_inp.detach())
|
||||
|
||||
pitch_padding = mel2ph == 0
|
||||
|
||||
if hparams['pitch_type'] == 'cwt':
|
||||
pitch_padding = None
|
||||
ret['cwt'] = cwt_out = self.cwt_predictor(decoder_inp)
|
||||
stats_out = self.cwt_stats_layers(encoder_out[:, 0, :]) # [B, 2]
|
||||
mean = ret['f0_mean'] = stats_out[:, 0]
|
||||
std = ret['f0_std'] = stats_out[:, 1]
|
||||
cwt_spec = cwt_out[:, :, :10]
|
||||
if f0 is None:
|
||||
std = std * hparams['cwt_std_scale']
|
||||
f0 = self.cwt2f0_norm(cwt_spec, mean, std, mel2ph)
|
||||
if hparams['use_uv']:
|
||||
assert cwt_out.shape[-1] == 11
|
||||
uv = cwt_out[:, :, -1] > 0
|
||||
elif hparams['pitch_ar']:
|
||||
ret['pitch_pred'] = pitch_pred = self.pitch_predictor(decoder_inp, f0 if self.training else None)
|
||||
if f0 is None:
|
||||
f0 = pitch_pred[:, :, 0]
|
||||
else:
|
||||
ret['pitch_pred'] = pitch_pred = self.pitch_predictor(decoder_inp)
|
||||
if f0 is None:
|
||||
f0 = pitch_pred[:, :, 0]
|
||||
if hparams['use_uv'] and uv is None:
|
||||
uv = pitch_pred[:, :, 1] > 0
|
||||
ret['f0_denorm'] = f0_denorm = denorm_f0(f0, uv, hparams, pitch_padding=pitch_padding)
|
||||
if pitch_padding is not None:
|
||||
f0[pitch_padding] = 0
|
||||
|
||||
pitch = f0_to_coarse(f0_denorm) # start from 0
|
||||
pitch_embed = self.pitch_embed(pitch)
|
||||
return pitch_embed
|
||||
|
||||
def run_decoder(self, decoder_inp, tgt_nonpadding, ret, infer, **kwargs):
|
||||
x = decoder_inp # [B, T, H]
|
||||
x = self.decoder(x)
|
||||
x = self.mel_out(x)
|
||||
return x * tgt_nonpadding
|
||||
|
||||
def cwt2f0_norm(self, cwt_spec, mean, std, mel2ph):
|
||||
f0 = cwt2f0(cwt_spec, mean, std, hparams['cwt_scales'])
|
||||
f0 = torch.cat(
|
||||
[f0] + [f0[:, -1:]] * (mel2ph.shape[1] - f0.shape[1]), 1)
|
||||
f0_norm = norm_f0(f0, None, hparams)
|
||||
return f0_norm
|
||||
|
||||
def out2mel(self, out):
|
||||
return out
|
||||
|
||||
@staticmethod
|
||||
def mel_norm(x):
|
||||
return (x + 5.5) / (6.3 / 2) - 1
|
||||
|
||||
@staticmethod
|
||||
def mel_denorm(x):
|
||||
return (x + 1) * (6.3 / 2) - 5.5
|
||||
@@ -0,0 +1,149 @@
|
||||
from modules.commons.common_layers import *
|
||||
from utils.hparams import hparams
|
||||
from modules.fastspeech.tts_modules import PitchPredictor
|
||||
from utils.pitch_utils import denorm_f0
|
||||
|
||||
|
||||
class Prenet(nn.Module):
|
||||
def __init__(self, in_dim=80, out_dim=256, kernel=5, n_layers=3, strides=None):
|
||||
super(Prenet, self).__init__()
|
||||
padding = kernel // 2
|
||||
self.layers = []
|
||||
self.strides = strides if strides is not None else [1] * n_layers
|
||||
for l in range(n_layers):
|
||||
self.layers.append(nn.Sequential(
|
||||
nn.Conv1d(in_dim, out_dim, kernel_size=kernel, padding=padding, stride=self.strides[l]),
|
||||
nn.ReLU(),
|
||||
nn.BatchNorm1d(out_dim)
|
||||
))
|
||||
in_dim = out_dim
|
||||
self.layers = nn.ModuleList(self.layers)
|
||||
self.out_proj = nn.Linear(out_dim, out_dim)
|
||||
|
||||
def forward(self, x):
|
||||
"""
|
||||
|
||||
:param x: [B, T, 80]
|
||||
:return: [L, B, T, H], [B, T, H]
|
||||
"""
|
||||
padding_mask = x.abs().sum(-1).eq(0).data # [B, T]
|
||||
nonpadding_mask_TB = 1 - padding_mask.float()[:, None, :] # [B, 1, T]
|
||||
x = x.transpose(1, 2)
|
||||
hiddens = []
|
||||
for i, l in enumerate(self.layers):
|
||||
nonpadding_mask_TB = nonpadding_mask_TB[:, :, ::self.strides[i]]
|
||||
x = l(x) * nonpadding_mask_TB
|
||||
hiddens.append(x)
|
||||
hiddens = torch.stack(hiddens, 0) # [L, B, H, T]
|
||||
hiddens = hiddens.transpose(2, 3) # [L, B, T, H]
|
||||
x = self.out_proj(x.transpose(1, 2)) # [B, T, H]
|
||||
x = x * nonpadding_mask_TB.transpose(1, 2)
|
||||
return hiddens, x
|
||||
|
||||
|
||||
class ConvBlock(nn.Module):
|
||||
def __init__(self, idim=80, n_chans=256, kernel_size=3, stride=1, norm='gn', dropout=0):
|
||||
super().__init__()
|
||||
self.conv = ConvNorm(idim, n_chans, kernel_size, stride=stride)
|
||||
self.norm = norm
|
||||
if self.norm == 'bn':
|
||||
self.norm = nn.BatchNorm1d(n_chans)
|
||||
elif self.norm == 'in':
|
||||
self.norm = nn.InstanceNorm1d(n_chans, affine=True)
|
||||
elif self.norm == 'gn':
|
||||
self.norm = nn.GroupNorm(n_chans // 16, n_chans)
|
||||
elif self.norm == 'ln':
|
||||
self.norm = LayerNorm(n_chans // 16, n_chans)
|
||||
elif self.norm == 'wn':
|
||||
self.conv = torch.nn.utils.weight_norm(self.conv.conv)
|
||||
self.dropout = nn.Dropout(dropout)
|
||||
self.relu = nn.ReLU()
|
||||
|
||||
def forward(self, x):
|
||||
"""
|
||||
|
||||
:param x: [B, C, T]
|
||||
:return: [B, C, T]
|
||||
"""
|
||||
x = self.conv(x)
|
||||
if not isinstance(self.norm, str):
|
||||
if self.norm == 'none':
|
||||
pass
|
||||
elif self.norm == 'ln':
|
||||
x = self.norm(x.transpose(1, 2)).transpose(1, 2)
|
||||
else:
|
||||
x = self.norm(x)
|
||||
x = self.relu(x)
|
||||
x = self.dropout(x)
|
||||
return x
|
||||
|
||||
|
||||
class ConvStacks(nn.Module):
|
||||
def __init__(self, idim=80, n_layers=5, n_chans=256, odim=32, kernel_size=5, norm='gn',
|
||||
dropout=0, strides=None, res=True):
|
||||
super().__init__()
|
||||
self.conv = torch.nn.ModuleList()
|
||||
self.kernel_size = kernel_size
|
||||
self.res = res
|
||||
self.in_proj = Linear(idim, n_chans)
|
||||
if strides is None:
|
||||
strides = [1] * n_layers
|
||||
else:
|
||||
assert len(strides) == n_layers
|
||||
for idx in range(n_layers):
|
||||
self.conv.append(ConvBlock(
|
||||
n_chans, n_chans, kernel_size, stride=strides[idx], norm=norm, dropout=dropout))
|
||||
self.out_proj = Linear(n_chans, odim)
|
||||
|
||||
def forward(self, x, return_hiddens=False):
|
||||
"""
|
||||
|
||||
:param x: [B, T, H]
|
||||
:return: [B, T, H]
|
||||
"""
|
||||
x = self.in_proj(x)
|
||||
x = x.transpose(1, -1) # (B, idim, Tmax)
|
||||
hiddens = []
|
||||
for f in self.conv:
|
||||
x_ = f(x)
|
||||
x = x + x_ if self.res else x_ # (B, C, Tmax)
|
||||
hiddens.append(x)
|
||||
x = x.transpose(1, -1)
|
||||
x = self.out_proj(x) # (B, Tmax, H)
|
||||
if return_hiddens:
|
||||
hiddens = torch.stack(hiddens, 1) # [B, L, C, T]
|
||||
return x, hiddens
|
||||
return x
|
||||
|
||||
|
||||
class PitchExtractor(nn.Module):
|
||||
def __init__(self, n_mel_bins=80, conv_layers=2):
|
||||
super().__init__()
|
||||
self.hidden_size = hparams['hidden_size']
|
||||
self.predictor_hidden = hparams['predictor_hidden'] if hparams['predictor_hidden'] > 0 else self.hidden_size
|
||||
self.conv_layers = conv_layers
|
||||
|
||||
self.mel_prenet = Prenet(n_mel_bins, self.hidden_size, strides=[1, 1, 1])
|
||||
if self.conv_layers > 0:
|
||||
self.mel_encoder = ConvStacks(
|
||||
idim=self.hidden_size, n_chans=self.hidden_size, odim=self.hidden_size, n_layers=self.conv_layers)
|
||||
self.pitch_predictor = PitchPredictor(
|
||||
self.hidden_size, n_chans=self.predictor_hidden,
|
||||
n_layers=5, dropout_rate=0.1, odim=2,
|
||||
padding=hparams['ffn_padding'], kernel_size=hparams['predictor_kernel'])
|
||||
|
||||
def forward(self, mel_input=None):
|
||||
ret = {}
|
||||
mel_hidden = self.mel_prenet(mel_input)[1]
|
||||
if self.conv_layers > 0:
|
||||
mel_hidden = self.mel_encoder(mel_hidden)
|
||||
|
||||
ret['pitch_pred'] = pitch_pred = self.pitch_predictor(mel_hidden)
|
||||
|
||||
pitch_padding = mel_input.abs().sum(-1) == 0
|
||||
use_uv = hparams['pitch_type'] == 'frame' and hparams['use_uv']
|
||||
|
||||
ret['f0_denorm_pred'] = denorm_f0(
|
||||
pitch_pred[:, :, 0], (pitch_pred[:, :, 1] > 0) if use_uv else None,
|
||||
hparams, pitch_padding=pitch_padding)
|
||||
return ret
|
||||
@@ -0,0 +1,357 @@
|
||||
import logging
|
||||
import math
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.nn import functional as F
|
||||
|
||||
from modules.commons.espnet_positional_embedding import RelPositionalEncoding
|
||||
from modules.commons.common_layers import SinusoidalPositionalEmbedding, Linear, EncSALayer, DecSALayer, BatchNorm1dTBC
|
||||
from utils.hparams import hparams
|
||||
|
||||
DEFAULT_MAX_SOURCE_POSITIONS = 2000
|
||||
DEFAULT_MAX_TARGET_POSITIONS = 2000
|
||||
|
||||
|
||||
class TransformerEncoderLayer(nn.Module):
|
||||
def __init__(self, hidden_size, dropout, kernel_size=None, num_heads=2, norm='ln'):
|
||||
super().__init__()
|
||||
self.hidden_size = hidden_size
|
||||
self.dropout = dropout
|
||||
self.num_heads = num_heads
|
||||
self.op = EncSALayer(
|
||||
hidden_size, num_heads, dropout=dropout,
|
||||
attention_dropout=0.0, relu_dropout=dropout,
|
||||
kernel_size=kernel_size
|
||||
if kernel_size is not None else hparams['enc_ffn_kernel_size'],
|
||||
padding=hparams['ffn_padding'],
|
||||
norm=norm, act=hparams['ffn_act'])
|
||||
|
||||
def forward(self, x, **kwargs):
|
||||
return self.op(x, **kwargs)
|
||||
|
||||
|
||||
######################
|
||||
# fastspeech modules
|
||||
######################
|
||||
class LayerNorm(torch.nn.LayerNorm):
|
||||
"""Layer normalization module.
|
||||
:param int nout: output dim size
|
||||
:param int dim: dimension to be normalized
|
||||
"""
|
||||
|
||||
def __init__(self, nout, dim=-1):
|
||||
"""Construct an LayerNorm object."""
|
||||
super(LayerNorm, self).__init__(nout, eps=1e-12)
|
||||
self.dim = dim
|
||||
|
||||
def forward(self, x):
|
||||
"""Apply layer normalization.
|
||||
:param torch.Tensor x: input tensor
|
||||
:return: layer normalized tensor
|
||||
:rtype torch.Tensor
|
||||
"""
|
||||
if self.dim == -1:
|
||||
return super(LayerNorm, self).forward(x)
|
||||
return super(LayerNorm, self).forward(x.transpose(1, -1)).transpose(1, -1)
|
||||
|
||||
|
||||
class DurationPredictor(torch.nn.Module):
|
||||
"""Duration predictor module.
|
||||
This is a module of duration predictor described in `FastSpeech: Fast, Robust and Controllable Text to Speech`_.
|
||||
The duration predictor predicts a duration of each frame in log domain from the hidden embeddings of encoder.
|
||||
.. _`FastSpeech: Fast, Robust and Controllable Text to Speech`:
|
||||
https://arxiv.org/pdf/1905.09263.pdf
|
||||
Note:
|
||||
The calculation domain of outputs is different between in `forward` and in `inference`. In `forward`,
|
||||
the outputs are calculated in log domain but in `inference`, those are calculated in linear domain.
|
||||
"""
|
||||
|
||||
def __init__(self, idim, n_layers=2, n_chans=384, kernel_size=3, dropout_rate=0.1, offset=1.0, padding='SAME'):
|
||||
"""Initilize duration predictor module.
|
||||
Args:
|
||||
idim (int): Input dimension.
|
||||
n_layers (int, optional): Number of convolutional layers.
|
||||
n_chans (int, optional): Number of channels of convolutional layers.
|
||||
kernel_size (int, optional): Kernel size of convolutional layers.
|
||||
dropout_rate (float, optional): Dropout rate.
|
||||
offset (float, optional): Offset value to avoid nan in log domain.
|
||||
"""
|
||||
super(DurationPredictor, self).__init__()
|
||||
self.offset = offset
|
||||
self.conv = torch.nn.ModuleList()
|
||||
self.kernel_size = kernel_size
|
||||
self.padding = padding
|
||||
for idx in range(n_layers):
|
||||
in_chans = idim if idx == 0 else n_chans
|
||||
self.conv += [torch.nn.Sequential(
|
||||
torch.nn.ConstantPad1d(((kernel_size - 1) // 2, (kernel_size - 1) // 2)
|
||||
if padding == 'SAME'
|
||||
else (kernel_size - 1, 0), 0),
|
||||
torch.nn.Conv1d(in_chans, n_chans, kernel_size, stride=1, padding=0),
|
||||
torch.nn.ReLU(),
|
||||
LayerNorm(n_chans, dim=1),
|
||||
torch.nn.Dropout(dropout_rate)
|
||||
)]
|
||||
if hparams['dur_loss'] in ['mse', 'huber']:
|
||||
odims = 1
|
||||
elif hparams['dur_loss'] == 'mog':
|
||||
odims = 15
|
||||
elif hparams['dur_loss'] == 'crf':
|
||||
odims = 32
|
||||
from torchcrf import CRF
|
||||
self.crf = CRF(odims, batch_first=True)
|
||||
self.linear = torch.nn.Linear(n_chans, odims)
|
||||
|
||||
def _forward(self, xs, x_masks=None, is_inference=False):
|
||||
xs = xs.transpose(1, -1) # (B, idim, Tmax)
|
||||
for f in self.conv:
|
||||
xs = f(xs) # (B, C, Tmax)
|
||||
if x_masks is not None:
|
||||
xs = xs * (1 - x_masks.float())[:, None, :]
|
||||
|
||||
xs = self.linear(xs.transpose(1, -1)) # [B, T, C]
|
||||
xs = xs * (1 - x_masks.float())[:, :, None] # (B, T, C)
|
||||
if is_inference:
|
||||
return self.out2dur(xs), xs
|
||||
else:
|
||||
if hparams['dur_loss'] in ['mse']:
|
||||
xs = xs.squeeze(-1) # (B, Tmax)
|
||||
return xs
|
||||
|
||||
def out2dur(self, xs):
|
||||
if hparams['dur_loss'] in ['mse']:
|
||||
# NOTE: calculate in log domain
|
||||
xs = xs.squeeze(-1) # (B, Tmax)
|
||||
dur = torch.clamp(torch.round(xs.exp() - self.offset), min=0).long() # avoid negative value
|
||||
elif hparams['dur_loss'] == 'mog':
|
||||
return NotImplementedError
|
||||
elif hparams['dur_loss'] == 'crf':
|
||||
dur = torch.LongTensor(self.crf.decode(xs)).cuda()
|
||||
return dur
|
||||
|
||||
def forward(self, xs, x_masks=None):
|
||||
"""Calculate forward propagation.
|
||||
Args:
|
||||
xs (Tensor): Batch of input sequences (B, Tmax, idim).
|
||||
x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
|
||||
Returns:
|
||||
Tensor: Batch of predicted durations in log domain (B, Tmax).
|
||||
"""
|
||||
return self._forward(xs, x_masks, False)
|
||||
|
||||
def inference(self, xs, x_masks=None):
|
||||
"""Inference duration.
|
||||
Args:
|
||||
xs (Tensor): Batch of input sequences (B, Tmax, idim).
|
||||
x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
|
||||
Returns:
|
||||
LongTensor: Batch of predicted durations in linear domain (B, Tmax).
|
||||
"""
|
||||
return self._forward(xs, x_masks, True)
|
||||
|
||||
|
||||
class LengthRegulator(torch.nn.Module):
|
||||
def __init__(self, pad_value=0.0):
|
||||
super(LengthRegulator, self).__init__()
|
||||
self.pad_value = pad_value
|
||||
|
||||
def forward(self, dur, dur_padding=None, alpha=1.0):
|
||||
"""
|
||||
Example (no batch dim version):
|
||||
1. dur = [2,2,3]
|
||||
2. token_idx = [[1],[2],[3]], dur_cumsum = [2,4,7], dur_cumsum_prev = [0,2,4]
|
||||
3. token_mask = [[1,1,0,0,0,0,0],
|
||||
[0,0,1,1,0,0,0],
|
||||
[0,0,0,0,1,1,1]]
|
||||
4. token_idx * token_mask = [[1,1,0,0,0,0,0],
|
||||
[0,0,2,2,0,0,0],
|
||||
[0,0,0,0,3,3,3]]
|
||||
5. (token_idx * token_mask).sum(0) = [1,1,2,2,3,3,3]
|
||||
|
||||
:param dur: Batch of durations of each frame (B, T_txt)
|
||||
:param dur_padding: Batch of padding of each frame (B, T_txt)
|
||||
:param alpha: duration rescale coefficient
|
||||
:return:
|
||||
mel2ph (B, T_speech)
|
||||
"""
|
||||
assert alpha > 0
|
||||
dur = torch.round(dur.float() * alpha).long()
|
||||
if dur_padding is not None:
|
||||
dur = dur * (1 - dur_padding.long())
|
||||
token_idx = torch.arange(1, dur.shape[1] + 1)[None, :, None].to(dur.device)
|
||||
dur_cumsum = torch.cumsum(dur, 1)
|
||||
dur_cumsum_prev = F.pad(dur_cumsum, [1, -1], mode='constant', value=0)
|
||||
|
||||
pos_idx = torch.arange(dur.sum(-1).max())[None, None].to(dur.device)
|
||||
token_mask = (pos_idx >= dur_cumsum_prev[:, :, None]) & (pos_idx < dur_cumsum[:, :, None])
|
||||
mel2ph = (token_idx * token_mask.long()).sum(1)
|
||||
return mel2ph
|
||||
|
||||
|
||||
class PitchPredictor(torch.nn.Module):
|
||||
def __init__(self, idim, n_layers=5, n_chans=384, odim=2, kernel_size=5,
|
||||
dropout_rate=0.1, padding='SAME'):
|
||||
"""Initilize pitch predictor module.
|
||||
Args:
|
||||
idim (int): Input dimension.
|
||||
n_layers (int, optional): Number of convolutional layers.
|
||||
n_chans (int, optional): Number of channels of convolutional layers.
|
||||
kernel_size (int, optional): Kernel size of convolutional layers.
|
||||
dropout_rate (float, optional): Dropout rate.
|
||||
"""
|
||||
super(PitchPredictor, self).__init__()
|
||||
self.conv = torch.nn.ModuleList()
|
||||
self.kernel_size = kernel_size
|
||||
self.padding = padding
|
||||
for idx in range(n_layers):
|
||||
in_chans = idim if idx == 0 else n_chans
|
||||
self.conv += [torch.nn.Sequential(
|
||||
torch.nn.ConstantPad1d(((kernel_size - 1) // 2, (kernel_size - 1) // 2)
|
||||
if padding == 'SAME'
|
||||
else (kernel_size - 1, 0), 0),
|
||||
torch.nn.Conv1d(in_chans, n_chans, kernel_size, stride=1, padding=0),
|
||||
torch.nn.ReLU(),
|
||||
LayerNorm(n_chans, dim=1),
|
||||
torch.nn.Dropout(dropout_rate)
|
||||
)]
|
||||
self.linear = torch.nn.Linear(n_chans, odim)
|
||||
self.embed_positions = SinusoidalPositionalEmbedding(idim, 0, init_size=4096)
|
||||
self.pos_embed_alpha = nn.Parameter(torch.Tensor([1]))
|
||||
|
||||
def forward(self, xs):
|
||||
"""
|
||||
|
||||
:param xs: [B, T, H]
|
||||
:return: [B, T, H]
|
||||
"""
|
||||
positions = self.pos_embed_alpha * self.embed_positions(xs[..., 0])
|
||||
xs = xs + positions
|
||||
xs = xs.transpose(1, -1) # (B, idim, Tmax)
|
||||
for f in self.conv:
|
||||
xs = f(xs) # (B, C, Tmax)
|
||||
# NOTE: calculate in log domain
|
||||
xs = self.linear(xs.transpose(1, -1)) # (B, Tmax, H)
|
||||
return xs
|
||||
|
||||
|
||||
class EnergyPredictor(PitchPredictor):
|
||||
pass
|
||||
|
||||
|
||||
def mel2ph_to_dur(mel2ph, T_txt, max_dur=None):
|
||||
B, _ = mel2ph.shape
|
||||
dur = mel2ph.new_zeros(B, T_txt + 1).scatter_add(1, mel2ph, torch.ones_like(mel2ph))
|
||||
dur = dur[:, 1:]
|
||||
if max_dur is not None:
|
||||
dur = dur.clamp(max=max_dur)
|
||||
return dur
|
||||
|
||||
|
||||
class FFTBlocks(nn.Module):
|
||||
def __init__(self, hidden_size, num_layers, ffn_kernel_size=9, dropout=None, num_heads=2,
|
||||
use_pos_embed=True, use_last_norm=True, norm='ln', use_pos_embed_alpha=True):
|
||||
super().__init__()
|
||||
self.num_layers = num_layers
|
||||
embed_dim = self.hidden_size = hidden_size
|
||||
self.dropout = dropout if dropout is not None else hparams['dropout']
|
||||
self.use_pos_embed = use_pos_embed
|
||||
self.use_last_norm = use_last_norm
|
||||
if use_pos_embed:
|
||||
self.max_source_positions = DEFAULT_MAX_TARGET_POSITIONS
|
||||
self.padding_idx = 0
|
||||
self.pos_embed_alpha = nn.Parameter(torch.Tensor([1])) if use_pos_embed_alpha else 1
|
||||
self.embed_positions = SinusoidalPositionalEmbedding(
|
||||
embed_dim, self.padding_idx, init_size=DEFAULT_MAX_TARGET_POSITIONS,
|
||||
)
|
||||
|
||||
self.layers = nn.ModuleList([])
|
||||
self.layers.extend([
|
||||
TransformerEncoderLayer(self.hidden_size, self.dropout,
|
||||
kernel_size=ffn_kernel_size, num_heads=num_heads)
|
||||
for _ in range(self.num_layers)
|
||||
])
|
||||
if self.use_last_norm:
|
||||
if norm == 'ln':
|
||||
self.layer_norm = nn.LayerNorm(embed_dim)
|
||||
elif norm == 'bn':
|
||||
self.layer_norm = BatchNorm1dTBC(embed_dim)
|
||||
else:
|
||||
self.layer_norm = None
|
||||
|
||||
def forward(self, x, padding_mask=None, attn_mask=None, return_hiddens=False):
|
||||
"""
|
||||
:param x: [B, T, C]
|
||||
:param padding_mask: [B, T]
|
||||
:return: [B, T, C] or [L, B, T, C]
|
||||
"""
|
||||
padding_mask = x.abs().sum(-1).eq(0).data if padding_mask is None else padding_mask
|
||||
nonpadding_mask_TB = 1 - padding_mask.transpose(0, 1).float()[:, :, None] # [T, B, 1]
|
||||
if self.use_pos_embed:
|
||||
positions = self.pos_embed_alpha * self.embed_positions(x[..., 0])
|
||||
x = x + positions
|
||||
x = F.dropout(x, p=self.dropout, training=self.training)
|
||||
# B x T x C -> T x B x C
|
||||
x = x.transpose(0, 1) * nonpadding_mask_TB
|
||||
hiddens = []
|
||||
for layer in self.layers:
|
||||
x = layer(x, encoder_padding_mask=padding_mask, attn_mask=attn_mask) * nonpadding_mask_TB
|
||||
hiddens.append(x)
|
||||
if self.use_last_norm:
|
||||
x = self.layer_norm(x) * nonpadding_mask_TB
|
||||
if return_hiddens:
|
||||
x = torch.stack(hiddens, 0) # [L, T, B, C]
|
||||
x = x.transpose(1, 2) # [L, B, T, C]
|
||||
else:
|
||||
x = x.transpose(0, 1) # [B, T, C]
|
||||
return x
|
||||
|
||||
|
||||
class FastspeechEncoder(FFTBlocks):
|
||||
def __init__(self, embed_tokens, hidden_size=None, num_layers=None, kernel_size=None, num_heads=2):
|
||||
hidden_size = hparams['hidden_size'] if hidden_size is None else hidden_size
|
||||
kernel_size = hparams['enc_ffn_kernel_size'] if kernel_size is None else kernel_size
|
||||
num_layers = hparams['dec_layers'] if num_layers is None else num_layers
|
||||
super().__init__(hidden_size, num_layers, kernel_size, num_heads=num_heads,
|
||||
use_pos_embed=False) # use_pos_embed_alpha for compatibility
|
||||
self.embed_tokens = embed_tokens
|
||||
self.embed_scale = math.sqrt(hidden_size)
|
||||
self.padding_idx = 0
|
||||
if hparams.get('rel_pos') is not None and hparams['rel_pos']:
|
||||
self.embed_positions = RelPositionalEncoding(hidden_size, dropout_rate=0.0)
|
||||
else:
|
||||
self.embed_positions = SinusoidalPositionalEmbedding(
|
||||
hidden_size, self.padding_idx, init_size=DEFAULT_MAX_TARGET_POSITIONS,
|
||||
)
|
||||
|
||||
def forward(self, txt_tokens):
|
||||
"""
|
||||
|
||||
:param txt_tokens: [B, T]
|
||||
:return: {
|
||||
'encoder_out': [T x B x C]
|
||||
}
|
||||
"""
|
||||
encoder_padding_mask = txt_tokens.eq(self.padding_idx).data
|
||||
x = self.forward_embedding(txt_tokens) # [B, T, H]
|
||||
x = super(FastspeechEncoder, self).forward(x, encoder_padding_mask)
|
||||
return x
|
||||
|
||||
def forward_embedding(self, txt_tokens):
|
||||
# embed tokens and positions
|
||||
x = self.embed_scale * self.embed_tokens(txt_tokens)
|
||||
if hparams['use_pos_embed']:
|
||||
positions = self.embed_positions(txt_tokens)
|
||||
x = x + positions
|
||||
x = F.dropout(x, p=self.dropout, training=self.training)
|
||||
return x
|
||||
|
||||
|
||||
class FastspeechDecoder(FFTBlocks):
|
||||
def __init__(self, hidden_size=None, num_layers=None, kernel_size=None, num_heads=None):
|
||||
num_heads = hparams['num_heads'] if num_heads is None else num_heads
|
||||
hidden_size = hparams['hidden_size'] if hidden_size is None else hidden_size
|
||||
kernel_size = hparams['dec_ffn_kernel_size'] if kernel_size is None else kernel_size
|
||||
num_layers = hparams['dec_layers'] if num_layers is None else num_layers
|
||||
super().__init__(hidden_size, num_layers, kernel_size, num_heads=num_heads)
|
||||
|
||||
@@ -0,0 +1,365 @@
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
import torch.nn as nn
|
||||
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
||||
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
||||
|
||||
from modules.parallel_wavegan.layers import UpsampleNetwork, ConvInUpsampleNetwork
|
||||
from modules.parallel_wavegan.models.source import SourceModuleHnNSF
|
||||
import numpy as np
|
||||
|
||||
LRELU_SLOPE = 0.1
|
||||
|
||||
|
||||
def init_weights(m, mean=0.0, std=0.01):
|
||||
classname = m.__class__.__name__
|
||||
if classname.find("Conv") != -1:
|
||||
m.weight.data.normal_(mean, std)
|
||||
|
||||
|
||||
def apply_weight_norm(m):
|
||||
classname = m.__class__.__name__
|
||||
if classname.find("Conv") != -1:
|
||||
weight_norm(m)
|
||||
|
||||
|
||||
def get_padding(kernel_size, dilation=1):
|
||||
return int((kernel_size * dilation - dilation) / 2)
|
||||
|
||||
|
||||
class ResBlock1(torch.nn.Module):
|
||||
def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5)):
|
||||
super(ResBlock1, self).__init__()
|
||||
self.h = h
|
||||
self.convs1 = nn.ModuleList([
|
||||
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
|
||||
padding=get_padding(kernel_size, dilation[0]))),
|
||||
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
|
||||
padding=get_padding(kernel_size, dilation[1]))),
|
||||
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
|
||||
padding=get_padding(kernel_size, dilation[2])))
|
||||
])
|
||||
self.convs1.apply(init_weights)
|
||||
|
||||
self.convs2 = nn.ModuleList([
|
||||
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
||||
padding=get_padding(kernel_size, 1))),
|
||||
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
||||
padding=get_padding(kernel_size, 1))),
|
||||
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
||||
padding=get_padding(kernel_size, 1)))
|
||||
])
|
||||
self.convs2.apply(init_weights)
|
||||
|
||||
def forward(self, x):
|
||||
for c1, c2 in zip(self.convs1, self.convs2):
|
||||
xt = F.leaky_relu(x, LRELU_SLOPE)
|
||||
xt = c1(xt)
|
||||
xt = F.leaky_relu(xt, LRELU_SLOPE)
|
||||
xt = c2(xt)
|
||||
x = xt + x
|
||||
return x
|
||||
|
||||
def remove_weight_norm(self):
|
||||
for l in self.convs1:
|
||||
remove_weight_norm(l)
|
||||
for l in self.convs2:
|
||||
remove_weight_norm(l)
|
||||
|
||||
|
||||
class ResBlock2(torch.nn.Module):
|
||||
def __init__(self, h, channels, kernel_size=3, dilation=(1, 3)):
|
||||
super(ResBlock2, self).__init__()
|
||||
self.h = h
|
||||
self.convs = nn.ModuleList([
|
||||
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
|
||||
padding=get_padding(kernel_size, dilation[0]))),
|
||||
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
|
||||
padding=get_padding(kernel_size, dilation[1])))
|
||||
])
|
||||
self.convs.apply(init_weights)
|
||||
|
||||
def forward(self, x):
|
||||
for c in self.convs:
|
||||
xt = F.leaky_relu(x, LRELU_SLOPE)
|
||||
xt = c(xt)
|
||||
x = xt + x
|
||||
return x
|
||||
|
||||
def remove_weight_norm(self):
|
||||
for l in self.convs:
|
||||
remove_weight_norm(l)
|
||||
|
||||
|
||||
class Conv1d1x1(Conv1d):
|
||||
"""1x1 Conv1d with customized initialization."""
|
||||
|
||||
def __init__(self, in_channels, out_channels, bias):
|
||||
"""Initialize 1x1 Conv1d module."""
|
||||
super(Conv1d1x1, self).__init__(in_channels, out_channels,
|
||||
kernel_size=1, padding=0,
|
||||
dilation=1, bias=bias)
|
||||
|
||||
|
||||
class HifiGanGenerator(torch.nn.Module):
|
||||
def __init__(self, h, c_out=1):
|
||||
super(HifiGanGenerator, self).__init__()
|
||||
self.h = h
|
||||
self.num_kernels = len(h['resblock_kernel_sizes'])
|
||||
self.num_upsamples = len(h['upsample_rates'])
|
||||
|
||||
if h['use_pitch_embed']:
|
||||
self.harmonic_num = 8
|
||||
self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(h['upsample_rates']))
|
||||
self.m_source = SourceModuleHnNSF(
|
||||
sampling_rate=h['audio_sample_rate'],
|
||||
harmonic_num=self.harmonic_num)
|
||||
self.noise_convs = nn.ModuleList()
|
||||
self.conv_pre = weight_norm(Conv1d(80, h['upsample_initial_channel'], 7, 1, padding=3))
|
||||
resblock = ResBlock1 if h['resblock'] == '1' else ResBlock2
|
||||
|
||||
self.ups = nn.ModuleList()
|
||||
for i, (u, k) in enumerate(zip(h['upsample_rates'], h['upsample_kernel_sizes'])):
|
||||
c_cur = h['upsample_initial_channel'] // (2 ** (i + 1))
|
||||
self.ups.append(weight_norm(
|
||||
ConvTranspose1d(c_cur * 2, c_cur, k, u, padding=(k - u) // 2)))
|
||||
if h['use_pitch_embed']:
|
||||
if i + 1 < len(h['upsample_rates']):
|
||||
stride_f0 = np.prod(h['upsample_rates'][i + 1:])
|
||||
self.noise_convs.append(Conv1d(
|
||||
1, c_cur, kernel_size=stride_f0 * 2, stride=stride_f0, padding=stride_f0 // 2))
|
||||
else:
|
||||
self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
|
||||
|
||||
self.resblocks = nn.ModuleList()
|
||||
for i in range(len(self.ups)):
|
||||
ch = h['upsample_initial_channel'] // (2 ** (i + 1))
|
||||
for j, (k, d) in enumerate(zip(h['resblock_kernel_sizes'], h['resblock_dilation_sizes'])):
|
||||
self.resblocks.append(resblock(h, ch, k, d))
|
||||
|
||||
self.conv_post = weight_norm(Conv1d(ch, c_out, 7, 1, padding=3))
|
||||
self.ups.apply(init_weights)
|
||||
self.conv_post.apply(init_weights)
|
||||
|
||||
def forward(self, x, f0=None):
|
||||
if f0 is not None:
|
||||
# harmonic-source signal, noise-source signal, uv flag
|
||||
f0 = self.f0_upsamp(f0[:, None]).transpose(1, 2)
|
||||
har_source, noi_source, uv = self.m_source(f0)
|
||||
har_source = har_source.transpose(1, 2)
|
||||
|
||||
x = self.conv_pre(x)
|
||||
for i in range(self.num_upsamples):
|
||||
x = F.leaky_relu(x, LRELU_SLOPE)
|
||||
x = self.ups[i](x)
|
||||
if f0 is not None:
|
||||
x_source = self.noise_convs[i](har_source)
|
||||
x = x + x_source
|
||||
xs = None
|
||||
for j in range(self.num_kernels):
|
||||
if xs is None:
|
||||
xs = self.resblocks[i * self.num_kernels + j](x)
|
||||
else:
|
||||
xs += self.resblocks[i * self.num_kernels + j](x)
|
||||
x = xs / self.num_kernels
|
||||
x = F.leaky_relu(x)
|
||||
x = self.conv_post(x)
|
||||
x = torch.tanh(x)
|
||||
|
||||
return x
|
||||
|
||||
def remove_weight_norm(self):
|
||||
print('Removing weight norm...')
|
||||
for l in self.ups:
|
||||
remove_weight_norm(l)
|
||||
for l in self.resblocks:
|
||||
l.remove_weight_norm()
|
||||
remove_weight_norm(self.conv_pre)
|
||||
remove_weight_norm(self.conv_post)
|
||||
|
||||
|
||||
class DiscriminatorP(torch.nn.Module):
|
||||
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False, use_cond=False, c_in=1):
|
||||
super(DiscriminatorP, self).__init__()
|
||||
self.use_cond = use_cond
|
||||
if use_cond:
|
||||
from utils.hparams import hparams
|
||||
t = hparams['hop_size']
|
||||
self.cond_net = torch.nn.ConvTranspose1d(80, 1, t * 2, stride=t, padding=t // 2)
|
||||
c_in = 2
|
||||
|
||||
self.period = period
|
||||
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
||||
self.convs = nn.ModuleList([
|
||||
norm_f(Conv2d(c_in, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
|
||||
norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
|
||||
norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
|
||||
norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
|
||||
norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(2, 0))),
|
||||
])
|
||||
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
||||
|
||||
def forward(self, x, mel):
|
||||
fmap = []
|
||||
if self.use_cond:
|
||||
x_mel = self.cond_net(mel)
|
||||
x = torch.cat([x_mel, x], 1)
|
||||
# 1d to 2d
|
||||
b, c, t = x.shape
|
||||
if t % self.period != 0: # pad first
|
||||
n_pad = self.period - (t % self.period)
|
||||
x = F.pad(x, (0, n_pad), "reflect")
|
||||
t = t + n_pad
|
||||
x = x.view(b, c, t // self.period, self.period)
|
||||
|
||||
for l in self.convs:
|
||||
x = l(x)
|
||||
x = F.leaky_relu(x, LRELU_SLOPE)
|
||||
fmap.append(x)
|
||||
x = self.conv_post(x)
|
||||
fmap.append(x)
|
||||
x = torch.flatten(x, 1, -1)
|
||||
|
||||
return x, fmap
|
||||
|
||||
|
||||
class MultiPeriodDiscriminator(torch.nn.Module):
|
||||
def __init__(self, use_cond=False, c_in=1):
|
||||
super(MultiPeriodDiscriminator, self).__init__()
|
||||
self.discriminators = nn.ModuleList([
|
||||
DiscriminatorP(2, use_cond=use_cond, c_in=c_in),
|
||||
DiscriminatorP(3, use_cond=use_cond, c_in=c_in),
|
||||
DiscriminatorP(5, use_cond=use_cond, c_in=c_in),
|
||||
DiscriminatorP(7, use_cond=use_cond, c_in=c_in),
|
||||
DiscriminatorP(11, use_cond=use_cond, c_in=c_in),
|
||||
])
|
||||
|
||||
def forward(self, y, y_hat, mel=None):
|
||||
y_d_rs = []
|
||||
y_d_gs = []
|
||||
fmap_rs = []
|
||||
fmap_gs = []
|
||||
for i, d in enumerate(self.discriminators):
|
||||
y_d_r, fmap_r = d(y, mel)
|
||||
y_d_g, fmap_g = d(y_hat, mel)
|
||||
y_d_rs.append(y_d_r)
|
||||
fmap_rs.append(fmap_r)
|
||||
y_d_gs.append(y_d_g)
|
||||
fmap_gs.append(fmap_g)
|
||||
|
||||
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
||||
|
||||
|
||||
class DiscriminatorS(torch.nn.Module):
|
||||
def __init__(self, use_spectral_norm=False, use_cond=False, upsample_rates=None, c_in=1):
|
||||
super(DiscriminatorS, self).__init__()
|
||||
self.use_cond = use_cond
|
||||
if use_cond:
|
||||
t = np.prod(upsample_rates)
|
||||
self.cond_net = torch.nn.ConvTranspose1d(80, 1, t * 2, stride=t, padding=t // 2)
|
||||
c_in = 2
|
||||
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
||||
self.convs = nn.ModuleList([
|
||||
norm_f(Conv1d(c_in, 128, 15, 1, padding=7)),
|
||||
norm_f(Conv1d(128, 128, 41, 2, groups=4, padding=20)),
|
||||
norm_f(Conv1d(128, 256, 41, 2, groups=16, padding=20)),
|
||||
norm_f(Conv1d(256, 512, 41, 4, groups=16, padding=20)),
|
||||
norm_f(Conv1d(512, 1024, 41, 4, groups=16, padding=20)),
|
||||
norm_f(Conv1d(1024, 1024, 41, 1, groups=16, padding=20)),
|
||||
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
||||
])
|
||||
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
||||
|
||||
def forward(self, x, mel):
|
||||
if self.use_cond:
|
||||
x_mel = self.cond_net(mel)
|
||||
x = torch.cat([x_mel, x], 1)
|
||||
fmap = []
|
||||
for l in self.convs:
|
||||
x = l(x)
|
||||
x = F.leaky_relu(x, LRELU_SLOPE)
|
||||
fmap.append(x)
|
||||
x = self.conv_post(x)
|
||||
fmap.append(x)
|
||||
x = torch.flatten(x, 1, -1)
|
||||
|
||||
return x, fmap
|
||||
|
||||
|
||||
class MultiScaleDiscriminator(torch.nn.Module):
|
||||
def __init__(self, use_cond=False, c_in=1):
|
||||
super(MultiScaleDiscriminator, self).__init__()
|
||||
from utils.hparams import hparams
|
||||
self.discriminators = nn.ModuleList([
|
||||
DiscriminatorS(use_spectral_norm=True, use_cond=use_cond,
|
||||
upsample_rates=[4, 4, hparams['hop_size'] // 16],
|
||||
c_in=c_in),
|
||||
DiscriminatorS(use_cond=use_cond,
|
||||
upsample_rates=[4, 4, hparams['hop_size'] // 32],
|
||||
c_in=c_in),
|
||||
DiscriminatorS(use_cond=use_cond,
|
||||
upsample_rates=[4, 4, hparams['hop_size'] // 64],
|
||||
c_in=c_in),
|
||||
])
|
||||
self.meanpools = nn.ModuleList([
|
||||
AvgPool1d(4, 2, padding=1),
|
||||
AvgPool1d(4, 2, padding=1)
|
||||
])
|
||||
|
||||
def forward(self, y, y_hat, mel=None):
|
||||
y_d_rs = []
|
||||
y_d_gs = []
|
||||
fmap_rs = []
|
||||
fmap_gs = []
|
||||
for i, d in enumerate(self.discriminators):
|
||||
if i != 0:
|
||||
y = self.meanpools[i - 1](y)
|
||||
y_hat = self.meanpools[i - 1](y_hat)
|
||||
y_d_r, fmap_r = d(y, mel)
|
||||
y_d_g, fmap_g = d(y_hat, mel)
|
||||
y_d_rs.append(y_d_r)
|
||||
fmap_rs.append(fmap_r)
|
||||
y_d_gs.append(y_d_g)
|
||||
fmap_gs.append(fmap_g)
|
||||
|
||||
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
||||
|
||||
|
||||
def feature_loss(fmap_r, fmap_g):
|
||||
loss = 0
|
||||
for dr, dg in zip(fmap_r, fmap_g):
|
||||
for rl, gl in zip(dr, dg):
|
||||
loss += torch.mean(torch.abs(rl - gl))
|
||||
|
||||
return loss * 2
|
||||
|
||||
|
||||
def discriminator_loss(disc_real_outputs, disc_generated_outputs):
|
||||
r_losses = 0
|
||||
g_losses = 0
|
||||
for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
|
||||
r_loss = torch.mean((1 - dr) ** 2)
|
||||
g_loss = torch.mean(dg ** 2)
|
||||
r_losses += r_loss
|
||||
g_losses += g_loss
|
||||
r_losses = r_losses / len(disc_real_outputs)
|
||||
g_losses = g_losses / len(disc_real_outputs)
|
||||
return r_losses, g_losses
|
||||
|
||||
|
||||
def cond_discriminator_loss(outputs):
|
||||
loss = 0
|
||||
for dg in outputs:
|
||||
g_loss = torch.mean(dg ** 2)
|
||||
loss += g_loss
|
||||
loss = loss / len(outputs)
|
||||
return loss
|
||||
|
||||
|
||||
def generator_loss(disc_outputs):
|
||||
loss = 0
|
||||
for dg in disc_outputs:
|
||||
l = torch.mean((1 - dg) ** 2)
|
||||
loss += l
|
||||
loss = loss / len(disc_outputs)
|
||||
return loss
|
||||
@@ -0,0 +1,80 @@
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.utils.data
|
||||
from librosa.filters import mel as librosa_mel_fn
|
||||
from scipy.io.wavfile import read
|
||||
|
||||
MAX_WAV_VALUE = 32768.0
|
||||
|
||||
|
||||
def load_wav(full_path):
|
||||
sampling_rate, data = read(full_path)
|
||||
return data, sampling_rate
|
||||
|
||||
|
||||
def dynamic_range_compression(x, C=1, clip_val=1e-5):
|
||||
return np.log(np.clip(x, a_min=clip_val, a_max=None) * C)
|
||||
|
||||
|
||||
def dynamic_range_decompression(x, C=1):
|
||||
return np.exp(x) / C
|
||||
|
||||
|
||||
def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
|
||||
return torch.log(torch.clamp(x, min=clip_val) * C)
|
||||
|
||||
|
||||
def dynamic_range_decompression_torch(x, C=1):
|
||||
return torch.exp(x) / C
|
||||
|
||||
|
||||
def spectral_normalize_torch(magnitudes):
|
||||
output = dynamic_range_compression_torch(magnitudes)
|
||||
return output
|
||||
|
||||
|
||||
def spectral_de_normalize_torch(magnitudes):
|
||||
output = dynamic_range_decompression_torch(magnitudes)
|
||||
return output
|
||||
|
||||
|
||||
mel_basis = {}
|
||||
hann_window = {}
|
||||
|
||||
|
||||
def mel_spectrogram(y, hparams, center=False, complex=False):
|
||||
# hop_size: 512 # For 22050Hz, 275 ~= 12.5 ms (0.0125 * sample_rate)
|
||||
# win_size: 2048 # For 22050Hz, 1100 ~= 50 ms (If None, win_size: fft_size) (0.05 * sample_rate)
|
||||
# fmin: 55 # Set this to 55 if your speaker is male! if female, 95 should help taking off noise. (To test depending on dataset. Pitch info: male~[65, 260], female~[100, 525])
|
||||
# fmax: 10000 # To be increased/reduced depending on data.
|
||||
# fft_size: 2048 # Extra window size is filled with 0 paddings to match this parameter
|
||||
# n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax,
|
||||
n_fft = hparams['fft_size']
|
||||
num_mels = hparams['audio_num_mel_bins']
|
||||
sampling_rate = hparams['audio_sample_rate']
|
||||
hop_size = hparams['hop_size']
|
||||
win_size = hparams['win_size']
|
||||
fmin = hparams['fmin']
|
||||
fmax = hparams['fmax']
|
||||
y = y.clamp(min=-1., max=1.)
|
||||
global mel_basis, hann_window
|
||||
if fmax not in mel_basis:
|
||||
mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)
|
||||
mel_basis[str(fmax) + '_' + str(y.device)] = torch.from_numpy(mel).float().to(y.device)
|
||||
hann_window[str(y.device)] = torch.hann_window(win_size).to(y.device)
|
||||
|
||||
y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
|
||||
mode='reflect')
|
||||
y = y.squeeze(1)
|
||||
|
||||
spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[str(y.device)],
|
||||
center=center, pad_mode='reflect', normalized=False, onesided=True)
|
||||
|
||||
if not complex:
|
||||
spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
|
||||
spec = torch.matmul(mel_basis[str(fmax) + '_' + str(y.device)], spec)
|
||||
spec = spectral_normalize_torch(spec)
|
||||
else:
|
||||
B, C, T, _ = spec.shape
|
||||
spec = spec.transpose(1, 2) # [B, T, n_fft, 2]
|
||||
return spec
|
||||
@@ -0,0 +1,5 @@
|
||||
from .causal_conv import * # NOQA
|
||||
from .pqmf import * # NOQA
|
||||
from .residual_block import * # NOQA
|
||||
from modules.parallel_wavegan.layers.residual_stack import * # NOQA
|
||||
from .upsample import * # NOQA
|
||||
@@ -0,0 +1,56 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2020 Tomoki Hayashi
|
||||
# MIT License (https://opensource.org/licenses/MIT)
|
||||
|
||||
"""Causal convolusion layer modules."""
|
||||
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
class CausalConv1d(torch.nn.Module):
|
||||
"""CausalConv1d module with customized initialization."""
|
||||
|
||||
def __init__(self, in_channels, out_channels, kernel_size,
|
||||
dilation=1, bias=True, pad="ConstantPad1d", pad_params={"value": 0.0}):
|
||||
"""Initialize CausalConv1d module."""
|
||||
super(CausalConv1d, self).__init__()
|
||||
self.pad = getattr(torch.nn, pad)((kernel_size - 1) * dilation, **pad_params)
|
||||
self.conv = torch.nn.Conv1d(in_channels, out_channels, kernel_size,
|
||||
dilation=dilation, bias=bias)
|
||||
|
||||
def forward(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor (B, in_channels, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, out_channels, T).
|
||||
|
||||
"""
|
||||
return self.conv(self.pad(x))[:, :, :x.size(2)]
|
||||
|
||||
|
||||
class CausalConvTranspose1d(torch.nn.Module):
|
||||
"""CausalConvTranspose1d module with customized initialization."""
|
||||
|
||||
def __init__(self, in_channels, out_channels, kernel_size, stride, bias=True):
|
||||
"""Initialize CausalConvTranspose1d module."""
|
||||
super(CausalConvTranspose1d, self).__init__()
|
||||
self.deconv = torch.nn.ConvTranspose1d(
|
||||
in_channels, out_channels, kernel_size, stride, bias=bias)
|
||||
self.stride = stride
|
||||
|
||||
def forward(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor (B, in_channels, T_in).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, out_channels, T_out).
|
||||
|
||||
"""
|
||||
return self.deconv(x)[:, :, :-self.stride]
|
||||
@@ -0,0 +1,129 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2020 Tomoki Hayashi
|
||||
# MIT License (https://opensource.org/licenses/MIT)
|
||||
|
||||
"""Pseudo QMF modules."""
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
from scipy.signal import kaiser
|
||||
|
||||
|
||||
def design_prototype_filter(taps=62, cutoff_ratio=0.15, beta=9.0):
|
||||
"""Design prototype filter for PQMF.
|
||||
|
||||
This method is based on `A Kaiser window approach for the design of prototype
|
||||
filters of cosine modulated filterbanks`_.
|
||||
|
||||
Args:
|
||||
taps (int): The number of filter taps.
|
||||
cutoff_ratio (float): Cut-off frequency ratio.
|
||||
beta (float): Beta coefficient for kaiser window.
|
||||
|
||||
Returns:
|
||||
ndarray: Impluse response of prototype filter (taps + 1,).
|
||||
|
||||
.. _`A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`:
|
||||
https://ieeexplore.ieee.org/abstract/document/681427
|
||||
|
||||
"""
|
||||
# check the arguments are valid
|
||||
assert taps % 2 == 0, "The number of taps mush be even number."
|
||||
assert 0.0 < cutoff_ratio < 1.0, "Cutoff ratio must be > 0.0 and < 1.0."
|
||||
|
||||
# make initial filter
|
||||
omega_c = np.pi * cutoff_ratio
|
||||
with np.errstate(invalid='ignore'):
|
||||
h_i = np.sin(omega_c * (np.arange(taps + 1) - 0.5 * taps)) \
|
||||
/ (np.pi * (np.arange(taps + 1) - 0.5 * taps))
|
||||
h_i[taps // 2] = np.cos(0) * cutoff_ratio # fix nan due to indeterminate form
|
||||
|
||||
# apply kaiser window
|
||||
w = kaiser(taps + 1, beta)
|
||||
h = h_i * w
|
||||
|
||||
return h
|
||||
|
||||
|
||||
class PQMF(torch.nn.Module):
|
||||
"""PQMF module.
|
||||
|
||||
This module is based on `Near-perfect-reconstruction pseudo-QMF banks`_.
|
||||
|
||||
.. _`Near-perfect-reconstruction pseudo-QMF banks`:
|
||||
https://ieeexplore.ieee.org/document/258122
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, subbands=4, taps=62, cutoff_ratio=0.15, beta=9.0):
|
||||
"""Initilize PQMF module.
|
||||
|
||||
Args:
|
||||
subbands (int): The number of subbands.
|
||||
taps (int): The number of filter taps.
|
||||
cutoff_ratio (float): Cut-off frequency ratio.
|
||||
beta (float): Beta coefficient for kaiser window.
|
||||
|
||||
"""
|
||||
super(PQMF, self).__init__()
|
||||
|
||||
# define filter coefficient
|
||||
h_proto = design_prototype_filter(taps, cutoff_ratio, beta)
|
||||
h_analysis = np.zeros((subbands, len(h_proto)))
|
||||
h_synthesis = np.zeros((subbands, len(h_proto)))
|
||||
for k in range(subbands):
|
||||
h_analysis[k] = 2 * h_proto * np.cos(
|
||||
(2 * k + 1) * (np.pi / (2 * subbands)) *
|
||||
(np.arange(taps + 1) - ((taps - 1) / 2)) +
|
||||
(-1) ** k * np.pi / 4)
|
||||
h_synthesis[k] = 2 * h_proto * np.cos(
|
||||
(2 * k + 1) * (np.pi / (2 * subbands)) *
|
||||
(np.arange(taps + 1) - ((taps - 1) / 2)) -
|
||||
(-1) ** k * np.pi / 4)
|
||||
|
||||
# convert to tensor
|
||||
analysis_filter = torch.from_numpy(h_analysis).float().unsqueeze(1)
|
||||
synthesis_filter = torch.from_numpy(h_synthesis).float().unsqueeze(0)
|
||||
|
||||
# register coefficients as beffer
|
||||
self.register_buffer("analysis_filter", analysis_filter)
|
||||
self.register_buffer("synthesis_filter", synthesis_filter)
|
||||
|
||||
# filter for downsampling & upsampling
|
||||
updown_filter = torch.zeros((subbands, subbands, subbands)).float()
|
||||
for k in range(subbands):
|
||||
updown_filter[k, k, 0] = 1.0
|
||||
self.register_buffer("updown_filter", updown_filter)
|
||||
self.subbands = subbands
|
||||
|
||||
# keep padding info
|
||||
self.pad_fn = torch.nn.ConstantPad1d(taps // 2, 0.0)
|
||||
|
||||
def analysis(self, x):
|
||||
"""Analysis with PQMF.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor (B, 1, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, subbands, T // subbands).
|
||||
|
||||
"""
|
||||
x = F.conv1d(self.pad_fn(x), self.analysis_filter)
|
||||
return F.conv1d(x, self.updown_filter, stride=self.subbands)
|
||||
|
||||
def synthesis(self, x):
|
||||
"""Synthesis with PQMF.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor (B, subbands, T // subbands).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, 1, T).
|
||||
|
||||
"""
|
||||
x = F.conv_transpose1d(x, self.updown_filter * self.subbands, stride=self.subbands)
|
||||
return F.conv1d(self.pad_fn(x), self.synthesis_filter)
|
||||
@@ -0,0 +1,129 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""Residual block module in WaveNet.
|
||||
|
||||
This code is modified from https://github.com/r9y9/wavenet_vocoder.
|
||||
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
|
||||
class Conv1d(torch.nn.Conv1d):
|
||||
"""Conv1d module with customized initialization."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initialize Conv1d module."""
|
||||
super(Conv1d, self).__init__(*args, **kwargs)
|
||||
|
||||
def reset_parameters(self):
|
||||
"""Reset parameters."""
|
||||
torch.nn.init.kaiming_normal_(self.weight, nonlinearity="relu")
|
||||
if self.bias is not None:
|
||||
torch.nn.init.constant_(self.bias, 0.0)
|
||||
|
||||
|
||||
class Conv1d1x1(Conv1d):
|
||||
"""1x1 Conv1d with customized initialization."""
|
||||
|
||||
def __init__(self, in_channels, out_channels, bias):
|
||||
"""Initialize 1x1 Conv1d module."""
|
||||
super(Conv1d1x1, self).__init__(in_channels, out_channels,
|
||||
kernel_size=1, padding=0,
|
||||
dilation=1, bias=bias)
|
||||
|
||||
|
||||
class ResidualBlock(torch.nn.Module):
|
||||
"""Residual block module in WaveNet."""
|
||||
|
||||
def __init__(self,
|
||||
kernel_size=3,
|
||||
residual_channels=64,
|
||||
gate_channels=128,
|
||||
skip_channels=64,
|
||||
aux_channels=80,
|
||||
dropout=0.0,
|
||||
dilation=1,
|
||||
bias=True,
|
||||
use_causal_conv=False
|
||||
):
|
||||
"""Initialize ResidualBlock module.
|
||||
|
||||
Args:
|
||||
kernel_size (int): Kernel size of dilation convolution layer.
|
||||
residual_channels (int): Number of channels for residual connection.
|
||||
skip_channels (int): Number of channels for skip connection.
|
||||
aux_channels (int): Local conditioning channels i.e. auxiliary input dimension.
|
||||
dropout (float): Dropout probability.
|
||||
dilation (int): Dilation factor.
|
||||
bias (bool): Whether to add bias parameter in convolution layers.
|
||||
use_causal_conv (bool): Whether to use use_causal_conv or non-use_causal_conv convolution.
|
||||
|
||||
"""
|
||||
super(ResidualBlock, self).__init__()
|
||||
self.dropout = dropout
|
||||
# no future time stamps available
|
||||
if use_causal_conv:
|
||||
padding = (kernel_size - 1) * dilation
|
||||
else:
|
||||
assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
|
||||
padding = (kernel_size - 1) // 2 * dilation
|
||||
self.use_causal_conv = use_causal_conv
|
||||
|
||||
# dilation conv
|
||||
self.conv = Conv1d(residual_channels, gate_channels, kernel_size,
|
||||
padding=padding, dilation=dilation, bias=bias)
|
||||
|
||||
# local conditioning
|
||||
if aux_channels > 0:
|
||||
self.conv1x1_aux = Conv1d1x1(aux_channels, gate_channels, bias=False)
|
||||
else:
|
||||
self.conv1x1_aux = None
|
||||
|
||||
# conv output is split into two groups
|
||||
gate_out_channels = gate_channels // 2
|
||||
self.conv1x1_out = Conv1d1x1(gate_out_channels, residual_channels, bias=bias)
|
||||
self.conv1x1_skip = Conv1d1x1(gate_out_channels, skip_channels, bias=bias)
|
||||
|
||||
def forward(self, x, c):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor (B, residual_channels, T).
|
||||
c (Tensor): Local conditioning auxiliary tensor (B, aux_channels, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor for residual connection (B, residual_channels, T).
|
||||
Tensor: Output tensor for skip connection (B, skip_channels, T).
|
||||
|
||||
"""
|
||||
residual = x
|
||||
x = F.dropout(x, p=self.dropout, training=self.training)
|
||||
x = self.conv(x)
|
||||
|
||||
# remove future time steps if use_causal_conv conv
|
||||
x = x[:, :, :residual.size(-1)] if self.use_causal_conv else x
|
||||
|
||||
# split into two part for gated activation
|
||||
splitdim = 1
|
||||
xa, xb = x.split(x.size(splitdim) // 2, dim=splitdim)
|
||||
|
||||
# local conditioning
|
||||
if c is not None:
|
||||
assert self.conv1x1_aux is not None
|
||||
c = self.conv1x1_aux(c)
|
||||
ca, cb = c.split(c.size(splitdim) // 2, dim=splitdim)
|
||||
xa, xb = xa + ca, xb + cb
|
||||
|
||||
x = torch.tanh(xa) * torch.sigmoid(xb)
|
||||
|
||||
# for skip connection
|
||||
s = self.conv1x1_skip(x)
|
||||
|
||||
# for residual connection
|
||||
x = (self.conv1x1_out(x) + residual) * math.sqrt(0.5)
|
||||
|
||||
return x, s
|
||||
@@ -0,0 +1,75 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2020 Tomoki Hayashi
|
||||
# MIT License (https://opensource.org/licenses/MIT)
|
||||
|
||||
"""Residual stack module in MelGAN."""
|
||||
|
||||
import torch
|
||||
|
||||
from . import CausalConv1d
|
||||
|
||||
|
||||
class ResidualStack(torch.nn.Module):
|
||||
"""Residual stack module introduced in MelGAN."""
|
||||
|
||||
def __init__(self,
|
||||
kernel_size=3,
|
||||
channels=32,
|
||||
dilation=1,
|
||||
bias=True,
|
||||
nonlinear_activation="LeakyReLU",
|
||||
nonlinear_activation_params={"negative_slope": 0.2},
|
||||
pad="ReflectionPad1d",
|
||||
pad_params={},
|
||||
use_causal_conv=False,
|
||||
):
|
||||
"""Initialize ResidualStack module.
|
||||
|
||||
Args:
|
||||
kernel_size (int): Kernel size of dilation convolution layer.
|
||||
channels (int): Number of channels of convolution layers.
|
||||
dilation (int): Dilation factor.
|
||||
bias (bool): Whether to add bias parameter in convolution layers.
|
||||
nonlinear_activation (str): Activation function module name.
|
||||
nonlinear_activation_params (dict): Hyperparameters for activation function.
|
||||
pad (str): Padding function module name before dilated convolution layer.
|
||||
pad_params (dict): Hyperparameters for padding function.
|
||||
use_causal_conv (bool): Whether to use causal convolution.
|
||||
|
||||
"""
|
||||
super(ResidualStack, self).__init__()
|
||||
|
||||
# defile residual stack part
|
||||
if not use_causal_conv:
|
||||
assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
|
||||
self.stack = torch.nn.Sequential(
|
||||
getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
|
||||
getattr(torch.nn, pad)((kernel_size - 1) // 2 * dilation, **pad_params),
|
||||
torch.nn.Conv1d(channels, channels, kernel_size, dilation=dilation, bias=bias),
|
||||
getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
|
||||
torch.nn.Conv1d(channels, channels, 1, bias=bias),
|
||||
)
|
||||
else:
|
||||
self.stack = torch.nn.Sequential(
|
||||
getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
|
||||
CausalConv1d(channels, channels, kernel_size, dilation=dilation,
|
||||
bias=bias, pad=pad, pad_params=pad_params),
|
||||
getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
|
||||
torch.nn.Conv1d(channels, channels, 1, bias=bias),
|
||||
)
|
||||
|
||||
# defile extra layer for skip connection
|
||||
self.skip_layer = torch.nn.Conv1d(channels, channels, 1, bias=bias)
|
||||
|
||||
def forward(self, c):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
c (Tensor): Input tensor (B, channels, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, chennels, T).
|
||||
|
||||
"""
|
||||
return self.stack(c) + self.skip_layer(c)
|
||||
@@ -0,0 +1,129 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2020 MINH ANH (@dathudeptrai)
|
||||
# MIT License (https://opensource.org/licenses/MIT)
|
||||
|
||||
"""Tensorflow Layer modules complatible with pytorch."""
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
class TFReflectionPad1d(tf.keras.layers.Layer):
|
||||
"""Tensorflow ReflectionPad1d module."""
|
||||
|
||||
def __init__(self, padding_size):
|
||||
"""Initialize TFReflectionPad1d module.
|
||||
|
||||
Args:
|
||||
padding_size (int): Padding size.
|
||||
|
||||
"""
|
||||
super(TFReflectionPad1d, self).__init__()
|
||||
self.padding_size = padding_size
|
||||
|
||||
@tf.function
|
||||
def call(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor (B, T, 1, C).
|
||||
|
||||
Returns:
|
||||
Tensor: Padded tensor (B, T + 2 * padding_size, 1, C).
|
||||
|
||||
"""
|
||||
return tf.pad(x, [[0, 0], [self.padding_size, self.padding_size], [0, 0], [0, 0]], "REFLECT")
|
||||
|
||||
|
||||
class TFConvTranspose1d(tf.keras.layers.Layer):
|
||||
"""Tensorflow ConvTranspose1d module."""
|
||||
|
||||
def __init__(self, channels, kernel_size, stride, padding):
|
||||
"""Initialize TFConvTranspose1d( module.
|
||||
|
||||
Args:
|
||||
channels (int): Number of channels.
|
||||
kernel_size (int): kernel size.
|
||||
strides (int): Stride width.
|
||||
padding (str): Padding type ("same" or "valid").
|
||||
|
||||
"""
|
||||
super(TFConvTranspose1d, self).__init__()
|
||||
self.conv1d_transpose = tf.keras.layers.Conv2DTranspose(
|
||||
filters=channels,
|
||||
kernel_size=(kernel_size, 1),
|
||||
strides=(stride, 1),
|
||||
padding=padding,
|
||||
)
|
||||
|
||||
@tf.function
|
||||
def call(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor (B, T, 1, C).
|
||||
|
||||
Returns:
|
||||
Tensors: Output tensor (B, T', 1, C').
|
||||
|
||||
"""
|
||||
x = self.conv1d_transpose(x)
|
||||
return x
|
||||
|
||||
|
||||
class TFResidualStack(tf.keras.layers.Layer):
|
||||
"""Tensorflow ResidualStack module."""
|
||||
|
||||
def __init__(self,
|
||||
kernel_size,
|
||||
channels,
|
||||
dilation,
|
||||
bias,
|
||||
nonlinear_activation,
|
||||
nonlinear_activation_params,
|
||||
padding,
|
||||
):
|
||||
"""Initialize TFResidualStack module.
|
||||
|
||||
Args:
|
||||
kernel_size (int): Kernel size.
|
||||
channles (int): Number of channels.
|
||||
dilation (int): Dilation ine.
|
||||
bias (bool): Whether to add bias parameter in convolution layers.
|
||||
nonlinear_activation (str): Activation function module name.
|
||||
nonlinear_activation_params (dict): Hyperparameters for activation function.
|
||||
padding (str): Padding type ("same" or "valid").
|
||||
|
||||
"""
|
||||
super(TFResidualStack, self).__init__()
|
||||
self.block = [
|
||||
getattr(tf.keras.layers, nonlinear_activation)(**nonlinear_activation_params),
|
||||
TFReflectionPad1d(dilation),
|
||||
tf.keras.layers.Conv2D(
|
||||
filters=channels,
|
||||
kernel_size=(kernel_size, 1),
|
||||
dilation_rate=(dilation, 1),
|
||||
use_bias=bias,
|
||||
padding="valid",
|
||||
),
|
||||
getattr(tf.keras.layers, nonlinear_activation)(**nonlinear_activation_params),
|
||||
tf.keras.layers.Conv2D(filters=channels, kernel_size=1, use_bias=bias)
|
||||
]
|
||||
self.shortcut = tf.keras.layers.Conv2D(filters=channels, kernel_size=1, use_bias=bias)
|
||||
|
||||
@tf.function
|
||||
def call(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor (B, T, 1, C).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, T, 1, C).
|
||||
|
||||
"""
|
||||
_x = tf.identity(x)
|
||||
for i, layer in enumerate(self.block):
|
||||
_x = layer(_x)
|
||||
shortcut = self.shortcut(x)
|
||||
return shortcut + _x
|
||||
@@ -0,0 +1,183 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""Upsampling module.
|
||||
|
||||
This code is modified from https://github.com/r9y9/wavenet_vocoder.
|
||||
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
from . import Conv1d
|
||||
|
||||
|
||||
class Stretch2d(torch.nn.Module):
|
||||
"""Stretch2d module."""
|
||||
|
||||
def __init__(self, x_scale, y_scale, mode="nearest"):
|
||||
"""Initialize Stretch2d module.
|
||||
|
||||
Args:
|
||||
x_scale (int): X scaling factor (Time axis in spectrogram).
|
||||
y_scale (int): Y scaling factor (Frequency axis in spectrogram).
|
||||
mode (str): Interpolation mode.
|
||||
|
||||
"""
|
||||
super(Stretch2d, self).__init__()
|
||||
self.x_scale = x_scale
|
||||
self.y_scale = y_scale
|
||||
self.mode = mode
|
||||
|
||||
def forward(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor (B, C, F, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Interpolated tensor (B, C, F * y_scale, T * x_scale),
|
||||
|
||||
"""
|
||||
return F.interpolate(
|
||||
x, scale_factor=(self.y_scale, self.x_scale), mode=self.mode)
|
||||
|
||||
|
||||
class Conv2d(torch.nn.Conv2d):
|
||||
"""Conv2d module with customized initialization."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initialize Conv2d module."""
|
||||
super(Conv2d, self).__init__(*args, **kwargs)
|
||||
|
||||
def reset_parameters(self):
|
||||
"""Reset parameters."""
|
||||
self.weight.data.fill_(1. / np.prod(self.kernel_size))
|
||||
if self.bias is not None:
|
||||
torch.nn.init.constant_(self.bias, 0.0)
|
||||
|
||||
|
||||
class UpsampleNetwork(torch.nn.Module):
|
||||
"""Upsampling network module."""
|
||||
|
||||
def __init__(self,
|
||||
upsample_scales,
|
||||
nonlinear_activation=None,
|
||||
nonlinear_activation_params={},
|
||||
interpolate_mode="nearest",
|
||||
freq_axis_kernel_size=1,
|
||||
use_causal_conv=False,
|
||||
):
|
||||
"""Initialize upsampling network module.
|
||||
|
||||
Args:
|
||||
upsample_scales (list): List of upsampling scales.
|
||||
nonlinear_activation (str): Activation function name.
|
||||
nonlinear_activation_params (dict): Arguments for specified activation function.
|
||||
interpolate_mode (str): Interpolation mode.
|
||||
freq_axis_kernel_size (int): Kernel size in the direction of frequency axis.
|
||||
|
||||
"""
|
||||
super(UpsampleNetwork, self).__init__()
|
||||
self.use_causal_conv = use_causal_conv
|
||||
self.up_layers = torch.nn.ModuleList()
|
||||
for scale in upsample_scales:
|
||||
# interpolation layer
|
||||
stretch = Stretch2d(scale, 1, interpolate_mode)
|
||||
self.up_layers += [stretch]
|
||||
|
||||
# conv layer
|
||||
assert (freq_axis_kernel_size - 1) % 2 == 0, "Not support even number freq axis kernel size."
|
||||
freq_axis_padding = (freq_axis_kernel_size - 1) // 2
|
||||
kernel_size = (freq_axis_kernel_size, scale * 2 + 1)
|
||||
if use_causal_conv:
|
||||
padding = (freq_axis_padding, scale * 2)
|
||||
else:
|
||||
padding = (freq_axis_padding, scale)
|
||||
conv = Conv2d(1, 1, kernel_size=kernel_size, padding=padding, bias=False)
|
||||
self.up_layers += [conv]
|
||||
|
||||
# nonlinear
|
||||
if nonlinear_activation is not None:
|
||||
nonlinear = getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params)
|
||||
self.up_layers += [nonlinear]
|
||||
|
||||
def forward(self, c):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
c : Input tensor (B, C, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Upsampled tensor (B, C, T'), where T' = T * prod(upsample_scales).
|
||||
|
||||
"""
|
||||
c = c.unsqueeze(1) # (B, 1, C, T)
|
||||
for f in self.up_layers:
|
||||
if self.use_causal_conv and isinstance(f, Conv2d):
|
||||
c = f(c)[..., :c.size(-1)]
|
||||
else:
|
||||
c = f(c)
|
||||
return c.squeeze(1) # (B, C, T')
|
||||
|
||||
|
||||
class ConvInUpsampleNetwork(torch.nn.Module):
|
||||
"""Convolution + upsampling network module."""
|
||||
|
||||
def __init__(self,
|
||||
upsample_scales,
|
||||
nonlinear_activation=None,
|
||||
nonlinear_activation_params={},
|
||||
interpolate_mode="nearest",
|
||||
freq_axis_kernel_size=1,
|
||||
aux_channels=80,
|
||||
aux_context_window=0,
|
||||
use_causal_conv=False
|
||||
):
|
||||
"""Initialize convolution + upsampling network module.
|
||||
|
||||
Args:
|
||||
upsample_scales (list): List of upsampling scales.
|
||||
nonlinear_activation (str): Activation function name.
|
||||
nonlinear_activation_params (dict): Arguments for specified activation function.
|
||||
mode (str): Interpolation mode.
|
||||
freq_axis_kernel_size (int): Kernel size in the direction of frequency axis.
|
||||
aux_channels (int): Number of channels of pre-convolutional layer.
|
||||
aux_context_window (int): Context window size of the pre-convolutional layer.
|
||||
use_causal_conv (bool): Whether to use causal structure.
|
||||
|
||||
"""
|
||||
super(ConvInUpsampleNetwork, self).__init__()
|
||||
self.aux_context_window = aux_context_window
|
||||
self.use_causal_conv = use_causal_conv and aux_context_window > 0
|
||||
# To capture wide-context information in conditional features
|
||||
kernel_size = aux_context_window + 1 if use_causal_conv else 2 * aux_context_window + 1
|
||||
# NOTE(kan-bayashi): Here do not use padding because the input is already padded
|
||||
self.conv_in = Conv1d(aux_channels, aux_channels, kernel_size=kernel_size, bias=False)
|
||||
self.upsample = UpsampleNetwork(
|
||||
upsample_scales=upsample_scales,
|
||||
nonlinear_activation=nonlinear_activation,
|
||||
nonlinear_activation_params=nonlinear_activation_params,
|
||||
interpolate_mode=interpolate_mode,
|
||||
freq_axis_kernel_size=freq_axis_kernel_size,
|
||||
use_causal_conv=use_causal_conv,
|
||||
)
|
||||
|
||||
def forward(self, c):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
c : Input tensor (B, C, T').
|
||||
|
||||
Returns:
|
||||
Tensor: Upsampled tensor (B, C, T),
|
||||
where T = (T' - aux_context_window * 2) * prod(upsample_scales).
|
||||
|
||||
Note:
|
||||
The length of inputs considers the context window size.
|
||||
|
||||
"""
|
||||
c_ = self.conv_in(c)
|
||||
c = c_[:, :, :-self.aux_context_window] if self.use_causal_conv else c_
|
||||
return self.upsample(c)
|
||||
@@ -0,0 +1 @@
|
||||
from .stft_loss import * # NOQA
|
||||
@@ -0,0 +1,153 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2019 Tomoki Hayashi
|
||||
# MIT License (https://opensource.org/licenses/MIT)
|
||||
|
||||
"""STFT-based Loss modules."""
|
||||
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
|
||||
def stft(x, fft_size, hop_size, win_length, window):
|
||||
"""Perform STFT and convert to magnitude spectrogram.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input signal tensor (B, T).
|
||||
fft_size (int): FFT size.
|
||||
hop_size (int): Hop size.
|
||||
win_length (int): Window length.
|
||||
window (str): Window function type.
|
||||
|
||||
Returns:
|
||||
Tensor: Magnitude spectrogram (B, #frames, fft_size // 2 + 1).
|
||||
|
||||
"""
|
||||
x_stft = torch.stft(x, fft_size, hop_size, win_length, window)
|
||||
real = x_stft[..., 0]
|
||||
imag = x_stft[..., 1]
|
||||
|
||||
# NOTE(kan-bayashi): clamp is needed to avoid nan or inf
|
||||
return torch.sqrt(torch.clamp(real ** 2 + imag ** 2, min=1e-7)).transpose(2, 1)
|
||||
|
||||
|
||||
class SpectralConvergengeLoss(torch.nn.Module):
|
||||
"""Spectral convergence loss module."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initilize spectral convergence loss module."""
|
||||
super(SpectralConvergengeLoss, self).__init__()
|
||||
|
||||
def forward(self, x_mag, y_mag):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x_mag (Tensor): Magnitude spectrogram of predicted signal (B, #frames, #freq_bins).
|
||||
y_mag (Tensor): Magnitude spectrogram of groundtruth signal (B, #frames, #freq_bins).
|
||||
|
||||
Returns:
|
||||
Tensor: Spectral convergence loss value.
|
||||
|
||||
"""
|
||||
return torch.norm(y_mag - x_mag, p="fro") / torch.norm(y_mag, p="fro")
|
||||
|
||||
|
||||
class LogSTFTMagnitudeLoss(torch.nn.Module):
|
||||
"""Log STFT magnitude loss module."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initilize los STFT magnitude loss module."""
|
||||
super(LogSTFTMagnitudeLoss, self).__init__()
|
||||
|
||||
def forward(self, x_mag, y_mag):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x_mag (Tensor): Magnitude spectrogram of predicted signal (B, #frames, #freq_bins).
|
||||
y_mag (Tensor): Magnitude spectrogram of groundtruth signal (B, #frames, #freq_bins).
|
||||
|
||||
Returns:
|
||||
Tensor: Log STFT magnitude loss value.
|
||||
|
||||
"""
|
||||
return F.l1_loss(torch.log(y_mag), torch.log(x_mag))
|
||||
|
||||
|
||||
class STFTLoss(torch.nn.Module):
|
||||
"""STFT loss module."""
|
||||
|
||||
def __init__(self, fft_size=1024, shift_size=120, win_length=600, window="hann_window"):
|
||||
"""Initialize STFT loss module."""
|
||||
super(STFTLoss, self).__init__()
|
||||
self.fft_size = fft_size
|
||||
self.shift_size = shift_size
|
||||
self.win_length = win_length
|
||||
self.window = getattr(torch, window)(win_length)
|
||||
self.spectral_convergenge_loss = SpectralConvergengeLoss()
|
||||
self.log_stft_magnitude_loss = LogSTFTMagnitudeLoss()
|
||||
|
||||
def forward(self, x, y):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Predicted signal (B, T).
|
||||
y (Tensor): Groundtruth signal (B, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Spectral convergence loss value.
|
||||
Tensor: Log STFT magnitude loss value.
|
||||
|
||||
"""
|
||||
x_mag = stft(x, self.fft_size, self.shift_size, self.win_length, self.window)
|
||||
y_mag = stft(y, self.fft_size, self.shift_size, self.win_length, self.window)
|
||||
sc_loss = self.spectral_convergenge_loss(x_mag, y_mag)
|
||||
mag_loss = self.log_stft_magnitude_loss(x_mag, y_mag)
|
||||
|
||||
return sc_loss, mag_loss
|
||||
|
||||
|
||||
class MultiResolutionSTFTLoss(torch.nn.Module):
|
||||
"""Multi resolution STFT loss module."""
|
||||
|
||||
def __init__(self,
|
||||
fft_sizes=[1024, 2048, 512],
|
||||
hop_sizes=[120, 240, 50],
|
||||
win_lengths=[600, 1200, 240],
|
||||
window="hann_window"):
|
||||
"""Initialize Multi resolution STFT loss module.
|
||||
|
||||
Args:
|
||||
fft_sizes (list): List of FFT sizes.
|
||||
hop_sizes (list): List of hop sizes.
|
||||
win_lengths (list): List of window lengths.
|
||||
window (str): Window function type.
|
||||
|
||||
"""
|
||||
super(MultiResolutionSTFTLoss, self).__init__()
|
||||
assert len(fft_sizes) == len(hop_sizes) == len(win_lengths)
|
||||
self.stft_losses = torch.nn.ModuleList()
|
||||
for fs, ss, wl in zip(fft_sizes, hop_sizes, win_lengths):
|
||||
self.stft_losses += [STFTLoss(fs, ss, wl, window)]
|
||||
|
||||
def forward(self, x, y):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Predicted signal (B, T).
|
||||
y (Tensor): Groundtruth signal (B, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Multi resolution spectral convergence loss value.
|
||||
Tensor: Multi resolution log STFT magnitude loss value.
|
||||
|
||||
"""
|
||||
sc_loss = 0.0
|
||||
mag_loss = 0.0
|
||||
for f in self.stft_losses:
|
||||
sc_l, mag_l = f(x, y)
|
||||
sc_loss += sc_l
|
||||
mag_loss += mag_l
|
||||
sc_loss /= len(self.stft_losses)
|
||||
mag_loss /= len(self.stft_losses)
|
||||
|
||||
return sc_loss, mag_loss
|
||||
@@ -0,0 +1,2 @@
|
||||
from .melgan import * # NOQA
|
||||
from .parallel_wavegan import * # NOQA
|
||||
@@ -0,0 +1,427 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2020 Tomoki Hayashi
|
||||
# MIT License (https://opensource.org/licenses/MIT)
|
||||
|
||||
"""MelGAN Modules."""
|
||||
|
||||
import logging
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from modules.parallel_wavegan.layers import CausalConv1d
|
||||
from modules.parallel_wavegan.layers import CausalConvTranspose1d
|
||||
from modules.parallel_wavegan.layers import ResidualStack
|
||||
|
||||
|
||||
class MelGANGenerator(torch.nn.Module):
|
||||
"""MelGAN generator module."""
|
||||
|
||||
def __init__(self,
|
||||
in_channels=80,
|
||||
out_channels=1,
|
||||
kernel_size=7,
|
||||
channels=512,
|
||||
bias=True,
|
||||
upsample_scales=[8, 8, 2, 2],
|
||||
stack_kernel_size=3,
|
||||
stacks=3,
|
||||
nonlinear_activation="LeakyReLU",
|
||||
nonlinear_activation_params={"negative_slope": 0.2},
|
||||
pad="ReflectionPad1d",
|
||||
pad_params={},
|
||||
use_final_nonlinear_activation=True,
|
||||
use_weight_norm=True,
|
||||
use_causal_conv=False,
|
||||
):
|
||||
"""Initialize MelGANGenerator module.
|
||||
|
||||
Args:
|
||||
in_channels (int): Number of input channels.
|
||||
out_channels (int): Number of output channels.
|
||||
kernel_size (int): Kernel size of initial and final conv layer.
|
||||
channels (int): Initial number of channels for conv layer.
|
||||
bias (bool): Whether to add bias parameter in convolution layers.
|
||||
upsample_scales (list): List of upsampling scales.
|
||||
stack_kernel_size (int): Kernel size of dilated conv layers in residual stack.
|
||||
stacks (int): Number of stacks in a single residual stack.
|
||||
nonlinear_activation (str): Activation function module name.
|
||||
nonlinear_activation_params (dict): Hyperparameters for activation function.
|
||||
pad (str): Padding function module name before dilated convolution layer.
|
||||
pad_params (dict): Hyperparameters for padding function.
|
||||
use_final_nonlinear_activation (torch.nn.Module): Activation function for the final layer.
|
||||
use_weight_norm (bool): Whether to use weight norm.
|
||||
If set to true, it will be applied to all of the conv layers.
|
||||
use_causal_conv (bool): Whether to use causal convolution.
|
||||
|
||||
"""
|
||||
super(MelGANGenerator, self).__init__()
|
||||
|
||||
# check hyper parameters is valid
|
||||
assert channels >= np.prod(upsample_scales)
|
||||
assert channels % (2 ** len(upsample_scales)) == 0
|
||||
if not use_causal_conv:
|
||||
assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
|
||||
|
||||
# add initial layer
|
||||
layers = []
|
||||
if not use_causal_conv:
|
||||
layers += [
|
||||
getattr(torch.nn, pad)((kernel_size - 1) // 2, **pad_params),
|
||||
torch.nn.Conv1d(in_channels, channels, kernel_size, bias=bias),
|
||||
]
|
||||
else:
|
||||
layers += [
|
||||
CausalConv1d(in_channels, channels, kernel_size,
|
||||
bias=bias, pad=pad, pad_params=pad_params),
|
||||
]
|
||||
|
||||
for i, upsample_scale in enumerate(upsample_scales):
|
||||
# add upsampling layer
|
||||
layers += [getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params)]
|
||||
if not use_causal_conv:
|
||||
layers += [
|
||||
torch.nn.ConvTranspose1d(
|
||||
channels // (2 ** i),
|
||||
channels // (2 ** (i + 1)),
|
||||
upsample_scale * 2,
|
||||
stride=upsample_scale,
|
||||
padding=upsample_scale // 2 + upsample_scale % 2,
|
||||
output_padding=upsample_scale % 2,
|
||||
bias=bias,
|
||||
)
|
||||
]
|
||||
else:
|
||||
layers += [
|
||||
CausalConvTranspose1d(
|
||||
channels // (2 ** i),
|
||||
channels // (2 ** (i + 1)),
|
||||
upsample_scale * 2,
|
||||
stride=upsample_scale,
|
||||
bias=bias,
|
||||
)
|
||||
]
|
||||
|
||||
# add residual stack
|
||||
for j in range(stacks):
|
||||
layers += [
|
||||
ResidualStack(
|
||||
kernel_size=stack_kernel_size,
|
||||
channels=channels // (2 ** (i + 1)),
|
||||
dilation=stack_kernel_size ** j,
|
||||
bias=bias,
|
||||
nonlinear_activation=nonlinear_activation,
|
||||
nonlinear_activation_params=nonlinear_activation_params,
|
||||
pad=pad,
|
||||
pad_params=pad_params,
|
||||
use_causal_conv=use_causal_conv,
|
||||
)
|
||||
]
|
||||
|
||||
# add final layer
|
||||
layers += [getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params)]
|
||||
if not use_causal_conv:
|
||||
layers += [
|
||||
getattr(torch.nn, pad)((kernel_size - 1) // 2, **pad_params),
|
||||
torch.nn.Conv1d(channels // (2 ** (i + 1)), out_channels, kernel_size, bias=bias),
|
||||
]
|
||||
else:
|
||||
layers += [
|
||||
CausalConv1d(channels // (2 ** (i + 1)), out_channels, kernel_size,
|
||||
bias=bias, pad=pad, pad_params=pad_params),
|
||||
]
|
||||
if use_final_nonlinear_activation:
|
||||
layers += [torch.nn.Tanh()]
|
||||
|
||||
# define the model as a single function
|
||||
self.melgan = torch.nn.Sequential(*layers)
|
||||
|
||||
# apply weight norm
|
||||
if use_weight_norm:
|
||||
self.apply_weight_norm()
|
||||
|
||||
# reset parameters
|
||||
self.reset_parameters()
|
||||
|
||||
def forward(self, c):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
c (Tensor): Input tensor (B, channels, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, 1, T ** prod(upsample_scales)).
|
||||
|
||||
"""
|
||||
return self.melgan(c)
|
||||
|
||||
def remove_weight_norm(self):
|
||||
"""Remove weight normalization module from all of the layers."""
|
||||
def _remove_weight_norm(m):
|
||||
try:
|
||||
logging.debug(f"Weight norm is removed from {m}.")
|
||||
torch.nn.utils.remove_weight_norm(m)
|
||||
except ValueError: # this module didn't have weight norm
|
||||
return
|
||||
|
||||
self.apply(_remove_weight_norm)
|
||||
|
||||
def apply_weight_norm(self):
|
||||
"""Apply weight normalization module from all of the layers."""
|
||||
def _apply_weight_norm(m):
|
||||
if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.ConvTranspose1d):
|
||||
torch.nn.utils.weight_norm(m)
|
||||
logging.debug(f"Weight norm is applied to {m}.")
|
||||
|
||||
self.apply(_apply_weight_norm)
|
||||
|
||||
def reset_parameters(self):
|
||||
"""Reset parameters.
|
||||
|
||||
This initialization follows official implementation manner.
|
||||
https://github.com/descriptinc/melgan-neurips/blob/master/spec2wav/modules.py
|
||||
|
||||
"""
|
||||
def _reset_parameters(m):
|
||||
if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.ConvTranspose1d):
|
||||
m.weight.data.normal_(0.0, 0.02)
|
||||
logging.debug(f"Reset parameters in {m}.")
|
||||
|
||||
self.apply(_reset_parameters)
|
||||
|
||||
|
||||
class MelGANDiscriminator(torch.nn.Module):
|
||||
"""MelGAN discriminator module."""
|
||||
|
||||
def __init__(self,
|
||||
in_channels=1,
|
||||
out_channels=1,
|
||||
kernel_sizes=[5, 3],
|
||||
channels=16,
|
||||
max_downsample_channels=1024,
|
||||
bias=True,
|
||||
downsample_scales=[4, 4, 4, 4],
|
||||
nonlinear_activation="LeakyReLU",
|
||||
nonlinear_activation_params={"negative_slope": 0.2},
|
||||
pad="ReflectionPad1d",
|
||||
pad_params={},
|
||||
):
|
||||
"""Initilize MelGAN discriminator module.
|
||||
|
||||
Args:
|
||||
in_channels (int): Number of input channels.
|
||||
out_channels (int): Number of output channels.
|
||||
kernel_sizes (list): List of two kernel sizes. The prod will be used for the first conv layer,
|
||||
and the first and the second kernel sizes will be used for the last two layers.
|
||||
For example if kernel_sizes = [5, 3], the first layer kernel size will be 5 * 3 = 15,
|
||||
the last two layers' kernel size will be 5 and 3, respectively.
|
||||
channels (int): Initial number of channels for conv layer.
|
||||
max_downsample_channels (int): Maximum number of channels for downsampling layers.
|
||||
bias (bool): Whether to add bias parameter in convolution layers.
|
||||
downsample_scales (list): List of downsampling scales.
|
||||
nonlinear_activation (str): Activation function module name.
|
||||
nonlinear_activation_params (dict): Hyperparameters for activation function.
|
||||
pad (str): Padding function module name before dilated convolution layer.
|
||||
pad_params (dict): Hyperparameters for padding function.
|
||||
|
||||
"""
|
||||
super(MelGANDiscriminator, self).__init__()
|
||||
self.layers = torch.nn.ModuleList()
|
||||
|
||||
# check kernel size is valid
|
||||
assert len(kernel_sizes) == 2
|
||||
assert kernel_sizes[0] % 2 == 1
|
||||
assert kernel_sizes[1] % 2 == 1
|
||||
|
||||
# add first layer
|
||||
self.layers += [
|
||||
torch.nn.Sequential(
|
||||
getattr(torch.nn, pad)((np.prod(kernel_sizes) - 1) // 2, **pad_params),
|
||||
torch.nn.Conv1d(in_channels, channels, np.prod(kernel_sizes), bias=bias),
|
||||
getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
|
||||
)
|
||||
]
|
||||
|
||||
# add downsample layers
|
||||
in_chs = channels
|
||||
for downsample_scale in downsample_scales:
|
||||
out_chs = min(in_chs * downsample_scale, max_downsample_channels)
|
||||
self.layers += [
|
||||
torch.nn.Sequential(
|
||||
torch.nn.Conv1d(
|
||||
in_chs, out_chs,
|
||||
kernel_size=downsample_scale * 10 + 1,
|
||||
stride=downsample_scale,
|
||||
padding=downsample_scale * 5,
|
||||
groups=in_chs // 4,
|
||||
bias=bias,
|
||||
),
|
||||
getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
|
||||
)
|
||||
]
|
||||
in_chs = out_chs
|
||||
|
||||
# add final layers
|
||||
out_chs = min(in_chs * 2, max_downsample_channels)
|
||||
self.layers += [
|
||||
torch.nn.Sequential(
|
||||
torch.nn.Conv1d(
|
||||
in_chs, out_chs, kernel_sizes[0],
|
||||
padding=(kernel_sizes[0] - 1) // 2,
|
||||
bias=bias,
|
||||
),
|
||||
getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
|
||||
)
|
||||
]
|
||||
self.layers += [
|
||||
torch.nn.Conv1d(
|
||||
out_chs, out_channels, kernel_sizes[1],
|
||||
padding=(kernel_sizes[1] - 1) // 2,
|
||||
bias=bias,
|
||||
),
|
||||
]
|
||||
|
||||
def forward(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input noise signal (B, 1, T).
|
||||
|
||||
Returns:
|
||||
List: List of output tensors of each layer.
|
||||
|
||||
"""
|
||||
outs = []
|
||||
for f in self.layers:
|
||||
x = f(x)
|
||||
outs += [x]
|
||||
|
||||
return outs
|
||||
|
||||
|
||||
class MelGANMultiScaleDiscriminator(torch.nn.Module):
|
||||
"""MelGAN multi-scale discriminator module."""
|
||||
|
||||
def __init__(self,
|
||||
in_channels=1,
|
||||
out_channels=1,
|
||||
scales=3,
|
||||
downsample_pooling="AvgPool1d",
|
||||
# follow the official implementation setting
|
||||
downsample_pooling_params={
|
||||
"kernel_size": 4,
|
||||
"stride": 2,
|
||||
"padding": 1,
|
||||
"count_include_pad": False,
|
||||
},
|
||||
kernel_sizes=[5, 3],
|
||||
channels=16,
|
||||
max_downsample_channels=1024,
|
||||
bias=True,
|
||||
downsample_scales=[4, 4, 4, 4],
|
||||
nonlinear_activation="LeakyReLU",
|
||||
nonlinear_activation_params={"negative_slope": 0.2},
|
||||
pad="ReflectionPad1d",
|
||||
pad_params={},
|
||||
use_weight_norm=True,
|
||||
):
|
||||
"""Initilize MelGAN multi-scale discriminator module.
|
||||
|
||||
Args:
|
||||
in_channels (int): Number of input channels.
|
||||
out_channels (int): Number of output channels.
|
||||
downsample_pooling (str): Pooling module name for downsampling of the inputs.
|
||||
downsample_pooling_params (dict): Parameters for the above pooling module.
|
||||
kernel_sizes (list): List of two kernel sizes. The sum will be used for the first conv layer,
|
||||
and the first and the second kernel sizes will be used for the last two layers.
|
||||
channels (int): Initial number of channels for conv layer.
|
||||
max_downsample_channels (int): Maximum number of channels for downsampling layers.
|
||||
bias (bool): Whether to add bias parameter in convolution layers.
|
||||
downsample_scales (list): List of downsampling scales.
|
||||
nonlinear_activation (str): Activation function module name.
|
||||
nonlinear_activation_params (dict): Hyperparameters for activation function.
|
||||
pad (str): Padding function module name before dilated convolution layer.
|
||||
pad_params (dict): Hyperparameters for padding function.
|
||||
use_causal_conv (bool): Whether to use causal convolution.
|
||||
|
||||
"""
|
||||
super(MelGANMultiScaleDiscriminator, self).__init__()
|
||||
self.discriminators = torch.nn.ModuleList()
|
||||
|
||||
# add discriminators
|
||||
for _ in range(scales):
|
||||
self.discriminators += [
|
||||
MelGANDiscriminator(
|
||||
in_channels=in_channels,
|
||||
out_channels=out_channels,
|
||||
kernel_sizes=kernel_sizes,
|
||||
channels=channels,
|
||||
max_downsample_channels=max_downsample_channels,
|
||||
bias=bias,
|
||||
downsample_scales=downsample_scales,
|
||||
nonlinear_activation=nonlinear_activation,
|
||||
nonlinear_activation_params=nonlinear_activation_params,
|
||||
pad=pad,
|
||||
pad_params=pad_params,
|
||||
)
|
||||
]
|
||||
self.pooling = getattr(torch.nn, downsample_pooling)(**downsample_pooling_params)
|
||||
|
||||
# apply weight norm
|
||||
if use_weight_norm:
|
||||
self.apply_weight_norm()
|
||||
|
||||
# reset parameters
|
||||
self.reset_parameters()
|
||||
|
||||
def forward(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input noise signal (B, 1, T).
|
||||
|
||||
Returns:
|
||||
List: List of list of each discriminator outputs, which consists of each layer output tensors.
|
||||
|
||||
"""
|
||||
outs = []
|
||||
for f in self.discriminators:
|
||||
outs += [f(x)]
|
||||
x = self.pooling(x)
|
||||
|
||||
return outs
|
||||
|
||||
def remove_weight_norm(self):
|
||||
"""Remove weight normalization module from all of the layers."""
|
||||
def _remove_weight_norm(m):
|
||||
try:
|
||||
logging.debug(f"Weight norm is removed from {m}.")
|
||||
torch.nn.utils.remove_weight_norm(m)
|
||||
except ValueError: # this module didn't have weight norm
|
||||
return
|
||||
|
||||
self.apply(_remove_weight_norm)
|
||||
|
||||
def apply_weight_norm(self):
|
||||
"""Apply weight normalization module from all of the layers."""
|
||||
def _apply_weight_norm(m):
|
||||
if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.ConvTranspose1d):
|
||||
torch.nn.utils.weight_norm(m)
|
||||
logging.debug(f"Weight norm is applied to {m}.")
|
||||
|
||||
self.apply(_apply_weight_norm)
|
||||
|
||||
def reset_parameters(self):
|
||||
"""Reset parameters.
|
||||
|
||||
This initialization follows official implementation manner.
|
||||
https://github.com/descriptinc/melgan-neurips/blob/master/spec2wav/modules.py
|
||||
|
||||
"""
|
||||
def _reset_parameters(m):
|
||||
if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.ConvTranspose1d):
|
||||
m.weight.data.normal_(0.0, 0.02)
|
||||
logging.debug(f"Reset parameters in {m}.")
|
||||
|
||||
self.apply(_reset_parameters)
|
||||
@@ -0,0 +1,434 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2019 Tomoki Hayashi
|
||||
# MIT License (https://opensource.org/licenses/MIT)
|
||||
|
||||
"""Parallel WaveGAN Modules."""
|
||||
|
||||
import logging
|
||||
import math
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from modules.parallel_wavegan.layers import Conv1d
|
||||
from modules.parallel_wavegan.layers import Conv1d1x1
|
||||
from modules.parallel_wavegan.layers import ResidualBlock
|
||||
from modules.parallel_wavegan.layers import upsample
|
||||
from modules.parallel_wavegan import models
|
||||
|
||||
|
||||
class ParallelWaveGANGenerator(torch.nn.Module):
|
||||
"""Parallel WaveGAN Generator module."""
|
||||
|
||||
def __init__(self,
|
||||
in_channels=1,
|
||||
out_channels=1,
|
||||
kernel_size=3,
|
||||
layers=30,
|
||||
stacks=3,
|
||||
residual_channels=64,
|
||||
gate_channels=128,
|
||||
skip_channels=64,
|
||||
aux_channels=80,
|
||||
aux_context_window=2,
|
||||
dropout=0.0,
|
||||
bias=True,
|
||||
use_weight_norm=True,
|
||||
use_causal_conv=False,
|
||||
upsample_conditional_features=True,
|
||||
upsample_net="ConvInUpsampleNetwork",
|
||||
upsample_params={"upsample_scales": [4, 4, 4, 4]},
|
||||
use_pitch_embed=False,
|
||||
):
|
||||
"""Initialize Parallel WaveGAN Generator module.
|
||||
|
||||
Args:
|
||||
in_channels (int): Number of input channels.
|
||||
out_channels (int): Number of output channels.
|
||||
kernel_size (int): Kernel size of dilated convolution.
|
||||
layers (int): Number of residual block layers.
|
||||
stacks (int): Number of stacks i.e., dilation cycles.
|
||||
residual_channels (int): Number of channels in residual conv.
|
||||
gate_channels (int): Number of channels in gated conv.
|
||||
skip_channels (int): Number of channels in skip conv.
|
||||
aux_channels (int): Number of channels for auxiliary feature conv.
|
||||
aux_context_window (int): Context window size for auxiliary feature.
|
||||
dropout (float): Dropout rate. 0.0 means no dropout applied.
|
||||
bias (bool): Whether to use bias parameter in conv layer.
|
||||
use_weight_norm (bool): Whether to use weight norm.
|
||||
If set to true, it will be applied to all of the conv layers.
|
||||
use_causal_conv (bool): Whether to use causal structure.
|
||||
upsample_conditional_features (bool): Whether to use upsampling network.
|
||||
upsample_net (str): Upsampling network architecture.
|
||||
upsample_params (dict): Upsampling network parameters.
|
||||
|
||||
"""
|
||||
super(ParallelWaveGANGenerator, self).__init__()
|
||||
self.in_channels = in_channels
|
||||
self.out_channels = out_channels
|
||||
self.aux_channels = aux_channels
|
||||
self.layers = layers
|
||||
self.stacks = stacks
|
||||
self.kernel_size = kernel_size
|
||||
|
||||
# check the number of layers and stacks
|
||||
assert layers % stacks == 0
|
||||
layers_per_stack = layers // stacks
|
||||
|
||||
# define first convolution
|
||||
self.first_conv = Conv1d1x1(in_channels, residual_channels, bias=True)
|
||||
|
||||
# define conv + upsampling network
|
||||
if upsample_conditional_features:
|
||||
upsample_params.update({
|
||||
"use_causal_conv": use_causal_conv,
|
||||
})
|
||||
if upsample_net == "MelGANGenerator":
|
||||
assert aux_context_window == 0
|
||||
upsample_params.update({
|
||||
"use_weight_norm": False, # not to apply twice
|
||||
"use_final_nonlinear_activation": False,
|
||||
})
|
||||
self.upsample_net = getattr(models, upsample_net)(**upsample_params)
|
||||
else:
|
||||
if upsample_net == "ConvInUpsampleNetwork":
|
||||
upsample_params.update({
|
||||
"aux_channels": aux_channels,
|
||||
"aux_context_window": aux_context_window,
|
||||
})
|
||||
self.upsample_net = getattr(upsample, upsample_net)(**upsample_params)
|
||||
else:
|
||||
self.upsample_net = None
|
||||
|
||||
# define residual blocks
|
||||
self.conv_layers = torch.nn.ModuleList()
|
||||
for layer in range(layers):
|
||||
dilation = 2 ** (layer % layers_per_stack)
|
||||
conv = ResidualBlock(
|
||||
kernel_size=kernel_size,
|
||||
residual_channels=residual_channels,
|
||||
gate_channels=gate_channels,
|
||||
skip_channels=skip_channels,
|
||||
aux_channels=aux_channels,
|
||||
dilation=dilation,
|
||||
dropout=dropout,
|
||||
bias=bias,
|
||||
use_causal_conv=use_causal_conv,
|
||||
)
|
||||
self.conv_layers += [conv]
|
||||
|
||||
# define output layers
|
||||
self.last_conv_layers = torch.nn.ModuleList([
|
||||
torch.nn.ReLU(inplace=True),
|
||||
Conv1d1x1(skip_channels, skip_channels, bias=True),
|
||||
torch.nn.ReLU(inplace=True),
|
||||
Conv1d1x1(skip_channels, out_channels, bias=True),
|
||||
])
|
||||
|
||||
self.use_pitch_embed = use_pitch_embed
|
||||
if use_pitch_embed:
|
||||
self.pitch_embed = nn.Embedding(300, aux_channels, 0)
|
||||
self.c_proj = nn.Linear(2 * aux_channels, aux_channels)
|
||||
|
||||
# apply weight norm
|
||||
if use_weight_norm:
|
||||
self.apply_weight_norm()
|
||||
|
||||
def forward(self, x, c=None, pitch=None, **kwargs):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input noise signal (B, C_in, T).
|
||||
c (Tensor): Local conditioning auxiliary features (B, C ,T').
|
||||
pitch (Tensor): Local conditioning pitch (B, T').
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, C_out, T)
|
||||
|
||||
"""
|
||||
# perform upsampling
|
||||
if c is not None and self.upsample_net is not None:
|
||||
if self.use_pitch_embed:
|
||||
p = self.pitch_embed(pitch)
|
||||
c = self.c_proj(torch.cat([c.transpose(1, 2), p], -1)).transpose(1, 2)
|
||||
c = self.upsample_net(c)
|
||||
assert c.size(-1) == x.size(-1), (c.size(-1), x.size(-1))
|
||||
|
||||
# encode to hidden representation
|
||||
x = self.first_conv(x)
|
||||
skips = 0
|
||||
for f in self.conv_layers:
|
||||
x, h = f(x, c)
|
||||
skips += h
|
||||
skips *= math.sqrt(1.0 / len(self.conv_layers))
|
||||
|
||||
# apply final layers
|
||||
x = skips
|
||||
for f in self.last_conv_layers:
|
||||
x = f(x)
|
||||
|
||||
return x
|
||||
|
||||
def remove_weight_norm(self):
|
||||
"""Remove weight normalization module from all of the layers."""
|
||||
def _remove_weight_norm(m):
|
||||
try:
|
||||
logging.debug(f"Weight norm is removed from {m}.")
|
||||
torch.nn.utils.remove_weight_norm(m)
|
||||
except ValueError: # this module didn't have weight norm
|
||||
return
|
||||
|
||||
self.apply(_remove_weight_norm)
|
||||
|
||||
def apply_weight_norm(self):
|
||||
"""Apply weight normalization module from all of the layers."""
|
||||
def _apply_weight_norm(m):
|
||||
if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.Conv2d):
|
||||
torch.nn.utils.weight_norm(m)
|
||||
logging.debug(f"Weight norm is applied to {m}.")
|
||||
|
||||
self.apply(_apply_weight_norm)
|
||||
|
||||
@staticmethod
|
||||
def _get_receptive_field_size(layers, stacks, kernel_size,
|
||||
dilation=lambda x: 2 ** x):
|
||||
assert layers % stacks == 0
|
||||
layers_per_cycle = layers // stacks
|
||||
dilations = [dilation(i % layers_per_cycle) for i in range(layers)]
|
||||
return (kernel_size - 1) * sum(dilations) + 1
|
||||
|
||||
@property
|
||||
def receptive_field_size(self):
|
||||
"""Return receptive field size."""
|
||||
return self._get_receptive_field_size(self.layers, self.stacks, self.kernel_size)
|
||||
|
||||
|
||||
class ParallelWaveGANDiscriminator(torch.nn.Module):
|
||||
"""Parallel WaveGAN Discriminator module."""
|
||||
|
||||
def __init__(self,
|
||||
in_channels=1,
|
||||
out_channels=1,
|
||||
kernel_size=3,
|
||||
layers=10,
|
||||
conv_channels=64,
|
||||
dilation_factor=1,
|
||||
nonlinear_activation="LeakyReLU",
|
||||
nonlinear_activation_params={"negative_slope": 0.2},
|
||||
bias=True,
|
||||
use_weight_norm=True,
|
||||
):
|
||||
"""Initialize Parallel WaveGAN Discriminator module.
|
||||
|
||||
Args:
|
||||
in_channels (int): Number of input channels.
|
||||
out_channels (int): Number of output channels.
|
||||
kernel_size (int): Number of output channels.
|
||||
layers (int): Number of conv layers.
|
||||
conv_channels (int): Number of chnn layers.
|
||||
dilation_factor (int): Dilation factor. For example, if dilation_factor = 2,
|
||||
the dilation will be 2, 4, 8, ..., and so on.
|
||||
nonlinear_activation (str): Nonlinear function after each conv.
|
||||
nonlinear_activation_params (dict): Nonlinear function parameters
|
||||
bias (bool): Whether to use bias parameter in conv.
|
||||
use_weight_norm (bool) Whether to use weight norm.
|
||||
If set to true, it will be applied to all of the conv layers.
|
||||
|
||||
"""
|
||||
super(ParallelWaveGANDiscriminator, self).__init__()
|
||||
assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
|
||||
assert dilation_factor > 0, "Dilation factor must be > 0."
|
||||
self.conv_layers = torch.nn.ModuleList()
|
||||
conv_in_channels = in_channels
|
||||
for i in range(layers - 1):
|
||||
if i == 0:
|
||||
dilation = 1
|
||||
else:
|
||||
dilation = i if dilation_factor == 1 else dilation_factor ** i
|
||||
conv_in_channels = conv_channels
|
||||
padding = (kernel_size - 1) // 2 * dilation
|
||||
conv_layer = [
|
||||
Conv1d(conv_in_channels, conv_channels,
|
||||
kernel_size=kernel_size, padding=padding,
|
||||
dilation=dilation, bias=bias),
|
||||
getattr(torch.nn, nonlinear_activation)(inplace=True, **nonlinear_activation_params)
|
||||
]
|
||||
self.conv_layers += conv_layer
|
||||
padding = (kernel_size - 1) // 2
|
||||
last_conv_layer = Conv1d(
|
||||
conv_in_channels, out_channels,
|
||||
kernel_size=kernel_size, padding=padding, bias=bias)
|
||||
self.conv_layers += [last_conv_layer]
|
||||
|
||||
# apply weight norm
|
||||
if use_weight_norm:
|
||||
self.apply_weight_norm()
|
||||
|
||||
def forward(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input noise signal (B, 1, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, 1, T)
|
||||
|
||||
"""
|
||||
for f in self.conv_layers:
|
||||
x = f(x)
|
||||
return x
|
||||
|
||||
def apply_weight_norm(self):
|
||||
"""Apply weight normalization module from all of the layers."""
|
||||
def _apply_weight_norm(m):
|
||||
if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.Conv2d):
|
||||
torch.nn.utils.weight_norm(m)
|
||||
logging.debug(f"Weight norm is applied to {m}.")
|
||||
|
||||
self.apply(_apply_weight_norm)
|
||||
|
||||
def remove_weight_norm(self):
|
||||
"""Remove weight normalization module from all of the layers."""
|
||||
def _remove_weight_norm(m):
|
||||
try:
|
||||
logging.debug(f"Weight norm is removed from {m}.")
|
||||
torch.nn.utils.remove_weight_norm(m)
|
||||
except ValueError: # this module didn't have weight norm
|
||||
return
|
||||
|
||||
self.apply(_remove_weight_norm)
|
||||
|
||||
|
||||
class ResidualParallelWaveGANDiscriminator(torch.nn.Module):
|
||||
"""Parallel WaveGAN Discriminator module."""
|
||||
|
||||
def __init__(self,
|
||||
in_channels=1,
|
||||
out_channels=1,
|
||||
kernel_size=3,
|
||||
layers=30,
|
||||
stacks=3,
|
||||
residual_channels=64,
|
||||
gate_channels=128,
|
||||
skip_channels=64,
|
||||
dropout=0.0,
|
||||
bias=True,
|
||||
use_weight_norm=True,
|
||||
use_causal_conv=False,
|
||||
nonlinear_activation="LeakyReLU",
|
||||
nonlinear_activation_params={"negative_slope": 0.2},
|
||||
):
|
||||
"""Initialize Parallel WaveGAN Discriminator module.
|
||||
|
||||
Args:
|
||||
in_channels (int): Number of input channels.
|
||||
out_channels (int): Number of output channels.
|
||||
kernel_size (int): Kernel size of dilated convolution.
|
||||
layers (int): Number of residual block layers.
|
||||
stacks (int): Number of stacks i.e., dilation cycles.
|
||||
residual_channels (int): Number of channels in residual conv.
|
||||
gate_channels (int): Number of channels in gated conv.
|
||||
skip_channels (int): Number of channels in skip conv.
|
||||
dropout (float): Dropout rate. 0.0 means no dropout applied.
|
||||
bias (bool): Whether to use bias parameter in conv.
|
||||
use_weight_norm (bool): Whether to use weight norm.
|
||||
If set to true, it will be applied to all of the conv layers.
|
||||
use_causal_conv (bool): Whether to use causal structure.
|
||||
nonlinear_activation_params (dict): Nonlinear function parameters
|
||||
|
||||
"""
|
||||
super(ResidualParallelWaveGANDiscriminator, self).__init__()
|
||||
assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
|
||||
|
||||
self.in_channels = in_channels
|
||||
self.out_channels = out_channels
|
||||
self.layers = layers
|
||||
self.stacks = stacks
|
||||
self.kernel_size = kernel_size
|
||||
|
||||
# check the number of layers and stacks
|
||||
assert layers % stacks == 0
|
||||
layers_per_stack = layers // stacks
|
||||
|
||||
# define first convolution
|
||||
self.first_conv = torch.nn.Sequential(
|
||||
Conv1d1x1(in_channels, residual_channels, bias=True),
|
||||
getattr(torch.nn, nonlinear_activation)(
|
||||
inplace=True, **nonlinear_activation_params),
|
||||
)
|
||||
|
||||
# define residual blocks
|
||||
self.conv_layers = torch.nn.ModuleList()
|
||||
for layer in range(layers):
|
||||
dilation = 2 ** (layer % layers_per_stack)
|
||||
conv = ResidualBlock(
|
||||
kernel_size=kernel_size,
|
||||
residual_channels=residual_channels,
|
||||
gate_channels=gate_channels,
|
||||
skip_channels=skip_channels,
|
||||
aux_channels=-1,
|
||||
dilation=dilation,
|
||||
dropout=dropout,
|
||||
bias=bias,
|
||||
use_causal_conv=use_causal_conv,
|
||||
)
|
||||
self.conv_layers += [conv]
|
||||
|
||||
# define output layers
|
||||
self.last_conv_layers = torch.nn.ModuleList([
|
||||
getattr(torch.nn, nonlinear_activation)(
|
||||
inplace=True, **nonlinear_activation_params),
|
||||
Conv1d1x1(skip_channels, skip_channels, bias=True),
|
||||
getattr(torch.nn, nonlinear_activation)(
|
||||
inplace=True, **nonlinear_activation_params),
|
||||
Conv1d1x1(skip_channels, out_channels, bias=True),
|
||||
])
|
||||
|
||||
# apply weight norm
|
||||
if use_weight_norm:
|
||||
self.apply_weight_norm()
|
||||
|
||||
def forward(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input noise signal (B, 1, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Output tensor (B, 1, T)
|
||||
|
||||
"""
|
||||
x = self.first_conv(x)
|
||||
|
||||
skips = 0
|
||||
for f in self.conv_layers:
|
||||
x, h = f(x, None)
|
||||
skips += h
|
||||
skips *= math.sqrt(1.0 / len(self.conv_layers))
|
||||
|
||||
# apply final layers
|
||||
x = skips
|
||||
for f in self.last_conv_layers:
|
||||
x = f(x)
|
||||
return x
|
||||
|
||||
def apply_weight_norm(self):
|
||||
"""Apply weight normalization module from all of the layers."""
|
||||
def _apply_weight_norm(m):
|
||||
if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.Conv2d):
|
||||
torch.nn.utils.weight_norm(m)
|
||||
logging.debug(f"Weight norm is applied to {m}.")
|
||||
|
||||
self.apply(_apply_weight_norm)
|
||||
|
||||
def remove_weight_norm(self):
|
||||
"""Remove weight normalization module from all of the layers."""
|
||||
def _remove_weight_norm(m):
|
||||
try:
|
||||
logging.debug(f"Weight norm is removed from {m}.")
|
||||
torch.nn.utils.remove_weight_norm(m)
|
||||
except ValueError: # this module didn't have weight norm
|
||||
return
|
||||
|
||||
self.apply(_remove_weight_norm)
|
||||
@@ -0,0 +1,538 @@
|
||||
import torch
|
||||
import numpy as np
|
||||
import sys
|
||||
import torch.nn.functional as torch_nn_func
|
||||
|
||||
|
||||
class SineGen(torch.nn.Module):
|
||||
""" Definition of sine generator
|
||||
SineGen(samp_rate, harmonic_num = 0,
|
||||
sine_amp = 0.1, noise_std = 0.003,
|
||||
voiced_threshold = 0,
|
||||
flag_for_pulse=False)
|
||||
|
||||
samp_rate: sampling rate in Hz
|
||||
harmonic_num: number of harmonic overtones (default 0)
|
||||
sine_amp: amplitude of sine-wavefrom (default 0.1)
|
||||
noise_std: std of Gaussian noise (default 0.003)
|
||||
voiced_thoreshold: F0 threshold for U/V classification (default 0)
|
||||
flag_for_pulse: this SinGen is used inside PulseGen (default False)
|
||||
|
||||
Note: when flag_for_pulse is True, the first time step of a voiced
|
||||
segment is always sin(np.pi) or cos(0)
|
||||
"""
|
||||
|
||||
def __init__(self, samp_rate, harmonic_num=0,
|
||||
sine_amp=0.1, noise_std=0.003,
|
||||
voiced_threshold=0,
|
||||
flag_for_pulse=False):
|
||||
super(SineGen, self).__init__()
|
||||
self.sine_amp = sine_amp
|
||||
self.noise_std = noise_std
|
||||
self.harmonic_num = harmonic_num
|
||||
self.dim = self.harmonic_num + 1
|
||||
self.sampling_rate = samp_rate
|
||||
self.voiced_threshold = voiced_threshold
|
||||
self.flag_for_pulse = flag_for_pulse
|
||||
|
||||
def _f02uv(self, f0):
|
||||
# generate uv signal
|
||||
uv = torch.ones_like(f0)
|
||||
uv = uv * (f0 > self.voiced_threshold)
|
||||
return uv
|
||||
|
||||
def _f02sine(self, f0_values):
|
||||
""" f0_values: (batchsize, length, dim)
|
||||
where dim indicates fundamental tone and overtones
|
||||
"""
|
||||
# convert to F0 in rad. The interger part n can be ignored
|
||||
# because 2 * np.pi * n doesn't affect phase
|
||||
rad_values = (f0_values / self.sampling_rate) % 1
|
||||
|
||||
# initial phase noise (no noise for fundamental component)
|
||||
rand_ini = torch.rand(f0_values.shape[0], f0_values.shape[2], \
|
||||
device=f0_values.device)
|
||||
rand_ini[:, 0] = 0
|
||||
rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
|
||||
|
||||
# instantanouse phase sine[t] = sin(2*pi \sum_i=1 ^{t} rad)
|
||||
if not self.flag_for_pulse:
|
||||
# for normal case
|
||||
|
||||
# To prevent torch.cumsum numerical overflow,
|
||||
# it is necessary to add -1 whenever \sum_k=1^n rad_value_k > 1.
|
||||
# Buffer tmp_over_one_idx indicates the time step to add -1.
|
||||
# This will not change F0 of sine because (x-1) * 2*pi = x * 2*pi
|
||||
tmp_over_one = torch.cumsum(rad_values, 1) % 1
|
||||
tmp_over_one_idx = (tmp_over_one[:, 1:, :] -
|
||||
tmp_over_one[:, :-1, :]) < 0
|
||||
cumsum_shift = torch.zeros_like(rad_values)
|
||||
cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
|
||||
|
||||
sines = torch.sin(torch.cumsum(rad_values + cumsum_shift, dim=1)
|
||||
* 2 * np.pi)
|
||||
else:
|
||||
# If necessary, make sure that the first time step of every
|
||||
# voiced segments is sin(pi) or cos(0)
|
||||
# This is used for pulse-train generation
|
||||
|
||||
# identify the last time step in unvoiced segments
|
||||
uv = self._f02uv(f0_values)
|
||||
uv_1 = torch.roll(uv, shifts=-1, dims=1)
|
||||
uv_1[:, -1, :] = 1
|
||||
u_loc = (uv < 1) * (uv_1 > 0)
|
||||
|
||||
# get the instantanouse phase
|
||||
tmp_cumsum = torch.cumsum(rad_values, dim=1)
|
||||
# different batch needs to be processed differently
|
||||
for idx in range(f0_values.shape[0]):
|
||||
temp_sum = tmp_cumsum[idx, u_loc[idx, :, 0], :]
|
||||
temp_sum[1:, :] = temp_sum[1:, :] - temp_sum[0:-1, :]
|
||||
# stores the accumulation of i.phase within
|
||||
# each voiced segments
|
||||
tmp_cumsum[idx, :, :] = 0
|
||||
tmp_cumsum[idx, u_loc[idx, :, 0], :] = temp_sum
|
||||
|
||||
# rad_values - tmp_cumsum: remove the accumulation of i.phase
|
||||
# within the previous voiced segment.
|
||||
i_phase = torch.cumsum(rad_values - tmp_cumsum, dim=1)
|
||||
|
||||
# get the sines
|
||||
sines = torch.cos(i_phase * 2 * np.pi)
|
||||
return sines
|
||||
|
||||
def forward(self, f0):
|
||||
""" sine_tensor, uv = forward(f0)
|
||||
input F0: tensor(batchsize=1, length, dim=1)
|
||||
f0 for unvoiced steps should be 0
|
||||
output sine_tensor: tensor(batchsize=1, length, dim)
|
||||
output uv: tensor(batchsize=1, length, 1)
|
||||
"""
|
||||
with torch.no_grad():
|
||||
f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim,
|
||||
device=f0.device)
|
||||
# fundamental component
|
||||
f0_buf[:, :, 0] = f0[:, :, 0]
|
||||
for idx in np.arange(self.harmonic_num):
|
||||
# idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
|
||||
f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (idx + 2)
|
||||
|
||||
# generate sine waveforms
|
||||
sine_waves = self._f02sine(f0_buf) * self.sine_amp
|
||||
|
||||
# generate uv signal
|
||||
# uv = torch.ones(f0.shape)
|
||||
# uv = uv * (f0 > self.voiced_threshold)
|
||||
uv = self._f02uv(f0)
|
||||
|
||||
# noise: for unvoiced should be similar to sine_amp
|
||||
# std = self.sine_amp/3 -> max value ~ self.sine_amp
|
||||
# . for voiced regions is self.noise_std
|
||||
noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
|
||||
noise = noise_amp * torch.randn_like(sine_waves)
|
||||
|
||||
# first: set the unvoiced part to 0 by uv
|
||||
# then: additive noise
|
||||
sine_waves = sine_waves * uv + noise
|
||||
return sine_waves, uv, noise
|
||||
|
||||
|
||||
class PulseGen(torch.nn.Module):
|
||||
""" Definition of Pulse train generator
|
||||
|
||||
There are many ways to implement pulse generator.
|
||||
Here, PulseGen is based on SinGen. For a perfect
|
||||
"""
|
||||
def __init__(self, samp_rate, pulse_amp = 0.1,
|
||||
noise_std = 0.003, voiced_threshold = 0):
|
||||
super(PulseGen, self).__init__()
|
||||
self.pulse_amp = pulse_amp
|
||||
self.sampling_rate = samp_rate
|
||||
self.voiced_threshold = voiced_threshold
|
||||
self.noise_std = noise_std
|
||||
self.l_sinegen = SineGen(self.sampling_rate, harmonic_num=0, \
|
||||
sine_amp=self.pulse_amp, noise_std=0, \
|
||||
voiced_threshold=self.voiced_threshold, \
|
||||
flag_for_pulse=True)
|
||||
|
||||
def forward(self, f0):
|
||||
""" Pulse train generator
|
||||
pulse_train, uv = forward(f0)
|
||||
input F0: tensor(batchsize=1, length, dim=1)
|
||||
f0 for unvoiced steps should be 0
|
||||
output pulse_train: tensor(batchsize=1, length, dim)
|
||||
output uv: tensor(batchsize=1, length, 1)
|
||||
|
||||
Note: self.l_sine doesn't make sure that the initial phase of
|
||||
a voiced segment is np.pi, the first pulse in a voiced segment
|
||||
may not be at the first time step within a voiced segment
|
||||
"""
|
||||
with torch.no_grad():
|
||||
sine_wav, uv, noise = self.l_sinegen(f0)
|
||||
|
||||
# sine without additive noise
|
||||
pure_sine = sine_wav - noise
|
||||
|
||||
# step t corresponds to a pulse if
|
||||
# sine[t] > sine[t+1] & sine[t] > sine[t-1]
|
||||
# & sine[t-1], sine[t+1], and sine[t] are voiced
|
||||
# or
|
||||
# sine[t] is voiced, sine[t-1] is unvoiced
|
||||
# we use torch.roll to simulate sine[t+1] and sine[t-1]
|
||||
sine_1 = torch.roll(pure_sine, shifts=1, dims=1)
|
||||
uv_1 = torch.roll(uv, shifts=1, dims=1)
|
||||
uv_1[:, 0, :] = 0
|
||||
sine_2 = torch.roll(pure_sine, shifts=-1, dims=1)
|
||||
uv_2 = torch.roll(uv, shifts=-1, dims=1)
|
||||
uv_2[:, -1, :] = 0
|
||||
|
||||
loc = (pure_sine > sine_1) * (pure_sine > sine_2) \
|
||||
* (uv_1 > 0) * (uv_2 > 0) * (uv > 0) \
|
||||
+ (uv_1 < 1) * (uv > 0)
|
||||
|
||||
# pulse train without noise
|
||||
pulse_train = pure_sine * loc
|
||||
|
||||
# additive noise to pulse train
|
||||
# note that noise from sinegen is zero in voiced regions
|
||||
pulse_noise = torch.randn_like(pure_sine) * self.noise_std
|
||||
|
||||
# with additive noise on pulse, and unvoiced regions
|
||||
pulse_train += pulse_noise * loc + pulse_noise * (1 - uv)
|
||||
return pulse_train, sine_wav, uv, pulse_noise
|
||||
|
||||
|
||||
class SignalsConv1d(torch.nn.Module):
|
||||
""" Filtering input signal with time invariant filter
|
||||
Note: FIRFilter conducted filtering given fixed FIR weight
|
||||
SignalsConv1d convolves two signals
|
||||
Note: this is based on torch.nn.functional.conv1d
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(SignalsConv1d, self).__init__()
|
||||
|
||||
def forward(self, signal, system_ir):
|
||||
""" output = forward(signal, system_ir)
|
||||
|
||||
signal: (batchsize, length1, dim)
|
||||
system_ir: (length2, dim)
|
||||
|
||||
output: (batchsize, length1, dim)
|
||||
"""
|
||||
if signal.shape[-1] != system_ir.shape[-1]:
|
||||
print("Error: SignalsConv1d expects shape:")
|
||||
print("signal (batchsize, length1, dim)")
|
||||
print("system_id (batchsize, length2, dim)")
|
||||
print("But received signal: {:s}".format(str(signal.shape)))
|
||||
print(" system_ir: {:s}".format(str(system_ir.shape)))
|
||||
sys.exit(1)
|
||||
padding_length = system_ir.shape[0] - 1
|
||||
groups = signal.shape[-1]
|
||||
|
||||
# pad signal on the left
|
||||
signal_pad = torch_nn_func.pad(signal.permute(0, 2, 1), \
|
||||
(padding_length, 0))
|
||||
# prepare system impulse response as (dim, 1, length2)
|
||||
# also flip the impulse response
|
||||
ir = torch.flip(system_ir.unsqueeze(1).permute(2, 1, 0), \
|
||||
dims=[2])
|
||||
# convolute
|
||||
output = torch_nn_func.conv1d(signal_pad, ir, groups=groups)
|
||||
return output.permute(0, 2, 1)
|
||||
|
||||
|
||||
class CyclicNoiseGen_v1(torch.nn.Module):
|
||||
""" CyclicnoiseGen_v1
|
||||
Cyclic noise with a single parameter of beta.
|
||||
Pytorch v1 implementation assumes f_t is also fixed
|
||||
"""
|
||||
|
||||
def __init__(self, samp_rate,
|
||||
noise_std=0.003, voiced_threshold=0):
|
||||
super(CyclicNoiseGen_v1, self).__init__()
|
||||
self.samp_rate = samp_rate
|
||||
self.noise_std = noise_std
|
||||
self.voiced_threshold = voiced_threshold
|
||||
|
||||
self.l_pulse = PulseGen(samp_rate, pulse_amp=1.0,
|
||||
noise_std=noise_std,
|
||||
voiced_threshold=voiced_threshold)
|
||||
self.l_conv = SignalsConv1d()
|
||||
|
||||
def noise_decay(self, beta, f0mean):
|
||||
""" decayed_noise = noise_decay(beta, f0mean)
|
||||
decayed_noise = n[t]exp(-t * f_mean / beta / samp_rate)
|
||||
|
||||
beta: (dim=1) or (batchsize=1, 1, dim=1)
|
||||
f0mean (batchsize=1, 1, dim=1)
|
||||
|
||||
decayed_noise (batchsize=1, length, dim=1)
|
||||
"""
|
||||
with torch.no_grad():
|
||||
# exp(-1.0 n / T) < 0.01 => n > -log(0.01)*T = 4.60*T
|
||||
# truncate the noise when decayed by -40 dB
|
||||
length = 4.6 * self.samp_rate / f0mean
|
||||
length = length.int()
|
||||
time_idx = torch.arange(0, length, device=beta.device)
|
||||
time_idx = time_idx.unsqueeze(0).unsqueeze(2)
|
||||
time_idx = time_idx.repeat(beta.shape[0], 1, beta.shape[2])
|
||||
|
||||
noise = torch.randn(time_idx.shape, device=beta.device)
|
||||
|
||||
# due to Pytorch implementation, use f0_mean as the f0 factor
|
||||
decay = torch.exp(-time_idx * f0mean / beta / self.samp_rate)
|
||||
return noise * self.noise_std * decay
|
||||
|
||||
def forward(self, f0s, beta):
|
||||
""" Producde cyclic-noise
|
||||
"""
|
||||
# pulse train
|
||||
pulse_train, sine_wav, uv, noise = self.l_pulse(f0s)
|
||||
pure_pulse = pulse_train - noise
|
||||
|
||||
# decayed_noise (length, dim=1)
|
||||
if (uv < 1).all():
|
||||
# all unvoiced
|
||||
cyc_noise = torch.zeros_like(sine_wav)
|
||||
else:
|
||||
f0mean = f0s[uv > 0].mean()
|
||||
|
||||
decayed_noise = self.noise_decay(beta, f0mean)[0, :, :]
|
||||
# convolute
|
||||
cyc_noise = self.l_conv(pure_pulse, decayed_noise)
|
||||
|
||||
# add noise in invoiced segments
|
||||
cyc_noise = cyc_noise + noise * (1.0 - uv)
|
||||
return cyc_noise, pulse_train, sine_wav, uv, noise
|
||||
|
||||
|
||||
class SineGen(torch.nn.Module):
|
||||
""" Definition of sine generator
|
||||
SineGen(samp_rate, harmonic_num = 0,
|
||||
sine_amp = 0.1, noise_std = 0.003,
|
||||
voiced_threshold = 0,
|
||||
flag_for_pulse=False)
|
||||
|
||||
samp_rate: sampling rate in Hz
|
||||
harmonic_num: number of harmonic overtones (default 0)
|
||||
sine_amp: amplitude of sine-wavefrom (default 0.1)
|
||||
noise_std: std of Gaussian noise (default 0.003)
|
||||
voiced_thoreshold: F0 threshold for U/V classification (default 0)
|
||||
flag_for_pulse: this SinGen is used inside PulseGen (default False)
|
||||
|
||||
Note: when flag_for_pulse is True, the first time step of a voiced
|
||||
segment is always sin(np.pi) or cos(0)
|
||||
"""
|
||||
|
||||
def __init__(self, samp_rate, harmonic_num=0,
|
||||
sine_amp=0.1, noise_std=0.003,
|
||||
voiced_threshold=0,
|
||||
flag_for_pulse=False):
|
||||
super(SineGen, self).__init__()
|
||||
self.sine_amp = sine_amp
|
||||
self.noise_std = noise_std
|
||||
self.harmonic_num = harmonic_num
|
||||
self.dim = self.harmonic_num + 1
|
||||
self.sampling_rate = samp_rate
|
||||
self.voiced_threshold = voiced_threshold
|
||||
self.flag_for_pulse = flag_for_pulse
|
||||
|
||||
def _f02uv(self, f0):
|
||||
# generate uv signal
|
||||
uv = torch.ones_like(f0)
|
||||
uv = uv * (f0 > self.voiced_threshold)
|
||||
return uv
|
||||
|
||||
def _f02sine(self, f0_values):
|
||||
""" f0_values: (batchsize, length, dim)
|
||||
where dim indicates fundamental tone and overtones
|
||||
"""
|
||||
# convert to F0 in rad. The interger part n can be ignored
|
||||
# because 2 * np.pi * n doesn't affect phase
|
||||
rad_values = (f0_values / self.sampling_rate) % 1
|
||||
|
||||
# initial phase noise (no noise for fundamental component)
|
||||
rand_ini = torch.rand(f0_values.shape[0], f0_values.shape[2], \
|
||||
device=f0_values.device)
|
||||
rand_ini[:, 0] = 0
|
||||
rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
|
||||
|
||||
# instantanouse phase sine[t] = sin(2*pi \sum_i=1 ^{t} rad)
|
||||
if not self.flag_for_pulse:
|
||||
# for normal case
|
||||
|
||||
# To prevent torch.cumsum numerical overflow,
|
||||
# it is necessary to add -1 whenever \sum_k=1^n rad_value_k > 1.
|
||||
# Buffer tmp_over_one_idx indicates the time step to add -1.
|
||||
# This will not change F0 of sine because (x-1) * 2*pi = x * 2*pi
|
||||
tmp_over_one = torch.cumsum(rad_values, 1) % 1
|
||||
tmp_over_one_idx = (tmp_over_one[:, 1:, :] -
|
||||
tmp_over_one[:, :-1, :]) < 0
|
||||
cumsum_shift = torch.zeros_like(rad_values)
|
||||
cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
|
||||
|
||||
sines = torch.sin(torch.cumsum(rad_values + cumsum_shift, dim=1)
|
||||
* 2 * np.pi)
|
||||
else:
|
||||
# If necessary, make sure that the first time step of every
|
||||
# voiced segments is sin(pi) or cos(0)
|
||||
# This is used for pulse-train generation
|
||||
|
||||
# identify the last time step in unvoiced segments
|
||||
uv = self._f02uv(f0_values)
|
||||
uv_1 = torch.roll(uv, shifts=-1, dims=1)
|
||||
uv_1[:, -1, :] = 1
|
||||
u_loc = (uv < 1) * (uv_1 > 0)
|
||||
|
||||
# get the instantanouse phase
|
||||
tmp_cumsum = torch.cumsum(rad_values, dim=1)
|
||||
# different batch needs to be processed differently
|
||||
for idx in range(f0_values.shape[0]):
|
||||
temp_sum = tmp_cumsum[idx, u_loc[idx, :, 0], :]
|
||||
temp_sum[1:, :] = temp_sum[1:, :] - temp_sum[0:-1, :]
|
||||
# stores the accumulation of i.phase within
|
||||
# each voiced segments
|
||||
tmp_cumsum[idx, :, :] = 0
|
||||
tmp_cumsum[idx, u_loc[idx, :, 0], :] = temp_sum
|
||||
|
||||
# rad_values - tmp_cumsum: remove the accumulation of i.phase
|
||||
# within the previous voiced segment.
|
||||
i_phase = torch.cumsum(rad_values - tmp_cumsum, dim=1)
|
||||
|
||||
# get the sines
|
||||
sines = torch.cos(i_phase * 2 * np.pi)
|
||||
return sines
|
||||
|
||||
def forward(self, f0):
|
||||
""" sine_tensor, uv = forward(f0)
|
||||
input F0: tensor(batchsize=1, length, dim=1)
|
||||
f0 for unvoiced steps should be 0
|
||||
output sine_tensor: tensor(batchsize=1, length, dim)
|
||||
output uv: tensor(batchsize=1, length, 1)
|
||||
"""
|
||||
with torch.no_grad():
|
||||
f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim, \
|
||||
device=f0.device)
|
||||
# fundamental component
|
||||
f0_buf[:, :, 0] = f0[:, :, 0]
|
||||
for idx in np.arange(self.harmonic_num):
|
||||
# idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
|
||||
f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (idx + 2)
|
||||
|
||||
# generate sine waveforms
|
||||
sine_waves = self._f02sine(f0_buf) * self.sine_amp
|
||||
|
||||
# generate uv signal
|
||||
# uv = torch.ones(f0.shape)
|
||||
# uv = uv * (f0 > self.voiced_threshold)
|
||||
uv = self._f02uv(f0)
|
||||
|
||||
# noise: for unvoiced should be similar to sine_amp
|
||||
# std = self.sine_amp/3 -> max value ~ self.sine_amp
|
||||
# . for voiced regions is self.noise_std
|
||||
noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
|
||||
noise = noise_amp * torch.randn_like(sine_waves)
|
||||
|
||||
# first: set the unvoiced part to 0 by uv
|
||||
# then: additive noise
|
||||
sine_waves = sine_waves * uv + noise
|
||||
return sine_waves, uv, noise
|
||||
|
||||
|
||||
class SourceModuleCycNoise_v1(torch.nn.Module):
|
||||
""" SourceModuleCycNoise_v1
|
||||
SourceModule(sampling_rate, noise_std=0.003, voiced_threshod=0)
|
||||
sampling_rate: sampling_rate in Hz
|
||||
|
||||
noise_std: std of Gaussian noise (default: 0.003)
|
||||
voiced_threshold: threshold to set U/V given F0 (default: 0)
|
||||
|
||||
cyc, noise, uv = SourceModuleCycNoise_v1(F0_upsampled, beta)
|
||||
F0_upsampled (batchsize, length, 1)
|
||||
beta (1)
|
||||
cyc (batchsize, length, 1)
|
||||
noise (batchsize, length, 1)
|
||||
uv (batchsize, length, 1)
|
||||
"""
|
||||
|
||||
def __init__(self, sampling_rate, noise_std=0.003, voiced_threshod=0):
|
||||
super(SourceModuleCycNoise_v1, self).__init__()
|
||||
self.sampling_rate = sampling_rate
|
||||
self.noise_std = noise_std
|
||||
self.l_cyc_gen = CyclicNoiseGen_v1(sampling_rate, noise_std,
|
||||
voiced_threshod)
|
||||
|
||||
def forward(self, f0_upsamped, beta):
|
||||
"""
|
||||
cyc, noise, uv = SourceModuleCycNoise_v1(F0, beta)
|
||||
F0_upsampled (batchsize, length, 1)
|
||||
beta (1)
|
||||
cyc (batchsize, length, 1)
|
||||
noise (batchsize, length, 1)
|
||||
uv (batchsize, length, 1)
|
||||
"""
|
||||
# source for harmonic branch
|
||||
cyc, pulse, sine, uv, add_noi = self.l_cyc_gen(f0_upsamped, beta)
|
||||
|
||||
# source for noise branch, in the same shape as uv
|
||||
noise = torch.randn_like(uv) * self.noise_std / 3
|
||||
return cyc, noise, uv
|
||||
|
||||
|
||||
class SourceModuleHnNSF(torch.nn.Module):
|
||||
""" SourceModule for hn-nsf
|
||||
SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
|
||||
add_noise_std=0.003, voiced_threshod=0)
|
||||
sampling_rate: sampling_rate in Hz
|
||||
harmonic_num: number of harmonic above F0 (default: 0)
|
||||
sine_amp: amplitude of sine source signal (default: 0.1)
|
||||
add_noise_std: std of additive Gaussian noise (default: 0.003)
|
||||
note that amplitude of noise in unvoiced is decided
|
||||
by sine_amp
|
||||
voiced_threshold: threhold to set U/V given F0 (default: 0)
|
||||
|
||||
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
||||
F0_sampled (batchsize, length, 1)
|
||||
Sine_source (batchsize, length, 1)
|
||||
noise_source (batchsize, length 1)
|
||||
uv (batchsize, length, 1)
|
||||
"""
|
||||
|
||||
def __init__(self, sampling_rate, harmonic_num=0, sine_amp=0.1,
|
||||
add_noise_std=0.003, voiced_threshod=0):
|
||||
super(SourceModuleHnNSF, self).__init__()
|
||||
|
||||
self.sine_amp = sine_amp
|
||||
self.noise_std = add_noise_std
|
||||
|
||||
# to produce sine waveforms
|
||||
self.l_sin_gen = SineGen(sampling_rate, harmonic_num,
|
||||
sine_amp, add_noise_std, voiced_threshod)
|
||||
|
||||
# to merge source harmonics into a single excitation
|
||||
self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
|
||||
self.l_tanh = torch.nn.Tanh()
|
||||
|
||||
def forward(self, x):
|
||||
"""
|
||||
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
||||
F0_sampled (batchsize, length, 1)
|
||||
Sine_source (batchsize, length, 1)
|
||||
noise_source (batchsize, length 1)
|
||||
"""
|
||||
# source for harmonic branch
|
||||
sine_wavs, uv, _ = self.l_sin_gen(x)
|
||||
sine_merge = self.l_tanh(self.l_linear(sine_wavs))
|
||||
|
||||
# source for noise branch, in the same shape as uv
|
||||
noise = torch.randn_like(uv) * self.sine_amp / 3
|
||||
return sine_merge, noise, uv
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
source = SourceModuleCycNoise_v1(24000)
|
||||
x = torch.randn(16, 25600, 1)
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
from torch.optim import * # NOQA
|
||||
from .radam import * # NOQA
|
||||
@@ -0,0 +1,91 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""RAdam optimizer.
|
||||
|
||||
This code is drived from https://github.com/LiyuanLucasLiu/RAdam.
|
||||
"""
|
||||
|
||||
import math
|
||||
import torch
|
||||
|
||||
from torch.optim.optimizer import Optimizer
|
||||
|
||||
|
||||
class RAdam(Optimizer):
|
||||
"""Rectified Adam optimizer."""
|
||||
|
||||
def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0):
|
||||
"""Initilize RAdam optimizer."""
|
||||
defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)
|
||||
self.buffer = [[None, None, None] for ind in range(10)]
|
||||
super(RAdam, self).__init__(params, defaults)
|
||||
|
||||
def __setstate__(self, state):
|
||||
"""Set state."""
|
||||
super(RAdam, self).__setstate__(state)
|
||||
|
||||
def step(self, closure=None):
|
||||
"""Run one step."""
|
||||
loss = None
|
||||
if closure is not None:
|
||||
loss = closure()
|
||||
|
||||
for group in self.param_groups:
|
||||
|
||||
for p in group['params']:
|
||||
if p.grad is None:
|
||||
continue
|
||||
grad = p.grad.data.float()
|
||||
if grad.is_sparse:
|
||||
raise RuntimeError('RAdam does not support sparse gradients')
|
||||
|
||||
p_data_fp32 = p.data.float()
|
||||
|
||||
state = self.state[p]
|
||||
|
||||
if len(state) == 0:
|
||||
state['step'] = 0
|
||||
state['exp_avg'] = torch.zeros_like(p_data_fp32)
|
||||
state['exp_avg_sq'] = torch.zeros_like(p_data_fp32)
|
||||
else:
|
||||
state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32)
|
||||
state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32)
|
||||
|
||||
exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
|
||||
beta1, beta2 = group['betas']
|
||||
|
||||
exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad)
|
||||
exp_avg.mul_(beta1).add_(1 - beta1, grad)
|
||||
|
||||
state['step'] += 1
|
||||
buffered = self.buffer[int(state['step'] % 10)]
|
||||
if state['step'] == buffered[0]:
|
||||
N_sma, step_size = buffered[1], buffered[2]
|
||||
else:
|
||||
buffered[0] = state['step']
|
||||
beta2_t = beta2 ** state['step']
|
||||
N_sma_max = 2 / (1 - beta2) - 1
|
||||
N_sma = N_sma_max - 2 * state['step'] * beta2_t / (1 - beta2_t)
|
||||
buffered[1] = N_sma
|
||||
|
||||
# more conservative since it's an approximated value
|
||||
if N_sma >= 5:
|
||||
step_size = math.sqrt(
|
||||
(1 - beta2_t) * (N_sma - 4) / (N_sma_max - 4) * (N_sma - 2) / N_sma * N_sma_max / (N_sma_max - 2)) / (1 - beta1 ** state['step']) # NOQA
|
||||
else:
|
||||
step_size = 1.0 / (1 - beta1 ** state['step'])
|
||||
buffered[2] = step_size
|
||||
|
||||
if group['weight_decay'] != 0:
|
||||
p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32)
|
||||
|
||||
# more conservative since it's an approximated value
|
||||
if N_sma >= 5:
|
||||
denom = exp_avg_sq.sqrt().add_(group['eps'])
|
||||
p_data_fp32.addcdiv_(-step_size * group['lr'], exp_avg, denom)
|
||||
else:
|
||||
p_data_fp32.add_(-step_size * group['lr'], exp_avg)
|
||||
|
||||
p.data.copy_(p_data_fp32)
|
||||
|
||||
return loss
|
||||
@@ -0,0 +1,100 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2019 Tomoki Hayashi
|
||||
# MIT License (https://opensource.org/licenses/MIT)
|
||||
|
||||
"""STFT-based Loss modules."""
|
||||
import librosa
|
||||
import torch
|
||||
|
||||
from modules.parallel_wavegan.losses import LogSTFTMagnitudeLoss, SpectralConvergengeLoss, stft
|
||||
|
||||
|
||||
class STFTLoss(torch.nn.Module):
|
||||
"""STFT loss module."""
|
||||
|
||||
def __init__(self, fft_size=1024, shift_size=120, win_length=600, window="hann_window",
|
||||
use_mel_loss=False):
|
||||
"""Initialize STFT loss module."""
|
||||
super(STFTLoss, self).__init__()
|
||||
self.fft_size = fft_size
|
||||
self.shift_size = shift_size
|
||||
self.win_length = win_length
|
||||
self.window = getattr(torch, window)(win_length)
|
||||
self.spectral_convergenge_loss = SpectralConvergengeLoss()
|
||||
self.log_stft_magnitude_loss = LogSTFTMagnitudeLoss()
|
||||
self.use_mel_loss = use_mel_loss
|
||||
self.mel_basis = None
|
||||
|
||||
def forward(self, x, y):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Predicted signal (B, T).
|
||||
y (Tensor): Groundtruth signal (B, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Spectral convergence loss value.
|
||||
Tensor: Log STFT magnitude loss value.
|
||||
|
||||
"""
|
||||
x_mag = stft(x, self.fft_size, self.shift_size, self.win_length, self.window)
|
||||
y_mag = stft(y, self.fft_size, self.shift_size, self.win_length, self.window)
|
||||
if self.use_mel_loss:
|
||||
if self.mel_basis is None:
|
||||
self.mel_basis = torch.from_numpy(librosa.filters.mel(22050, self.fft_size, 80)).cuda().T
|
||||
x_mag = x_mag @ self.mel_basis
|
||||
y_mag = y_mag @ self.mel_basis
|
||||
|
||||
sc_loss = self.spectral_convergenge_loss(x_mag, y_mag)
|
||||
mag_loss = self.log_stft_magnitude_loss(x_mag, y_mag)
|
||||
|
||||
return sc_loss, mag_loss
|
||||
|
||||
|
||||
class MultiResolutionSTFTLoss(torch.nn.Module):
|
||||
"""Multi resolution STFT loss module."""
|
||||
|
||||
def __init__(self,
|
||||
fft_sizes=[1024, 2048, 512],
|
||||
hop_sizes=[120, 240, 50],
|
||||
win_lengths=[600, 1200, 240],
|
||||
window="hann_window",
|
||||
use_mel_loss=False):
|
||||
"""Initialize Multi resolution STFT loss module.
|
||||
|
||||
Args:
|
||||
fft_sizes (list): List of FFT sizes.
|
||||
hop_sizes (list): List of hop sizes.
|
||||
win_lengths (list): List of window lengths.
|
||||
window (str): Window function type.
|
||||
|
||||
"""
|
||||
super(MultiResolutionSTFTLoss, self).__init__()
|
||||
assert len(fft_sizes) == len(hop_sizes) == len(win_lengths)
|
||||
self.stft_losses = torch.nn.ModuleList()
|
||||
for fs, ss, wl in zip(fft_sizes, hop_sizes, win_lengths):
|
||||
self.stft_losses += [STFTLoss(fs, ss, wl, window, use_mel_loss)]
|
||||
|
||||
def forward(self, x, y):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Predicted signal (B, T).
|
||||
y (Tensor): Groundtruth signal (B, T).
|
||||
|
||||
Returns:
|
||||
Tensor: Multi resolution spectral convergence loss value.
|
||||
Tensor: Multi resolution log STFT magnitude loss value.
|
||||
|
||||
"""
|
||||
sc_loss = 0.0
|
||||
mag_loss = 0.0
|
||||
for f in self.stft_losses:
|
||||
sc_l, mag_l = f(x, y)
|
||||
sc_loss += sc_l
|
||||
mag_loss += mag_l
|
||||
sc_loss /= len(self.stft_losses)
|
||||
mag_loss /= len(self.stft_losses)
|
||||
|
||||
return sc_loss, mag_loss
|
||||
@@ -0,0 +1 @@
|
||||
from .utils import * # NOQA
|
||||
@@ -0,0 +1,169 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2019 Tomoki Hayashi
|
||||
# MIT License (https://opensource.org/licenses/MIT)
|
||||
|
||||
"""Utility functions."""
|
||||
|
||||
import fnmatch
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
import h5py
|
||||
import numpy as np
|
||||
|
||||
|
||||
def find_files(root_dir, query="*.wav", include_root_dir=True):
|
||||
"""Find files recursively.
|
||||
|
||||
Args:
|
||||
root_dir (str): Root root_dir to find.
|
||||
query (str): Query to find.
|
||||
include_root_dir (bool): If False, root_dir name is not included.
|
||||
|
||||
Returns:
|
||||
list: List of found filenames.
|
||||
|
||||
"""
|
||||
files = []
|
||||
for root, dirnames, filenames in os.walk(root_dir, followlinks=True):
|
||||
for filename in fnmatch.filter(filenames, query):
|
||||
files.append(os.path.join(root, filename))
|
||||
if not include_root_dir:
|
||||
files = [file_.replace(root_dir + "/", "") for file_ in files]
|
||||
|
||||
return files
|
||||
|
||||
|
||||
def read_hdf5(hdf5_name, hdf5_path):
|
||||
"""Read hdf5 dataset.
|
||||
|
||||
Args:
|
||||
hdf5_name (str): Filename of hdf5 file.
|
||||
hdf5_path (str): Dataset name in hdf5 file.
|
||||
|
||||
Return:
|
||||
any: Dataset values.
|
||||
|
||||
"""
|
||||
if not os.path.exists(hdf5_name):
|
||||
logging.error(f"There is no such a hdf5 file ({hdf5_name}).")
|
||||
sys.exit(1)
|
||||
|
||||
hdf5_file = h5py.File(hdf5_name, "r")
|
||||
|
||||
if hdf5_path not in hdf5_file:
|
||||
logging.error(f"There is no such a data in hdf5 file. ({hdf5_path})")
|
||||
sys.exit(1)
|
||||
|
||||
hdf5_data = hdf5_file[hdf5_path][()]
|
||||
hdf5_file.close()
|
||||
|
||||
return hdf5_data
|
||||
|
||||
|
||||
def write_hdf5(hdf5_name, hdf5_path, write_data, is_overwrite=True):
|
||||
"""Write dataset to hdf5.
|
||||
|
||||
Args:
|
||||
hdf5_name (str): Hdf5 dataset filename.
|
||||
hdf5_path (str): Dataset path in hdf5.
|
||||
write_data (ndarray): Data to write.
|
||||
is_overwrite (bool): Whether to overwrite dataset.
|
||||
|
||||
"""
|
||||
# convert to numpy array
|
||||
write_data = np.array(write_data)
|
||||
|
||||
# check folder existence
|
||||
folder_name, _ = os.path.split(hdf5_name)
|
||||
if not os.path.exists(folder_name) and len(folder_name) != 0:
|
||||
os.makedirs(folder_name)
|
||||
|
||||
# check hdf5 existence
|
||||
if os.path.exists(hdf5_name):
|
||||
# if already exists, open with r+ mode
|
||||
hdf5_file = h5py.File(hdf5_name, "r+")
|
||||
# check dataset existence
|
||||
if hdf5_path in hdf5_file:
|
||||
if is_overwrite:
|
||||
logging.warning("Dataset in hdf5 file already exists. "
|
||||
"recreate dataset in hdf5.")
|
||||
hdf5_file.__delitem__(hdf5_path)
|
||||
else:
|
||||
logging.error("Dataset in hdf5 file already exists. "
|
||||
"if you want to overwrite, please set is_overwrite = True.")
|
||||
hdf5_file.close()
|
||||
sys.exit(1)
|
||||
else:
|
||||
# if not exists, open with w mode
|
||||
hdf5_file = h5py.File(hdf5_name, "w")
|
||||
|
||||
# write data to hdf5
|
||||
hdf5_file.create_dataset(hdf5_path, data=write_data)
|
||||
hdf5_file.flush()
|
||||
hdf5_file.close()
|
||||
|
||||
|
||||
class HDF5ScpLoader(object):
|
||||
"""Loader class for a fests.scp file of hdf5 file.
|
||||
|
||||
Examples:
|
||||
key1 /some/path/a.h5:feats
|
||||
key2 /some/path/b.h5:feats
|
||||
key3 /some/path/c.h5:feats
|
||||
key4 /some/path/d.h5:feats
|
||||
...
|
||||
>>> loader = HDF5ScpLoader("hdf5.scp")
|
||||
>>> array = loader["key1"]
|
||||
|
||||
key1 /some/path/a.h5
|
||||
key2 /some/path/b.h5
|
||||
key3 /some/path/c.h5
|
||||
key4 /some/path/d.h5
|
||||
...
|
||||
>>> loader = HDF5ScpLoader("hdf5.scp", "feats")
|
||||
>>> array = loader["key1"]
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, feats_scp, default_hdf5_path="feats"):
|
||||
"""Initialize HDF5 scp loader.
|
||||
|
||||
Args:
|
||||
feats_scp (str): Kaldi-style feats.scp file with hdf5 format.
|
||||
default_hdf5_path (str): Path in hdf5 file. If the scp contain the info, not used.
|
||||
|
||||
"""
|
||||
self.default_hdf5_path = default_hdf5_path
|
||||
with open(feats_scp) as f:
|
||||
lines = [line.replace("\n", "") for line in f.readlines()]
|
||||
self.data = {}
|
||||
for line in lines:
|
||||
key, value = line.split()
|
||||
self.data[key] = value
|
||||
|
||||
def get_path(self, key):
|
||||
"""Get hdf5 file path for a given key."""
|
||||
return self.data[key]
|
||||
|
||||
def __getitem__(self, key):
|
||||
"""Get ndarray for a given key."""
|
||||
p = self.data[key]
|
||||
if ":" in p:
|
||||
return read_hdf5(*p.split(":"))
|
||||
else:
|
||||
return read_hdf5(p, self.default_hdf5_path)
|
||||
|
||||
def __len__(self):
|
||||
"""Return the length of the scp file."""
|
||||
return len(self.data)
|
||||
|
||||
def __iter__(self):
|
||||
"""Return the iterator of the scp file."""
|
||||
return iter(self.data)
|
||||
|
||||
def keys(self):
|
||||
"""Return the keys of the scp file."""
|
||||
return self.data.keys()
|
||||
@@ -0,0 +1,30 @@
|
||||
matplotlib
|
||||
librosa==0.8.0
|
||||
tqdm
|
||||
pandas
|
||||
numba==0.53.1
|
||||
numpy==1.19.2
|
||||
scipy==1.5.4
|
||||
PyYAML==5.3.1
|
||||
tensorboardX
|
||||
pyloudnorm
|
||||
setuptools>=41.0.0
|
||||
g2p_en
|
||||
resemblyzer
|
||||
webrtcvad
|
||||
tensorboard==2.6.0
|
||||
scikit-learn==0.24.1
|
||||
scikit-image==0.16.2
|
||||
textgrid
|
||||
jiwer
|
||||
pycwt
|
||||
PyWavelets
|
||||
praat-parselmouth==0.3.3
|
||||
jieba
|
||||
einops
|
||||
chardet
|
||||
pretty-midi==0.2.9
|
||||
pytorch-lightning==0.7.1
|
||||
h5py==3.1.0
|
||||
pypinyin==0.39.0
|
||||
g2pM==0.1.2.5
|
||||
@@ -0,0 +1,118 @@
|
||||
absl-py==0.11.0
|
||||
alignment==1.0.10
|
||||
altgraph==0.17
|
||||
appdirs==1.4.4
|
||||
async-timeout==3.0.1
|
||||
audioread==2.1.9
|
||||
backcall==0.2.0
|
||||
blinker==1.4
|
||||
brotlipy==0.7.0
|
||||
cachetools==4.2.0
|
||||
certifi==2020.12.5
|
||||
cffi==1.14.4
|
||||
chardet==4.0.0
|
||||
click==7.1.2
|
||||
cycler==0.10.0
|
||||
Cython==0.29.21
|
||||
cytoolz==0.11.0
|
||||
decorator==4.4.2
|
||||
Distance==0.1.3
|
||||
einops==0.3.0
|
||||
et-xmlfile==1.0.1
|
||||
fsspec==0.8.4
|
||||
future==0.18.2
|
||||
g2p-en==2.1.0
|
||||
g2pM==0.1.2.5
|
||||
google-auth==1.24.0
|
||||
google-auth-oauthlib==0.4.2
|
||||
grpcio==1.34.0
|
||||
h5py==3.1.0
|
||||
horology==1.1.0
|
||||
httplib2==0.18.1
|
||||
idna==2.10
|
||||
imageio==2.9.0
|
||||
inflect==5.0.2
|
||||
ipdb==0.13.4
|
||||
ipython==7.19.0
|
||||
ipython-genutils==0.2.0
|
||||
jdcal==1.4.1
|
||||
jedi==0.17.2
|
||||
jieba==0.42.1
|
||||
jiwer==2.2.0
|
||||
joblib==1.0.0
|
||||
kiwisolver==1.3.1
|
||||
librosa==0.8.0
|
||||
llvmlite==0.31.0
|
||||
Markdown==3.3.3
|
||||
matplotlib==3.3.3
|
||||
miditoolkit==0.1.7
|
||||
mido==1.2.9
|
||||
music21==5.7.2
|
||||
networkx==2.5
|
||||
nltk==3.5
|
||||
numba==0.48.0
|
||||
numpy==1.19.4
|
||||
oauth2client==4.1.3
|
||||
oauthlib==3.1.0
|
||||
olefile==0.46
|
||||
packaging==20.7
|
||||
pandas==1.2.0
|
||||
parso==0.7.1
|
||||
patsy==0.5.1
|
||||
pexpect==4.8.0
|
||||
pickleshare==0.7.5
|
||||
Pillow==8.0.1
|
||||
pooch==1.3.0
|
||||
praat-parselmouth==0.3.3
|
||||
prompt-toolkit==3.0.8
|
||||
protobuf==3.13.0
|
||||
ptyprocess==0.6.0
|
||||
pyasn1==0.4.8
|
||||
pyasn1-modules==0.2.8
|
||||
pycparser==2.20
|
||||
pycwt==0.3.0a22
|
||||
Pygments==2.7.3
|
||||
PyInstaller==3.6
|
||||
PyJWT==1.7.1
|
||||
pyloudnorm==0.1.0
|
||||
pyparsing==2.4.7
|
||||
pypinyin==0.39.0
|
||||
PySocks==1.7.1
|
||||
python-dateutil==2.8.1
|
||||
python-Levenshtein==0.12.0
|
||||
pytorch-lightning==0.7.1
|
||||
pytz==2020.5
|
||||
PyWavelets==1.1.1
|
||||
pyworld==0.2.12
|
||||
PyYAML==5.3.1
|
||||
regex==2020.11.13
|
||||
requests==2.25.1
|
||||
requests-oauthlib==1.3.0
|
||||
resampy==0.2.2
|
||||
Resemblyzer==0.1.1.dev0
|
||||
rsa==4.6
|
||||
scikit-image==0.16.2
|
||||
scikit-learn==0.22.2.post1
|
||||
scipy==1.5.4
|
||||
six==1.15.0
|
||||
SoundFile==0.10.3.post1
|
||||
stopit==1.1.1
|
||||
tensorboard==2.4.0
|
||||
tensorboard-plugin-wit==1.7.0
|
||||
tensorboardX==2.1
|
||||
TextGrid==1.5
|
||||
threadpoolctl==2.1.0
|
||||
toolz==0.11.1
|
||||
torch==1.6.0
|
||||
torchaudio==0.6.0
|
||||
torchvision==0.7.0
|
||||
tqdm==4.54.1
|
||||
traitlets==5.0.5
|
||||
typing==3.7.4.3
|
||||
urllib3==1.26.2
|
||||
uuid==1.30
|
||||
wcwidth==0.2.5
|
||||
webencodings==0.5.1
|
||||
webrtcvad==2.0.10
|
||||
Werkzeug==1.0.1
|
||||
pretty-midi==0.2.9
|
||||
@@ -0,0 +1,76 @@
|
||||
absl-py==0.15.0
|
||||
appdirs==1.4.4
|
||||
audioread==2.1.9
|
||||
beautifulsoup4==4.10.0
|
||||
certifi==2021.10.8
|
||||
cffi==1.15.0
|
||||
charset-normalizer==2.0.7
|
||||
cycler==0.11.0
|
||||
Cython==0.29.24
|
||||
decorator==4.4.2
|
||||
dlib==19.22.1
|
||||
einops==0.3.2
|
||||
future==0.18.2
|
||||
g2p-en==2.1.0
|
||||
google==3.0.0
|
||||
grpcio==1.42.0
|
||||
h5py==2.8.0
|
||||
horology==1.2.0
|
||||
idna==3.3
|
||||
imageio==2.10.1
|
||||
imageio-ffmpeg==0.4.5
|
||||
importlib-metadata==4.8.1
|
||||
joblib==1.1.0
|
||||
kiwisolver==1.3.2
|
||||
librosa==0.8.0
|
||||
llvmlite==0.31.0
|
||||
Markdown==3.3.4
|
||||
matplotlib==3.4.3
|
||||
miditoolkit==0.1.7
|
||||
moviepy==1.0.3
|
||||
numba==0.48.0
|
||||
numpy==1.20.0
|
||||
opencv-python==4.5.4.58
|
||||
packaging==21.2
|
||||
pandas==1.3.4
|
||||
Pillow==8.4.0
|
||||
pooch==1.5.2
|
||||
praat-parselmouth==0.3.3
|
||||
proglog==0.1.9
|
||||
protobuf==3.19.1
|
||||
pycparser==2.20
|
||||
pycwt==0.3.0a22
|
||||
pydub==0.25.1
|
||||
pyloudnorm==0.1.0
|
||||
pyparsing==2.4.7
|
||||
pypinyin==0.43.0
|
||||
python-dateutil==2.8.2
|
||||
pytorch-lightning==0.7.1
|
||||
pytorch-ssim==0.1
|
||||
pytz==2021.3
|
||||
pyworld==0.3.0
|
||||
PyYAML==6.0
|
||||
requests==2.26.0
|
||||
resampy==0.2.2
|
||||
Resemblyzer==0.1.1.dev0
|
||||
scikit-image==0.16.2
|
||||
scikit-learn==0.22
|
||||
scipy==1.3.0
|
||||
six==1.16.0
|
||||
sklearn==0.0
|
||||
SoundFile==0.10.3.post1
|
||||
soupsieve==2.3
|
||||
sympy==1.9
|
||||
tensorboard==1.15.0
|
||||
tensorboardX==2.4
|
||||
test-tube==0.7.5
|
||||
TextGrid==1.5
|
||||
torch @ https://download.pytorch.org/whl/nightly/cu113/torch-1.10.0.dev20210907%2Bcu113-cp37-cp37m-linux_x86_64.whl
|
||||
torchvision==0.9.1
|
||||
tqdm==4.62.3
|
||||
typing-extensions==3.10.0.2
|
||||
urllib3==1.26.7
|
||||
uuid==1.30
|
||||
webrtcvad==2.0.10
|
||||
Werkzeug==2.0.2
|
||||
zipp==3.6.0
|
||||
@@ -0,0 +1,26 @@
|
||||
# The way to apply for PopCS
|
||||
Thanks for your attention to our works. Please write the email to jinglinliu@zju.edu.cn with:
|
||||
|
||||
"
|
||||
|
||||
name: ***
|
||||
|
||||
affiliations: *** (school or institution)
|
||||
|
||||
research fields: ***
|
||||
|
||||
We want to apply for PopCS and agree to the dataset license: CC by-nc-sa 4.0 (NonCommercial!).
|
||||
|
||||
We accept full responsibility for our use of the dataset and shall defend and indemnify the authors of DiffSinger, against any and all claims arising from our use of the dataset, including but not limited to our use of any copies of copyrighted audio files that we may create from the dataset.
|
||||
|
||||
We hereby represent that we are fully authorized to enter into this agreement on behalf of my employer.
|
||||
|
||||
We will cite your paper if these codes or data have been used. We will not distribute the download link to others without informing the authors of DiffSinger.
|
||||
|
||||
"
|
||||
|
||||
Then we will provide the download link to you.
|
||||
|
||||
**Please note that, if you are using PopCS, it means that you have accepted the terms above.**
|
||||
|
||||
**Please use your Official Email Address (like xxx@zju.edu.cn)! Thank you!**
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 198 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 142 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 214 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 174 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 213 KiB |
@@ -0,0 +1,360 @@
|
||||
import glob
|
||||
import re
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
|
||||
import matplotlib
|
||||
|
||||
matplotlib.use('Agg')
|
||||
|
||||
from utils.hparams import hparams, set_hparams
|
||||
import random
|
||||
import sys
|
||||
import numpy as np
|
||||
import torch.distributed as dist
|
||||
from pytorch_lightning.loggers import TensorBoardLogger
|
||||
from utils.pl_utils import LatestModelCheckpoint, BaseTrainer, data_loader, DDP
|
||||
from torch import nn
|
||||
import torch.utils.data
|
||||
import utils
|
||||
import logging
|
||||
import os
|
||||
|
||||
torch.multiprocessing.set_sharing_strategy(os.getenv('TORCH_SHARE_STRATEGY', 'file_system'))
|
||||
|
||||
log_format = '%(asctime)s %(message)s'
|
||||
logging.basicConfig(stream=sys.stdout, level=logging.INFO,
|
||||
format=log_format, datefmt='%m/%d %I:%M:%S %p')
|
||||
|
||||
|
||||
class BaseDataset(torch.utils.data.Dataset):
|
||||
def __init__(self, shuffle):
|
||||
super().__init__()
|
||||
self.hparams = hparams
|
||||
self.shuffle = shuffle
|
||||
self.sort_by_len = hparams['sort_by_len']
|
||||
self.sizes = None
|
||||
|
||||
@property
|
||||
def _sizes(self):
|
||||
return self.sizes
|
||||
|
||||
def __getitem__(self, index):
|
||||
raise NotImplementedError
|
||||
|
||||
def collater(self, samples):
|
||||
raise NotImplementedError
|
||||
|
||||
def __len__(self):
|
||||
return len(self._sizes)
|
||||
|
||||
def num_tokens(self, index):
|
||||
return self.size(index)
|
||||
|
||||
def size(self, index):
|
||||
"""Return an example's size as a float or tuple. This value is used when
|
||||
filtering a dataset with ``--max-positions``."""
|
||||
size = min(self._sizes[index], hparams['max_frames'])
|
||||
return size
|
||||
|
||||
def ordered_indices(self):
|
||||
"""Return an ordered list of indices. Batches will be constructed based
|
||||
on this order."""
|
||||
if self.shuffle:
|
||||
indices = np.random.permutation(len(self))
|
||||
if self.sort_by_len:
|
||||
indices = indices[np.argsort(np.array(self._sizes)[indices], kind='mergesort')]
|
||||
# 先random, 然后稳定排序, 保证排序后同长度的数据顺序是依照random permutation的 (被其随机打乱).
|
||||
else:
|
||||
indices = np.arange(len(self))
|
||||
return indices
|
||||
|
||||
@property
|
||||
def num_workers(self):
|
||||
return int(os.getenv('NUM_WORKERS', hparams['ds_workers']))
|
||||
|
||||
|
||||
class BaseTask(nn.Module):
|
||||
def __init__(self, *args, **kwargs):
|
||||
# dataset configs
|
||||
super(BaseTask, self).__init__(*args, **kwargs)
|
||||
self.current_epoch = 0
|
||||
self.global_step = 0
|
||||
self.loaded_optimizer_states_dict = {}
|
||||
self.trainer = None
|
||||
self.logger = None
|
||||
self.on_gpu = False
|
||||
self.use_dp = False
|
||||
self.use_ddp = False
|
||||
self.example_input_array = None
|
||||
|
||||
self.max_tokens = hparams['max_tokens']
|
||||
self.max_sentences = hparams['max_sentences']
|
||||
self.max_eval_tokens = hparams['max_eval_tokens']
|
||||
if self.max_eval_tokens == -1:
|
||||
hparams['max_eval_tokens'] = self.max_eval_tokens = self.max_tokens
|
||||
self.max_eval_sentences = hparams['max_eval_sentences']
|
||||
if self.max_eval_sentences == -1:
|
||||
hparams['max_eval_sentences'] = self.max_eval_sentences = self.max_sentences
|
||||
|
||||
self.model = None
|
||||
self.training_losses_meter = None
|
||||
|
||||
###########
|
||||
# Training, validation and testing
|
||||
###########
|
||||
def build_model(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def load_ckpt(self, ckpt_base_dir, current_model_name=None, model_name='model', force=True, strict=True):
|
||||
# This function is updated on 2021.12.13
|
||||
if current_model_name is None:
|
||||
current_model_name = model_name
|
||||
utils.load_ckpt(self.__getattr__(current_model_name), ckpt_base_dir, current_model_name, force, strict)
|
||||
|
||||
def on_epoch_start(self):
|
||||
self.training_losses_meter = {'total_loss': utils.AvgrageMeter()}
|
||||
|
||||
def _training_step(self, sample, batch_idx, optimizer_idx):
|
||||
"""
|
||||
|
||||
:param sample:
|
||||
:param batch_idx:
|
||||
:return: total loss: torch.Tensor, loss_log: dict
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def training_step(self, sample, batch_idx, optimizer_idx=-1):
|
||||
loss_ret = self._training_step(sample, batch_idx, optimizer_idx)
|
||||
self.opt_idx = optimizer_idx
|
||||
if loss_ret is None:
|
||||
return {'loss': None}
|
||||
total_loss, log_outputs = loss_ret
|
||||
log_outputs = utils.tensors_to_scalars(log_outputs)
|
||||
for k, v in log_outputs.items():
|
||||
if k not in self.training_losses_meter:
|
||||
self.training_losses_meter[k] = utils.AvgrageMeter()
|
||||
if not np.isnan(v):
|
||||
self.training_losses_meter[k].update(v)
|
||||
self.training_losses_meter['total_loss'].update(total_loss.item())
|
||||
|
||||
try:
|
||||
log_outputs['lr'] = self.scheduler.get_lr()
|
||||
if isinstance(log_outputs['lr'], list):
|
||||
log_outputs['lr'] = log_outputs['lr'][0]
|
||||
except:
|
||||
pass
|
||||
|
||||
# log_outputs['all_loss'] = total_loss.item()
|
||||
progress_bar_log = log_outputs
|
||||
tb_log = {f'tr/{k}': v for k, v in log_outputs.items()}
|
||||
return {
|
||||
'loss': total_loss,
|
||||
'progress_bar': progress_bar_log,
|
||||
'log': tb_log
|
||||
}
|
||||
|
||||
def optimizer_step(self, epoch, batch_idx, optimizer, optimizer_idx):
|
||||
optimizer.step()
|
||||
optimizer.zero_grad()
|
||||
if self.scheduler is not None:
|
||||
self.scheduler.step(self.global_step // hparams['accumulate_grad_batches'])
|
||||
|
||||
def on_epoch_end(self):
|
||||
loss_outputs = {k: round(v.avg, 4) for k, v in self.training_losses_meter.items()}
|
||||
print(f"\n==============\n "
|
||||
f"Epoch {self.current_epoch} ended. Steps: {self.global_step}. {loss_outputs}"
|
||||
f"\n==============\n")
|
||||
|
||||
def validation_step(self, sample, batch_idx):
|
||||
"""
|
||||
|
||||
:param sample:
|
||||
:param batch_idx:
|
||||
:return: output: dict
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def _validation_end(self, outputs):
|
||||
"""
|
||||
|
||||
:param outputs:
|
||||
:return: loss_output: dict
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def validation_end(self, outputs):
|
||||
loss_output = self._validation_end(outputs)
|
||||
print(f"\n==============\n "
|
||||
f"valid results: {loss_output}"
|
||||
f"\n==============\n")
|
||||
return {
|
||||
'log': {f'val/{k}': v for k, v in loss_output.items()},
|
||||
'val_loss': loss_output['total_loss']
|
||||
}
|
||||
|
||||
def build_scheduler(self, optimizer):
|
||||
raise NotImplementedError
|
||||
|
||||
def build_optimizer(self, model):
|
||||
raise NotImplementedError
|
||||
|
||||
def configure_optimizers(self):
|
||||
optm = self.build_optimizer(self.model)
|
||||
self.scheduler = self.build_scheduler(optm)
|
||||
return [optm]
|
||||
|
||||
def test_start(self):
|
||||
pass
|
||||
|
||||
def test_step(self, sample, batch_idx):
|
||||
return self.validation_step(sample, batch_idx)
|
||||
|
||||
def test_end(self, outputs):
|
||||
return self.validation_end(outputs)
|
||||
|
||||
###########
|
||||
# Running configuration
|
||||
###########
|
||||
|
||||
@classmethod
|
||||
def start(cls):
|
||||
set_hparams()
|
||||
os.environ['MASTER_PORT'] = str(random.randint(15000, 30000))
|
||||
random.seed(hparams['seed'])
|
||||
np.random.seed(hparams['seed'])
|
||||
task = cls()
|
||||
work_dir = hparams['work_dir']
|
||||
trainer = BaseTrainer(checkpoint_callback=LatestModelCheckpoint(
|
||||
filepath=work_dir,
|
||||
verbose=True,
|
||||
monitor='val_loss',
|
||||
mode='min',
|
||||
num_ckpt_keep=hparams['num_ckpt_keep'],
|
||||
save_best=hparams['save_best'],
|
||||
period=1 if hparams['save_ckpt'] else 100000
|
||||
),
|
||||
logger=TensorBoardLogger(
|
||||
save_dir=work_dir,
|
||||
name='lightning_logs',
|
||||
version='lastest'
|
||||
),
|
||||
gradient_clip_val=hparams['clip_grad_norm'],
|
||||
val_check_interval=hparams['val_check_interval'],
|
||||
row_log_interval=hparams['log_interval'],
|
||||
max_updates=hparams['max_updates'],
|
||||
num_sanity_val_steps=hparams['num_sanity_val_steps'] if not hparams[
|
||||
'validate'] else 10000,
|
||||
accumulate_grad_batches=hparams['accumulate_grad_batches'])
|
||||
if not hparams['infer']: # train
|
||||
t = datetime.now().strftime('%Y%m%d%H%M%S')
|
||||
code_dir = f'{work_dir}/codes/{t}'
|
||||
subprocess.check_call(f'mkdir -p "{code_dir}"', shell=True)
|
||||
for c in hparams['save_codes']:
|
||||
subprocess.check_call(f'cp -r "{c}" "{code_dir}/"', shell=True)
|
||||
print(f"| Copied codes to {code_dir}.")
|
||||
trainer.checkpoint_callback.task = task
|
||||
trainer.fit(task)
|
||||
else:
|
||||
trainer.test(task)
|
||||
|
||||
def configure_ddp(self, model, device_ids):
|
||||
model = DDP(
|
||||
model,
|
||||
device_ids=device_ids,
|
||||
find_unused_parameters=True
|
||||
)
|
||||
if dist.get_rank() != 0 and not hparams['debug']:
|
||||
sys.stdout = open(os.devnull, "w")
|
||||
sys.stderr = open(os.devnull, "w")
|
||||
random.seed(hparams['seed'])
|
||||
np.random.seed(hparams['seed'])
|
||||
return model
|
||||
|
||||
def training_end(self, *args, **kwargs):
|
||||
return None
|
||||
|
||||
def init_ddp_connection(self, proc_rank, world_size):
|
||||
set_hparams(print_hparams=False)
|
||||
# guarantees unique ports across jobs from same grid search
|
||||
default_port = 12910
|
||||
# if user gave a port number, use that one instead
|
||||
try:
|
||||
default_port = os.environ['MASTER_PORT']
|
||||
except Exception:
|
||||
os.environ['MASTER_PORT'] = str(default_port)
|
||||
|
||||
# figure out the root node addr
|
||||
root_node = '127.0.0.2'
|
||||
root_node = self.trainer.resolve_root_node_address(root_node)
|
||||
os.environ['MASTER_ADDR'] = root_node
|
||||
dist.init_process_group('nccl', rank=proc_rank, world_size=world_size)
|
||||
|
||||
@data_loader
|
||||
def train_dataloader(self):
|
||||
return None
|
||||
|
||||
@data_loader
|
||||
def test_dataloader(self):
|
||||
return None
|
||||
|
||||
@data_loader
|
||||
def val_dataloader(self):
|
||||
return None
|
||||
|
||||
def on_load_checkpoint(self, checkpoint):
|
||||
pass
|
||||
|
||||
def on_save_checkpoint(self, checkpoint):
|
||||
pass
|
||||
|
||||
def on_sanity_check_start(self):
|
||||
pass
|
||||
|
||||
def on_train_start(self):
|
||||
pass
|
||||
|
||||
def on_train_end(self):
|
||||
pass
|
||||
|
||||
def on_batch_start(self, batch):
|
||||
pass
|
||||
|
||||
def on_batch_end(self):
|
||||
pass
|
||||
|
||||
def on_pre_performance_check(self):
|
||||
pass
|
||||
|
||||
def on_post_performance_check(self):
|
||||
pass
|
||||
|
||||
def on_before_zero_grad(self, optimizer):
|
||||
pass
|
||||
|
||||
def on_after_backward(self):
|
||||
pass
|
||||
|
||||
def backward(self, loss, optimizer):
|
||||
loss.backward()
|
||||
|
||||
def grad_norm(self, norm_type):
|
||||
results = {}
|
||||
total_norm = 0
|
||||
for name, p in self.named_parameters():
|
||||
if p.requires_grad:
|
||||
try:
|
||||
param_norm = p.grad.data.norm(norm_type)
|
||||
total_norm += param_norm ** norm_type
|
||||
norm = param_norm ** (1 / norm_type)
|
||||
|
||||
grad = round(norm.data.cpu().numpy().flatten()[0], 3)
|
||||
results['grad_{}_norm_{}'.format(norm_type, name)] = grad
|
||||
except Exception:
|
||||
# this param had no grad
|
||||
pass
|
||||
|
||||
total_norm = total_norm ** (1. / norm_type)
|
||||
grad = round(total_norm.data.cpu().numpy().flatten()[0], 3)
|
||||
results['grad_{}_norm_total'.format(norm_type)] = grad
|
||||
return results
|
||||
@@ -0,0 +1,15 @@
|
||||
import importlib
|
||||
from utils.hparams import set_hparams, hparams
|
||||
|
||||
|
||||
def run_task():
|
||||
assert hparams['task_cls'] != ''
|
||||
pkg = ".".join(hparams["task_cls"].split(".")[:-1])
|
||||
cls_name = hparams["task_cls"].split(".")[-1]
|
||||
task_cls = getattr(importlib.import_module(pkg), cls_name)
|
||||
task_cls.start()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
set_hparams()
|
||||
run_task()
|
||||
@@ -0,0 +1,511 @@
|
||||
import matplotlib
|
||||
|
||||
matplotlib.use('Agg')
|
||||
|
||||
from utils import audio
|
||||
import matplotlib.pyplot as plt
|
||||
from data_gen.tts.data_gen_utils import get_pitch
|
||||
from tasks.tts.fs2_utils import FastSpeechDataset
|
||||
from utils.cwt import cwt2f0
|
||||
from utils.pl_utils import data_loader
|
||||
import os
|
||||
from multiprocessing.pool import Pool
|
||||
from tqdm import tqdm
|
||||
from modules.fastspeech.tts_modules import mel2ph_to_dur
|
||||
from utils.hparams import hparams
|
||||
from utils.plot import spec_to_figure, dur_to_figure, f0_to_figure
|
||||
from utils.pitch_utils import denorm_f0
|
||||
from modules.fastspeech.fs2 import FastSpeech2
|
||||
from tasks.tts.tts import TtsTask
|
||||
import torch
|
||||
import torch.optim
|
||||
import torch.utils.data
|
||||
import torch.nn.functional as F
|
||||
import utils
|
||||
import torch.distributions
|
||||
import numpy as np
|
||||
from modules.commons.ssim import ssim
|
||||
|
||||
class FastSpeech2Task(TtsTask):
|
||||
def __init__(self):
|
||||
super(FastSpeech2Task, self).__init__()
|
||||
self.dataset_cls = FastSpeechDataset
|
||||
self.mse_loss_fn = torch.nn.MSELoss()
|
||||
mel_losses = hparams['mel_loss'].split("|")
|
||||
self.loss_and_lambda = {}
|
||||
for i, l in enumerate(mel_losses):
|
||||
if l == '':
|
||||
continue
|
||||
if ':' in l:
|
||||
l, lbd = l.split(":")
|
||||
lbd = float(lbd)
|
||||
else:
|
||||
lbd = 1.0
|
||||
self.loss_and_lambda[l] = lbd
|
||||
print("| Mel losses:", self.loss_and_lambda)
|
||||
self.sil_ph = self.phone_encoder.sil_phonemes()
|
||||
|
||||
@data_loader
|
||||
def train_dataloader(self):
|
||||
train_dataset = self.dataset_cls(hparams['train_set_name'], shuffle=True)
|
||||
return self.build_dataloader(train_dataset, True, self.max_tokens, self.max_sentences,
|
||||
endless=hparams['endless_ds'])
|
||||
|
||||
@data_loader
|
||||
def val_dataloader(self):
|
||||
valid_dataset = self.dataset_cls(hparams['valid_set_name'], shuffle=False)
|
||||
return self.build_dataloader(valid_dataset, False, self.max_eval_tokens, self.max_eval_sentences)
|
||||
|
||||
@data_loader
|
||||
def test_dataloader(self):
|
||||
test_dataset = self.dataset_cls(hparams['test_set_name'], shuffle=False)
|
||||
return self.build_dataloader(test_dataset, False, self.max_eval_tokens,
|
||||
self.max_eval_sentences, batch_by_size=False)
|
||||
|
||||
def build_tts_model(self):
|
||||
self.model = FastSpeech2(self.phone_encoder)
|
||||
|
||||
def build_model(self):
|
||||
self.build_tts_model()
|
||||
if hparams['load_ckpt'] != '':
|
||||
self.load_ckpt(hparams['load_ckpt'], strict=True)
|
||||
utils.print_arch(self.model)
|
||||
return self.model
|
||||
|
||||
def _training_step(self, sample, batch_idx, _):
|
||||
loss_output = self.run_model(self.model, sample)
|
||||
total_loss = sum([v for v in loss_output.values() if isinstance(v, torch.Tensor) and v.requires_grad])
|
||||
loss_output['batch_size'] = sample['txt_tokens'].size()[0]
|
||||
return total_loss, loss_output
|
||||
|
||||
def validation_step(self, sample, batch_idx):
|
||||
outputs = {}
|
||||
outputs['losses'] = {}
|
||||
outputs['losses'], model_out = self.run_model(self.model, sample, return_output=True)
|
||||
outputs['total_loss'] = sum(outputs['losses'].values())
|
||||
outputs['nsamples'] = sample['nsamples']
|
||||
mel_out = self.model.out2mel(model_out['mel_out'])
|
||||
outputs = utils.tensors_to_scalars(outputs)
|
||||
# if sample['mels'].shape[0] == 1:
|
||||
# self.add_laplace_var(mel_out, sample['mels'], outputs)
|
||||
if batch_idx < hparams['num_valid_plots']:
|
||||
self.plot_mel(batch_idx, sample['mels'], mel_out)
|
||||
self.plot_dur(batch_idx, sample, model_out)
|
||||
if hparams['use_pitch_embed']:
|
||||
self.plot_pitch(batch_idx, sample, model_out)
|
||||
return outputs
|
||||
|
||||
def _validation_end(self, outputs):
|
||||
all_losses_meter = {
|
||||
'total_loss': utils.AvgrageMeter(),
|
||||
}
|
||||
for output in outputs:
|
||||
n = output['nsamples']
|
||||
for k, v in output['losses'].items():
|
||||
if k not in all_losses_meter:
|
||||
all_losses_meter[k] = utils.AvgrageMeter()
|
||||
all_losses_meter[k].update(v, n)
|
||||
all_losses_meter['total_loss'].update(output['total_loss'], n)
|
||||
return {k: round(v.avg, 4) for k, v in all_losses_meter.items()}
|
||||
|
||||
def run_model(self, model, sample, return_output=False):
|
||||
txt_tokens = sample['txt_tokens'] # [B, T_t]
|
||||
target = sample['mels'] # [B, T_s, 80]
|
||||
mel2ph = sample['mel2ph'] # [B, T_s]
|
||||
f0 = sample['f0']
|
||||
uv = sample['uv']
|
||||
energy = sample['energy']
|
||||
spk_embed = sample.get('spk_embed') if not hparams['use_spk_id'] else sample.get('spk_ids')
|
||||
if hparams['pitch_type'] == 'cwt':
|
||||
cwt_spec = sample[f'cwt_spec']
|
||||
f0_mean = sample['f0_mean']
|
||||
f0_std = sample['f0_std']
|
||||
sample['f0_cwt'] = f0 = model.cwt2f0_norm(cwt_spec, f0_mean, f0_std, mel2ph)
|
||||
|
||||
output = model(txt_tokens, mel2ph=mel2ph, spk_embed=spk_embed,
|
||||
ref_mels=target, f0=f0, uv=uv, energy=energy, infer=False)
|
||||
|
||||
losses = {}
|
||||
self.add_mel_loss(output['mel_out'], target, losses)
|
||||
self.add_dur_loss(output['dur'], mel2ph, txt_tokens, losses=losses)
|
||||
if hparams['use_pitch_embed']:
|
||||
self.add_pitch_loss(output, sample, losses)
|
||||
if hparams['use_energy_embed']:
|
||||
self.add_energy_loss(output['energy_pred'], energy, losses)
|
||||
if not return_output:
|
||||
return losses
|
||||
else:
|
||||
return losses, output
|
||||
|
||||
############
|
||||
# losses
|
||||
############
|
||||
def add_mel_loss(self, mel_out, target, losses, postfix='', mel_mix_loss=None):
|
||||
if mel_mix_loss is None:
|
||||
for loss_name, lbd in self.loss_and_lambda.items():
|
||||
if 'l1' == loss_name:
|
||||
l = self.l1_loss(mel_out, target)
|
||||
elif 'mse' == loss_name:
|
||||
raise NotImplementedError
|
||||
elif 'ssim' == loss_name:
|
||||
l = self.ssim_loss(mel_out, target)
|
||||
elif 'gdl' == loss_name:
|
||||
raise NotImplementedError
|
||||
losses[f'{loss_name}{postfix}'] = l * lbd
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
def l1_loss(self, decoder_output, target):
|
||||
# decoder_output : B x T x n_mel
|
||||
# target : B x T x n_mel
|
||||
l1_loss = F.l1_loss(decoder_output, target, reduction='none')
|
||||
weights = self.weights_nonzero_speech(target)
|
||||
l1_loss = (l1_loss * weights).sum() / weights.sum()
|
||||
return l1_loss
|
||||
|
||||
def ssim_loss(self, decoder_output, target, bias=6.0):
|
||||
# decoder_output : B x T x n_mel
|
||||
# target : B x T x n_mel
|
||||
assert decoder_output.shape == target.shape
|
||||
weights = self.weights_nonzero_speech(target)
|
||||
decoder_output = decoder_output[:, None] + bias
|
||||
target = target[:, None] + bias
|
||||
ssim_loss = 1 - ssim(decoder_output, target, size_average=False)
|
||||
ssim_loss = (ssim_loss * weights).sum() / weights.sum()
|
||||
return ssim_loss
|
||||
|
||||
def add_dur_loss(self, dur_pred, mel2ph, txt_tokens, losses=None):
|
||||
"""
|
||||
|
||||
:param dur_pred: [B, T], float, log scale
|
||||
:param mel2ph: [B, T]
|
||||
:param txt_tokens: [B, T]
|
||||
:param losses:
|
||||
:return:
|
||||
"""
|
||||
B, T = txt_tokens.shape
|
||||
nonpadding = (txt_tokens != 0).float()
|
||||
dur_gt = mel2ph_to_dur(mel2ph, T).float() * nonpadding
|
||||
is_sil = torch.zeros_like(txt_tokens).bool()
|
||||
for p in self.sil_ph:
|
||||
is_sil = is_sil | (txt_tokens == self.phone_encoder.encode(p)[0])
|
||||
is_sil = is_sil.float() # [B, T_txt]
|
||||
|
||||
# phone duration loss
|
||||
if hparams['dur_loss'] == 'mse':
|
||||
losses['pdur'] = F.mse_loss(dur_pred, (dur_gt + 1).log(), reduction='none')
|
||||
losses['pdur'] = (losses['pdur'] * nonpadding).sum() / nonpadding.sum()
|
||||
dur_pred = (dur_pred.exp() - 1).clamp(min=0)
|
||||
elif hparams['dur_loss'] == 'mog':
|
||||
return NotImplementedError
|
||||
elif hparams['dur_loss'] == 'crf':
|
||||
losses['pdur'] = -self.model.dur_predictor.crf(
|
||||
dur_pred, dur_gt.long().clamp(min=0, max=31), mask=nonpadding > 0, reduction='mean')
|
||||
losses['pdur'] = losses['pdur'] * hparams['lambda_ph_dur']
|
||||
|
||||
# use linear scale for sent and word duration
|
||||
if hparams['lambda_word_dur'] > 0:
|
||||
word_id = (is_sil.cumsum(-1) * (1 - is_sil)).long()
|
||||
word_dur_p = dur_pred.new_zeros([B, word_id.max() + 1]).scatter_add(1, word_id, dur_pred)[:, 1:]
|
||||
word_dur_g = dur_gt.new_zeros([B, word_id.max() + 1]).scatter_add(1, word_id, dur_gt)[:, 1:]
|
||||
wdur_loss = F.mse_loss((word_dur_p + 1).log(), (word_dur_g + 1).log(), reduction='none')
|
||||
word_nonpadding = (word_dur_g > 0).float()
|
||||
wdur_loss = (wdur_loss * word_nonpadding).sum() / word_nonpadding.sum()
|
||||
losses['wdur'] = wdur_loss * hparams['lambda_word_dur']
|
||||
if hparams['lambda_sent_dur'] > 0:
|
||||
sent_dur_p = dur_pred.sum(-1)
|
||||
sent_dur_g = dur_gt.sum(-1)
|
||||
sdur_loss = F.mse_loss((sent_dur_p + 1).log(), (sent_dur_g + 1).log(), reduction='mean')
|
||||
losses['sdur'] = sdur_loss.mean() * hparams['lambda_sent_dur']
|
||||
|
||||
def add_pitch_loss(self, output, sample, losses):
|
||||
if hparams['pitch_type'] == 'ph':
|
||||
nonpadding = (sample['txt_tokens'] != 0).float()
|
||||
pitch_loss_fn = F.l1_loss if hparams['pitch_loss'] == 'l1' else F.mse_loss
|
||||
losses['f0'] = (pitch_loss_fn(output['pitch_pred'][:, :, 0], sample['f0'],
|
||||
reduction='none') * nonpadding).sum() \
|
||||
/ nonpadding.sum() * hparams['lambda_f0']
|
||||
return
|
||||
mel2ph = sample['mel2ph'] # [B, T_s]
|
||||
f0 = sample['f0']
|
||||
uv = sample['uv']
|
||||
nonpadding = (mel2ph != 0).float()
|
||||
if hparams['pitch_type'] == 'cwt':
|
||||
cwt_spec = sample[f'cwt_spec']
|
||||
f0_mean = sample['f0_mean']
|
||||
f0_std = sample['f0_std']
|
||||
cwt_pred = output['cwt'][:, :, :10]
|
||||
f0_mean_pred = output['f0_mean']
|
||||
f0_std_pred = output['f0_std']
|
||||
losses['C'] = self.cwt_loss(cwt_pred, cwt_spec) * hparams['lambda_f0']
|
||||
if hparams['use_uv']:
|
||||
assert output['cwt'].shape[-1] == 11
|
||||
uv_pred = output['cwt'][:, :, -1]
|
||||
losses['uv'] = (F.binary_cross_entropy_with_logits(uv_pred, uv, reduction='none') * nonpadding) \
|
||||
.sum() / nonpadding.sum() * hparams['lambda_uv']
|
||||
losses['f0_mean'] = F.l1_loss(f0_mean_pred, f0_mean) * hparams['lambda_f0']
|
||||
losses['f0_std'] = F.l1_loss(f0_std_pred, f0_std) * hparams['lambda_f0']
|
||||
if hparams['cwt_add_f0_loss']:
|
||||
f0_cwt_ = self.model.cwt2f0_norm(cwt_pred, f0_mean_pred, f0_std_pred, mel2ph)
|
||||
self.add_f0_loss(f0_cwt_[:, :, None], f0, uv, losses, nonpadding=nonpadding)
|
||||
elif hparams['pitch_type'] == 'frame':
|
||||
self.add_f0_loss(output['pitch_pred'], f0, uv, losses, nonpadding=nonpadding)
|
||||
|
||||
def add_f0_loss(self, p_pred, f0, uv, losses, nonpadding):
|
||||
assert p_pred[..., 0].shape == f0.shape
|
||||
if hparams['use_uv']:
|
||||
assert p_pred[..., 1].shape == uv.shape
|
||||
losses['uv'] = (F.binary_cross_entropy_with_logits(
|
||||
p_pred[:, :, 1], uv, reduction='none') * nonpadding).sum() \
|
||||
/ nonpadding.sum() * hparams['lambda_uv']
|
||||
nonpadding = nonpadding * (uv == 0).float()
|
||||
|
||||
f0_pred = p_pred[:, :, 0]
|
||||
if hparams['pitch_loss'] in ['l1', 'l2']:
|
||||
pitch_loss_fn = F.l1_loss if hparams['pitch_loss'] == 'l1' else F.mse_loss
|
||||
losses['f0'] = (pitch_loss_fn(f0_pred, f0, reduction='none') * nonpadding).sum() \
|
||||
/ nonpadding.sum() * hparams['lambda_f0']
|
||||
elif hparams['pitch_loss'] == 'ssim':
|
||||
return NotImplementedError
|
||||
|
||||
def cwt_loss(self, cwt_p, cwt_g):
|
||||
if hparams['cwt_loss'] == 'l1':
|
||||
return F.l1_loss(cwt_p, cwt_g)
|
||||
if hparams['cwt_loss'] == 'l2':
|
||||
return F.mse_loss(cwt_p, cwt_g)
|
||||
if hparams['cwt_loss'] == 'ssim':
|
||||
return self.ssim_loss(cwt_p, cwt_g, 20)
|
||||
|
||||
def add_energy_loss(self, energy_pred, energy, losses):
|
||||
nonpadding = (energy != 0).float()
|
||||
loss = (F.mse_loss(energy_pred, energy, reduction='none') * nonpadding).sum() / nonpadding.sum()
|
||||
loss = loss * hparams['lambda_energy']
|
||||
losses['e'] = loss
|
||||
|
||||
|
||||
############
|
||||
# validation plots
|
||||
############
|
||||
def plot_mel(self, batch_idx, spec, spec_out, name=None):
|
||||
spec_cat = torch.cat([spec, spec_out], -1)
|
||||
name = f'mel_{batch_idx}' if name is None else name
|
||||
vmin = hparams['mel_vmin']
|
||||
vmax = hparams['mel_vmax']
|
||||
self.logger.experiment.add_figure(name, spec_to_figure(spec_cat[0], vmin, vmax), self.global_step)
|
||||
|
||||
def plot_dur(self, batch_idx, sample, model_out):
|
||||
T_txt = sample['txt_tokens'].shape[1]
|
||||
dur_gt = mel2ph_to_dur(sample['mel2ph'], T_txt)[0]
|
||||
dur_pred = self.model.dur_predictor.out2dur(model_out['dur']).float()
|
||||
txt = self.phone_encoder.decode(sample['txt_tokens'][0].cpu().numpy())
|
||||
txt = txt.split(" ")
|
||||
self.logger.experiment.add_figure(
|
||||
f'dur_{batch_idx}', dur_to_figure(dur_gt, dur_pred, txt), self.global_step)
|
||||
|
||||
def plot_pitch(self, batch_idx, sample, model_out):
|
||||
f0 = sample['f0']
|
||||
if hparams['pitch_type'] == 'ph':
|
||||
mel2ph = sample['mel2ph']
|
||||
f0 = self.expand_f0_ph(f0, mel2ph)
|
||||
f0_pred = self.expand_f0_ph(model_out['pitch_pred'][:, :, 0], mel2ph)
|
||||
self.logger.experiment.add_figure(
|
||||
f'f0_{batch_idx}', f0_to_figure(f0[0], None, f0_pred[0]), self.global_step)
|
||||
return
|
||||
f0 = denorm_f0(f0, sample['uv'], hparams)
|
||||
if hparams['pitch_type'] == 'cwt':
|
||||
# cwt
|
||||
cwt_out = model_out['cwt']
|
||||
cwt_spec = cwt_out[:, :, :10]
|
||||
cwt = torch.cat([cwt_spec, sample['cwt_spec']], -1)
|
||||
self.logger.experiment.add_figure(f'cwt_{batch_idx}', spec_to_figure(cwt[0]), self.global_step)
|
||||
# f0
|
||||
f0_pred = cwt2f0(cwt_spec, model_out['f0_mean'], model_out['f0_std'], hparams['cwt_scales'])
|
||||
if hparams['use_uv']:
|
||||
assert cwt_out.shape[-1] == 11
|
||||
uv_pred = cwt_out[:, :, -1] > 0
|
||||
f0_pred[uv_pred > 0] = 0
|
||||
f0_cwt = denorm_f0(sample['f0_cwt'], sample['uv'], hparams)
|
||||
self.logger.experiment.add_figure(
|
||||
f'f0_{batch_idx}', f0_to_figure(f0[0], f0_cwt[0], f0_pred[0]), self.global_step)
|
||||
elif hparams['pitch_type'] == 'frame':
|
||||
# f0
|
||||
uv_pred = model_out['pitch_pred'][:, :, 1] > 0
|
||||
pitch_pred = denorm_f0(model_out['pitch_pred'][:, :, 0], uv_pred, hparams)
|
||||
self.logger.experiment.add_figure(
|
||||
f'f0_{batch_idx}', f0_to_figure(f0[0], None, pitch_pred[0]), self.global_step)
|
||||
|
||||
############
|
||||
# infer
|
||||
############
|
||||
def test_step(self, sample, batch_idx):
|
||||
spk_embed = sample.get('spk_embed') if not hparams['use_spk_id'] else sample.get('spk_ids')
|
||||
txt_tokens = sample['txt_tokens']
|
||||
mel2ph, uv, f0 = None, None, None
|
||||
ref_mels = None
|
||||
if hparams['profile_infer']:
|
||||
pass
|
||||
else:
|
||||
if hparams['use_gt_dur']:
|
||||
mel2ph = sample['mel2ph']
|
||||
if hparams['use_gt_f0']:
|
||||
f0 = sample['f0']
|
||||
uv = sample['uv']
|
||||
print('Here using gt f0!!')
|
||||
if hparams.get('use_midi') is not None and hparams['use_midi']:
|
||||
outputs = self.model(
|
||||
txt_tokens, spk_embed=spk_embed, mel2ph=mel2ph, f0=f0, uv=uv, ref_mels=ref_mels, infer=True,
|
||||
pitch_midi=sample['pitch_midi'], midi_dur=sample.get('midi_dur'), is_slur=sample.get('is_slur'))
|
||||
else:
|
||||
outputs = self.model(
|
||||
txt_tokens, spk_embed=spk_embed, mel2ph=mel2ph, f0=f0, uv=uv, ref_mels=ref_mels, infer=True)
|
||||
sample['outputs'] = self.model.out2mel(outputs['mel_out'])
|
||||
sample['mel2ph_pred'] = outputs['mel2ph']
|
||||
if hparams.get('pe_enable') is not None and hparams['pe_enable']:
|
||||
sample['f0'] = self.pe(sample['mels'])['f0_denorm_pred'] # pe predict from GT mel
|
||||
sample['f0_pred'] = self.pe(sample['outputs'])['f0_denorm_pred'] # pe predict from Pred mel
|
||||
else:
|
||||
sample['f0'] = denorm_f0(sample['f0'], sample['uv'], hparams)
|
||||
sample['f0_pred'] = outputs.get('f0_denorm')
|
||||
return self.after_infer(sample)
|
||||
|
||||
def after_infer(self, predictions):
|
||||
if self.saving_result_pool is None and not hparams['profile_infer']:
|
||||
self.saving_result_pool = Pool(min(int(os.getenv('N_PROC', os.cpu_count())), 16))
|
||||
self.saving_results_futures = []
|
||||
predictions = utils.unpack_dict_to_list(predictions)
|
||||
t = tqdm(predictions)
|
||||
for num_predictions, prediction in enumerate(t):
|
||||
for k, v in prediction.items():
|
||||
if type(v) is torch.Tensor:
|
||||
prediction[k] = v.cpu().numpy()
|
||||
|
||||
item_name = prediction.get('item_name')
|
||||
text = prediction.get('text').replace(":", "%3A")[:80]
|
||||
|
||||
# remove paddings
|
||||
mel_gt = prediction["mels"]
|
||||
mel_gt_mask = np.abs(mel_gt).sum(-1) > 0
|
||||
mel_gt = mel_gt[mel_gt_mask]
|
||||
mel2ph_gt = prediction.get("mel2ph")
|
||||
mel2ph_gt = mel2ph_gt[mel_gt_mask] if mel2ph_gt is not None else None
|
||||
mel_pred = prediction["outputs"]
|
||||
mel_pred_mask = np.abs(mel_pred).sum(-1) > 0
|
||||
mel_pred = mel_pred[mel_pred_mask]
|
||||
mel_gt = np.clip(mel_gt, hparams['mel_vmin'], hparams['mel_vmax'])
|
||||
mel_pred = np.clip(mel_pred, hparams['mel_vmin'], hparams['mel_vmax'])
|
||||
|
||||
mel2ph_pred = prediction.get("mel2ph_pred")
|
||||
if mel2ph_pred is not None:
|
||||
if len(mel2ph_pred) > len(mel_pred_mask):
|
||||
mel2ph_pred = mel2ph_pred[:len(mel_pred_mask)]
|
||||
mel2ph_pred = mel2ph_pred[mel_pred_mask]
|
||||
|
||||
f0_gt = prediction.get("f0")
|
||||
f0_pred = prediction.get("f0_pred")
|
||||
if f0_pred is not None:
|
||||
f0_gt = f0_gt[mel_gt_mask]
|
||||
if len(f0_pred) > len(mel_pred_mask):
|
||||
f0_pred = f0_pred[:len(mel_pred_mask)]
|
||||
f0_pred = f0_pred[mel_pred_mask]
|
||||
|
||||
str_phs = None
|
||||
if self.phone_encoder is not None and 'txt_tokens' in prediction:
|
||||
str_phs = self.phone_encoder.decode(prediction['txt_tokens'], strip_padding=True)
|
||||
gen_dir = os.path.join(hparams['work_dir'],
|
||||
f'generated_{self.trainer.global_step}_{hparams["gen_dir_name"]}')
|
||||
wav_pred = self.vocoder.spec2wav(mel_pred, f0=f0_pred)
|
||||
if not hparams['profile_infer']:
|
||||
os.makedirs(gen_dir, exist_ok=True)
|
||||
os.makedirs(f'{gen_dir}/wavs', exist_ok=True)
|
||||
os.makedirs(f'{gen_dir}/plot', exist_ok=True)
|
||||
os.makedirs(os.path.join(hparams['work_dir'], 'P_mels_npy'), exist_ok=True)
|
||||
os.makedirs(os.path.join(hparams['work_dir'], 'G_mels_npy'), exist_ok=True)
|
||||
self.saving_results_futures.append(
|
||||
self.saving_result_pool.apply_async(self.save_result, args=[
|
||||
wav_pred, mel_pred, 'P', item_name, text, gen_dir, str_phs, mel2ph_pred, f0_gt, f0_pred]))
|
||||
|
||||
if mel_gt is not None and hparams['save_gt']:
|
||||
wav_gt = self.vocoder.spec2wav(mel_gt, f0=f0_gt)
|
||||
self.saving_results_futures.append(
|
||||
self.saving_result_pool.apply_async(self.save_result, args=[
|
||||
wav_gt, mel_gt, 'G', item_name, text, gen_dir, str_phs, mel2ph_gt, f0_gt, f0_pred]))
|
||||
if hparams['save_f0']:
|
||||
import matplotlib.pyplot as plt
|
||||
# f0_pred_, _ = get_pitch(wav_pred, mel_pred, hparams)
|
||||
f0_pred_ = f0_pred
|
||||
f0_gt_, _ = get_pitch(wav_gt, mel_gt, hparams)
|
||||
fig = plt.figure()
|
||||
plt.plot(f0_pred_, label=r'$f0_P$')
|
||||
plt.plot(f0_gt_, label=r'$f0_G$')
|
||||
if hparams.get('pe_enable') is not None and hparams['pe_enable']:
|
||||
# f0_midi = prediction.get("f0_midi")
|
||||
# f0_midi = f0_midi[mel_gt_mask]
|
||||
# plt.plot(f0_midi, label=r'$f0_M$')
|
||||
pass
|
||||
plt.legend()
|
||||
plt.tight_layout()
|
||||
plt.savefig(f'{gen_dir}/plot/[F0][{item_name}]{text}.png', format='png')
|
||||
plt.close(fig)
|
||||
|
||||
t.set_description(
|
||||
f"Pred_shape: {mel_pred.shape}, gt_shape: {mel_gt.shape}")
|
||||
else:
|
||||
if 'gen_wav_time' not in self.stats:
|
||||
self.stats['gen_wav_time'] = 0
|
||||
self.stats['gen_wav_time'] += len(wav_pred) / hparams['audio_sample_rate']
|
||||
print('gen_wav_time: ', self.stats['gen_wav_time'])
|
||||
|
||||
return {}
|
||||
|
||||
@staticmethod
|
||||
def save_result(wav_out, mel, prefix, item_name, text, gen_dir, str_phs=None, mel2ph=None, gt_f0=None, pred_f0=None):
|
||||
item_name = item_name.replace('/', '-')
|
||||
base_fn = f'[{item_name}][{prefix}]'
|
||||
|
||||
if text is not None:
|
||||
base_fn += text
|
||||
base_fn += ('-' + hparams['exp_name'])
|
||||
np.save(os.path.join(hparams['work_dir'], f'{prefix}_mels_npy', item_name), mel)
|
||||
audio.save_wav(wav_out, f'{gen_dir}/wavs/{base_fn}.wav', hparams['audio_sample_rate'],
|
||||
norm=hparams['out_wav_norm'])
|
||||
fig = plt.figure(figsize=(14, 10))
|
||||
spec_vmin = hparams['mel_vmin']
|
||||
spec_vmax = hparams['mel_vmax']
|
||||
heatmap = plt.pcolor(mel.T, vmin=spec_vmin, vmax=spec_vmax)
|
||||
fig.colorbar(heatmap)
|
||||
if hparams.get('pe_enable') is not None and hparams['pe_enable']:
|
||||
gt_f0 = (gt_f0 - 100) / (800 - 100) * 80 * (gt_f0 > 0)
|
||||
pred_f0 = (pred_f0 - 100) / (800 - 100) * 80 * (pred_f0 > 0)
|
||||
plt.plot(pred_f0, c='white', linewidth=1, alpha=0.6)
|
||||
plt.plot(gt_f0, c='red', linewidth=1, alpha=0.6)
|
||||
else:
|
||||
f0, _ = get_pitch(wav_out, mel, hparams)
|
||||
f0 = (f0 - 100) / (800 - 100) * 80 * (f0 > 0)
|
||||
plt.plot(f0, c='white', linewidth=1, alpha=0.6)
|
||||
if mel2ph is not None and str_phs is not None:
|
||||
decoded_txt = str_phs.split(" ")
|
||||
dur = mel2ph_to_dur(torch.LongTensor(mel2ph)[None, :], len(decoded_txt))[0].numpy()
|
||||
dur = [0] + list(np.cumsum(dur))
|
||||
for i in range(len(dur) - 1):
|
||||
shift = (i % 20) + 1
|
||||
plt.text(dur[i], shift, decoded_txt[i])
|
||||
plt.hlines(shift, dur[i], dur[i + 1], colors='b' if decoded_txt[i] != '|' else 'black')
|
||||
plt.vlines(dur[i], 0, 5, colors='b' if decoded_txt[i] != '|' else 'black',
|
||||
alpha=1, linewidth=1)
|
||||
plt.tight_layout()
|
||||
plt.savefig(f'{gen_dir}/plot/{base_fn}.png', format='png', dpi=1000)
|
||||
plt.close(fig)
|
||||
|
||||
##############
|
||||
# utils
|
||||
##############
|
||||
@staticmethod
|
||||
def expand_f0_ph(f0, mel2ph):
|
||||
f0 = denorm_f0(f0, None, hparams)
|
||||
f0 = F.pad(f0, [1, 0])
|
||||
f0 = torch.gather(f0, 1, mel2ph) # [B, T_mel]
|
||||
return f0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
FastSpeech2Task.start()
|
||||
@@ -0,0 +1,173 @@
|
||||
import matplotlib
|
||||
|
||||
matplotlib.use('Agg')
|
||||
|
||||
import glob
|
||||
import importlib
|
||||
from utils.cwt import get_lf0_cwt
|
||||
import os
|
||||
import torch.optim
|
||||
import torch.utils.data
|
||||
from utils.indexed_datasets import IndexedDataset
|
||||
from utils.pitch_utils import norm_interp_f0
|
||||
import numpy as np
|
||||
from tasks.base_task import BaseDataset
|
||||
import torch
|
||||
import torch.optim
|
||||
import torch.utils.data
|
||||
import utils
|
||||
import torch.distributions
|
||||
from utils.hparams import hparams
|
||||
|
||||
|
||||
class FastSpeechDataset(BaseDataset):
|
||||
def __init__(self, prefix, shuffle=False):
|
||||
super().__init__(shuffle)
|
||||
self.data_dir = hparams['binary_data_dir']
|
||||
self.prefix = prefix
|
||||
self.hparams = hparams
|
||||
self.sizes = np.load(f'{self.data_dir}/{self.prefix}_lengths.npy')
|
||||
self.indexed_ds = None
|
||||
# self.name2spk_id={}
|
||||
|
||||
# pitch stats
|
||||
f0_stats_fn = f'{self.data_dir}/train_f0s_mean_std.npy'
|
||||
if os.path.exists(f0_stats_fn):
|
||||
hparams['f0_mean'], hparams['f0_std'] = self.f0_mean, self.f0_std = np.load(f0_stats_fn)
|
||||
hparams['f0_mean'] = float(hparams['f0_mean'])
|
||||
hparams['f0_std'] = float(hparams['f0_std'])
|
||||
else:
|
||||
hparams['f0_mean'], hparams['f0_std'] = self.f0_mean, self.f0_std = None, None
|
||||
|
||||
if prefix == 'test':
|
||||
if hparams['test_input_dir'] != '':
|
||||
self.indexed_ds, self.sizes = self.load_test_inputs(hparams['test_input_dir'])
|
||||
else:
|
||||
if hparams['num_test_samples'] > 0:
|
||||
self.avail_idxs = list(range(hparams['num_test_samples'])) + hparams['test_ids']
|
||||
self.sizes = [self.sizes[i] for i in self.avail_idxs]
|
||||
|
||||
if hparams['pitch_type'] == 'cwt':
|
||||
_, hparams['cwt_scales'] = get_lf0_cwt(np.ones(10))
|
||||
|
||||
def _get_item(self, index):
|
||||
if hasattr(self, 'avail_idxs') and self.avail_idxs is not None:
|
||||
index = self.avail_idxs[index]
|
||||
if self.indexed_ds is None:
|
||||
self.indexed_ds = IndexedDataset(f'{self.data_dir}/{self.prefix}')
|
||||
return self.indexed_ds[index]
|
||||
|
||||
def __getitem__(self, index):
|
||||
hparams = self.hparams
|
||||
item = self._get_item(index)
|
||||
max_frames = hparams['max_frames']
|
||||
spec = torch.Tensor(item['mel'])[:max_frames]
|
||||
energy = (spec.exp() ** 2).sum(-1).sqrt()
|
||||
mel2ph = torch.LongTensor(item['mel2ph'])[:max_frames] if 'mel2ph' in item else None
|
||||
f0, uv = norm_interp_f0(item["f0"][:max_frames], hparams)
|
||||
phone = torch.LongTensor(item['phone'][:hparams['max_input_tokens']])
|
||||
pitch = torch.LongTensor(item.get("pitch"))[:max_frames]
|
||||
# print(item.keys(), item['mel'].shape, spec.shape)
|
||||
sample = {
|
||||
"id": index,
|
||||
"item_name": item['item_name'],
|
||||
"text": item['txt'],
|
||||
"txt_token": phone,
|
||||
"mel": spec,
|
||||
"pitch": pitch,
|
||||
"energy": energy,
|
||||
"f0": f0,
|
||||
"uv": uv,
|
||||
"mel2ph": mel2ph,
|
||||
"mel_nonpadding": spec.abs().sum(-1) > 0,
|
||||
}
|
||||
if self.hparams['use_spk_embed']:
|
||||
sample["spk_embed"] = torch.Tensor(item['spk_embed'])
|
||||
if self.hparams['use_spk_id']:
|
||||
sample["spk_id"] = item['spk_id']
|
||||
# sample['spk_id'] = 0
|
||||
# for key in self.name2spk_id.keys():
|
||||
# if key in item['item_name']:
|
||||
# sample['spk_id'] = self.name2spk_id[key]
|
||||
# break
|
||||
if self.hparams['pitch_type'] == 'cwt':
|
||||
cwt_spec = torch.Tensor(item['cwt_spec'])[:max_frames]
|
||||
f0_mean = item.get('f0_mean', item.get('cwt_mean'))
|
||||
f0_std = item.get('f0_std', item.get('cwt_std'))
|
||||
sample.update({"cwt_spec": cwt_spec, "f0_mean": f0_mean, "f0_std": f0_std})
|
||||
elif self.hparams['pitch_type'] == 'ph':
|
||||
f0_phlevel_sum = torch.zeros_like(phone).float().scatter_add(0, mel2ph - 1, f0)
|
||||
f0_phlevel_num = torch.zeros_like(phone).float().scatter_add(
|
||||
0, mel2ph - 1, torch.ones_like(f0)).clamp_min(1)
|
||||
sample["f0_ph"] = f0_phlevel_sum / f0_phlevel_num
|
||||
return sample
|
||||
|
||||
def collater(self, samples):
|
||||
if len(samples) == 0:
|
||||
return {}
|
||||
id = torch.LongTensor([s['id'] for s in samples])
|
||||
item_names = [s['item_name'] for s in samples]
|
||||
text = [s['text'] for s in samples]
|
||||
txt_tokens = utils.collate_1d([s['txt_token'] for s in samples], 0)
|
||||
f0 = utils.collate_1d([s['f0'] for s in samples], 0.0)
|
||||
pitch = utils.collate_1d([s['pitch'] for s in samples])
|
||||
uv = utils.collate_1d([s['uv'] for s in samples])
|
||||
energy = utils.collate_1d([s['energy'] for s in samples], 0.0)
|
||||
mel2ph = utils.collate_1d([s['mel2ph'] for s in samples], 0.0) \
|
||||
if samples[0]['mel2ph'] is not None else None
|
||||
mels = utils.collate_2d([s['mel'] for s in samples], 0.0)
|
||||
txt_lengths = torch.LongTensor([s['txt_token'].numel() for s in samples])
|
||||
mel_lengths = torch.LongTensor([s['mel'].shape[0] for s in samples])
|
||||
|
||||
batch = {
|
||||
'id': id,
|
||||
'item_name': item_names,
|
||||
'nsamples': len(samples),
|
||||
'text': text,
|
||||
'txt_tokens': txt_tokens,
|
||||
'txt_lengths': txt_lengths,
|
||||
'mels': mels,
|
||||
'mel_lengths': mel_lengths,
|
||||
'mel2ph': mel2ph,
|
||||
'energy': energy,
|
||||
'pitch': pitch,
|
||||
'f0': f0,
|
||||
'uv': uv,
|
||||
}
|
||||
|
||||
if self.hparams['use_spk_embed']:
|
||||
spk_embed = torch.stack([s['spk_embed'] for s in samples])
|
||||
batch['spk_embed'] = spk_embed
|
||||
if self.hparams['use_spk_id']:
|
||||
spk_ids = torch.LongTensor([s['spk_id'] for s in samples])
|
||||
batch['spk_ids'] = spk_ids
|
||||
if self.hparams['pitch_type'] == 'cwt':
|
||||
cwt_spec = utils.collate_2d([s['cwt_spec'] for s in samples])
|
||||
f0_mean = torch.Tensor([s['f0_mean'] for s in samples])
|
||||
f0_std = torch.Tensor([s['f0_std'] for s in samples])
|
||||
batch.update({'cwt_spec': cwt_spec, 'f0_mean': f0_mean, 'f0_std': f0_std})
|
||||
elif self.hparams['pitch_type'] == 'ph':
|
||||
batch['f0'] = utils.collate_1d([s['f0_ph'] for s in samples])
|
||||
|
||||
return batch
|
||||
|
||||
def load_test_inputs(self, test_input_dir, spk_id=0):
|
||||
inp_wav_paths = glob.glob(f'{test_input_dir}/*.wav') + glob.glob(f'{test_input_dir}/*.mp3')
|
||||
sizes = []
|
||||
items = []
|
||||
|
||||
binarizer_cls = hparams.get("binarizer_cls", 'data_gen.tts.base_binarizerr.BaseBinarizer')
|
||||
pkg = ".".join(binarizer_cls.split(".")[:-1])
|
||||
cls_name = binarizer_cls.split(".")[-1]
|
||||
binarizer_cls = getattr(importlib.import_module(pkg), cls_name)
|
||||
binarization_args = hparams['binarization_args']
|
||||
|
||||
for wav_fn in inp_wav_paths:
|
||||
item_name = os.path.basename(wav_fn)
|
||||
ph = txt = tg_fn = ''
|
||||
wav_fn = wav_fn
|
||||
encoder = None
|
||||
item = binarizer_cls.process_item(item_name, ph, txt, tg_fn, wav_fn, spk_id, encoder, binarization_args)
|
||||
items.append(item)
|
||||
sizes.append(item['len'])
|
||||
return items, sizes
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
import matplotlib
|
||||
matplotlib.use('Agg')
|
||||
|
||||
import torch
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
from tasks.base_task import BaseDataset
|
||||
from tasks.tts.fs2 import FastSpeech2Task
|
||||
from modules.fastspeech.pe import PitchExtractor
|
||||
import utils
|
||||
from utils.indexed_datasets import IndexedDataset
|
||||
from utils.hparams import hparams
|
||||
from utils.plot import f0_to_figure
|
||||
from utils.pitch_utils import norm_interp_f0, denorm_f0
|
||||
|
||||
|
||||
class PeDataset(BaseDataset):
|
||||
def __init__(self, prefix, shuffle=False):
|
||||
super().__init__(shuffle)
|
||||
self.data_dir = hparams['binary_data_dir']
|
||||
self.prefix = prefix
|
||||
self.hparams = hparams
|
||||
self.sizes = np.load(f'{self.data_dir}/{self.prefix}_lengths.npy')
|
||||
self.indexed_ds = None
|
||||
|
||||
# pitch stats
|
||||
f0_stats_fn = f'{self.data_dir}/train_f0s_mean_std.npy'
|
||||
if os.path.exists(f0_stats_fn):
|
||||
hparams['f0_mean'], hparams['f0_std'] = self.f0_mean, self.f0_std = np.load(f0_stats_fn)
|
||||
hparams['f0_mean'] = float(hparams['f0_mean'])
|
||||
hparams['f0_std'] = float(hparams['f0_std'])
|
||||
else:
|
||||
hparams['f0_mean'], hparams['f0_std'] = self.f0_mean, self.f0_std = None, None
|
||||
|
||||
if prefix == 'test':
|
||||
if hparams['num_test_samples'] > 0:
|
||||
self.avail_idxs = list(range(hparams['num_test_samples'])) + hparams['test_ids']
|
||||
self.sizes = [self.sizes[i] for i in self.avail_idxs]
|
||||
|
||||
def _get_item(self, index):
|
||||
if hasattr(self, 'avail_idxs') and self.avail_idxs is not None:
|
||||
index = self.avail_idxs[index]
|
||||
if self.indexed_ds is None:
|
||||
self.indexed_ds = IndexedDataset(f'{self.data_dir}/{self.prefix}')
|
||||
return self.indexed_ds[index]
|
||||
|
||||
def __getitem__(self, index):
|
||||
hparams = self.hparams
|
||||
item = self._get_item(index)
|
||||
max_frames = hparams['max_frames']
|
||||
spec = torch.Tensor(item['mel'])[:max_frames]
|
||||
# mel2ph = torch.LongTensor(item['mel2ph'])[:max_frames] if 'mel2ph' in item else None
|
||||
f0, uv = norm_interp_f0(item["f0"][:max_frames], hparams)
|
||||
pitch = torch.LongTensor(item.get("pitch"))[:max_frames]
|
||||
# print(item.keys(), item['mel'].shape, spec.shape)
|
||||
sample = {
|
||||
"id": index,
|
||||
"item_name": item['item_name'],
|
||||
"text": item['txt'],
|
||||
"mel": spec,
|
||||
"pitch": pitch,
|
||||
"f0": f0,
|
||||
"uv": uv,
|
||||
# "mel2ph": mel2ph,
|
||||
# "mel_nonpadding": spec.abs().sum(-1) > 0,
|
||||
}
|
||||
return sample
|
||||
|
||||
def collater(self, samples):
|
||||
if len(samples) == 0:
|
||||
return {}
|
||||
id = torch.LongTensor([s['id'] for s in samples])
|
||||
item_names = [s['item_name'] for s in samples]
|
||||
text = [s['text'] for s in samples]
|
||||
f0 = utils.collate_1d([s['f0'] for s in samples], 0.0)
|
||||
pitch = utils.collate_1d([s['pitch'] for s in samples])
|
||||
uv = utils.collate_1d([s['uv'] for s in samples])
|
||||
mels = utils.collate_2d([s['mel'] for s in samples], 0.0)
|
||||
mel_lengths = torch.LongTensor([s['mel'].shape[0] for s in samples])
|
||||
# mel2ph = utils.collate_1d([s['mel2ph'] for s in samples], 0.0) \
|
||||
# if samples[0]['mel2ph'] is not None else None
|
||||
# mel_nonpaddings = utils.collate_1d([s['mel_nonpadding'].float() for s in samples], 0.0)
|
||||
|
||||
batch = {
|
||||
'id': id,
|
||||
'item_name': item_names,
|
||||
'nsamples': len(samples),
|
||||
'text': text,
|
||||
'mels': mels,
|
||||
'mel_lengths': mel_lengths,
|
||||
'pitch': pitch,
|
||||
# 'mel2ph': mel2ph,
|
||||
# 'mel_nonpaddings': mel_nonpaddings,
|
||||
'f0': f0,
|
||||
'uv': uv,
|
||||
}
|
||||
return batch
|
||||
|
||||
|
||||
class PitchExtractionTask(FastSpeech2Task):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.dataset_cls = PeDataset
|
||||
|
||||
def build_tts_model(self):
|
||||
self.model = PitchExtractor(conv_layers=hparams['pitch_extractor_conv_layers'])
|
||||
|
||||
# def build_scheduler(self, optimizer):
|
||||
# return torch.optim.lr_scheduler.StepLR(optimizer, hparams['decay_steps'], gamma=0.5)
|
||||
def _training_step(self, sample, batch_idx, _):
|
||||
loss_output = self.run_model(self.model, sample)
|
||||
total_loss = sum([v for v in loss_output.values() if isinstance(v, torch.Tensor) and v.requires_grad])
|
||||
loss_output['batch_size'] = sample['mels'].size()[0]
|
||||
return total_loss, loss_output
|
||||
|
||||
def validation_step(self, sample, batch_idx):
|
||||
outputs = {}
|
||||
outputs['losses'] = {}
|
||||
outputs['losses'], model_out = self.run_model(self.model, sample, return_output=True, infer=True)
|
||||
outputs['total_loss'] = sum(outputs['losses'].values())
|
||||
outputs['nsamples'] = sample['nsamples']
|
||||
outputs = utils.tensors_to_scalars(outputs)
|
||||
if batch_idx < hparams['num_valid_plots']:
|
||||
self.plot_pitch(batch_idx, model_out, sample)
|
||||
return outputs
|
||||
|
||||
def run_model(self, model, sample, return_output=False, infer=False):
|
||||
f0 = sample['f0']
|
||||
uv = sample['uv']
|
||||
output = model(sample['mels'])
|
||||
losses = {}
|
||||
self.add_pitch_loss(output, sample, losses)
|
||||
if not return_output:
|
||||
return losses
|
||||
else:
|
||||
return losses, output
|
||||
|
||||
def plot_pitch(self, batch_idx, model_out, sample):
|
||||
gt_f0 = denorm_f0(sample['f0'], sample['uv'], hparams)
|
||||
self.logger.experiment.add_figure(
|
||||
f'f0_{batch_idx}',
|
||||
f0_to_figure(gt_f0[0], None, model_out['f0_denorm_pred'][0]),
|
||||
self.global_step)
|
||||
|
||||
def add_pitch_loss(self, output, sample, losses):
|
||||
# mel2ph = sample['mel2ph'] # [B, T_s]
|
||||
mel = sample['mels']
|
||||
f0 = sample['f0']
|
||||
uv = sample['uv']
|
||||
# nonpadding = (mel2ph != 0).float() if hparams['pitch_type'] == 'frame' \
|
||||
# else (sample['txt_tokens'] != 0).float()
|
||||
nonpadding = (mel.abs().sum(-1) > 0).float() # sample['mel_nonpaddings']
|
||||
# print(nonpadding[0][-8:], nonpadding.shape)
|
||||
self.add_f0_loss(output['pitch_pred'], f0, uv, losses, nonpadding=nonpadding)
|
||||
@@ -0,0 +1,131 @@
|
||||
from multiprocessing.pool import Pool
|
||||
|
||||
import matplotlib
|
||||
|
||||
from utils.pl_utils import data_loader
|
||||
from utils.training_utils import RSQRTSchedule
|
||||
from vocoders.base_vocoder import get_vocoder_cls, BaseVocoder
|
||||
from modules.fastspeech.pe import PitchExtractor
|
||||
|
||||
matplotlib.use('Agg')
|
||||
import os
|
||||
import numpy as np
|
||||
from tqdm import tqdm
|
||||
import torch.distributed as dist
|
||||
|
||||
from tasks.base_task import BaseTask
|
||||
from utils.hparams import hparams
|
||||
from utils.text_encoder import TokenTextEncoder
|
||||
import json
|
||||
|
||||
import torch
|
||||
import torch.optim
|
||||
import torch.utils.data
|
||||
import utils
|
||||
|
||||
|
||||
|
||||
class TtsTask(BaseTask):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.vocoder = None
|
||||
self.phone_encoder = self.build_phone_encoder(hparams['binary_data_dir'])
|
||||
self.padding_idx = self.phone_encoder.pad()
|
||||
self.eos_idx = self.phone_encoder.eos()
|
||||
self.seg_idx = self.phone_encoder.seg()
|
||||
self.saving_result_pool = None
|
||||
self.saving_results_futures = None
|
||||
self.stats = {}
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def build_scheduler(self, optimizer):
|
||||
return RSQRTSchedule(optimizer)
|
||||
|
||||
def build_optimizer(self, model):
|
||||
self.optimizer = optimizer = torch.optim.AdamW(
|
||||
model.parameters(),
|
||||
lr=hparams['lr'])
|
||||
return optimizer
|
||||
|
||||
def build_dataloader(self, dataset, shuffle, max_tokens=None, max_sentences=None,
|
||||
required_batch_size_multiple=-1, endless=False, batch_by_size=True):
|
||||
devices_cnt = torch.cuda.device_count()
|
||||
if devices_cnt == 0:
|
||||
devices_cnt = 1
|
||||
if required_batch_size_multiple == -1:
|
||||
required_batch_size_multiple = devices_cnt
|
||||
|
||||
def shuffle_batches(batches):
|
||||
np.random.shuffle(batches)
|
||||
return batches
|
||||
|
||||
if max_tokens is not None:
|
||||
max_tokens *= devices_cnt
|
||||
if max_sentences is not None:
|
||||
max_sentences *= devices_cnt
|
||||
indices = dataset.ordered_indices()
|
||||
if batch_by_size:
|
||||
batch_sampler = utils.batch_by_size(
|
||||
indices, dataset.num_tokens, max_tokens=max_tokens, max_sentences=max_sentences,
|
||||
required_batch_size_multiple=required_batch_size_multiple,
|
||||
)
|
||||
else:
|
||||
batch_sampler = []
|
||||
for i in range(0, len(indices), max_sentences):
|
||||
batch_sampler.append(indices[i:i + max_sentences])
|
||||
|
||||
if shuffle:
|
||||
batches = shuffle_batches(list(batch_sampler))
|
||||
if endless:
|
||||
batches = [b for _ in range(1000) for b in shuffle_batches(list(batch_sampler))]
|
||||
else:
|
||||
batches = batch_sampler
|
||||
if endless:
|
||||
batches = [b for _ in range(1000) for b in batches]
|
||||
num_workers = dataset.num_workers
|
||||
if self.trainer.use_ddp:
|
||||
num_replicas = dist.get_world_size()
|
||||
rank = dist.get_rank()
|
||||
batches = [x[rank::num_replicas] for x in batches if len(x) % num_replicas == 0]
|
||||
return torch.utils.data.DataLoader(dataset,
|
||||
collate_fn=dataset.collater,
|
||||
batch_sampler=batches,
|
||||
num_workers=num_workers,
|
||||
pin_memory=False)
|
||||
|
||||
def build_phone_encoder(self, data_dir):
|
||||
phone_list_file = os.path.join(data_dir, 'phone_set.json')
|
||||
|
||||
phone_list = json.load(open(phone_list_file))
|
||||
return TokenTextEncoder(None, vocab_list=phone_list, replace_oov=',')
|
||||
|
||||
def build_optimizer(self, model):
|
||||
self.optimizer = optimizer = torch.optim.AdamW(
|
||||
model.parameters(),
|
||||
lr=hparams['lr'])
|
||||
return optimizer
|
||||
|
||||
def test_start(self):
|
||||
self.saving_result_pool = Pool(8)
|
||||
self.saving_results_futures = []
|
||||
self.vocoder: BaseVocoder = get_vocoder_cls(hparams)()
|
||||
if hparams.get('pe_enable') is not None and hparams['pe_enable']:
|
||||
self.pe = PitchExtractor().cuda()
|
||||
utils.load_ckpt(self.pe, hparams['pe_ckpt'], 'model', strict=True)
|
||||
self.pe.eval()
|
||||
def test_end(self, outputs):
|
||||
self.saving_result_pool.close()
|
||||
[f.get() for f in tqdm(self.saving_results_futures)]
|
||||
self.saving_result_pool.join()
|
||||
return {}
|
||||
|
||||
##########
|
||||
# utils
|
||||
##########
|
||||
def weights_nonzero_speech(self, target):
|
||||
# target : B x T x mel
|
||||
# Assign weight 1.0 to all labels except for padding (id=0).
|
||||
dim = target.size(-1)
|
||||
return target.abs().sum(-1, keepdim=True).ne(0).float().repeat(1, 1, dim)
|
||||
|
||||
if __name__ == '__main__':
|
||||
TtsTask.start()
|
||||
@@ -0,0 +1,24 @@
|
||||
task_cls: usr.task.DiffFsTask
|
||||
pitch_type: frame
|
||||
timesteps: 100
|
||||
dilation_cycle_length: 1
|
||||
residual_layers: 20
|
||||
residual_channels: 256
|
||||
lr: 0.001
|
||||
decay_steps: 50000
|
||||
keep_bins: 80
|
||||
spec_min: [ ]
|
||||
spec_max: [ ]
|
||||
|
||||
content_cond_steps: [ ] # [ 0, 10000 ]
|
||||
spk_cond_steps: [ ] # [ 0, 10000 ]
|
||||
# train and eval
|
||||
fs2_ckpt: ''
|
||||
max_updates: 400000
|
||||
# max_updates: 200000
|
||||
use_gt_dur: true
|
||||
use_gt_f0: true
|
||||
gen_tgt_spk_id: -1
|
||||
max_sentences: 48
|
||||
num_sanity_val_steps: 1
|
||||
num_valid_plots: 1
|
||||
@@ -0,0 +1,43 @@
|
||||
base_config:
|
||||
- configs/tts/lj/fs2.yaml
|
||||
- ./base.yaml
|
||||
# spec_min and spec_max are calculated on the training set.
|
||||
spec_min: [ -4.7574, -4.6783, -4.6431, -4.5832, -4.5390, -4.6771, -4.8089, -4.7672,
|
||||
-4.5784, -4.7755, -4.7150, -4.8919, -4.8271, -4.7389, -4.6047, -4.7759,
|
||||
-4.6799, -4.8201, -4.7823, -4.8262, -4.7857, -4.7545, -4.9358, -4.9733,
|
||||
-5.1134, -5.1395, -4.9016, -4.8434, -5.0189, -4.8460, -5.0529, -4.9510,
|
||||
-5.0217, -5.0049, -5.1831, -5.1445, -5.1015, -5.0281, -4.9887, -4.9916,
|
||||
-4.9785, -4.9071, -4.9488, -5.0342, -4.9332, -5.0650, -4.8924, -5.0875,
|
||||
-5.0483, -5.0848, -5.1809, -5.0677, -5.0015, -5.0792, -5.0636, -5.2413,
|
||||
-5.1421, -5.1710, -5.3256, -5.0511, -5.1186, -5.0057, -5.0446, -5.1173,
|
||||
-5.0325, -5.1085, -5.0053, -5.0755, -5.1176, -5.1004, -5.2153, -5.2757,
|
||||
-5.3025, -5.2867, -5.2918, -5.3328, -5.2731, -5.2985, -5.2400, -5.2211 ]
|
||||
spec_max: [ -0.5982, -0.0778, 0.1205, 0.2747, 0.4657, 0.5123, 0.5684, 0.7093,
|
||||
0.6461, 0.6420, 0.7316, 0.7715, 0.7681, 0.8349, 0.7815, 0.7591,
|
||||
0.7910, 0.7433, 0.7352, 0.6869, 0.6854, 0.6623, 0.5353, 0.6492,
|
||||
0.6909, 0.6106, 0.5761, 0.5936, 0.5638, 0.4054, 0.4545, 0.3589,
|
||||
0.3037, 0.3380, 0.1599, 0.2433, 0.2741, 0.2130, 0.1569, 0.1911,
|
||||
0.2324, 0.1586, 0.1221, 0.0341, -0.0558, 0.0553, -0.1153, -0.0933,
|
||||
-0.1171, -0.0050, -0.1519, -0.1629, -0.0522, -0.0739, -0.2069, -0.2405,
|
||||
-0.1244, -0.2116, -0.1361, -0.1575, -0.1442, 0.0513, -0.1567, -0.2000,
|
||||
0.0086, -0.0698, 0.1385, 0.0941, 0.1864, 0.1225, 0.2176, 0.2566,
|
||||
0.1670, 0.1007, 0.1444, 0.0888, 0.1998, 0.2414, 0.2932, 0.3047 ]
|
||||
|
||||
task_cls: usr.diffspeech_task.DiffSpeechTask
|
||||
vocoder: vocoders.hifigan.HifiGAN
|
||||
vocoder_ckpt: checkpoints/0414_hifi_lj_1
|
||||
num_valid_plots: 10
|
||||
use_gt_dur: false
|
||||
use_gt_f0: false
|
||||
pitch_type: cwt
|
||||
pitch_extractor: 'parselmouth'
|
||||
max_updates: 160000
|
||||
lr: 0.001
|
||||
timesteps: 100
|
||||
K_step: 71
|
||||
diff_loss_type: l1
|
||||
diff_decoder_type: 'wavenet'
|
||||
schedule_type: 'linear'
|
||||
max_beta: 0.06
|
||||
fs2_ckpt: checkpoints/fs2_lj_1/model_ckpt_steps_150000.ckpt
|
||||
save_gt: true
|
||||
@@ -0,0 +1,17 @@
|
||||
base_config:
|
||||
- ./lj_ds_beta6.yaml
|
||||
|
||||
fs2_ckpt: ''
|
||||
gaussian_start: True
|
||||
max_beta: 0.02
|
||||
timesteps: 1000
|
||||
K_step: 1000
|
||||
pndm_speedup: 10
|
||||
|
||||
pitch_type: frame
|
||||
use_pitch_embed: false # using diffusion to model pitch curve
|
||||
lambda_f0: 0.
|
||||
lambda_uv: 0.
|
||||
#rel_pos: true
|
||||
|
||||
max_updates: 320000
|
||||
@@ -0,0 +1,63 @@
|
||||
base_config:
|
||||
- configs/singing/fs2.yaml
|
||||
- usr/configs/midi/cascade/opencs/opencpop_statis.yaml
|
||||
|
||||
audio_sample_rate: 24000
|
||||
hop_size: 128 # Hop size.
|
||||
fft_size: 512 # FFT size.
|
||||
win_size: 512 # FFT size.
|
||||
fmin: 30
|
||||
fmax: 12000
|
||||
min_level_db: -120
|
||||
|
||||
binarization_args:
|
||||
with_wav: true
|
||||
with_spk_embed: false
|
||||
with_align: true
|
||||
raw_data_dir: 'data/raw/opencpop/segments'
|
||||
processed_data_dir: 'xxx'
|
||||
binarizer_cls: data_gen.singing.binarize.OpencpopBinarizer
|
||||
|
||||
|
||||
binary_data_dir: 'data/binary/opencpop-midi-dp'
|
||||
use_midi: true # for midi exp
|
||||
use_gt_f0: false # for midi exp
|
||||
use_gt_dur: false # for further midi exp
|
||||
lambda_f0: 1.0
|
||||
lambda_uv: 1.0
|
||||
#lambda_energy: 0.1
|
||||
lambda_ph_dur: 1.0
|
||||
lambda_sent_dur: 1.0
|
||||
lambda_word_dur: 1.0
|
||||
predictor_grad: 0.1
|
||||
pe_enable: false
|
||||
pe_ckpt: ''
|
||||
|
||||
num_spk: 1
|
||||
test_prefixes: [
|
||||
'2044',
|
||||
'2086',
|
||||
'2092',
|
||||
'2093',
|
||||
'2100',
|
||||
]
|
||||
|
||||
task_cls: usr.diffsinger_task.AuxDecoderMIDITask
|
||||
#vocoder: usr.singingvocoder.highgan.HighGAN
|
||||
#vocoder_ckpt: checkpoints/h_2_model/checkpoint-530000steps.pkl
|
||||
vocoder: vocoders.hifigan.HifiGAN
|
||||
vocoder_ckpt: checkpoints/0109_hifigan_bigpopcs_hop128
|
||||
|
||||
use_nsf: true
|
||||
|
||||
# config for experiments
|
||||
max_frames: 5000
|
||||
max_tokens: 40000
|
||||
predictor_layers: 5
|
||||
rel_pos: true
|
||||
dur_predictor_layers: 5 # *
|
||||
|
||||
use_spk_embed: false
|
||||
num_valid_plots: 10
|
||||
max_updates: 160000
|
||||
save_gt: true
|
||||
@@ -0,0 +1,33 @@
|
||||
base_config:
|
||||
- usr/configs/popcs_ds_beta6.yaml
|
||||
- usr/configs/midi/cascade/opencs/opencpop_statis.yaml
|
||||
|
||||
binarizer_cls: data_gen.singing.binarize.OpencpopBinarizer
|
||||
binary_data_dir: 'data/binary/opencpop-midi-dp'
|
||||
|
||||
#switch_midi2f0_step: 174000
|
||||
use_midi: true # for midi exp
|
||||
use_gt_f0: false # for midi exp
|
||||
use_gt_dur: false # for further midi exp
|
||||
lambda_f0: 1.0
|
||||
lambda_uv: 1.0
|
||||
#lambda_energy: 0.1
|
||||
lambda_ph_dur: 1.0
|
||||
lambda_sent_dur: 1.0
|
||||
lambda_word_dur: 1.0
|
||||
predictor_grad: 0.1
|
||||
pe_enable: false
|
||||
pe_ckpt: ''
|
||||
|
||||
fs2_ckpt: 'checkpoints/0302_opencpop_fs_midi/model_ckpt_steps_160000.ckpt' #
|
||||
#num_valid_plots: 0
|
||||
task_cls: usr.diffsinger_task.DiffSingerMIDITask
|
||||
|
||||
K_step: 60
|
||||
max_tokens: 40000
|
||||
predictor_layers: 5
|
||||
dilation_cycle_length: 4 # *
|
||||
rel_pos: true
|
||||
dur_predictor_layers: 5 # *
|
||||
max_updates: 160000
|
||||
gaussian_start: false
|
||||
@@ -0,0 +1,41 @@
|
||||
spec_min: [-6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6.,
|
||||
-6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6.,
|
||||
-6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6.,
|
||||
-6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6.,
|
||||
-6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6.,
|
||||
-6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6., -6.,
|
||||
-6., -6., -6., -6., -6., -6., -6., -6.]
|
||||
spec_max: [-7.9453e-01, -8.1116e-01, -6.1631e-01, -3.0679e-01, -1.3863e-01,
|
||||
-5.0652e-02, -1.1563e-01, -1.0679e-01, -9.1068e-02, -6.2174e-02,
|
||||
-7.5302e-02, -7.2217e-02, -6.3815e-02, -7.3299e-02, 7.3610e-03,
|
||||
-7.2508e-02, -5.0234e-02, -1.6534e-01, -2.6928e-01, -2.0782e-01,
|
||||
-2.0823e-01, -1.1702e-01, -7.0128e-02, -6.5868e-02, -1.2675e-02,
|
||||
1.5121e-03, -8.9902e-02, -2.1392e-01, -2.3789e-01, -2.8922e-01,
|
||||
-3.0405e-01, -2.3029e-01, -2.2088e-01, -2.1542e-01, -2.9367e-01,
|
||||
-3.0137e-01, -3.8281e-01, -4.3590e-01, -2.8681e-01, -4.6855e-01,
|
||||
-5.7485e-01, -4.7022e-01, -5.4266e-01, -4.4848e-01, -6.4120e-01,
|
||||
-6.8700e-01, -6.4860e-01, -7.6436e-01, -4.9971e-01, -7.1068e-01,
|
||||
-6.9724e-01, -6.1487e-01, -5.5843e-01, -6.9773e-01, -5.7502e-01,
|
||||
-7.0919e-01, -8.2431e-01, -8.4213e-01, -9.0431e-01, -8.2840e-01,
|
||||
-7.7945e-01, -8.2758e-01, -8.7699e-01, -1.0532e+00, -1.0766e+00,
|
||||
-1.1198e+00, -1.0185e+00, -9.8983e-01, -1.0001e+00, -1.0756e+00,
|
||||
-1.0024e+00, -1.0304e+00, -1.0579e+00, -1.0188e+00, -1.0500e+00,
|
||||
-1.0842e+00, -1.0923e+00, -1.1223e+00, -1.2381e+00, -1.6467e+00]
|
||||
|
||||
mel_vmin: -6. #-6.
|
||||
mel_vmax: 1.5
|
||||
wav2spec_eps: 1e-6
|
||||
|
||||
raw_data_dir: 'data/raw/opencpop/segments'
|
||||
processed_data_dir: 'xxx'
|
||||
binary_data_dir: 'data/binary/opencpop-midi-dp'
|
||||
datasets: [
|
||||
'opencpop',
|
||||
]
|
||||
test_prefixes: [
|
||||
'2044',
|
||||
'2086',
|
||||
'2092',
|
||||
'2093',
|
||||
'2100',
|
||||
]
|
||||
@@ -0,0 +1,42 @@
|
||||
base_config:
|
||||
- usr/configs/popcs_ds_beta6.yaml
|
||||
- usr/configs/midi/cascade/opencs/opencpop_statis.yaml
|
||||
|
||||
binarizer_cls: data_gen.singing.binarize.OpencpopBinarizer
|
||||
binary_data_dir: 'data/binary/opencpop-midi-dp'
|
||||
|
||||
#switch_midi2f0_step: 174000
|
||||
use_midi: true # for midi exp
|
||||
use_gt_dur: false # for further midi exp
|
||||
lambda_ph_dur: 1.0
|
||||
lambda_sent_dur: 1.0
|
||||
lambda_word_dur: 1.0
|
||||
predictor_grad: 0.1
|
||||
dur_predictor_layers: 5 # *
|
||||
|
||||
|
||||
fs2_ckpt: '' #
|
||||
#num_valid_plots: 0
|
||||
task_cls: usr.diffsinger_task.DiffSingerMIDITask
|
||||
|
||||
# for diffusion schedule
|
||||
timesteps: 1000
|
||||
K_step: 1000
|
||||
max_beta: 0.02
|
||||
max_tokens: 36000
|
||||
max_updates: 320000
|
||||
gaussian_start: True
|
||||
pndm_speedup: 40
|
||||
|
||||
use_pitch_embed: false
|
||||
use_gt_f0: false # for midi exp
|
||||
|
||||
lambda_f0: 0.
|
||||
lambda_uv: 0.
|
||||
dilation_cycle_length: 4 # *
|
||||
rel_pos: true
|
||||
predictor_layers: 5
|
||||
pe_enable: true
|
||||
pe_ckpt: 'checkpoints/0102_xiaoma_pe'
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user