mirror of
https://github.com/babysor/Realtime-Voice-Clone-Chinese.git
synced 2026-02-09 21:36:51 +08:00
Compare commits
13 Commits
restruct-p
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a191587417 | ||
|
|
d3ba597be9 | ||
|
|
6134c94b4d | ||
|
|
c04a1097bf | ||
|
|
9b4f8cc6c9 | ||
|
|
96993a5c61 | ||
|
|
70cc3988d3 | ||
|
|
c5998bfe71 | ||
|
|
c997dbdf66 | ||
|
|
47cc597ad0 | ||
|
|
8c895ed2c6 | ||
|
|
2e57bf3f11 | ||
|
|
11a5e2a141 |
@@ -1,4 +0,0 @@
|
|||||||
*/saved_models
|
|
||||||
!vocoder/saved_models/pretrained/**
|
|
||||||
!encoder/saved_models/pretrained.pt
|
|
||||||
/datasets
|
|
||||||
9
.gitignore
vendored
9
.gitignore
vendored
@@ -14,11 +14,8 @@
|
|||||||
*.bcf
|
*.bcf
|
||||||
*.toc
|
*.toc
|
||||||
*.sh
|
*.sh
|
||||||
data/ckpt
|
*/saved_models
|
||||||
!data/ckpt/vocoder/pretrained/**
|
!vocoder/saved_models/pretrained/**
|
||||||
!data/ckpt/encoder/pretrained.pt
|
!encoder/saved_models/pretrained.pt
|
||||||
wavs
|
wavs
|
||||||
log
|
log
|
||||||
!/docker-entrypoint.sh
|
|
||||||
!/datasets_download/*.sh
|
|
||||||
/datasets
|
|
||||||
22
.vscode/launch.json
vendored
22
.vscode/launch.json
vendored
@@ -15,8 +15,7 @@
|
|||||||
"name": "Python: Vocoder Preprocess",
|
"name": "Python: Vocoder Preprocess",
|
||||||
"type": "python",
|
"type": "python",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "control\\cli\\vocoder_preprocess.py",
|
"program": "vocoder_preprocess.py",
|
||||||
"cwd": "${workspaceFolder}",
|
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"args": ["..\\audiodata"]
|
"args": ["..\\audiodata"]
|
||||||
},
|
},
|
||||||
@@ -24,8 +23,7 @@
|
|||||||
"name": "Python: Vocoder Train",
|
"name": "Python: Vocoder Train",
|
||||||
"type": "python",
|
"type": "python",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "control\\cli\\vocoder_train.py",
|
"program": "vocoder_train.py",
|
||||||
"cwd": "${workspaceFolder}",
|
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"args": ["dev", "..\\audiodata"]
|
"args": ["dev", "..\\audiodata"]
|
||||||
},
|
},
|
||||||
@@ -34,7 +32,6 @@
|
|||||||
"type": "python",
|
"type": "python",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "demo_toolbox.py",
|
"program": "demo_toolbox.py",
|
||||||
"cwd": "${workspaceFolder}",
|
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"args": ["-d","..\\audiodata"]
|
"args": ["-d","..\\audiodata"]
|
||||||
},
|
},
|
||||||
@@ -43,7 +40,6 @@
|
|||||||
"type": "python",
|
"type": "python",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "demo_toolbox.py",
|
"program": "demo_toolbox.py",
|
||||||
"cwd": "${workspaceFolder}",
|
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"args": ["-d","..\\audiodata","-vc"]
|
"args": ["-d","..\\audiodata","-vc"]
|
||||||
},
|
},
|
||||||
@@ -51,9 +47,9 @@
|
|||||||
"name": "Python: Synth Train",
|
"name": "Python: Synth Train",
|
||||||
"type": "python",
|
"type": "python",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "train.py",
|
"program": "synthesizer_train.py",
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"args": ["--type", "synth", "..\\audiodata\\SV2TTS\\synthesizer"]
|
"args": ["my_run", "..\\"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Python: PPG Convert",
|
"name": "Python: PPG Convert",
|
||||||
@@ -64,6 +60,14 @@
|
|||||||
"args": ["-c", ".\\ppg2mel\\saved_models\\seq2seq_mol_ppg2mel_vctk_libri_oneshotvc_r4_normMel_v2.yaml",
|
"args": ["-c", ".\\ppg2mel\\saved_models\\seq2seq_mol_ppg2mel_vctk_libri_oneshotvc_r4_normMel_v2.yaml",
|
||||||
"-m", ".\\ppg2mel\\saved_models\\best_loss_step_304000.pth", "--wav_dir", ".\\wavs\\input", "--ref_wav_path", ".\\wavs\\pkq.mp3", "-o", ".\\wavs\\output\\"
|
"-m", ".\\ppg2mel\\saved_models\\best_loss_step_304000.pth", "--wav_dir", ".\\wavs\\input", "--ref_wav_path", ".\\wavs\\pkq.mp3", "-o", ".\\wavs\\output\\"
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
{
|
||||||
|
"name": "GUI",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "mkgui\\base\\_cli.py",
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"args": []
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
17
Dockerfile
17
Dockerfile
@@ -1,17 +0,0 @@
|
|||||||
FROM pytorch/pytorch:latest
|
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y build-essential ffmpeg parallel aria2 && apt-get clean
|
|
||||||
|
|
||||||
COPY ./requirements.txt /workspace/requirements.txt
|
|
||||||
|
|
||||||
RUN pip install -r requirements.txt && pip install webrtcvad-wheels
|
|
||||||
|
|
||||||
COPY . /workspace
|
|
||||||
|
|
||||||
VOLUME [ "/datasets", "/workspace/synthesizer/saved_models/" ]
|
|
||||||
|
|
||||||
ENV DATASET_MIRROR=default FORCE_RETRAIN=false TRAIN_DATASETS=aidatatang_200zh\ magicdata\ aishell3\ data_aishell TRAIN_SKIP_EXISTING=true
|
|
||||||
|
|
||||||
EXPOSE 8080
|
|
||||||
|
|
||||||
ENTRYPOINT [ "/workspace/docker-entrypoint.sh" ]
|
|
||||||
71
README-CN.md
71
README-CN.md
@@ -20,15 +20,10 @@
|
|||||||
|
|
||||||
### 进行中的工作
|
### 进行中的工作
|
||||||
* GUI/客户端大升级与合并
|
* GUI/客户端大升级与合并
|
||||||
|
[X] 初始化框架 `./mkgui` (基于streamlit + fastapi)和 [技术设计](https://vaj2fgg8yn.feishu.cn/docs/doccnvotLWylBub8VJIjKzoEaee)
|
||||||
- [x] 初始化框架 `./mkgui` (基于streamlit + fastapi)和 [技术设计](https://vaj2fgg8yn.feishu.cn/docs/doccnvotLWylBub8VJIjKzoEaee)
|
[X] 增加 Voice Cloning and Conversion的演示页面
|
||||||
|
[X] 增加Voice Conversion的预处理preprocessing 和训练 training 页面
|
||||||
- [x] 增加 Voice Cloning and Conversion的演示页面
|
[ ] 增加其他的的预处理preprocessing 和训练 training 页面
|
||||||
|
|
||||||
- [x] 增加Voice Conversion的预处理preprocessing 和训练 training 页面
|
|
||||||
|
|
||||||
- [ ] 增加其他的的预处理preprocessing 和训练 training 页面
|
|
||||||
|
|
||||||
* 模型后端基于ESPnet2升级
|
* 模型后端基于ESPnet2升级
|
||||||
|
|
||||||
|
|
||||||
@@ -82,7 +77,7 @@
|
|||||||
对效果影响不大,已经预置3款,如果希望自己训练可以参考以下命令。
|
对效果影响不大,已经预置3款,如果希望自己训练可以参考以下命令。
|
||||||
* 预处理数据:
|
* 预处理数据:
|
||||||
`python vocoder_preprocess.py <datasets_root> -m <synthesizer_model_path>`
|
`python vocoder_preprocess.py <datasets_root> -m <synthesizer_model_path>`
|
||||||
> `<datasets_root>`替换为你的数据集目录,`<synthesizer_model_path>`替换为一个你最好的synthesizer模型目录,例如 *sythensizer\saved_models\xxx*
|
> `<datasets_root>`替换为你的数据集目录,`<synthesizer_model_path>`替换为一个你最好的synthesizer模型目录,例如 *sythensizer\saved_mode\xxx*
|
||||||
|
|
||||||
|
|
||||||
* 训练wavernn声码器:
|
* 训练wavernn声码器:
|
||||||
@@ -92,10 +87,7 @@
|
|||||||
* 训练hifigan声码器:
|
* 训练hifigan声码器:
|
||||||
`python vocoder_train.py <trainid> <datasets_root> hifigan`
|
`python vocoder_train.py <trainid> <datasets_root> hifigan`
|
||||||
> `<trainid>`替换为你想要的标识,同一标识再次训练时会延续原模型
|
> `<trainid>`替换为你想要的标识,同一标识再次训练时会延续原模型
|
||||||
* 训练fregan声码器:
|
|
||||||
`python vocoder_train.py <trainid> <datasets_root> --config config.json fregan`
|
|
||||||
> `<trainid>`替换为你想要的标识,同一标识再次训练时会延续原模型
|
|
||||||
* 将GAN声码器的训练切换为多GPU模式:修改GAN文件夹下.json文件中的"num_gpus"参数
|
|
||||||
### 3. 启动程序或工具箱
|
### 3. 启动程序或工具箱
|
||||||
您可以尝试使用以下命令:
|
您可以尝试使用以下命令:
|
||||||
|
|
||||||
@@ -113,12 +105,12 @@
|
|||||||
### 4. 番外:语音转换Voice Conversion(PPG based)
|
### 4. 番外:语音转换Voice Conversion(PPG based)
|
||||||
想像柯南拿着变声器然后发出毛利小五郎的声音吗?本项目现基于PPG-VC,引入额外两个模块(PPG extractor + PPG2Mel), 可以实现变声功能。(文档不全,尤其是训练部分,正在努力补充中)
|
想像柯南拿着变声器然后发出毛利小五郎的声音吗?本项目现基于PPG-VC,引入额外两个模块(PPG extractor + PPG2Mel), 可以实现变声功能。(文档不全,尤其是训练部分,正在努力补充中)
|
||||||
#### 4.0 准备环境
|
#### 4.0 准备环境
|
||||||
* 确保项目以上环境已经安装ok,运行`pip install espnet` 来安装剩余的必要包。
|
* 确保项目以上环境已经安装ok,运行`pip install -r requirements_vc.txt` 来安装剩余的必要包。
|
||||||
* 下载以下模型 链接:https://pan.baidu.com/s/1bl_x_DHJSAUyN2fma-Q_Wg
|
* 下载以下模型 链接:https://pan.baidu.com/s/1bl_x_DHJSAUyN2fma-Q_Wg
|
||||||
提取码:gh41
|
提取码:gh41
|
||||||
* 24K采样率专用的vocoder(hifigan)到 *vocoder\saved_models\xxx*
|
* 24K采样率专用的vocoder(hifigan)到 *vocoder\saved_mode\xxx*
|
||||||
* 预训练的ppg特征encoder(ppg_extractor)到 *ppg_extractor\saved_models\xxx*
|
* 预训练的ppg特征encoder(ppg_extractor)到 *ppg_extractor\saved_mode\xxx*
|
||||||
* 预训练的PPG2Mel到 *ppg2mel\saved_models\xxx*
|
* 预训练的PPG2Mel到 *ppg2mel\saved_mode\xxx*
|
||||||
|
|
||||||
#### 4.1 使用数据集自己训练PPG2Mel模型 (可选)
|
#### 4.1 使用数据集自己训练PPG2Mel模型 (可选)
|
||||||
|
|
||||||
@@ -127,7 +119,7 @@
|
|||||||
`python pre4ppg.py <datasets_root> -d {dataset} -n {number}`
|
`python pre4ppg.py <datasets_root> -d {dataset} -n {number}`
|
||||||
可传入参数:
|
可传入参数:
|
||||||
* `-d {dataset}` 指定数据集,支持 aidatatang_200zh, 不传默认为aidatatang_200zh
|
* `-d {dataset}` 指定数据集,支持 aidatatang_200zh, 不传默认为aidatatang_200zh
|
||||||
* `-n {number}` 指定并行数,CPU 11700k在8的情况下,需要运行12到18小时!待优化
|
* `-n {number}` 指定并行数,CPU 11770k在8的情况下,需要运行12到18小时!待优化
|
||||||
> 假如你下载的 `aidatatang_200zh`文件放在D盘,`train`文件路径为 `D:\data\aidatatang_200zh\corpus\train` , 你的`datasets_root`就是 `D:\data\`
|
> 假如你下载的 `aidatatang_200zh`文件放在D盘,`train`文件路径为 `D:\data\aidatatang_200zh\corpus\train` , 你的`datasets_root`就是 `D:\data\`
|
||||||
|
|
||||||
* 训练合成器, 注意在上一步先下载好`ppg2mel.yaml`, 修改里面的地址指向预训练好的文件夹:
|
* 训练合成器, 注意在上一步先下载好`ppg2mel.yaml`, 修改里面的地址指向预训练好的文件夹:
|
||||||
@@ -136,7 +128,7 @@
|
|||||||
|
|
||||||
#### 4.2 启动工具箱VC模式
|
#### 4.2 启动工具箱VC模式
|
||||||
您可以尝试使用以下命令:
|
您可以尝试使用以下命令:
|
||||||
`python demo_toolbox.py -vc -d <datasets_root>`
|
`python demo_toolbox.py vc -d <datasets_root>`
|
||||||
> 请指定一个可用的数据集文件路径,如果有支持的数据集则会自动加载供调试,也同时会作为手动录制音频的存储目录。
|
> 请指定一个可用的数据集文件路径,如果有支持的数据集则会自动加载供调试,也同时会作为手动录制音频的存储目录。
|
||||||
<img width="971" alt="微信图片_20220305005351" src="https://user-images.githubusercontent.com/7423248/156805733-2b093dbc-d989-4e68-8609-db11f365886a.png">
|
<img width="971" alt="微信图片_20220305005351" src="https://user-images.githubusercontent.com/7423248/156805733-2b093dbc-d989-4e68-8609-db11f365886a.png">
|
||||||
|
|
||||||
@@ -147,36 +139,35 @@
|
|||||||
| --- | ----------- | ----- | --------------------- |
|
| --- | ----------- | ----- | --------------------- |
|
||||||
| [1803.09017](https://arxiv.org/abs/1803.09017) | GlobalStyleToken (synthesizer)| Style Tokens: Unsupervised Style Modeling, Control and Transfer in End-to-End Speech Synthesis | 本代码库 |
|
| [1803.09017](https://arxiv.org/abs/1803.09017) | GlobalStyleToken (synthesizer)| Style Tokens: Unsupervised Style Modeling, Control and Transfer in End-to-End Speech Synthesis | 本代码库 |
|
||||||
| [2010.05646](https://arxiv.org/abs/2010.05646) | HiFi-GAN (vocoder)| Generative Adversarial Networks for Efficient and High Fidelity Speech Synthesis | 本代码库 |
|
| [2010.05646](https://arxiv.org/abs/2010.05646) | HiFi-GAN (vocoder)| Generative Adversarial Networks for Efficient and High Fidelity Speech Synthesis | 本代码库 |
|
||||||
| [2106.02297](https://arxiv.org/abs/2106.02297) | Fre-GAN (vocoder)| Fre-GAN: Adversarial Frequency-consistent Audio Synthesis | 本代码库 |
|
|
||||||
|[**1806.04558**](https://arxiv.org/pdf/1806.04558.pdf) | SV2TTS | Transfer Learning from Speaker Verification to Multispeaker Text-To-Speech Synthesis | 本代码库 |
|
|[**1806.04558**](https://arxiv.org/pdf/1806.04558.pdf) | SV2TTS | Transfer Learning from Speaker Verification to Multispeaker Text-To-Speech Synthesis | 本代码库 |
|
||||||
|[1802.08435](https://arxiv.org/pdf/1802.08435.pdf) | WaveRNN (vocoder) | Efficient Neural Audio Synthesis | [fatchord/WaveRNN](https://github.com/fatchord/WaveRNN) |
|
|[1802.08435](https://arxiv.org/pdf/1802.08435.pdf) | WaveRNN (vocoder) | Efficient Neural Audio Synthesis | [fatchord/WaveRNN](https://github.com/fatchord/WaveRNN) |
|
||||||
|[1703.10135](https://arxiv.org/pdf/1703.10135.pdf) | Tacotron (synthesizer) | Tacotron: Towards End-to-End Speech Synthesis | [fatchord/WaveRNN](https://github.com/fatchord/WaveRNN)
|
|[1703.10135](https://arxiv.org/pdf/1703.10135.pdf) | Tacotron (synthesizer) | Tacotron: Towards End-to-End Speech Synthesis | [fatchord/WaveRNN](https://github.com/fatchord/WaveRNN)
|
||||||
|[1710.10467](https://arxiv.org/pdf/1710.10467.pdf) | GE2E (encoder)| Generalized End-To-End Loss for Speaker Verification | 本代码库 |
|
|[1710.10467](https://arxiv.org/pdf/1710.10467.pdf) | GE2E (encoder)| Generalized End-To-End Loss for Speaker Verification | 本代码库 |
|
||||||
|
|
||||||
## 常见问题(FQ&A)
|
## 常見問題(FQ&A)
|
||||||
#### 1.数据集在哪里下载?
|
#### 1.數據集哪裡下載?
|
||||||
| 数据集 | OpenSLR地址 | 其他源 (Google Drive, Baidu网盘等) |
|
| 数据集 | OpenSLR地址 | 其他源 (Google Drive, Baidu网盘等) |
|
||||||
| --- | ----------- | ---------------|
|
| --- | ----------- | ---------------|
|
||||||
| aidatatang_200zh | [OpenSLR](http://www.openslr.org/62/) | [Google Drive](https://drive.google.com/file/d/110A11KZoVe7vy6kXlLb6zVPLb_J91I_t/view?usp=sharing) |
|
| aidatatang_200zh | [OpenSLR](http://www.openslr.org/62/) | [Google Drive](https://drive.google.com/file/d/110A11KZoVe7vy6kXlLb6zVPLb_J91I_t/view?usp=sharing) |
|
||||||
| magicdata | [OpenSLR](http://www.openslr.org/68/) | [Google Drive (Dev set)](https://drive.google.com/file/d/1g5bWRUSNH68ycC6eNvtwh07nX3QhOOlo/view?usp=sharing) |
|
| magicdata | [OpenSLR](http://www.openslr.org/68/) | [Google Drive (Dev set)](https://drive.google.com/file/d/1g5bWRUSNH68ycC6eNvtwh07nX3QhOOlo/view?usp=sharing) |
|
||||||
| aishell3 | [OpenSLR](https://www.openslr.org/93/) | [Google Drive](https://drive.google.com/file/d/1shYp_o4Z0X0cZSKQDtFirct2luFUwKzZ/view?usp=sharing) |
|
| aishell3 | [OpenSLR](https://www.openslr.org/93/) | [Google Drive](https://drive.google.com/file/d/1shYp_o4Z0X0cZSKQDtFirct2luFUwKzZ/view?usp=sharing) |
|
||||||
| data_aishell | [OpenSLR](https://www.openslr.org/33/) | |
|
| data_aishell | [OpenSLR](https://www.openslr.org/33/) | |
|
||||||
> 解压 aidatatang_200zh 后,还需将 `aidatatang_200zh\corpus\train`下的文件全选解压缩
|
> 解壓 aidatatang_200zh 後,還需將 `aidatatang_200zh\corpus\train`下的檔案全選解壓縮
|
||||||
|
|
||||||
#### 2.`<datasets_root>`是什麼意思?
|
#### 2.`<datasets_root>`是什麼意思?
|
||||||
假如数据集路径为 `D:\data\aidatatang_200zh`,那么 `<datasets_root>`就是 `D:\data`
|
假如數據集路徑為 `D:\data\aidatatang_200zh`,那麼 `<datasets_root>`就是 `D:\data`
|
||||||
|
|
||||||
#### 3.训练模型显存不足
|
#### 3.訓練模型顯存不足
|
||||||
训练合成器时:将 `synthesizer/hparams.py`中的batch_size参数调小
|
訓練合成器時:將 `synthesizer/hparams.py`中的batch_size參數調小
|
||||||
```
|
```
|
||||||
//调整前
|
//調整前
|
||||||
tts_schedule = [(2, 1e-3, 20_000, 12), # Progressive training schedule
|
tts_schedule = [(2, 1e-3, 20_000, 12), # Progressive training schedule
|
||||||
(2, 5e-4, 40_000, 12), # (r, lr, step, batch_size)
|
(2, 5e-4, 40_000, 12), # (r, lr, step, batch_size)
|
||||||
(2, 2e-4, 80_000, 12), #
|
(2, 2e-4, 80_000, 12), #
|
||||||
(2, 1e-4, 160_000, 12), # r = reduction factor (# of mel frames
|
(2, 1e-4, 160_000, 12), # r = reduction factor (# of mel frames
|
||||||
(2, 3e-5, 320_000, 12), # synthesized for each decoder iteration)
|
(2, 3e-5, 320_000, 12), # synthesized for each decoder iteration)
|
||||||
(2, 1e-5, 640_000, 12)], # lr = learning rate
|
(2, 1e-5, 640_000, 12)], # lr = learning rate
|
||||||
//调整后
|
//調整後
|
||||||
tts_schedule = [(2, 1e-3, 20_000, 8), # Progressive training schedule
|
tts_schedule = [(2, 1e-3, 20_000, 8), # Progressive training schedule
|
||||||
(2, 5e-4, 40_000, 8), # (r, lr, step, batch_size)
|
(2, 5e-4, 40_000, 8), # (r, lr, step, batch_size)
|
||||||
(2, 2e-4, 80_000, 8), #
|
(2, 2e-4, 80_000, 8), #
|
||||||
@@ -185,15 +176,15 @@ tts_schedule = [(2, 1e-3, 20_000, 8), # Progressive training schedule
|
|||||||
(2, 1e-5, 640_000, 8)], # lr = learning rate
|
(2, 1e-5, 640_000, 8)], # lr = learning rate
|
||||||
```
|
```
|
||||||
|
|
||||||
声码器-预处理数据集时:将 `synthesizer/hparams.py`中的batch_size参数调小
|
聲碼器-預處理數據集時:將 `synthesizer/hparams.py`中的batch_size參數調小
|
||||||
```
|
```
|
||||||
//调整前
|
//調整前
|
||||||
### Data Preprocessing
|
### Data Preprocessing
|
||||||
max_mel_frames = 900,
|
max_mel_frames = 900,
|
||||||
rescale = True,
|
rescale = True,
|
||||||
rescaling_max = 0.9,
|
rescaling_max = 0.9,
|
||||||
synthesis_batch_size = 16, # For vocoder preprocessing and inference.
|
synthesis_batch_size = 16, # For vocoder preprocessing and inference.
|
||||||
//调整后
|
//調整後
|
||||||
### Data Preprocessing
|
### Data Preprocessing
|
||||||
max_mel_frames = 900,
|
max_mel_frames = 900,
|
||||||
rescale = True,
|
rescale = True,
|
||||||
@@ -201,16 +192,16 @@ tts_schedule = [(2, 1e-3, 20_000, 8), # Progressive training schedule
|
|||||||
synthesis_batch_size = 8, # For vocoder preprocessing and inference.
|
synthesis_batch_size = 8, # For vocoder preprocessing and inference.
|
||||||
```
|
```
|
||||||
|
|
||||||
声码器-训练声码器时:将 `vocoder/wavernn/hparams.py`中的batch_size参数调小
|
聲碼器-訓練聲碼器時:將 `vocoder/wavernn/hparams.py`中的batch_size參數調小
|
||||||
```
|
```
|
||||||
//调整前
|
//調整前
|
||||||
# Training
|
# Training
|
||||||
voc_batch_size = 100
|
voc_batch_size = 100
|
||||||
voc_lr = 1e-4
|
voc_lr = 1e-4
|
||||||
voc_gen_at_checkpoint = 5
|
voc_gen_at_checkpoint = 5
|
||||||
voc_pad = 2
|
voc_pad = 2
|
||||||
|
|
||||||
//调整后
|
//調整後
|
||||||
# Training
|
# Training
|
||||||
voc_batch_size = 6
|
voc_batch_size = 6
|
||||||
voc_lr = 1e-4
|
voc_lr = 1e-4
|
||||||
@@ -219,13 +210,13 @@ voc_pad =2
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### 4.碰到`RuntimeError: Error(s) in loading state_dict for Tacotron: size mismatch for encoder.embedding.weight: copying a param with shape torch.Size([70, 512]) from checkpoint, the shape in current model is torch.Size([75, 512]).`
|
#### 4.碰到`RuntimeError: Error(s) in loading state_dict for Tacotron: size mismatch for encoder.embedding.weight: copying a param with shape torch.Size([70, 512]) from checkpoint, the shape in current model is torch.Size([75, 512]).`
|
||||||
请参照 issue [#37](https://github.com/babysor/MockingBird/issues/37)
|
請參照 issue [#37](https://github.com/babysor/MockingBird/issues/37)
|
||||||
|
|
||||||
#### 5.如何改善CPU、GPU占用率?
|
#### 5.如何改善CPU、GPU佔用率?
|
||||||
视情况调整batch_size参数来改善
|
適情況調整batch_size參數來改善
|
||||||
|
|
||||||
#### 6.发生 `页面文件太小,无法完成操作`
|
#### 6.發生 `頁面文件太小,無法完成操作`
|
||||||
请参考这篇[文章](https://blog.csdn.net/qq_17755303/article/details/112564030),将虚拟内存更改为100G(102400),例如:文件放置D盘就更改D盘的虚拟内存
|
請參考這篇[文章](https://blog.csdn.net/qq_17755303/article/details/112564030),將虛擬內存更改為100G(102400),例如:档案放置D槽就更改D槽的虚拟内存
|
||||||
|
|
||||||
#### 7.什么时候算训练完成?
|
#### 7.什么时候算训练完成?
|
||||||
首先一定要出现注意力模型,其次是loss足够低,取决于硬件设备和数据集。拿本人的供参考,我的注意力是在 18k 步之后出现的,并且在 50k 步之后损失变得低于 0.4
|
首先一定要出现注意力模型,其次是loss足够低,取决于硬件设备和数据集。拿本人的供参考,我的注意力是在 18k 步之后出现的,并且在 50k 步之后损失变得低于 0.4
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -37,7 +37,7 @@
|
|||||||
* Install [ffmpeg](https://ffmpeg.org/download.html#get-packages).
|
* Install [ffmpeg](https://ffmpeg.org/download.html#get-packages).
|
||||||
* Run `pip install -r requirements.txt` to install the remaining necessary packages.
|
* Run `pip install -r requirements.txt` to install the remaining necessary packages.
|
||||||
* Install webrtcvad `pip install webrtcvad-wheels`(If you need)
|
* Install webrtcvad `pip install webrtcvad-wheels`(If you need)
|
||||||
> Note that we are using the pretrained encoder/vocoder but synthesizer since the original model is incompatible with the Chinese symbols. It means the demo_cli is not working at this moment.
|
> Note that we are using the pretrained encoder/vocoder but synthesizer, since the original model is incompatible with the Chinese sympols. It means the demo_cli is not working at this moment.
|
||||||
### 2. Prepare your models
|
### 2. Prepare your models
|
||||||
You can either train your models or use existing ones:
|
You can either train your models or use existing ones:
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ Allowing parameter `--dataset {dataset}` to support aidatatang_200zh, magicdata,
|
|||||||
| @author | https://pan.baidu.com/s/1iONvRxmkI-t1nHqxKytY3g [Baidu](https://pan.baidu.com/s/1iONvRxmkI-t1nHqxKytY3g) 4j5d | | 75k steps trained by multiple datasets
|
| @author | https://pan.baidu.com/s/1iONvRxmkI-t1nHqxKytY3g [Baidu](https://pan.baidu.com/s/1iONvRxmkI-t1nHqxKytY3g) 4j5d | | 75k steps trained by multiple datasets
|
||||||
| @author | https://pan.baidu.com/s/1fMh9IlgKJlL2PIiRTYDUvw [Baidu](https://pan.baidu.com/s/1fMh9IlgKJlL2PIiRTYDUvw) code:om7f | | 25k steps trained by multiple datasets, only works under version 0.0.1
|
| @author | https://pan.baidu.com/s/1fMh9IlgKJlL2PIiRTYDUvw [Baidu](https://pan.baidu.com/s/1fMh9IlgKJlL2PIiRTYDUvw) code:om7f | | 25k steps trained by multiple datasets, only works under version 0.0.1
|
||||||
|@FawenYo | https://drive.google.com/file/d/1H-YGOUHpmqKxJ9FRc6vAjPuqQki24UbC/view?usp=sharing https://u.teknik.io/AYxWf.pt | [input](https://github.com/babysor/MockingBird/wiki/audio/self_test.mp3) [output](https://github.com/babysor/MockingBird/wiki/audio/export.wav) | 200k steps with local accent of Taiwan, only works under version 0.0.1
|
|@FawenYo | https://drive.google.com/file/d/1H-YGOUHpmqKxJ9FRc6vAjPuqQki24UbC/view?usp=sharing https://u.teknik.io/AYxWf.pt | [input](https://github.com/babysor/MockingBird/wiki/audio/self_test.mp3) [output](https://github.com/babysor/MockingBird/wiki/audio/export.wav) | 200k steps with local accent of Taiwan, only works under version 0.0.1
|
||||||
|@miven| https://pan.baidu.com/s/1PI-hM3sn5wbeChRryX-RCQ code: 2021 https://www.aliyundrive.com/s/AwPsbo8mcSP code: z2m0 | https://www.bilibili.com/video/BV1uh411B7AD/ | only works under version 0.0.1
|
|@miven| https://pan.baidu.com/s/1PI-hM3sn5wbeChRryX-RCQ code:2021 | https://www.bilibili.com/video/BV1uh411B7AD/ | only works under version 0.0.1
|
||||||
|
|
||||||
#### 2.4 Train vocoder (Optional)
|
#### 2.4 Train vocoder (Optional)
|
||||||
> note: vocoder has little difference in effect, so you may not need to train a new one.
|
> note: vocoder has little difference in effect, so you may not need to train a new one.
|
||||||
@@ -90,11 +90,6 @@ You can then try to run:`python web.py` and open it in browser, default as `http
|
|||||||
You can then try the toolbox:
|
You can then try the toolbox:
|
||||||
`python demo_toolbox.py -d <datasets_root>`
|
`python demo_toolbox.py -d <datasets_root>`
|
||||||
|
|
||||||
#### 3.3 Using the command line
|
|
||||||
You can then try the command:
|
|
||||||
`python gen_voice.py <text_file.txt> your_wav_file.wav`
|
|
||||||
you may need to install cn2an by "pip install cn2an" for better digital number result.
|
|
||||||
|
|
||||||
## Reference
|
## Reference
|
||||||
> This repository is forked from [Real-Time-Voice-Cloning](https://github.com/CorentinJ/Real-Time-Voice-Cloning) which only support English.
|
> This repository is forked from [Real-Time-Voice-Cloning](https://github.com/CorentinJ/Real-Time-Voice-Cloning) which only support English.
|
||||||
|
|
||||||
@@ -102,7 +97,6 @@ you may need to install cn2an by "pip install cn2an" for better digital number r
|
|||||||
| --- | ----------- | ----- | --------------------- |
|
| --- | ----------- | ----- | --------------------- |
|
||||||
| [1803.09017](https://arxiv.org/abs/1803.09017) | GlobalStyleToken (synthesizer)| Style Tokens: Unsupervised Style Modeling, Control and Transfer in End-to-End Speech Synthesis | This repo |
|
| [1803.09017](https://arxiv.org/abs/1803.09017) | GlobalStyleToken (synthesizer)| Style Tokens: Unsupervised Style Modeling, Control and Transfer in End-to-End Speech Synthesis | This repo |
|
||||||
| [2010.05646](https://arxiv.org/abs/2010.05646) | HiFi-GAN (vocoder)| Generative Adversarial Networks for Efficient and High Fidelity Speech Synthesis | This repo |
|
| [2010.05646](https://arxiv.org/abs/2010.05646) | HiFi-GAN (vocoder)| Generative Adversarial Networks for Efficient and High Fidelity Speech Synthesis | This repo |
|
||||||
| [2106.02297](https://arxiv.org/abs/2106.02297) | Fre-GAN (vocoder)| Fre-GAN: Adversarial Frequency-consistent Audio Synthesis | This repo |
|
|
||||||
|[**1806.04558**](https://arxiv.org/pdf/1806.04558.pdf) | **SV2TTS** | **Transfer Learning from Speaker Verification to Multispeaker Text-To-Speech Synthesis** | This repo |
|
|[**1806.04558**](https://arxiv.org/pdf/1806.04558.pdf) | **SV2TTS** | **Transfer Learning from Speaker Verification to Multispeaker Text-To-Speech Synthesis** | This repo |
|
||||||
|[1802.08435](https://arxiv.org/pdf/1802.08435.pdf) | WaveRNN (vocoder) | Efficient Neural Audio Synthesis | [fatchord/WaveRNN](https://github.com/fatchord/WaveRNN) |
|
|[1802.08435](https://arxiv.org/pdf/1802.08435.pdf) | WaveRNN (vocoder) | Efficient Neural Audio Synthesis | [fatchord/WaveRNN](https://github.com/fatchord/WaveRNN) |
|
||||||
|[1703.10135](https://arxiv.org/pdf/1703.10135.pdf) | Tacotron (synthesizer) | Tacotron: Towards End-to-End Speech Synthesis | [fatchord/WaveRNN](https://github.com/fatchord/WaveRNN)
|
|[1703.10135](https://arxiv.org/pdf/1703.10135.pdf) | Tacotron (synthesizer) | Tacotron: Towards End-to-End Speech Synthesis | [fatchord/WaveRNN](https://github.com/fatchord/WaveRNN)
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
from models.encoder.params_model import model_embedding_size as speaker_embedding_size
|
from encoder.params_model import model_embedding_size as speaker_embedding_size
|
||||||
from utils.argutils import print_args
|
from utils.argutils import print_args
|
||||||
from utils.modelutils import check_model_paths
|
from utils.modelutils import check_model_paths
|
||||||
from models.synthesizer.inference import Synthesizer
|
from synthesizer.inference import Synthesizer
|
||||||
from models.encoder import inference as encoder
|
from encoder import inference as encoder
|
||||||
from models.vocoder import inference as vocoder
|
from vocoder import inference as vocoder
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import soundfile as sf
|
import soundfile as sf
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
import sys
|
|
||||||
import torch
|
|
||||||
import argparse
|
|
||||||
import numpy as np
|
|
||||||
from utils.hparams import HpsYaml
|
|
||||||
from models.ppg2mel.train.train_linglf02mel_seq2seq_oneshotvc import Solver
|
|
||||||
|
|
||||||
# For reproducibility, comment these may speed up training
|
|
||||||
torch.backends.cudnn.deterministic = True
|
|
||||||
torch.backends.cudnn.benchmark = False
|
|
||||||
|
|
||||||
def main():
|
|
||||||
# Arguments
|
|
||||||
parser = argparse.ArgumentParser(description=
|
|
||||||
'Training PPG2Mel VC model.')
|
|
||||||
parser.add_argument('--config', type=str,
|
|
||||||
help='Path to experiment config, e.g., config/vc.yaml')
|
|
||||||
parser.add_argument('--name', default=None, type=str, help='Name for logging.')
|
|
||||||
parser.add_argument('--logdir', default='log/', type=str,
|
|
||||||
help='Logging path.', required=False)
|
|
||||||
parser.add_argument('--ckpdir', default='ppg2mel/saved_models/', type=str,
|
|
||||||
help='Checkpoint path.', required=False)
|
|
||||||
parser.add_argument('--outdir', default='result/', type=str,
|
|
||||||
help='Decode output path.', required=False)
|
|
||||||
parser.add_argument('--load', default=None, type=str,
|
|
||||||
help='Load pre-trained model (for training only)', required=False)
|
|
||||||
parser.add_argument('--warm_start', action='store_true',
|
|
||||||
help='Load model weights only, ignore specified layers.')
|
|
||||||
parser.add_argument('--seed', default=0, type=int,
|
|
||||||
help='Random seed for reproducable results.', required=False)
|
|
||||||
parser.add_argument('--njobs', default=8, type=int,
|
|
||||||
help='Number of threads for dataloader/decoding.', required=False)
|
|
||||||
parser.add_argument('--cpu', action='store_true', help='Disable GPU training.')
|
|
||||||
parser.add_argument('--no-pin', action='store_true',
|
|
||||||
help='Disable pin-memory for dataloader')
|
|
||||||
parser.add_argument('--test', action='store_true', help='Test the model.')
|
|
||||||
parser.add_argument('--no-msg', action='store_true', help='Hide all messages.')
|
|
||||||
parser.add_argument('--finetune', action='store_true', help='Finetune model')
|
|
||||||
parser.add_argument('--oneshotvc', action='store_true', help='Oneshot VC model')
|
|
||||||
parser.add_argument('--bilstm', action='store_true', help='BiLSTM VC model')
|
|
||||||
parser.add_argument('--lsa', action='store_true', help='Use location-sensitive attention (LSA)')
|
|
||||||
|
|
||||||
###
|
|
||||||
paras = parser.parse_args()
|
|
||||||
setattr(paras, 'gpu', not paras.cpu)
|
|
||||||
setattr(paras, 'pin_memory', not paras.no_pin)
|
|
||||||
setattr(paras, 'verbose', not paras.no_msg)
|
|
||||||
# Make the config dict dot visitable
|
|
||||||
config = HpsYaml(paras.config)
|
|
||||||
|
|
||||||
np.random.seed(paras.seed)
|
|
||||||
torch.manual_seed(paras.seed)
|
|
||||||
if torch.cuda.is_available():
|
|
||||||
torch.cuda.manual_seed_all(paras.seed)
|
|
||||||
|
|
||||||
print(">>> OneShot VC training ...")
|
|
||||||
mode = "train"
|
|
||||||
solver = Solver(config, paras, mode)
|
|
||||||
solver.load_data()
|
|
||||||
solver.set_model()
|
|
||||||
solver.exec()
|
|
||||||
print(">>> Oneshot VC train finished!")
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
from pydantic import BaseModel, Field
|
|
||||||
import os
|
|
||||||
from pathlib import Path
|
|
||||||
from enum import Enum
|
|
||||||
from typing import Any
|
|
||||||
from models.synthesizer.hparams import hparams
|
|
||||||
from models.synthesizer.train import train as synt_train
|
|
||||||
|
|
||||||
# Constants
|
|
||||||
SYN_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}synthesizer"
|
|
||||||
ENC_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}encoder"
|
|
||||||
|
|
||||||
|
|
||||||
# EXT_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}ppg_extractor"
|
|
||||||
# CONV_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}ppg2mel"
|
|
||||||
# ENC_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}encoder"
|
|
||||||
|
|
||||||
# Pre-Load models
|
|
||||||
if os.path.isdir(SYN_MODELS_DIRT):
|
|
||||||
synthesizers = Enum('synthesizers', list((file.name, file) for file in Path(SYN_MODELS_DIRT).glob("**/*.pt")))
|
|
||||||
print("Loaded synthesizer models: " + str(len(synthesizers)))
|
|
||||||
else:
|
|
||||||
raise Exception(f"Model folder {SYN_MODELS_DIRT} doesn't exist.")
|
|
||||||
|
|
||||||
if os.path.isdir(ENC_MODELS_DIRT):
|
|
||||||
encoders = Enum('encoders', list((file.name, file) for file in Path(ENC_MODELS_DIRT).glob("**/*.pt")))
|
|
||||||
print("Loaded encoders models: " + str(len(encoders)))
|
|
||||||
else:
|
|
||||||
raise Exception(f"Model folder {ENC_MODELS_DIRT} doesn't exist.")
|
|
||||||
|
|
||||||
class Model(str, Enum):
|
|
||||||
DEFAULT = "default"
|
|
||||||
|
|
||||||
class Input(BaseModel):
|
|
||||||
model: Model = Field(
|
|
||||||
Model.DEFAULT, title="模型类型",
|
|
||||||
)
|
|
||||||
# datasets_root: str = Field(
|
|
||||||
# ..., alias="预处理数据根目录", description="输入目录(相对/绝对),不适用于ppg2mel模型",
|
|
||||||
# format=True,
|
|
||||||
# example="..\\trainning_data\\"
|
|
||||||
# )
|
|
||||||
input_root: str = Field(
|
|
||||||
..., alias="输入目录", description="预处理数据根目录",
|
|
||||||
format=True,
|
|
||||||
example=f"..{os.sep}audiodata{os.sep}SV2TTS{os.sep}synthesizer"
|
|
||||||
)
|
|
||||||
run_id: str = Field(
|
|
||||||
"", alias="新模型名/运行ID", description="使用新ID进行重新训练,否则选择下面的模型进行继续训练",
|
|
||||||
)
|
|
||||||
synthesizer: synthesizers = Field(
|
|
||||||
..., alias="已有合成模型",
|
|
||||||
description="选择语音合成模型文件."
|
|
||||||
)
|
|
||||||
gpu: bool = Field(
|
|
||||||
True, alias="GPU训练", description="选择“是”,则使用GPU训练",
|
|
||||||
)
|
|
||||||
verbose: bool = Field(
|
|
||||||
True, alias="打印详情", description="选择“是”,输出更多详情",
|
|
||||||
)
|
|
||||||
encoder: encoders = Field(
|
|
||||||
..., alias="语音编码模型",
|
|
||||||
description="选择语音编码模型文件."
|
|
||||||
)
|
|
||||||
save_every: int = Field(
|
|
||||||
1000, alias="更新间隔", description="每隔n步则更新一次模型",
|
|
||||||
)
|
|
||||||
backup_every: int = Field(
|
|
||||||
10000, alias="保存间隔", description="每隔n步则保存一次模型",
|
|
||||||
)
|
|
||||||
log_every: int = Field(
|
|
||||||
500, alias="打印间隔", description="每隔n步则打印一次训练统计",
|
|
||||||
)
|
|
||||||
|
|
||||||
class AudioEntity(BaseModel):
|
|
||||||
content: bytes
|
|
||||||
mel: Any
|
|
||||||
|
|
||||||
class Output(BaseModel):
|
|
||||||
__root__: int
|
|
||||||
|
|
||||||
def render_output_ui(self, streamlit_app) -> None: # type: ignore
|
|
||||||
"""Custom output UI.
|
|
||||||
If this method is implmeneted, it will be used instead of the default Output UI renderer.
|
|
||||||
"""
|
|
||||||
streamlit_app.subheader(f"Training started with code: {self.__root__}")
|
|
||||||
|
|
||||||
def train(input: Input) -> Output:
|
|
||||||
"""Train(训练)"""
|
|
||||||
|
|
||||||
print(">>> Start training ...")
|
|
||||||
force_restart = len(input.run_id) > 0
|
|
||||||
if not force_restart:
|
|
||||||
input.run_id = Path(input.synthesizer.value).name.split('.')[0]
|
|
||||||
|
|
||||||
synt_train(
|
|
||||||
input.run_id,
|
|
||||||
input.input_root,
|
|
||||||
f"data{os.sep}ckpt{os.sep}synthesizer",
|
|
||||||
input.save_every,
|
|
||||||
input.backup_every,
|
|
||||||
input.log_every,
|
|
||||||
force_restart,
|
|
||||||
hparams
|
|
||||||
)
|
|
||||||
return Output(__root__=0)
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
https://openslr.magicdatatech.com/resources/62/aidatatang_200zh.tgz
|
|
||||||
out=download/aidatatang_200zh.tgz
|
|
||||||
https://openslr.magicdatatech.com/resources/68/train_set.tar.gz
|
|
||||||
out=download/magicdata.tgz
|
|
||||||
https://openslr.magicdatatech.com/resources/93/data_aishell3.tgz
|
|
||||||
out=download/aishell3.tgz
|
|
||||||
https://openslr.magicdatatech.com/resources/33/data_aishell.tgz
|
|
||||||
out=download/data_aishell.tgz
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
https://openslr.elda.org/resources/62/aidatatang_200zh.tgz
|
|
||||||
out=download/aidatatang_200zh.tgz
|
|
||||||
https://openslr.elda.org/resources/68/train_set.tar.gz
|
|
||||||
out=download/magicdata.tgz
|
|
||||||
https://openslr.elda.org/resources/93/data_aishell3.tgz
|
|
||||||
out=download/aishell3.tgz
|
|
||||||
https://openslr.elda.org/resources/33/data_aishell.tgz
|
|
||||||
out=download/data_aishell.tgz
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
https://us.openslr.org/resources/62/aidatatang_200zh.tgz
|
|
||||||
out=download/aidatatang_200zh.tgz
|
|
||||||
https://us.openslr.org/resources/68/train_set.tar.gz
|
|
||||||
out=download/magicdata.tgz
|
|
||||||
https://us.openslr.org/resources/93/data_aishell3.tgz
|
|
||||||
out=download/aishell3.tgz
|
|
||||||
https://us.openslr.org/resources/33/data_aishell.tgz
|
|
||||||
out=download/data_aishell.tgz
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
0c0ace77fe8ee77db8d7542d6eb0b7ddf09b1bfb880eb93a7fbdbf4611e9984b /datasets/download/aidatatang_200zh.tgz
|
|
||||||
be2507d431ad59419ec871e60674caedb2b585f84ffa01fe359784686db0e0cc /datasets/download/aishell3.tgz
|
|
||||||
a4a0313cde0a933e0e01a451f77de0a23d6c942f4694af5bb7f40b9dc38143fe /datasets/download/data_aishell.tgz
|
|
||||||
1d2647c614b74048cfe16492570cc5146d800afdc07483a43b31809772632143 /datasets/download/magicdata.tgz
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
https://www.openslr.org/resources/62/aidatatang_200zh.tgz
|
|
||||||
out=download/aidatatang_200zh.tgz
|
|
||||||
https://www.openslr.org/resources/68/train_set.tar.gz
|
|
||||||
out=download/magicdata.tgz
|
|
||||||
https://www.openslr.org/resources/93/data_aishell3.tgz
|
|
||||||
out=download/aishell3.tgz
|
|
||||||
https://www.openslr.org/resources/33/data_aishell.tgz
|
|
||||||
out=download/data_aishell.tgz
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
set -Eeuo pipefail
|
|
||||||
|
|
||||||
aria2c -x 10 --disable-ipv6 --input-file /workspace/datasets_download/${DATASET_MIRROR}.txt --dir /datasets --continue
|
|
||||||
|
|
||||||
echo "Verifying sha256sum..."
|
|
||||||
parallel --will-cite -a /workspace/datasets_download/datasets.sha256sum "echo -n {} | sha256sum -c"
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
set -Eeuo pipefail
|
|
||||||
|
|
||||||
mkdir -p /datasets/aidatatang_200zh
|
|
||||||
if [ -z "$(ls -A /datasets/aidatatang_200zh)" ] ; then
|
|
||||||
tar xvz --directory /datasets/ -f /datasets/download/aidatatang_200zh.tgz --exclude 'aidatatang_200zh/corpus/dev/*' --exclude 'aidatatang_200zh/corpus/test/*'
|
|
||||||
cd /datasets/aidatatang_200zh/corpus/train/
|
|
||||||
cat *.tar.gz | tar zxvf - -i
|
|
||||||
rm -f *.tar.gz
|
|
||||||
fi
|
|
||||||
|
|
||||||
mkdir -p /datasets/magicdata
|
|
||||||
if [ -z "$(ls -A /datasets/magicdata)" ] ; then
|
|
||||||
tar xvz --directory /datasets/magicdata -f /datasets/download/magicdata.tgz train/
|
|
||||||
fi
|
|
||||||
|
|
||||||
mkdir -p /datasets/aishell3
|
|
||||||
if [ -z "$(ls -A /datasets/aishell3)" ] ; then
|
|
||||||
tar xvz --directory /datasets/aishell3 -f /datasets/download/aishell3.tgz train/
|
|
||||||
fi
|
|
||||||
|
|
||||||
mkdir -p /datasets/data_aishell
|
|
||||||
if [ -z "$(ls -A /datasets/data_aishell)" ] ; then
|
|
||||||
tar xvz --directory /datasets/ -f /datasets/download/data_aishell.tgz
|
|
||||||
cd /datasets/data_aishell/wav/
|
|
||||||
cat *.tar.gz | tar zxvf - -i --exclude 'dev/*' --exclude 'test/*'
|
|
||||||
rm -f *.tar.gz
|
|
||||||
fi
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from control.toolbox import Toolbox
|
from toolbox import Toolbox
|
||||||
from utils.argutils import print_args
|
from utils.argutils import print_args
|
||||||
from utils.modelutils import check_model_paths
|
from utils.modelutils import check_model_paths
|
||||||
import argparse
|
import argparse
|
||||||
@@ -17,15 +17,15 @@ if __name__ == '__main__':
|
|||||||
"supported datasets.", default=None)
|
"supported datasets.", default=None)
|
||||||
parser.add_argument("-vc", "--vc_mode", action="store_true",
|
parser.add_argument("-vc", "--vc_mode", action="store_true",
|
||||||
help="Voice Conversion Mode(PPG based)")
|
help="Voice Conversion Mode(PPG based)")
|
||||||
parser.add_argument("-e", "--enc_models_dir", type=Path, default=f"data{os.sep}ckpt{os.sep}encoder",
|
parser.add_argument("-e", "--enc_models_dir", type=Path, default="encoder/saved_models",
|
||||||
help="Directory containing saved encoder models")
|
help="Directory containing saved encoder models")
|
||||||
parser.add_argument("-s", "--syn_models_dir", type=Path, default=f"data{os.sep}ckpt{os.sep}synthesizer",
|
parser.add_argument("-s", "--syn_models_dir", type=Path, default="synthesizer/saved_models",
|
||||||
help="Directory containing saved synthesizer models")
|
help="Directory containing saved synthesizer models")
|
||||||
parser.add_argument("-v", "--voc_models_dir", type=Path, default=f"data{os.sep}ckpt{os.sep}vocoder",
|
parser.add_argument("-v", "--voc_models_dir", type=Path, default="vocoder/saved_models",
|
||||||
help="Directory containing saved vocoder models")
|
help="Directory containing saved vocoder models")
|
||||||
parser.add_argument("-ex", "--extractor_models_dir", type=Path, default=f"data{os.sep}ckpt{os.sep}ppg_extractor",
|
parser.add_argument("-ex", "--extractor_models_dir", type=Path, default="ppg_extractor/saved_models",
|
||||||
help="Directory containing saved extrator models")
|
help="Directory containing saved extrator models")
|
||||||
parser.add_argument("-cv", "--convertor_models_dir", type=Path, default=f"data{os.sep}ckpt{os.sep}ppg2mel",
|
parser.add_argument("-cv", "--convertor_models_dir", type=Path, default="ppg2mel/saved_models",
|
||||||
help="Directory containing saved convert models")
|
help="Directory containing saved convert models")
|
||||||
parser.add_argument("--cpu", action="store_true", help=\
|
parser.add_argument("--cpu", action="store_true", help=\
|
||||||
"If True, processing is done on CPU, even when a GPU is available.")
|
"If True, processing is done on CPU, even when a GPU is available.")
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
version: '3.8'
|
|
||||||
|
|
||||||
services:
|
|
||||||
server:
|
|
||||||
image: mockingbird:latest
|
|
||||||
build: .
|
|
||||||
volumes:
|
|
||||||
- ./datasets:/datasets
|
|
||||||
- ./synthesizer/saved_models:/workspace/synthesizer/saved_models
|
|
||||||
environment:
|
|
||||||
- DATASET_MIRROR=US
|
|
||||||
- FORCE_RETRAIN=false
|
|
||||||
- TRAIN_DATASETS=aidatatang_200zh magicdata aishell3 data_aishell
|
|
||||||
- TRAIN_SKIP_EXISTING=true
|
|
||||||
ports:
|
|
||||||
- 8080:8080
|
|
||||||
deploy:
|
|
||||||
resources:
|
|
||||||
reservations:
|
|
||||||
devices:
|
|
||||||
- driver: nvidia
|
|
||||||
device_ids: [ '0' ]
|
|
||||||
capabilities: [ gpu ]
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
if [ -z "$(ls -A /workspace/synthesizer/saved_models)" ] || [ "$FORCE_RETRAIN" = true ] ; then
|
|
||||||
/workspace/datasets_download/download.sh
|
|
||||||
/workspace/datasets_download/extract.sh
|
|
||||||
for DATASET in ${TRAIN_DATASETS}
|
|
||||||
do
|
|
||||||
if [ "$TRAIN_SKIP_EXISTING" = true ] ; then
|
|
||||||
python pre.py /datasets -d ${DATASET} -n $(nproc) --skip_existing
|
|
||||||
else
|
|
||||||
python pre.py /datasets -d ${DATASET} -n $(nproc)
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
python synthesizer_train.py mandarin /datasets/SV2TTS/synthesizer
|
|
||||||
fi
|
|
||||||
|
|
||||||
python web.py
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from scipy.ndimage.morphology import binary_dilation
|
from scipy.ndimage.morphology import binary_dilation
|
||||||
from models.encoder.params_data import *
|
from encoder.params_data import *
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
from warnings import warn
|
from warnings import warn
|
||||||
@@ -56,8 +56,8 @@ def wav_to_mel_spectrogram(wav):
|
|||||||
Note: this not a log-mel spectrogram.
|
Note: this not a log-mel spectrogram.
|
||||||
"""
|
"""
|
||||||
frames = librosa.feature.melspectrogram(
|
frames = librosa.feature.melspectrogram(
|
||||||
y=wav,
|
wav,
|
||||||
sr=sampling_rate,
|
sampling_rate,
|
||||||
n_fft=int(sampling_rate * mel_window_length / 1000),
|
n_fft=int(sampling_rate * mel_window_length / 1000),
|
||||||
hop_length=int(sampling_rate * mel_window_step / 1000),
|
hop_length=int(sampling_rate * mel_window_step / 1000),
|
||||||
n_mels=mel_n_channels
|
n_mels=mel_n_channels
|
||||||
2
encoder/data_objects/__init__.py
Normal file
2
encoder/data_objects/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from encoder.data_objects.speaker_verification_dataset import SpeakerVerificationDataset
|
||||||
|
from encoder.data_objects.speaker_verification_dataset import SpeakerVerificationDataLoader
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from models.encoder.data_objects.random_cycler import RandomCycler
|
from encoder.data_objects.random_cycler import RandomCycler
|
||||||
from models.encoder.data_objects.utterance import Utterance
|
from encoder.data_objects.utterance import Utterance
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# Contains the set of utterances of a single speaker
|
# Contains the set of utterances of a single speaker
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from typing import List
|
from typing import List
|
||||||
from models.encoder.data_objects.speaker import Speaker
|
from encoder.data_objects.speaker import Speaker
|
||||||
|
|
||||||
class SpeakerBatch:
|
class SpeakerBatch:
|
||||||
def __init__(self, speakers: List[Speaker], utterances_per_speaker: int, n_frames: int):
|
def __init__(self, speakers: List[Speaker], utterances_per_speaker: int, n_frames: int):
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
from models.encoder.data_objects.random_cycler import RandomCycler
|
from encoder.data_objects.random_cycler import RandomCycler
|
||||||
from models.encoder.data_objects.speaker_batch import SpeakerBatch
|
from encoder.data_objects.speaker_batch import SpeakerBatch
|
||||||
from models.encoder.data_objects.speaker import Speaker
|
from encoder.data_objects.speaker import Speaker
|
||||||
from models.encoder.params_data import partials_n_frames
|
from encoder.params_data import partials_n_frames
|
||||||
from torch.utils.data import Dataset, DataLoader
|
from torch.utils.data import Dataset, DataLoader
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
from models.encoder.params_data import *
|
from encoder.params_data import *
|
||||||
from models.encoder.model import SpeakerEncoder
|
from encoder.model import SpeakerEncoder
|
||||||
from models.encoder.audio import preprocess_wav # We want to expose this function from here
|
from encoder.audio import preprocess_wav # We want to expose this function from here
|
||||||
from matplotlib import cm
|
from matplotlib import cm
|
||||||
from models.encoder import audio
|
from encoder import audio
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from models.encoder.params_model import *
|
from encoder.params_model import *
|
||||||
from models.encoder.params_data import *
|
from encoder.params_data import *
|
||||||
from scipy.interpolate import interp1d
|
from scipy.interpolate import interp1d
|
||||||
from sklearn.metrics import roc_curve
|
from sklearn.metrics import roc_curve
|
||||||
from torch.nn.utils import clip_grad_norm_
|
from torch.nn.utils import clip_grad_norm_
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
from multiprocess.pool import ThreadPool
|
from multiprocess.pool import ThreadPool
|
||||||
from models.encoder.params_data import *
|
from encoder.params_data import *
|
||||||
from models.encoder.config import librispeech_datasets, anglophone_nationalites
|
from encoder.config import librispeech_datasets, anglophone_nationalites
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from models.encoder import audio
|
from encoder import audio
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -22,7 +22,7 @@ class DatasetLog:
|
|||||||
self._log_params()
|
self._log_params()
|
||||||
|
|
||||||
def _log_params(self):
|
def _log_params(self):
|
||||||
from models.encoder import params_data
|
from encoder import params_data
|
||||||
self.write_line("Parameter values:")
|
self.write_line("Parameter values:")
|
||||||
for param_name in (p for p in dir(params_data) if not p.startswith("__")):
|
for param_name in (p for p in dir(params_data) if not p.startswith("__")):
|
||||||
value = getattr(params_data, param_name)
|
value = getattr(params_data, param_name)
|
||||||
BIN
encoder/saved_models/pretrained.pt
Normal file
BIN
encoder/saved_models/pretrained.pt
Normal file
Binary file not shown.
@@ -1,7 +1,7 @@
|
|||||||
from models.encoder.visualizations import Visualizations
|
from encoder.visualizations import Visualizations
|
||||||
from models.encoder.data_objects import SpeakerVerificationDataLoader, SpeakerVerificationDataset
|
from encoder.data_objects import SpeakerVerificationDataLoader, SpeakerVerificationDataset
|
||||||
from models.encoder.params_model import *
|
from encoder.params_model import *
|
||||||
from models.encoder.model import SpeakerEncoder
|
from encoder.model import SpeakerEncoder
|
||||||
from utils.profiler import Profiler
|
from utils.profiler import Profiler
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import torch
|
import torch
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
from models.encoder.data_objects.speaker_verification_dataset import SpeakerVerificationDataset
|
from encoder.data_objects.speaker_verification_dataset import SpeakerVerificationDataset
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from time import perf_counter as timer
|
from time import perf_counter as timer
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
@@ -65,8 +65,8 @@ class Visualizations:
|
|||||||
def log_params(self):
|
def log_params(self):
|
||||||
if self.disabled:
|
if self.disabled:
|
||||||
return
|
return
|
||||||
from models.encoder import params_data
|
from encoder import params_data
|
||||||
from models.encoder import params_model
|
from encoder import params_model
|
||||||
param_string = "<b>Model parameters</b>:<br>"
|
param_string = "<b>Model parameters</b>:<br>"
|
||||||
for param_name in (p for p in dir(params_model) if not p.startswith("__")):
|
for param_name in (p for p in dir(params_model) if not p.startswith("__")):
|
||||||
value = getattr(params_model, param_name)
|
value = getattr(params_model, param_name)
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
import argparse
|
from encoder.preprocess import preprocess_librispeech, preprocess_voxceleb1, preprocess_voxceleb2, preprocess_aidatatang_200zh
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from models.encoder.preprocess import (preprocess_aidatatang_200zh,
|
|
||||||
preprocess_librispeech, preprocess_voxceleb1,
|
|
||||||
preprocess_voxceleb2)
|
|
||||||
from utils.argutils import print_args
|
from utils.argutils import print_args
|
||||||
|
from pathlib import Path
|
||||||
|
import argparse
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
class MyFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):
|
class MyFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from utils.argutils import print_args
|
from utils.argutils import print_args
|
||||||
from models.encoder.train import train
|
from encoder.train import train
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
120
gen_voice.py
120
gen_voice.py
@@ -1,120 +0,0 @@
|
|||||||
from models.synthesizer.inference import Synthesizer
|
|
||||||
from models.encoder import inference as encoder
|
|
||||||
from models.vocoder.hifigan import inference as gan_vocoder
|
|
||||||
from pathlib import Path
|
|
||||||
import numpy as np
|
|
||||||
import soundfile as sf
|
|
||||||
import torch
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import cn2an
|
|
||||||
|
|
||||||
vocoder = gan_vocoder
|
|
||||||
|
|
||||||
def gen_one_wav(synthesizer, in_fpath, embed, texts, file_name, seq):
|
|
||||||
embeds = [embed] * len(texts)
|
|
||||||
# If you know what the attention layer alignments are, you can retrieve them here by
|
|
||||||
# passing return_alignments=True
|
|
||||||
specs = synthesizer.synthesize_spectrograms(texts, embeds, style_idx=-1, min_stop_token=4, steps=400)
|
|
||||||
#spec = specs[0]
|
|
||||||
breaks = [spec.shape[1] for spec in specs]
|
|
||||||
spec = np.concatenate(specs, axis=1)
|
|
||||||
|
|
||||||
# If seed is specified, reset torch seed and reload vocoder
|
|
||||||
# Synthesizing the waveform is fairly straightforward. Remember that the longer the
|
|
||||||
# spectrogram, the more time-efficient the vocoder.
|
|
||||||
generated_wav, output_sample_rate = vocoder.infer_waveform(spec)
|
|
||||||
|
|
||||||
# Add breaks
|
|
||||||
b_ends = np.cumsum(np.array(breaks) * synthesizer.hparams.hop_size)
|
|
||||||
b_starts = np.concatenate(([0], b_ends[:-1]))
|
|
||||||
wavs = [generated_wav[start:end] for start, end, in zip(b_starts, b_ends)]
|
|
||||||
breaks = [np.zeros(int(0.15 * synthesizer.sample_rate))] * len(breaks)
|
|
||||||
generated_wav = np.concatenate([i for w, b in zip(wavs, breaks) for i in (w, b)])
|
|
||||||
|
|
||||||
## Post-generation
|
|
||||||
# There's a bug with sounddevice that makes the audio cut one second earlier, so we
|
|
||||||
# pad it.
|
|
||||||
|
|
||||||
# Trim excess silences to compensate for gaps in spectrograms (issue #53)
|
|
||||||
generated_wav = encoder.preprocess_wav(generated_wav)
|
|
||||||
generated_wav = generated_wav / np.abs(generated_wav).max() * 0.97
|
|
||||||
|
|
||||||
# Save it on the disk
|
|
||||||
model=os.path.basename(in_fpath)
|
|
||||||
filename = "%s_%d_%s.wav" %(file_name, seq, model)
|
|
||||||
sf.write(filename, generated_wav, synthesizer.sample_rate)
|
|
||||||
|
|
||||||
print("\nSaved output as %s\n\n" % filename)
|
|
||||||
|
|
||||||
|
|
||||||
def generate_wav(enc_model_fpath, syn_model_fpath, voc_model_fpath, in_fpath, input_txt, file_name):
|
|
||||||
if torch.cuda.is_available():
|
|
||||||
device_id = torch.cuda.current_device()
|
|
||||||
gpu_properties = torch.cuda.get_device_properties(device_id)
|
|
||||||
## Print some environment information (for debugging purposes)
|
|
||||||
print("Found %d GPUs available. Using GPU %d (%s) of compute capability %d.%d with "
|
|
||||||
"%.1fGb total memory.\n" %
|
|
||||||
(torch.cuda.device_count(),
|
|
||||||
device_id,
|
|
||||||
gpu_properties.name,
|
|
||||||
gpu_properties.major,
|
|
||||||
gpu_properties.minor,
|
|
||||||
gpu_properties.total_memory / 1e9))
|
|
||||||
else:
|
|
||||||
print("Using CPU for inference.\n")
|
|
||||||
|
|
||||||
print("Preparing the encoder, the synthesizer and the vocoder...")
|
|
||||||
encoder.load_model(enc_model_fpath)
|
|
||||||
synthesizer = Synthesizer(syn_model_fpath)
|
|
||||||
vocoder.load_model(voc_model_fpath)
|
|
||||||
|
|
||||||
encoder_wav = synthesizer.load_preprocess_wav(in_fpath)
|
|
||||||
embed, partial_embeds, _ = encoder.embed_utterance(encoder_wav, return_partials=True)
|
|
||||||
|
|
||||||
texts = input_txt.split("\n")
|
|
||||||
seq=0
|
|
||||||
each_num=1500
|
|
||||||
|
|
||||||
punctuation = '!,。、,' # punctuate and split/clean text
|
|
||||||
processed_texts = []
|
|
||||||
cur_num = 0
|
|
||||||
for text in texts:
|
|
||||||
for processed_text in re.sub(r'[{}]+'.format(punctuation), '\n', text).split('\n'):
|
|
||||||
if processed_text:
|
|
||||||
processed_texts.append(processed_text.strip())
|
|
||||||
cur_num += len(processed_text.strip())
|
|
||||||
if cur_num > each_num:
|
|
||||||
seq = seq +1
|
|
||||||
gen_one_wav(synthesizer, in_fpath, embed, processed_texts, file_name, seq)
|
|
||||||
processed_texts = []
|
|
||||||
cur_num = 0
|
|
||||||
|
|
||||||
if len(processed_texts)>0:
|
|
||||||
seq = seq +1
|
|
||||||
gen_one_wav(synthesizer, in_fpath, embed, processed_texts, file_name, seq)
|
|
||||||
|
|
||||||
if (len(sys.argv)>=3):
|
|
||||||
my_txt = ""
|
|
||||||
print("reading from :", sys.argv[1])
|
|
||||||
with open(sys.argv[1], "r") as f:
|
|
||||||
for line in f.readlines():
|
|
||||||
#line = line.strip('\n')
|
|
||||||
my_txt += line
|
|
||||||
txt_file_name = sys.argv[1]
|
|
||||||
wav_file_name = sys.argv[2]
|
|
||||||
|
|
||||||
output = cn2an.transform(my_txt, "an2cn")
|
|
||||||
print(output)
|
|
||||||
generate_wav(
|
|
||||||
Path("encoder/saved_models/pretrained.pt"),
|
|
||||||
Path("synthesizer/saved_models/mandarin.pt"),
|
|
||||||
Path("vocoder/saved_models/pretrained/g_hifigan.pt"), wav_file_name, output, txt_file_name
|
|
||||||
)
|
|
||||||
|
|
||||||
else:
|
|
||||||
print("please input the file name")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,27 +1,26 @@
|
|||||||
|
from asyncio.windows_events import NULL
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from models.encoder import inference as encoder
|
from encoder import inference as encoder
|
||||||
import librosa
|
import librosa
|
||||||
from scipy.io.wavfile import write
|
from scipy.io.wavfile import write
|
||||||
import re
|
import re
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from control.mkgui.base.components.types import FileContent
|
from mkgui.base.components.types import FileContent
|
||||||
from models.vocoder.hifigan import inference as gan_vocoder
|
from vocoder.hifigan import inference as gan_vocoder
|
||||||
from models.synthesizer.inference import Synthesizer
|
from synthesizer.inference import Synthesizer
|
||||||
from typing import Any, Tuple
|
from typing import Any
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
AUDIO_SAMPLES_DIR = f"data{os.sep}samples{os.sep}"
|
AUDIO_SAMPLES_DIR = 'samples\\'
|
||||||
SYN_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}synthesizer"
|
SYN_MODELS_DIRT = "synthesizer\\saved_models"
|
||||||
ENC_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}encoder"
|
ENC_MODELS_DIRT = "encoder\\saved_models"
|
||||||
VOC_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}vocoder"
|
VOC_MODELS_DIRT = "vocoder\\saved_models"
|
||||||
TEMP_SOURCE_AUDIO = f"wavs{os.sep}temp_source.wav"
|
TEMP_SOURCE_AUDIO = "wavs/temp_source.wav"
|
||||||
TEMP_RESULT_AUDIO = f"wavs{os.sep}temp_result.wav"
|
TEMP_RESULT_AUDIO = "wavs/temp_result.wav"
|
||||||
if not os.path.isdir("wavs"):
|
|
||||||
os.makedirs("wavs")
|
|
||||||
|
|
||||||
# Load local sample audio as options TODO: load dataset
|
# Load local sample audio as options TODO: load dataset
|
||||||
if os.path.isdir(AUDIO_SAMPLES_DIR):
|
if os.path.isdir(AUDIO_SAMPLES_DIR):
|
||||||
@@ -31,7 +30,7 @@ if os.path.isdir(SYN_MODELS_DIRT):
|
|||||||
synthesizers = Enum('synthesizers', list((file.name, file) for file in Path(SYN_MODELS_DIRT).glob("**/*.pt")))
|
synthesizers = Enum('synthesizers', list((file.name, file) for file in Path(SYN_MODELS_DIRT).glob("**/*.pt")))
|
||||||
print("Loaded synthesizer models: " + str(len(synthesizers)))
|
print("Loaded synthesizer models: " + str(len(synthesizers)))
|
||||||
else:
|
else:
|
||||||
raise Exception(f"Model folder {SYN_MODELS_DIRT} doesn't exist. 请将模型文件位置移动到上述位置中进行重试!")
|
raise Exception(f"Model folder {SYN_MODELS_DIRT} doesn't exist.")
|
||||||
|
|
||||||
if os.path.isdir(ENC_MODELS_DIRT):
|
if os.path.isdir(ENC_MODELS_DIRT):
|
||||||
encoders = Enum('encoders', list((file.name, file) for file in Path(ENC_MODELS_DIRT).glob("**/*.pt")))
|
encoders = Enum('encoders', list((file.name, file) for file in Path(ENC_MODELS_DIRT).glob("**/*.pt")))
|
||||||
@@ -51,11 +50,9 @@ class Input(BaseModel):
|
|||||||
..., example="欢迎使用工具箱, 现已支持中文输入!", alias="文本内容"
|
..., example="欢迎使用工具箱, 现已支持中文输入!", alias="文本内容"
|
||||||
)
|
)
|
||||||
local_audio_file: audio_input_selection = Field(
|
local_audio_file: audio_input_selection = Field(
|
||||||
..., alias="选择语音(本地wav)",
|
..., alias="输入语音(本地wav)",
|
||||||
description="选择本地语音文件."
|
description="选择本地语音文件."
|
||||||
)
|
)
|
||||||
record_audio_file: FileContent = Field(default=None, alias="录制语音",
|
|
||||||
description="录音.", is_recorder=True, mime_type="audio/wav")
|
|
||||||
upload_audio_file: FileContent = Field(default=None, alias="或上传语音",
|
upload_audio_file: FileContent = Field(default=None, alias="或上传语音",
|
||||||
description="拖拽或点击上传.", mime_type="audio/wav")
|
description="拖拽或点击上传.", mime_type="audio/wav")
|
||||||
encoder: encoders = Field(
|
encoder: encoders = Field(
|
||||||
@@ -76,7 +73,7 @@ class AudioEntity(BaseModel):
|
|||||||
mel: Any
|
mel: Any
|
||||||
|
|
||||||
class Output(BaseModel):
|
class Output(BaseModel):
|
||||||
__root__: Tuple[AudioEntity, AudioEntity]
|
__root__: tuple[AudioEntity, AudioEntity]
|
||||||
|
|
||||||
def render_output_ui(self, streamlit_app, input) -> None: # type: ignore
|
def render_output_ui(self, streamlit_app, input) -> None: # type: ignore
|
||||||
"""Custom output UI.
|
"""Custom output UI.
|
||||||
@@ -105,12 +102,7 @@ def synthesize(input: Input) -> Output:
|
|||||||
gan_vocoder.load_model(Path(input.vocoder.value))
|
gan_vocoder.load_model(Path(input.vocoder.value))
|
||||||
|
|
||||||
# load file
|
# load file
|
||||||
if input.record_audio_file != None:
|
if input.upload_audio_file != None:
|
||||||
with open(TEMP_SOURCE_AUDIO, "w+b") as f:
|
|
||||||
f.write(input.record_audio_file.as_bytes())
|
|
||||||
f.seek(0)
|
|
||||||
wav, sample_rate = librosa.load(TEMP_SOURCE_AUDIO)
|
|
||||||
elif input.upload_audio_file != None:
|
|
||||||
with open(TEMP_SOURCE_AUDIO, "w+b") as f:
|
with open(TEMP_SOURCE_AUDIO, "w+b") as f:
|
||||||
f.write(input.upload_audio_file.as_bytes())
|
f.write(input.upload_audio_file.as_bytes())
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
@@ -1,29 +1,31 @@
|
|||||||
import os
|
from asyncio.windows_events import NULL
|
||||||
from enum import Enum
|
from synthesizer.inference import Synthesizer
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any, Tuple
|
|
||||||
|
|
||||||
import librosa
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
import torch
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
from encoder import inference as speacker_encoder
|
||||||
|
import torch
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from enum import Enum
|
||||||
|
import ppg_extractor as Extractor
|
||||||
|
import ppg2mel as Convertor
|
||||||
|
import librosa
|
||||||
from scipy.io.wavfile import write
|
from scipy.io.wavfile import write
|
||||||
|
import re
|
||||||
|
import numpy as np
|
||||||
|
from mkgui.base.components.types import FileContent
|
||||||
|
from vocoder.hifigan import inference as gan_vocoder
|
||||||
|
from typing import Any
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
import models.ppg2mel as Convertor
|
|
||||||
import models.ppg_extractor as Extractor
|
|
||||||
from control.mkgui.base.components.types import FileContent
|
|
||||||
from models.encoder import inference as speacker_encoder
|
|
||||||
from models.synthesizer.inference import Synthesizer
|
|
||||||
from models.vocoder.hifigan import inference as gan_vocoder
|
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
AUDIO_SAMPLES_DIR = f'data{os.sep}samples{os.sep}'
|
AUDIO_SAMPLES_DIR = 'samples\\'
|
||||||
EXT_MODELS_DIRT = f'data{os.sep}ckpt{os.sep}ppg_extractor'
|
EXT_MODELS_DIRT = "ppg_extractor\\saved_models"
|
||||||
CONV_MODELS_DIRT = f'data{os.sep}ckpt{os.sep}ppg2mel'
|
CONV_MODELS_DIRT = "ppg2mel\\saved_models"
|
||||||
VOC_MODELS_DIRT = f'data{os.sep}ckpt{os.sep}vocoder'
|
VOC_MODELS_DIRT = "vocoder\\saved_models"
|
||||||
TEMP_SOURCE_AUDIO = f'wavs{os.sep}temp_source.wav'
|
TEMP_SOURCE_AUDIO = "wavs/temp_source.wav"
|
||||||
TEMP_TARGET_AUDIO = f'wavs{os.sep}temp_target.wav'
|
TEMP_TARGET_AUDIO = "wavs/temp_target.wav"
|
||||||
TEMP_RESULT_AUDIO = f'wavs{os.sep}temp_result.wav'
|
TEMP_RESULT_AUDIO = "wavs/temp_result.wav"
|
||||||
|
|
||||||
# Load local sample audio as options TODO: load dataset
|
# Load local sample audio as options TODO: load dataset
|
||||||
if os.path.isdir(AUDIO_SAMPLES_DIR):
|
if os.path.isdir(AUDIO_SAMPLES_DIR):
|
||||||
@@ -69,7 +71,7 @@ class Input(BaseModel):
|
|||||||
description="选择语音转换模型文件."
|
description="选择语音转换模型文件."
|
||||||
)
|
)
|
||||||
vocoder: vocoders = Field(
|
vocoder: vocoders = Field(
|
||||||
..., alias="语音解码模型",
|
..., alias="语音编码模型",
|
||||||
description="选择语音解码模型文件(目前只支持HifiGan类型)."
|
description="选择语音解码模型文件(目前只支持HifiGan类型)."
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -78,7 +80,7 @@ class AudioEntity(BaseModel):
|
|||||||
mel: Any
|
mel: Any
|
||||||
|
|
||||||
class Output(BaseModel):
|
class Output(BaseModel):
|
||||||
__root__: Tuple[AudioEntity, AudioEntity, AudioEntity]
|
__root__: tuple[AudioEntity, AudioEntity, AudioEntity]
|
||||||
|
|
||||||
def render_output_ui(self, streamlit_app, input) -> None: # type: ignore
|
def render_output_ui(self, streamlit_app, input) -> None: # type: ignore
|
||||||
"""Custom output UI.
|
"""Custom output UI.
|
||||||
@@ -131,10 +133,9 @@ def convert(input: Input) -> Output:
|
|||||||
|
|
||||||
ppg = extractor.extract_from_wav(src_wav)
|
ppg = extractor.extract_from_wav(src_wav)
|
||||||
# Import necessary dependency of Voice Conversion
|
# Import necessary dependency of Voice Conversion
|
||||||
from utils.f0_utils import (compute_f0, compute_mean_std, f02lf0,
|
from utils.f0_utils import compute_f0, f02lf0, compute_mean_std, get_converted_lf0uv
|
||||||
get_converted_lf0uv)
|
|
||||||
ref_lf0_mean, ref_lf0_std = compute_mean_std(f02lf0(compute_f0(ref_wav)))
|
ref_lf0_mean, ref_lf0_std = compute_mean_std(f02lf0(compute_f0(ref_wav)))
|
||||||
speacker_encoder.load_model(Path(f"data{os.sep}ckpt{os.sep}encoder{os.sep}pretrained_bak_5805000.pt"))
|
speacker_encoder.load_model(Path("encoder/saved_models/pretrained_bak_5805000.pt"))
|
||||||
embed = speacker_encoder.embed_utterance(ref_wav)
|
embed = speacker_encoder.embed_utterance(ref_wav)
|
||||||
lf0_uv = get_converted_lf0uv(src_wav, ref_lf0_mean, ref_lf0_std, convert=True)
|
lf0_uv = get_converted_lf0uv(src_wav, ref_lf0_mean, ref_lf0_std, convert=True)
|
||||||
min_len = min(ppg.shape[1], len(lf0_uv))
|
min_len = min(ppg.shape[1], len(lf0_uv))
|
||||||
@@ -37,12 +37,6 @@ def is_single_file_property(property: Dict) -> bool:
|
|||||||
# TODO: binary?
|
# TODO: binary?
|
||||||
return property.get("format") == "byte"
|
return property.get("format") == "byte"
|
||||||
|
|
||||||
def is_single_autio_property(property: Dict) -> bool:
|
|
||||||
if property.get("type") != "string":
|
|
||||||
return False
|
|
||||||
# TODO: binary?
|
|
||||||
return property.get("format") == "bytes"
|
|
||||||
|
|
||||||
|
|
||||||
def is_single_directory_property(property: Dict) -> bool:
|
def is_single_directory_property(property: Dict) -> bool:
|
||||||
if property.get("type") != "string":
|
if property.get("type") != "string":
|
||||||
@@ -2,7 +2,7 @@ import datetime
|
|||||||
import inspect
|
import inspect
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import sys
|
import sys
|
||||||
from os import getcwd, unlink, path
|
from os import getcwd, unlink
|
||||||
from platform import system
|
from platform import system
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
from typing import Any, Callable, Dict, List, Type
|
from typing import Any, Callable, Dict, List, Type
|
||||||
@@ -14,13 +14,14 @@ from fastapi.encoders import jsonable_encoder
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
from pydantic import BaseModel, ValidationError, parse_obj_as
|
from pydantic import BaseModel, ValidationError, parse_obj_as
|
||||||
|
|
||||||
from control.mkgui.base import Opyrator
|
from mkgui.base import Opyrator
|
||||||
from control.mkgui.base.core import name_to_title
|
from mkgui.base.core import name_to_title
|
||||||
from . import schema_utils
|
from mkgui.base.ui import schema_utils
|
||||||
from .streamlit_utils import CUSTOM_STREAMLIT_CSS
|
from mkgui.base.ui.streamlit_utils import CUSTOM_STREAMLIT_CSS
|
||||||
|
|
||||||
STREAMLIT_RUNNER_SNIPPET = """
|
STREAMLIT_RUNNER_SNIPPET = """
|
||||||
from control.mkgui.base.ui import render_streamlit_ui
|
from mkgui.base.ui import render_streamlit_ui
|
||||||
|
from mkgui.base import Opyrator
|
||||||
|
|
||||||
import streamlit as st
|
import streamlit as st
|
||||||
|
|
||||||
@@ -243,13 +244,6 @@ class InputUI:
|
|||||||
if "mime_type" in property:
|
if "mime_type" in property:
|
||||||
file_extension = mimetypes.guess_extension(property["mime_type"])
|
file_extension = mimetypes.guess_extension(property["mime_type"])
|
||||||
|
|
||||||
if "is_recorder" in property:
|
|
||||||
from audio_recorder_streamlit import audio_recorder
|
|
||||||
audio_bytes = audio_recorder()
|
|
||||||
if audio_bytes:
|
|
||||||
streamlit_app.audio(audio_bytes, format="audio/wav")
|
|
||||||
return audio_bytes
|
|
||||||
|
|
||||||
uploaded_file = streamlit_app.file_uploader(
|
uploaded_file = streamlit_app.file_uploader(
|
||||||
**streamlit_kwargs, accept_multiple_files=False, type=file_extension
|
**streamlit_kwargs, accept_multiple_files=False, type=file_extension
|
||||||
)
|
)
|
||||||
@@ -269,39 +263,6 @@ class InputUI:
|
|||||||
streamlit_app.video(bytes, format=property.get("mime_type"))
|
streamlit_app.video(bytes, format=property.get("mime_type"))
|
||||||
return bytes
|
return bytes
|
||||||
|
|
||||||
def _render_single_audio_input(
|
|
||||||
self, streamlit_app: st, key: str, property: Dict
|
|
||||||
) -> Any:
|
|
||||||
# streamlit_kwargs = self._get_default_streamlit_input_kwargs(key, property)
|
|
||||||
from audio_recorder_streamlit import audio_recorder
|
|
||||||
audio_bytes = audio_recorder()
|
|
||||||
if audio_bytes:
|
|
||||||
streamlit_app.audio(audio_bytes, format="audio/wav")
|
|
||||||
return audio_bytes
|
|
||||||
|
|
||||||
# file_extension = None
|
|
||||||
# if "mime_type" in property:
|
|
||||||
# file_extension = mimetypes.guess_extension(property["mime_type"])
|
|
||||||
|
|
||||||
# uploaded_file = streamlit_app.file_uploader(
|
|
||||||
# **streamlit_kwargs, accept_multiple_files=False, type=file_extension
|
|
||||||
# )
|
|
||||||
# if uploaded_file is None:
|
|
||||||
# return None
|
|
||||||
|
|
||||||
# bytes = uploaded_file.getvalue()
|
|
||||||
# if property.get("mime_type"):
|
|
||||||
# if is_compatible_audio(property["mime_type"]):
|
|
||||||
# # Show audio
|
|
||||||
# streamlit_app.audio(bytes, format=property.get("mime_type"))
|
|
||||||
# if is_compatible_image(property["mime_type"]):
|
|
||||||
# # Show image
|
|
||||||
# streamlit_app.image(bytes)
|
|
||||||
# if is_compatible_video(property["mime_type"]):
|
|
||||||
# # Show video
|
|
||||||
# streamlit_app.video(bytes, format=property.get("mime_type"))
|
|
||||||
# return bytes
|
|
||||||
|
|
||||||
def _render_single_string_input(
|
def _render_single_string_input(
|
||||||
self, streamlit_app: st, key: str, property: Dict
|
self, streamlit_app: st, key: str, property: Dict
|
||||||
) -> Any:
|
) -> Any:
|
||||||
@@ -846,20 +807,18 @@ class OutputUI:
|
|||||||
|
|
||||||
def getOpyrator(mode: str) -> Opyrator:
|
def getOpyrator(mode: str) -> Opyrator:
|
||||||
if mode == None or mode.startswith('VC'):
|
if mode == None or mode.startswith('VC'):
|
||||||
from control.mkgui.app_vc import convert
|
from mkgui.app_vc import convert
|
||||||
return Opyrator(convert)
|
return Opyrator(convert)
|
||||||
if mode == None or mode.startswith('预处理'):
|
if mode == None or mode.startswith('预处理'):
|
||||||
from control.mkgui.preprocess import preprocess
|
from mkgui.preprocess import preprocess
|
||||||
return Opyrator(preprocess)
|
return Opyrator(preprocess)
|
||||||
if mode == None or mode.startswith('模型训练'):
|
if mode == None or mode.startswith('模型训练'):
|
||||||
from control.mkgui.train import train
|
from mkgui.train import train
|
||||||
return Opyrator(train)
|
return Opyrator(train)
|
||||||
if mode == None or mode.startswith('模型训练(VC)'):
|
from mkgui.app import synthesize
|
||||||
from control.mkgui.train_vc import train_vc
|
|
||||||
return Opyrator(train_vc)
|
|
||||||
from control.mkgui.app import synthesize
|
|
||||||
return Opyrator(synthesize)
|
return Opyrator(synthesize)
|
||||||
|
|
||||||
|
|
||||||
def render_streamlit_ui() -> None:
|
def render_streamlit_ui() -> None:
|
||||||
# init
|
# init
|
||||||
session_state = st.session_state
|
session_state = st.session_state
|
||||||
@@ -870,7 +829,7 @@ def render_streamlit_ui() -> None:
|
|||||||
with st.spinner("Loading MockingBird GUI. Please wait..."):
|
with st.spinner("Loading MockingBird GUI. Please wait..."):
|
||||||
session_state.mode = st.sidebar.selectbox(
|
session_state.mode = st.sidebar.selectbox(
|
||||||
'模式选择',
|
'模式选择',
|
||||||
( "AI拟音", "VC拟音", "预处理", "模型训练", "模型训练(VC)")
|
( "AI拟音", "VC拟音", "预处理", "模型训练")
|
||||||
)
|
)
|
||||||
if "mode" in session_state:
|
if "mode" in session_state:
|
||||||
mode = session_state.mode
|
mode = session_state.mode
|
||||||
@@ -883,7 +842,7 @@ def render_streamlit_ui() -> None:
|
|||||||
col2.title(title)
|
col2.title(title)
|
||||||
col2.markdown("欢迎使用MockingBird Web 2")
|
col2.markdown("欢迎使用MockingBird Web 2")
|
||||||
|
|
||||||
image = Image.open(path.join('control','mkgui', 'static', 'mb.png'))
|
image = Image.open('.\\mkgui\\static\\mb.png')
|
||||||
col1.image(image)
|
col1.image(image)
|
||||||
|
|
||||||
st.markdown("---")
|
st.markdown("---")
|
||||||
@@ -891,13 +850,6 @@ def render_streamlit_ui() -> None:
|
|||||||
|
|
||||||
with left:
|
with left:
|
||||||
st.header("Control 控制")
|
st.header("Control 控制")
|
||||||
# if session_state.mode in ["AI拟音", "VC拟音"] :
|
|
||||||
# from audiorecorder import audiorecorder
|
|
||||||
# audio = audiorecorder("Click to record", "Recording...")
|
|
||||||
# if len(audio) > 0:
|
|
||||||
# # To play audio in frontend:
|
|
||||||
# st.audio(audio.tobytes())
|
|
||||||
|
|
||||||
InputUI(session_state=session_state, input_class=opyrator.input_type).render_ui(st)
|
InputUI(session_state=session_state, input_class=opyrator.input_type).render_ui(st)
|
||||||
execute_selected = st.button(opyrator.action)
|
execute_selected = st.button(opyrator.action)
|
||||||
if execute_selected:
|
if execute_selected:
|
||||||
@@ -2,12 +2,12 @@ from pydantic import BaseModel, Field
|
|||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, Tuple
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
EXT_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}ppg_extractor"
|
EXT_MODELS_DIRT = "ppg_extractor\\saved_models"
|
||||||
ENC_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}encoder"
|
ENC_MODELS_DIRT = "encoder\\saved_models"
|
||||||
|
|
||||||
|
|
||||||
if os.path.isdir(EXT_MODELS_DIRT):
|
if os.path.isdir(EXT_MODELS_DIRT):
|
||||||
@@ -70,7 +70,7 @@ class AudioEntity(BaseModel):
|
|||||||
mel: Any
|
mel: Any
|
||||||
|
|
||||||
class Output(BaseModel):
|
class Output(BaseModel):
|
||||||
__root__: Tuple[str, int]
|
__root__: tuple[str, int]
|
||||||
|
|
||||||
def render_output_ui(self, streamlit_app, input) -> None: # type: ignore
|
def render_output_ui(self, streamlit_app, input) -> None: # type: ignore
|
||||||
"""Custom output UI.
|
"""Custom output UI.
|
||||||
@@ -83,7 +83,7 @@ def preprocess(input: Input) -> Output:
|
|||||||
"""Preprocess(预处理)"""
|
"""Preprocess(预处理)"""
|
||||||
finished = 0
|
finished = 0
|
||||||
if input.model == Model.VC_PPG2MEL:
|
if input.model == Model.VC_PPG2MEL:
|
||||||
from models.ppg2mel.preprocess import preprocess_dataset
|
from ppg2mel.preprocess import preprocess_dataset
|
||||||
finished = preprocess_dataset(
|
finished = preprocess_dataset(
|
||||||
datasets_root=Path(input.datasets_root),
|
datasets_root=Path(input.datasets_root),
|
||||||
dataset=input.dataset,
|
dataset=input.dataset,
|
||||||
|
Before Width: | Height: | Size: 5.6 KiB After Width: | Height: | Size: 5.6 KiB |
@@ -2,16 +2,17 @@ from pydantic import BaseModel, Field
|
|||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, Tuple
|
from typing import Any
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from utils.hparams import HpsYaml
|
from utils.load_yaml import HpsYaml
|
||||||
from utils.util import AttrDict
|
from utils.util import AttrDict
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
|
# TODO: seperator for *unix systems
|
||||||
# Constants
|
# Constants
|
||||||
EXT_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}ppg_extractor"
|
EXT_MODELS_DIRT = "ppg_extractor\\saved_models"
|
||||||
CONV_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}ppg2mel"
|
CONV_MODELS_DIRT = "ppg2mel\\saved_models"
|
||||||
ENC_MODELS_DIRT = f"data{os.sep}ckpt{os.sep}encoder"
|
ENC_MODELS_DIRT = "encoder\\saved_models"
|
||||||
|
|
||||||
|
|
||||||
if os.path.isdir(EXT_MODELS_DIRT):
|
if os.path.isdir(EXT_MODELS_DIRT):
|
||||||
@@ -101,7 +102,7 @@ class AudioEntity(BaseModel):
|
|||||||
mel: Any
|
mel: Any
|
||||||
|
|
||||||
class Output(BaseModel):
|
class Output(BaseModel):
|
||||||
__root__: Tuple[str, int]
|
__root__: tuple[str, int]
|
||||||
|
|
||||||
def render_output_ui(self, streamlit_app, input) -> None: # type: ignore
|
def render_output_ui(self, streamlit_app, input) -> None: # type: ignore
|
||||||
"""Custom output UI.
|
"""Custom output UI.
|
||||||
@@ -110,8 +111,8 @@ class Output(BaseModel):
|
|||||||
sr, count = self.__root__
|
sr, count = self.__root__
|
||||||
streamlit_app.subheader(f"Dataset {sr} done processed total of {count}")
|
streamlit_app.subheader(f"Dataset {sr} done processed total of {count}")
|
||||||
|
|
||||||
def train_vc(input: Input) -> Output:
|
def train(input: Input) -> Output:
|
||||||
"""Train VC(训练 VC)"""
|
"""Train(训练)"""
|
||||||
|
|
||||||
print(">>> OneShot VC training ...")
|
print(">>> OneShot VC training ...")
|
||||||
params = AttrDict()
|
params = AttrDict()
|
||||||
@@ -144,7 +145,7 @@ def train_vc(input: Input) -> Output:
|
|||||||
if torch.cuda.is_available():
|
if torch.cuda.is_available():
|
||||||
torch.cuda.manual_seed_all(input.seed)
|
torch.cuda.manual_seed_all(input.seed)
|
||||||
mode = "train"
|
mode = "train"
|
||||||
from models.ppg2mel.train.train_linglf02mel_seq2seq_oneshotvc import Solver
|
from ppg2mel.train.train_linglf02mel_seq2seq_oneshotvc import Solver
|
||||||
solver = Solver(config, params, mode)
|
solver = Solver(config, params, mode)
|
||||||
solver.load_data()
|
solver.load_data()
|
||||||
solver.set_model()
|
solver.set_model()
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
from models.encoder.data_objects.speaker_verification_dataset import SpeakerVerificationDataset
|
|
||||||
from models.encoder.data_objects.speaker_verification_dataset import SpeakerVerificationDataLoader
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
import imp
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
class Base(nn.Module):
|
|
||||||
def __init__(self, stop_threshold):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
self.init_model()
|
|
||||||
self.num_params()
|
|
||||||
|
|
||||||
self.register_buffer("step", torch.zeros(1, dtype=torch.long))
|
|
||||||
self.register_buffer("stop_threshold", torch.tensor(stop_threshold, dtype=torch.float32))
|
|
||||||
|
|
||||||
@property
|
|
||||||
def r(self):
|
|
||||||
return self.decoder.r.item()
|
|
||||||
|
|
||||||
@r.setter
|
|
||||||
def r(self, value):
|
|
||||||
self.decoder.r = self.decoder.r.new_tensor(value, requires_grad=False)
|
|
||||||
|
|
||||||
def init_model(self):
|
|
||||||
for p in self.parameters():
|
|
||||||
if p.dim() > 1: nn.init.xavier_uniform_(p)
|
|
||||||
|
|
||||||
def finetune_partial(self, whitelist_layers):
|
|
||||||
self.zero_grad()
|
|
||||||
for name, child in self.named_children():
|
|
||||||
if name in whitelist_layers:
|
|
||||||
print("Trainable Layer: %s" % name)
|
|
||||||
print("Trainable Parameters: %.3f" % sum([np.prod(p.size()) for p in child.parameters()]))
|
|
||||||
for param in child.parameters():
|
|
||||||
param.requires_grad = False
|
|
||||||
|
|
||||||
def get_step(self):
|
|
||||||
return self.step.data.item()
|
|
||||||
|
|
||||||
def reset_step(self):
|
|
||||||
# assignment to parameters or buffers is overloaded, updates internal dict entry
|
|
||||||
self.step = self.step.data.new_tensor(1)
|
|
||||||
|
|
||||||
def log(self, path, msg):
|
|
||||||
with open(path, "a") as f:
|
|
||||||
print(msg, file=f)
|
|
||||||
|
|
||||||
def load(self, path, device, optimizer=None):
|
|
||||||
# Use device of model params as location for loaded state
|
|
||||||
checkpoint = torch.load(str(path), map_location=device)
|
|
||||||
if "model_state" in checkpoint:
|
|
||||||
state = checkpoint["model_state"]
|
|
||||||
else:
|
|
||||||
state = checkpoint["model"]
|
|
||||||
self.load_state_dict(state, strict=False)
|
|
||||||
|
|
||||||
if "optimizer_state" in checkpoint and optimizer is not None:
|
|
||||||
optimizer.load_state_dict(checkpoint["optimizer_state"])
|
|
||||||
|
|
||||||
def save(self, path, optimizer=None):
|
|
||||||
if optimizer is not None:
|
|
||||||
torch.save({
|
|
||||||
"model_state": self.state_dict(),
|
|
||||||
"optimizer_state": optimizer.state_dict(),
|
|
||||||
}, str(path))
|
|
||||||
else:
|
|
||||||
torch.save({
|
|
||||||
"model_state": self.state_dict(),
|
|
||||||
}, str(path))
|
|
||||||
|
|
||||||
|
|
||||||
def num_params(self, print_out=True):
|
|
||||||
parameters = filter(lambda p: p.requires_grad, self.parameters())
|
|
||||||
parameters = sum([np.prod(p.size()) for p in parameters]) / 1_000_000
|
|
||||||
if print_out:
|
|
||||||
print("Trainable Parameters: %.3fM" % parameters)
|
|
||||||
return parameters
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
from .common.batch_norm_conv import BatchNormConv
|
|
||||||
from .common.highway_network import HighwayNetwork
|
|
||||||
|
|
||||||
class CBHG(nn.Module):
|
|
||||||
def __init__(self, K, in_channels, channels, proj_channels, num_highways):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
# List of all rnns to call `flatten_parameters()` on
|
|
||||||
self._to_flatten = []
|
|
||||||
|
|
||||||
self.bank_kernels = [i for i in range(1, K + 1)]
|
|
||||||
self.conv1d_bank = nn.ModuleList()
|
|
||||||
for k in self.bank_kernels:
|
|
||||||
conv = BatchNormConv(in_channels, channels, k)
|
|
||||||
self.conv1d_bank.append(conv)
|
|
||||||
|
|
||||||
self.maxpool = nn.MaxPool1d(kernel_size=2, stride=1, padding=1)
|
|
||||||
|
|
||||||
self.conv_project1 = BatchNormConv(len(self.bank_kernels) * channels, proj_channels[0], 3)
|
|
||||||
self.conv_project2 = BatchNormConv(proj_channels[0], proj_channels[1], 3, relu=False)
|
|
||||||
|
|
||||||
# Fix the highway input if necessary
|
|
||||||
if proj_channels[-1] != channels:
|
|
||||||
self.highway_mismatch = True
|
|
||||||
self.pre_highway = nn.Linear(proj_channels[-1], channels, bias=False)
|
|
||||||
else:
|
|
||||||
self.highway_mismatch = False
|
|
||||||
|
|
||||||
self.highways = nn.ModuleList()
|
|
||||||
for i in range(num_highways):
|
|
||||||
hn = HighwayNetwork(channels)
|
|
||||||
self.highways.append(hn)
|
|
||||||
|
|
||||||
self.rnn = nn.GRU(channels, channels // 2, batch_first=True, bidirectional=True)
|
|
||||||
self._to_flatten.append(self.rnn)
|
|
||||||
|
|
||||||
# Avoid fragmentation of RNN parameters and associated warning
|
|
||||||
self._flatten_parameters()
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
# Although we `_flatten_parameters()` on init, when using DataParallel
|
|
||||||
# the model gets replicated, making it no longer guaranteed that the
|
|
||||||
# weights are contiguous in GPU memory. Hence, we must call it again
|
|
||||||
self.rnn.flatten_parameters()
|
|
||||||
|
|
||||||
# Save these for later
|
|
||||||
residual = x
|
|
||||||
seq_len = x.size(-1)
|
|
||||||
conv_bank = []
|
|
||||||
|
|
||||||
# Convolution Bank
|
|
||||||
for conv in self.conv1d_bank:
|
|
||||||
c = conv(x) # Convolution
|
|
||||||
conv_bank.append(c[:, :, :seq_len])
|
|
||||||
|
|
||||||
# Stack along the channel axis
|
|
||||||
conv_bank = torch.cat(conv_bank, dim=1)
|
|
||||||
|
|
||||||
# dump the last padding to fit residual
|
|
||||||
x = self.maxpool(conv_bank)[:, :, :seq_len]
|
|
||||||
|
|
||||||
# Conv1d projections
|
|
||||||
x = self.conv_project1(x)
|
|
||||||
x = self.conv_project2(x)
|
|
||||||
|
|
||||||
# Residual Connect
|
|
||||||
x = x + residual
|
|
||||||
|
|
||||||
# Through the highways
|
|
||||||
x = x.transpose(1, 2)
|
|
||||||
if self.highway_mismatch is True:
|
|
||||||
x = self.pre_highway(x)
|
|
||||||
for h in self.highways: x = h(x)
|
|
||||||
|
|
||||||
# And then the RNN
|
|
||||||
x, _ = self.rnn(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
def _flatten_parameters(self):
|
|
||||||
"""Calls `flatten_parameters` on all the rnns used by the WaveRNN. Used
|
|
||||||
to improve efficiency and avoid PyTorch yelling at us."""
|
|
||||||
[m.flatten_parameters() for m in self._to_flatten]
|
|
||||||
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
|
|
||||||
class BatchNormConv(nn.Module):
|
|
||||||
def __init__(self, in_channels, out_channels, kernel, relu=True):
|
|
||||||
super().__init__()
|
|
||||||
self.conv = nn.Conv1d(in_channels, out_channels, kernel, stride=1, padding=kernel // 2, bias=False)
|
|
||||||
self.bnorm = nn.BatchNorm1d(out_channels)
|
|
||||||
self.relu = relu
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = self.conv(x)
|
|
||||||
x = F.relu(x) if self.relu is True else x
|
|
||||||
return self.bnorm(x)
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
|
|
||||||
class HighwayNetwork(nn.Module):
|
|
||||||
def __init__(self, size):
|
|
||||||
super().__init__()
|
|
||||||
self.W1 = nn.Linear(size, size)
|
|
||||||
self.W2 = nn.Linear(size, size)
|
|
||||||
self.W1.bias.data.fill_(0.)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x1 = self.W1(x)
|
|
||||||
x2 = self.W2(x)
|
|
||||||
g = torch.sigmoid(x2)
|
|
||||||
y = g * F.relu(x1) + (1. - g) * x
|
|
||||||
return y
|
|
||||||
@@ -1,193 +0,0 @@
|
|||||||
import torch
|
|
||||||
from torch.nn import functional as F
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_MIN_BIN_WIDTH = 1e-3
|
|
||||||
DEFAULT_MIN_BIN_HEIGHT = 1e-3
|
|
||||||
DEFAULT_MIN_DERIVATIVE = 1e-3
|
|
||||||
|
|
||||||
|
|
||||||
def piecewise_rational_quadratic_transform(inputs,
|
|
||||||
unnormalized_widths,
|
|
||||||
unnormalized_heights,
|
|
||||||
unnormalized_derivatives,
|
|
||||||
inverse=False,
|
|
||||||
tails=None,
|
|
||||||
tail_bound=1.,
|
|
||||||
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
|
||||||
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
|
||||||
min_derivative=DEFAULT_MIN_DERIVATIVE):
|
|
||||||
|
|
||||||
if tails is None:
|
|
||||||
spline_fn = rational_quadratic_spline
|
|
||||||
spline_kwargs = {}
|
|
||||||
else:
|
|
||||||
spline_fn = unconstrained_rational_quadratic_spline
|
|
||||||
spline_kwargs = {
|
|
||||||
'tails': tails,
|
|
||||||
'tail_bound': tail_bound
|
|
||||||
}
|
|
||||||
|
|
||||||
outputs, logabsdet = spline_fn(
|
|
||||||
inputs=inputs,
|
|
||||||
unnormalized_widths=unnormalized_widths,
|
|
||||||
unnormalized_heights=unnormalized_heights,
|
|
||||||
unnormalized_derivatives=unnormalized_derivatives,
|
|
||||||
inverse=inverse,
|
|
||||||
min_bin_width=min_bin_width,
|
|
||||||
min_bin_height=min_bin_height,
|
|
||||||
min_derivative=min_derivative,
|
|
||||||
**spline_kwargs
|
|
||||||
)
|
|
||||||
return outputs, logabsdet
|
|
||||||
|
|
||||||
|
|
||||||
def searchsorted(bin_locations, inputs, eps=1e-6):
|
|
||||||
bin_locations[..., -1] += eps
|
|
||||||
return torch.sum(
|
|
||||||
inputs[..., None] >= bin_locations,
|
|
||||||
dim=-1
|
|
||||||
) - 1
|
|
||||||
|
|
||||||
|
|
||||||
def unconstrained_rational_quadratic_spline(inputs,
|
|
||||||
unnormalized_widths,
|
|
||||||
unnormalized_heights,
|
|
||||||
unnormalized_derivatives,
|
|
||||||
inverse=False,
|
|
||||||
tails='linear',
|
|
||||||
tail_bound=1.,
|
|
||||||
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
|
||||||
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
|
||||||
min_derivative=DEFAULT_MIN_DERIVATIVE):
|
|
||||||
inside_interval_mask = (inputs >= -tail_bound) & (inputs <= tail_bound)
|
|
||||||
outside_interval_mask = ~inside_interval_mask
|
|
||||||
|
|
||||||
outputs = torch.zeros_like(inputs)
|
|
||||||
logabsdet = torch.zeros_like(inputs)
|
|
||||||
|
|
||||||
if tails == 'linear':
|
|
||||||
unnormalized_derivatives = F.pad(unnormalized_derivatives, pad=(1, 1))
|
|
||||||
constant = np.log(np.exp(1 - min_derivative) - 1)
|
|
||||||
unnormalized_derivatives[..., 0] = constant
|
|
||||||
unnormalized_derivatives[..., -1] = constant
|
|
||||||
|
|
||||||
outputs[outside_interval_mask] = inputs[outside_interval_mask]
|
|
||||||
logabsdet[outside_interval_mask] = 0
|
|
||||||
else:
|
|
||||||
raise RuntimeError('{} tails are not implemented.'.format(tails))
|
|
||||||
|
|
||||||
outputs[inside_interval_mask], logabsdet[inside_interval_mask] = rational_quadratic_spline(
|
|
||||||
inputs=inputs[inside_interval_mask],
|
|
||||||
unnormalized_widths=unnormalized_widths[inside_interval_mask, :],
|
|
||||||
unnormalized_heights=unnormalized_heights[inside_interval_mask, :],
|
|
||||||
unnormalized_derivatives=unnormalized_derivatives[inside_interval_mask, :],
|
|
||||||
inverse=inverse,
|
|
||||||
left=-tail_bound, right=tail_bound, bottom=-tail_bound, top=tail_bound,
|
|
||||||
min_bin_width=min_bin_width,
|
|
||||||
min_bin_height=min_bin_height,
|
|
||||||
min_derivative=min_derivative
|
|
||||||
)
|
|
||||||
|
|
||||||
return outputs, logabsdet
|
|
||||||
|
|
||||||
def rational_quadratic_spline(inputs,
|
|
||||||
unnormalized_widths,
|
|
||||||
unnormalized_heights,
|
|
||||||
unnormalized_derivatives,
|
|
||||||
inverse=False,
|
|
||||||
left=0., right=1., bottom=0., top=1.,
|
|
||||||
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
|
||||||
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
|
||||||
min_derivative=DEFAULT_MIN_DERIVATIVE):
|
|
||||||
if torch.min(inputs) < left or torch.max(inputs) > right:
|
|
||||||
raise ValueError('Input to a transform is not within its domain')
|
|
||||||
|
|
||||||
num_bins = unnormalized_widths.shape[-1]
|
|
||||||
|
|
||||||
if min_bin_width * num_bins > 1.0:
|
|
||||||
raise ValueError('Minimal bin width too large for the number of bins')
|
|
||||||
if min_bin_height * num_bins > 1.0:
|
|
||||||
raise ValueError('Minimal bin height too large for the number of bins')
|
|
||||||
|
|
||||||
widths = F.softmax(unnormalized_widths, dim=-1)
|
|
||||||
widths = min_bin_width + (1 - min_bin_width * num_bins) * widths
|
|
||||||
cumwidths = torch.cumsum(widths, dim=-1)
|
|
||||||
cumwidths = F.pad(cumwidths, pad=(1, 0), mode='constant', value=0.0)
|
|
||||||
cumwidths = (right - left) * cumwidths + left
|
|
||||||
cumwidths[..., 0] = left
|
|
||||||
cumwidths[..., -1] = right
|
|
||||||
widths = cumwidths[..., 1:] - cumwidths[..., :-1]
|
|
||||||
|
|
||||||
derivatives = min_derivative + F.softplus(unnormalized_derivatives)
|
|
||||||
|
|
||||||
heights = F.softmax(unnormalized_heights, dim=-1)
|
|
||||||
heights = min_bin_height + (1 - min_bin_height * num_bins) * heights
|
|
||||||
cumheights = torch.cumsum(heights, dim=-1)
|
|
||||||
cumheights = F.pad(cumheights, pad=(1, 0), mode='constant', value=0.0)
|
|
||||||
cumheights = (top - bottom) * cumheights + bottom
|
|
||||||
cumheights[..., 0] = bottom
|
|
||||||
cumheights[..., -1] = top
|
|
||||||
heights = cumheights[..., 1:] - cumheights[..., :-1]
|
|
||||||
|
|
||||||
if inverse:
|
|
||||||
bin_idx = searchsorted(cumheights, inputs)[..., None]
|
|
||||||
else:
|
|
||||||
bin_idx = searchsorted(cumwidths, inputs)[..., None]
|
|
||||||
|
|
||||||
input_cumwidths = cumwidths.gather(-1, bin_idx)[..., 0]
|
|
||||||
input_bin_widths = widths.gather(-1, bin_idx)[..., 0]
|
|
||||||
|
|
||||||
input_cumheights = cumheights.gather(-1, bin_idx)[..., 0]
|
|
||||||
delta = heights / widths
|
|
||||||
input_delta = delta.gather(-1, bin_idx)[..., 0]
|
|
||||||
|
|
||||||
input_derivatives = derivatives.gather(-1, bin_idx)[..., 0]
|
|
||||||
input_derivatives_plus_one = derivatives[..., 1:].gather(-1, bin_idx)[..., 0]
|
|
||||||
|
|
||||||
input_heights = heights.gather(-1, bin_idx)[..., 0]
|
|
||||||
|
|
||||||
if inverse:
|
|
||||||
a = (((inputs - input_cumheights) * (input_derivatives
|
|
||||||
+ input_derivatives_plus_one
|
|
||||||
- 2 * input_delta)
|
|
||||||
+ input_heights * (input_delta - input_derivatives)))
|
|
||||||
b = (input_heights * input_derivatives
|
|
||||||
- (inputs - input_cumheights) * (input_derivatives
|
|
||||||
+ input_derivatives_plus_one
|
|
||||||
- 2 * input_delta))
|
|
||||||
c = - input_delta * (inputs - input_cumheights)
|
|
||||||
|
|
||||||
discriminant = b.pow(2) - 4 * a * c
|
|
||||||
assert (discriminant >= 0).all()
|
|
||||||
|
|
||||||
root = (2 * c) / (-b - torch.sqrt(discriminant))
|
|
||||||
outputs = root * input_bin_widths + input_cumwidths
|
|
||||||
|
|
||||||
theta_one_minus_theta = root * (1 - root)
|
|
||||||
denominator = input_delta + ((input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
|
||||||
* theta_one_minus_theta)
|
|
||||||
derivative_numerator = input_delta.pow(2) * (input_derivatives_plus_one * root.pow(2)
|
|
||||||
+ 2 * input_delta * theta_one_minus_theta
|
|
||||||
+ input_derivatives * (1 - root).pow(2))
|
|
||||||
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
|
||||||
|
|
||||||
return outputs, -logabsdet
|
|
||||||
else:
|
|
||||||
theta = (inputs - input_cumwidths) / input_bin_widths
|
|
||||||
theta_one_minus_theta = theta * (1 - theta)
|
|
||||||
|
|
||||||
numerator = input_heights * (input_delta * theta.pow(2)
|
|
||||||
+ input_derivatives * theta_one_minus_theta)
|
|
||||||
denominator = input_delta + ((input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
|
||||||
* theta_one_minus_theta)
|
|
||||||
outputs = input_cumheights + numerator / denominator
|
|
||||||
|
|
||||||
derivative_numerator = input_delta.pow(2) * (input_derivatives_plus_one * theta.pow(2)
|
|
||||||
+ 2 * input_delta * theta_one_minus_theta
|
|
||||||
+ input_derivatives * (1 - theta).pow(2))
|
|
||||||
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
|
||||||
|
|
||||||
return outputs, logabsdet
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
|
|
||||||
class LSA(nn.Module):
|
|
||||||
def __init__(self, attn_dim, kernel_size=31, filters=32):
|
|
||||||
super().__init__()
|
|
||||||
self.conv = nn.Conv1d(1, filters, padding=(kernel_size - 1) // 2, kernel_size=kernel_size, bias=True)
|
|
||||||
self.L = nn.Linear(filters, attn_dim, bias=False)
|
|
||||||
self.W = nn.Linear(attn_dim, attn_dim, bias=True) # Include the attention bias in this term
|
|
||||||
self.v = nn.Linear(attn_dim, 1, bias=False)
|
|
||||||
self.cumulative = None
|
|
||||||
self.attention = None
|
|
||||||
|
|
||||||
def init_attention(self, encoder_seq_proj):
|
|
||||||
device = encoder_seq_proj.device # use same device as parameters
|
|
||||||
b, t, c = encoder_seq_proj.size()
|
|
||||||
self.cumulative = torch.zeros(b, t, device=device)
|
|
||||||
self.attention = torch.zeros(b, t, device=device)
|
|
||||||
|
|
||||||
def forward(self, encoder_seq_proj, query, times, chars):
|
|
||||||
|
|
||||||
if times == 0: self.init_attention(encoder_seq_proj)
|
|
||||||
|
|
||||||
processed_query = self.W(query).unsqueeze(1)
|
|
||||||
|
|
||||||
location = self.cumulative.unsqueeze(1)
|
|
||||||
processed_loc = self.L(self.conv(location).transpose(1, 2))
|
|
||||||
|
|
||||||
u = self.v(torch.tanh(processed_query + encoder_seq_proj + processed_loc))
|
|
||||||
u = u.squeeze(-1)
|
|
||||||
|
|
||||||
# Mask zero padding chars
|
|
||||||
u = u * (chars != 0).float()
|
|
||||||
|
|
||||||
# Smooth Attention
|
|
||||||
# scores = torch.sigmoid(u) / torch.sigmoid(u).sum(dim=1, keepdim=True)
|
|
||||||
scores = F.softmax(u, dim=1)
|
|
||||||
self.attention = scores
|
|
||||||
self.cumulative = self.cumulative + self.attention
|
|
||||||
|
|
||||||
return scores.unsqueeze(-1).transpose(1, 2)
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
|
|
||||||
class PreNet(nn.Module):
|
|
||||||
def __init__(self, in_dims, fc1_dims=256, fc2_dims=128, dropout=0.5):
|
|
||||||
super().__init__()
|
|
||||||
self.fc1 = nn.Linear(in_dims, fc1_dims)
|
|
||||||
self.fc2 = nn.Linear(fc1_dims, fc2_dims)
|
|
||||||
self.p = dropout
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
"""forward
|
|
||||||
|
|
||||||
Args:
|
|
||||||
x (3D tensor with size `[batch_size, num_chars, tts_embed_dims]`): input texts list
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
3D tensor with size `[batch_size, num_chars, encoder_dims]`
|
|
||||||
|
|
||||||
"""
|
|
||||||
x = self.fc1(x)
|
|
||||||
x = F.relu(x)
|
|
||||||
x = F.dropout(x, self.p, training=True)
|
|
||||||
x = self.fc2(x)
|
|
||||||
x = F.relu(x)
|
|
||||||
x = F.dropout(x, self.p, training=True)
|
|
||||||
return x
|
|
||||||
@@ -1,675 +0,0 @@
|
|||||||
import math
|
|
||||||
import torch
|
|
||||||
from torch import nn
|
|
||||||
from torch.nn import functional as F
|
|
||||||
|
|
||||||
from torch.nn import Conv1d
|
|
||||||
from torch.nn.utils import weight_norm, remove_weight_norm
|
|
||||||
from utils.util import init_weights, get_padding, convert_pad_shape, convert_pad_shape, subsequent_mask, fused_add_tanh_sigmoid_multiply
|
|
||||||
from .common.transforms import piecewise_rational_quadratic_transform
|
|
||||||
|
|
||||||
LRELU_SLOPE = 0.1
|
|
||||||
|
|
||||||
class LayerNorm(nn.Module):
|
|
||||||
def __init__(self, channels, eps=1e-5):
|
|
||||||
super().__init__()
|
|
||||||
self.channels = channels
|
|
||||||
self.eps = eps
|
|
||||||
|
|
||||||
self.gamma = nn.Parameter(torch.ones(channels))
|
|
||||||
self.beta = nn.Parameter(torch.zeros(channels))
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = x.transpose(1, -1)
|
|
||||||
x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
|
|
||||||
return x.transpose(1, -1)
|
|
||||||
|
|
||||||
|
|
||||||
class ConvReluNorm(nn.Module):
|
|
||||||
def __init__(self, in_channels, hidden_channels, out_channels, kernel_size, n_layers, p_dropout):
|
|
||||||
super().__init__()
|
|
||||||
self.in_channels = in_channels
|
|
||||||
self.hidden_channels = hidden_channels
|
|
||||||
self.out_channels = out_channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
assert n_layers > 1, "Number of layers should be larger than 0."
|
|
||||||
|
|
||||||
self.conv_layers = nn.ModuleList()
|
|
||||||
self.norm_layers = nn.ModuleList()
|
|
||||||
self.conv_layers.append(nn.Conv1d(in_channels, hidden_channels, kernel_size, padding=kernel_size//2))
|
|
||||||
self.norm_layers.append(LayerNorm(hidden_channels))
|
|
||||||
self.relu_drop = nn.Sequential(
|
|
||||||
nn.ReLU(),
|
|
||||||
nn.Dropout(p_dropout))
|
|
||||||
for _ in range(n_layers-1):
|
|
||||||
self.conv_layers.append(nn.Conv1d(hidden_channels, hidden_channels, kernel_size, padding=kernel_size//2))
|
|
||||||
self.norm_layers.append(LayerNorm(hidden_channels))
|
|
||||||
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
|
||||||
self.proj.weight.data.zero_()
|
|
||||||
self.proj.bias.data.zero_()
|
|
||||||
|
|
||||||
def forward(self, x, x_mask):
|
|
||||||
x_org = x
|
|
||||||
for i in range(self.n_layers):
|
|
||||||
x = self.conv_layers[i](x * x_mask)
|
|
||||||
x = self.norm_layers[i](x)
|
|
||||||
x = self.relu_drop(x)
|
|
||||||
x = x_org + self.proj(x)
|
|
||||||
return x * x_mask
|
|
||||||
|
|
||||||
|
|
||||||
class DDSConv(nn.Module):
|
|
||||||
"""
|
|
||||||
Dilated and Depth-Separable Convolution
|
|
||||||
"""
|
|
||||||
def __init__(self, channels, kernel_size, n_layers, p_dropout=0.):
|
|
||||||
super().__init__()
|
|
||||||
self.channels = channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
|
|
||||||
self.drop = nn.Dropout(p_dropout)
|
|
||||||
self.convs_sep = nn.ModuleList()
|
|
||||||
self.convs_1x1 = nn.ModuleList()
|
|
||||||
self.norms_1 = nn.ModuleList()
|
|
||||||
self.norms_2 = nn.ModuleList()
|
|
||||||
for i in range(n_layers):
|
|
||||||
dilation = kernel_size ** i
|
|
||||||
padding = (kernel_size * dilation - dilation) // 2
|
|
||||||
self.convs_sep.append(nn.Conv1d(channels, channels, kernel_size,
|
|
||||||
groups=channels, dilation=dilation, padding=padding
|
|
||||||
))
|
|
||||||
self.convs_1x1.append(nn.Conv1d(channels, channels, 1))
|
|
||||||
self.norms_1.append(LayerNorm(channels))
|
|
||||||
self.norms_2.append(LayerNorm(channels))
|
|
||||||
|
|
||||||
def forward(self, x, x_mask, g=None):
|
|
||||||
if g is not None:
|
|
||||||
x = x + g
|
|
||||||
for i in range(self.n_layers):
|
|
||||||
y = self.convs_sep[i](x * x_mask)
|
|
||||||
y = self.norms_1[i](y)
|
|
||||||
y = F.gelu(y)
|
|
||||||
y = self.convs_1x1[i](y)
|
|
||||||
y = self.norms_2[i](y)
|
|
||||||
y = F.gelu(y)
|
|
||||||
y = self.drop(y)
|
|
||||||
x = x + y
|
|
||||||
return x * x_mask
|
|
||||||
|
|
||||||
|
|
||||||
class WN(torch.nn.Module):
|
|
||||||
def __init__(self, hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=0, p_dropout=0):
|
|
||||||
super(WN, self).__init__()
|
|
||||||
assert(kernel_size % 2 == 1)
|
|
||||||
self.hidden_channels =hidden_channels
|
|
||||||
self.kernel_size = kernel_size,
|
|
||||||
self.dilation_rate = dilation_rate
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.gin_channels = gin_channels
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
|
|
||||||
self.in_layers = torch.nn.ModuleList()
|
|
||||||
self.res_skip_layers = torch.nn.ModuleList()
|
|
||||||
self.drop = nn.Dropout(p_dropout)
|
|
||||||
|
|
||||||
if gin_channels != 0:
|
|
||||||
cond_layer = torch.nn.Conv1d(gin_channels, 2*hidden_channels*n_layers, 1)
|
|
||||||
self.cond_layer = torch.nn.utils.weight_norm(cond_layer, name='weight')
|
|
||||||
|
|
||||||
for i in range(n_layers):
|
|
||||||
dilation = dilation_rate ** i
|
|
||||||
padding = int((kernel_size * dilation - dilation) / 2)
|
|
||||||
in_layer = torch.nn.Conv1d(hidden_channels, 2*hidden_channels, kernel_size,
|
|
||||||
dilation=dilation, padding=padding)
|
|
||||||
in_layer = torch.nn.utils.weight_norm(in_layer, name='weight')
|
|
||||||
self.in_layers.append(in_layer)
|
|
||||||
|
|
||||||
# last one is not necessary
|
|
||||||
if i < n_layers - 1:
|
|
||||||
res_skip_channels = 2 * hidden_channels
|
|
||||||
else:
|
|
||||||
res_skip_channels = hidden_channels
|
|
||||||
|
|
||||||
res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
|
|
||||||
res_skip_layer = torch.nn.utils.weight_norm(res_skip_layer, name='weight')
|
|
||||||
self.res_skip_layers.append(res_skip_layer)
|
|
||||||
|
|
||||||
def forward(self, x, x_mask, g=None, **kwargs):
|
|
||||||
output = torch.zeros_like(x)
|
|
||||||
n_channels_tensor = torch.IntTensor([self.hidden_channels])
|
|
||||||
|
|
||||||
if g is not None:
|
|
||||||
g = self.cond_layer(g)
|
|
||||||
|
|
||||||
for i in range(self.n_layers):
|
|
||||||
x_in = self.in_layers[i](x)
|
|
||||||
if g is not None:
|
|
||||||
cond_offset = i * 2 * self.hidden_channels
|
|
||||||
g_l = g[:,cond_offset:cond_offset+2*self.hidden_channels,:]
|
|
||||||
else:
|
|
||||||
g_l = torch.zeros_like(x_in)
|
|
||||||
|
|
||||||
acts = fused_add_tanh_sigmoid_multiply(
|
|
||||||
x_in,
|
|
||||||
g_l,
|
|
||||||
n_channels_tensor)
|
|
||||||
acts = self.drop(acts)
|
|
||||||
|
|
||||||
res_skip_acts = self.res_skip_layers[i](acts)
|
|
||||||
if i < self.n_layers - 1:
|
|
||||||
res_acts = res_skip_acts[:,:self.hidden_channels,:]
|
|
||||||
x = (x + res_acts) * x_mask
|
|
||||||
output = output + res_skip_acts[:,self.hidden_channels:,:]
|
|
||||||
else:
|
|
||||||
output = output + res_skip_acts
|
|
||||||
return output * x_mask
|
|
||||||
|
|
||||||
def remove_weight_norm(self):
|
|
||||||
if self.gin_channels != 0:
|
|
||||||
torch.nn.utils.remove_weight_norm(self.cond_layer)
|
|
||||||
for l in self.in_layers:
|
|
||||||
torch.nn.utils.remove_weight_norm(l)
|
|
||||||
for l in self.res_skip_layers:
|
|
||||||
torch.nn.utils.remove_weight_norm(l)
|
|
||||||
|
|
||||||
|
|
||||||
class ResBlock1(torch.nn.Module):
|
|
||||||
def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
|
|
||||||
super(ResBlock1, self).__init__()
|
|
||||||
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, x_mask=None):
|
|
||||||
for c1, c2 in zip(self.convs1, self.convs2):
|
|
||||||
xt = F.leaky_relu(x, LRELU_SLOPE)
|
|
||||||
if x_mask is not None:
|
|
||||||
xt = xt * x_mask
|
|
||||||
xt = c1(xt)
|
|
||||||
xt = F.leaky_relu(xt, LRELU_SLOPE)
|
|
||||||
if x_mask is not None:
|
|
||||||
xt = xt * x_mask
|
|
||||||
xt = c2(xt)
|
|
||||||
x = xt + x
|
|
||||||
if x_mask is not None:
|
|
||||||
x = x * x_mask
|
|
||||||
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, channels, kernel_size=3, dilation=(1, 3)):
|
|
||||||
super(ResBlock2, self).__init__()
|
|
||||||
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, x_mask=None):
|
|
||||||
for c in self.convs:
|
|
||||||
xt = F.leaky_relu(x, LRELU_SLOPE)
|
|
||||||
if x_mask is not None:
|
|
||||||
xt = xt * x_mask
|
|
||||||
xt = c(xt)
|
|
||||||
x = xt + x
|
|
||||||
if x_mask is not None:
|
|
||||||
x = x * x_mask
|
|
||||||
return x
|
|
||||||
|
|
||||||
def remove_weight_norm(self):
|
|
||||||
for l in self.convs:
|
|
||||||
remove_weight_norm(l)
|
|
||||||
|
|
||||||
|
|
||||||
class Log(nn.Module):
|
|
||||||
def forward(self, x, x_mask, reverse=False, **kwargs):
|
|
||||||
if not reverse:
|
|
||||||
y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask
|
|
||||||
logdet = torch.sum(-y, [1, 2])
|
|
||||||
return y, logdet
|
|
||||||
else:
|
|
||||||
x = torch.exp(x) * x_mask
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Flip(nn.Module):
|
|
||||||
def forward(self, x, *args, reverse=False, **kwargs):
|
|
||||||
x = torch.flip(x, [1])
|
|
||||||
if not reverse:
|
|
||||||
logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device)
|
|
||||||
return x, logdet
|
|
||||||
else:
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class ElementwiseAffine(nn.Module):
|
|
||||||
def __init__(self, channels):
|
|
||||||
super().__init__()
|
|
||||||
self.channels = channels
|
|
||||||
self.m = nn.Parameter(torch.zeros(channels,1))
|
|
||||||
self.logs = nn.Parameter(torch.zeros(channels,1))
|
|
||||||
|
|
||||||
def forward(self, x, x_mask, reverse=False, **kwargs):
|
|
||||||
if not reverse:
|
|
||||||
y = self.m + torch.exp(self.logs) * x
|
|
||||||
y = y * x_mask
|
|
||||||
logdet = torch.sum(self.logs * x_mask, [1,2])
|
|
||||||
return y, logdet
|
|
||||||
else:
|
|
||||||
x = (x - self.m) * torch.exp(-self.logs) * x_mask
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class ResidualCouplingLayer(nn.Module):
|
|
||||||
def __init__(self,
|
|
||||||
channels,
|
|
||||||
hidden_channels,
|
|
||||||
kernel_size,
|
|
||||||
dilation_rate,
|
|
||||||
n_layers,
|
|
||||||
p_dropout=0,
|
|
||||||
gin_channels=0,
|
|
||||||
mean_only=False):
|
|
||||||
assert channels % 2 == 0, "channels should be divisible by 2"
|
|
||||||
super().__init__()
|
|
||||||
self.channels = channels
|
|
||||||
self.hidden_channels = hidden_channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.dilation_rate = dilation_rate
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.half_channels = channels // 2
|
|
||||||
self.mean_only = mean_only
|
|
||||||
|
|
||||||
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
|
|
||||||
self.enc = WN(hidden_channels, kernel_size, dilation_rate, n_layers, p_dropout=p_dropout, gin_channels=gin_channels)
|
|
||||||
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
|
|
||||||
self.post.weight.data.zero_()
|
|
||||||
self.post.bias.data.zero_()
|
|
||||||
|
|
||||||
def forward(self, x, x_mask, g=None, reverse=False):
|
|
||||||
x0, x1 = torch.split(x, [self.half_channels]*2, 1)
|
|
||||||
h = self.pre(x0) * x_mask
|
|
||||||
h = self.enc(h, x_mask, g=g)
|
|
||||||
stats = self.post(h) * x_mask
|
|
||||||
if not self.mean_only:
|
|
||||||
m, logs = torch.split(stats, [self.half_channels]*2, 1)
|
|
||||||
else:
|
|
||||||
m = stats
|
|
||||||
logs = torch.zeros_like(m)
|
|
||||||
|
|
||||||
if not reverse:
|
|
||||||
x1 = m + x1 * torch.exp(logs) * x_mask
|
|
||||||
x = torch.cat([x0, x1], 1)
|
|
||||||
logdet = torch.sum(logs, [1,2])
|
|
||||||
return x, logdet
|
|
||||||
else:
|
|
||||||
x1 = (x1 - m) * torch.exp(-logs) * x_mask
|
|
||||||
x = torch.cat([x0, x1], 1)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class ConvFlow(nn.Module):
|
|
||||||
def __init__(self, in_channels, filter_channels, kernel_size, n_layers, num_bins=10, tail_bound=5.0):
|
|
||||||
super().__init__()
|
|
||||||
self.in_channels = in_channels
|
|
||||||
self.filter_channels = filter_channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.num_bins = num_bins
|
|
||||||
self.tail_bound = tail_bound
|
|
||||||
self.half_channels = in_channels // 2
|
|
||||||
|
|
||||||
self.pre = nn.Conv1d(self.half_channels, filter_channels, 1)
|
|
||||||
self.convs = DDSConv(filter_channels, kernel_size, n_layers, p_dropout=0.)
|
|
||||||
self.proj = nn.Conv1d(filter_channels, self.half_channels * (num_bins * 3 - 1), 1)
|
|
||||||
self.proj.weight.data.zero_()
|
|
||||||
self.proj.bias.data.zero_()
|
|
||||||
|
|
||||||
def forward(self, x, x_mask, g=None, reverse=False):
|
|
||||||
x0, x1 = torch.split(x, [self.half_channels]*2, 1)
|
|
||||||
h = self.pre(x0)
|
|
||||||
h = self.convs(h, x_mask, g=g)
|
|
||||||
h = self.proj(h) * x_mask
|
|
||||||
|
|
||||||
b, c, t = x0.shape
|
|
||||||
h = h.reshape(b, c, -1, t).permute(0, 1, 3, 2) # [b, cx?, t] -> [b, c, t, ?]
|
|
||||||
|
|
||||||
unnormalized_widths = h[..., :self.num_bins] / math.sqrt(self.filter_channels)
|
|
||||||
unnormalized_heights = h[..., self.num_bins:2*self.num_bins] / math.sqrt(self.filter_channels)
|
|
||||||
unnormalized_derivatives = h[..., 2 * self.num_bins:]
|
|
||||||
|
|
||||||
x1, logabsdet = piecewise_rational_quadratic_transform(x1,
|
|
||||||
unnormalized_widths,
|
|
||||||
unnormalized_heights,
|
|
||||||
unnormalized_derivatives,
|
|
||||||
inverse=reverse,
|
|
||||||
tails='linear',
|
|
||||||
tail_bound=self.tail_bound
|
|
||||||
)
|
|
||||||
|
|
||||||
x = torch.cat([x0, x1], 1) * x_mask
|
|
||||||
logdet = torch.sum(logabsdet * x_mask, [1,2])
|
|
||||||
if not reverse:
|
|
||||||
return x, logdet
|
|
||||||
else:
|
|
||||||
return x
|
|
||||||
|
|
||||||
class Encoder(nn.Module):
|
|
||||||
def __init__(self, hidden_channels, filter_channels, n_heads, n_layers, kernel_size=1, p_dropout=0., window_size=4, **kwargs):
|
|
||||||
super().__init__()
|
|
||||||
self.hidden_channels = hidden_channels
|
|
||||||
self.filter_channels = filter_channels
|
|
||||||
self.n_heads = n_heads
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
self.window_size = window_size
|
|
||||||
|
|
||||||
self.drop = nn.Dropout(p_dropout)
|
|
||||||
self.attn_layers = nn.ModuleList()
|
|
||||||
self.norm_layers_1 = nn.ModuleList()
|
|
||||||
self.ffn_layers = nn.ModuleList()
|
|
||||||
self.norm_layers_2 = nn.ModuleList()
|
|
||||||
for i in range(self.n_layers):
|
|
||||||
self.attn_layers.append(MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout, window_size=window_size))
|
|
||||||
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
|
||||||
self.ffn_layers.append(FFN(hidden_channels, hidden_channels, filter_channels, kernel_size, p_dropout=p_dropout))
|
|
||||||
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
|
||||||
|
|
||||||
def forward(self, x, x_mask):
|
|
||||||
attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
|
||||||
x = x * x_mask
|
|
||||||
for i in range(self.n_layers):
|
|
||||||
y = self.attn_layers[i](x, x, attn_mask)
|
|
||||||
y = self.drop(y)
|
|
||||||
x = self.norm_layers_1[i](x + y)
|
|
||||||
|
|
||||||
y = self.ffn_layers[i](x, x_mask)
|
|
||||||
y = self.drop(y)
|
|
||||||
x = self.norm_layers_2[i](x + y)
|
|
||||||
x = x * x_mask
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Decoder(nn.Module):
|
|
||||||
def __init__(self, hidden_channels, filter_channels, n_heads, n_layers, kernel_size=1, p_dropout=0., proximal_bias=False, proximal_init=True, **kwargs):
|
|
||||||
super().__init__()
|
|
||||||
self.hidden_channels = hidden_channels
|
|
||||||
self.filter_channels = filter_channels
|
|
||||||
self.n_heads = n_heads
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
self.proximal_bias = proximal_bias
|
|
||||||
self.proximal_init = proximal_init
|
|
||||||
|
|
||||||
self.drop = nn.Dropout(p_dropout)
|
|
||||||
self.self_attn_layers = nn.ModuleList()
|
|
||||||
self.norm_layers_0 = nn.ModuleList()
|
|
||||||
self.encdec_attn_layers = nn.ModuleList()
|
|
||||||
self.norm_layers_1 = nn.ModuleList()
|
|
||||||
self.ffn_layers = nn.ModuleList()
|
|
||||||
self.norm_layers_2 = nn.ModuleList()
|
|
||||||
for i in range(self.n_layers):
|
|
||||||
self.self_attn_layers.append(MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout, proximal_bias=proximal_bias, proximal_init=proximal_init))
|
|
||||||
self.norm_layers_0.append(LayerNorm(hidden_channels))
|
|
||||||
self.encdec_attn_layers.append(MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout))
|
|
||||||
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
|
||||||
self.ffn_layers.append(FFN(hidden_channels, hidden_channels, filter_channels, kernel_size, p_dropout=p_dropout, causal=True))
|
|
||||||
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
|
||||||
|
|
||||||
def forward(self, x, x_mask, h, h_mask):
|
|
||||||
"""
|
|
||||||
x: decoder input
|
|
||||||
h: encoder output
|
|
||||||
"""
|
|
||||||
self_attn_mask = subsequent_mask(x_mask.size(2)).to(device=x.device, dtype=x.dtype)
|
|
||||||
encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
|
||||||
x = x * x_mask
|
|
||||||
for i in range(self.n_layers):
|
|
||||||
y = self.self_attn_layers[i](x, x, self_attn_mask)
|
|
||||||
y = self.drop(y)
|
|
||||||
x = self.norm_layers_0[i](x + y)
|
|
||||||
|
|
||||||
y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
|
|
||||||
y = self.drop(y)
|
|
||||||
x = self.norm_layers_1[i](x + y)
|
|
||||||
|
|
||||||
y = self.ffn_layers[i](x, x_mask)
|
|
||||||
y = self.drop(y)
|
|
||||||
x = self.norm_layers_2[i](x + y)
|
|
||||||
x = x * x_mask
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class MultiHeadAttention(nn.Module):
|
|
||||||
def __init__(self, channels, out_channels, n_heads, p_dropout=0., window_size=None, heads_share=True, block_length=None, proximal_bias=False, proximal_init=False):
|
|
||||||
super().__init__()
|
|
||||||
assert channels % n_heads == 0
|
|
||||||
|
|
||||||
self.channels = channels
|
|
||||||
self.out_channels = out_channels
|
|
||||||
self.n_heads = n_heads
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
self.window_size = window_size
|
|
||||||
self.heads_share = heads_share
|
|
||||||
self.block_length = block_length
|
|
||||||
self.proximal_bias = proximal_bias
|
|
||||||
self.proximal_init = proximal_init
|
|
||||||
self.attn = None
|
|
||||||
|
|
||||||
self.k_channels = channels // n_heads
|
|
||||||
self.conv_q = nn.Conv1d(channels, channels, 1)
|
|
||||||
self.conv_k = nn.Conv1d(channels, channels, 1)
|
|
||||||
self.conv_v = nn.Conv1d(channels, channels, 1)
|
|
||||||
self.conv_o = nn.Conv1d(channels, out_channels, 1)
|
|
||||||
self.drop = nn.Dropout(p_dropout)
|
|
||||||
|
|
||||||
if window_size is not None:
|
|
||||||
n_heads_rel = 1 if heads_share else n_heads
|
|
||||||
rel_stddev = self.k_channels**-0.5
|
|
||||||
self.emb_rel_k = nn.Parameter(torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels) * rel_stddev)
|
|
||||||
self.emb_rel_v = nn.Parameter(torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels) * rel_stddev)
|
|
||||||
|
|
||||||
nn.init.xavier_uniform_(self.conv_q.weight)
|
|
||||||
nn.init.xavier_uniform_(self.conv_k.weight)
|
|
||||||
nn.init.xavier_uniform_(self.conv_v.weight)
|
|
||||||
if proximal_init:
|
|
||||||
with torch.no_grad():
|
|
||||||
self.conv_k.weight.copy_(self.conv_q.weight)
|
|
||||||
self.conv_k.bias.copy_(self.conv_q.bias)
|
|
||||||
|
|
||||||
def forward(self, x, c, attn_mask=None):
|
|
||||||
q = self.conv_q(x)
|
|
||||||
k = self.conv_k(c)
|
|
||||||
v = self.conv_v(c)
|
|
||||||
|
|
||||||
x, self.attn = self.attention(q, k, v, mask=attn_mask)
|
|
||||||
|
|
||||||
x = self.conv_o(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
def attention(self, query, key, value, mask=None):
|
|
||||||
# reshape [b, d, t] -> [b, n_h, t, d_k]
|
|
||||||
b, d, t_s, t_t = (*key.size(), query.size(2))
|
|
||||||
query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
|
|
||||||
key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
|
||||||
value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
|
||||||
|
|
||||||
scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
|
|
||||||
if self.window_size is not None:
|
|
||||||
assert t_s == t_t, "Relative attention is only available for self-attention."
|
|
||||||
key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
|
|
||||||
rel_logits = self._matmul_with_relative_keys(query /math.sqrt(self.k_channels), key_relative_embeddings)
|
|
||||||
scores_local = self._relative_position_to_absolute_position(rel_logits)
|
|
||||||
scores = scores + scores_local
|
|
||||||
if self.proximal_bias:
|
|
||||||
assert t_s == t_t, "Proximal bias is only available for self-attention."
|
|
||||||
scores = scores + self._attention_bias_proximal(t_s).to(device=scores.device, dtype=scores.dtype)
|
|
||||||
if mask is not None:
|
|
||||||
scores = scores.masked_fill(mask == 0, -1e4)
|
|
||||||
if self.block_length is not None:
|
|
||||||
assert t_s == t_t, "Local attention is only available for self-attention."
|
|
||||||
block_mask = torch.ones_like(scores).triu(-self.block_length).tril(self.block_length)
|
|
||||||
scores = scores.masked_fill(block_mask == 0, -1e4)
|
|
||||||
p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
|
|
||||||
p_attn = self.drop(p_attn)
|
|
||||||
output = torch.matmul(p_attn, value)
|
|
||||||
if self.window_size is not None:
|
|
||||||
relative_weights = self._absolute_position_to_relative_position(p_attn)
|
|
||||||
value_relative_embeddings = self._get_relative_embeddings(self.emb_rel_v, t_s)
|
|
||||||
output = output + self._matmul_with_relative_values(relative_weights, value_relative_embeddings)
|
|
||||||
output = output.transpose(2, 3).contiguous().view(b, d, t_t) # [b, n_h, t_t, d_k] -> [b, d, t_t]
|
|
||||||
return output, p_attn
|
|
||||||
|
|
||||||
def _matmul_with_relative_values(self, x, y):
|
|
||||||
"""
|
|
||||||
x: [b, h, l, m]
|
|
||||||
y: [h or 1, m, d]
|
|
||||||
ret: [b, h, l, d]
|
|
||||||
"""
|
|
||||||
ret = torch.matmul(x, y.unsqueeze(0))
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def _matmul_with_relative_keys(self, x, y):
|
|
||||||
"""
|
|
||||||
x: [b, h, l, d]
|
|
||||||
y: [h or 1, m, d]
|
|
||||||
ret: [b, h, l, m]
|
|
||||||
"""
|
|
||||||
ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def _get_relative_embeddings(self, relative_embeddings, length):
|
|
||||||
max_relative_position = 2 * self.window_size + 1
|
|
||||||
# Pad first before slice to avoid using cond ops.
|
|
||||||
pad_length = max(length - (self.window_size + 1), 0)
|
|
||||||
slice_start_position = max((self.window_size + 1) - length, 0)
|
|
||||||
slice_end_position = slice_start_position + 2 * length - 1
|
|
||||||
if pad_length > 0:
|
|
||||||
padded_relative_embeddings = F.pad(
|
|
||||||
relative_embeddings,
|
|
||||||
convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]))
|
|
||||||
else:
|
|
||||||
padded_relative_embeddings = relative_embeddings
|
|
||||||
used_relative_embeddings = padded_relative_embeddings[:,slice_start_position:slice_end_position]
|
|
||||||
return used_relative_embeddings
|
|
||||||
|
|
||||||
def _relative_position_to_absolute_position(self, x):
|
|
||||||
"""
|
|
||||||
x: [b, h, l, 2*l-1]
|
|
||||||
ret: [b, h, l, l]
|
|
||||||
"""
|
|
||||||
batch, heads, length, _ = x.size()
|
|
||||||
# Concat columns of pad to shift from relative to absolute indexing.
|
|
||||||
x = F.pad(x, convert_pad_shape([[0,0],[0,0],[0,0],[0,1]]))
|
|
||||||
|
|
||||||
# Concat extra elements so to add up to shape (len+1, 2*len-1).
|
|
||||||
x_flat = x.view([batch, heads, length * 2 * length])
|
|
||||||
x_flat = F.pad(x_flat, convert_pad_shape([[0,0],[0,0],[0,length-1]]))
|
|
||||||
|
|
||||||
# Reshape and slice out the padded elements.
|
|
||||||
x_final = x_flat.view([batch, heads, length+1, 2*length-1])[:, :, :length, length-1:]
|
|
||||||
return x_final
|
|
||||||
|
|
||||||
def _absolute_position_to_relative_position(self, x):
|
|
||||||
"""
|
|
||||||
x: [b, h, l, l]
|
|
||||||
ret: [b, h, l, 2*l-1]
|
|
||||||
"""
|
|
||||||
batch, heads, length, _ = x.size()
|
|
||||||
# padd along column
|
|
||||||
x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length-1]]))
|
|
||||||
x_flat = x.view([batch, heads, length**2 + length*(length -1)])
|
|
||||||
# add 0's in the beginning that will skew the elements after reshape
|
|
||||||
x_flat = F.pad(x_flat, convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
|
|
||||||
x_final = x_flat.view([batch, heads, length, 2*length])[:,:,:,1:]
|
|
||||||
return x_final
|
|
||||||
|
|
||||||
def _attention_bias_proximal(self, length):
|
|
||||||
"""Bias for self-attention to encourage attention to close positions.
|
|
||||||
Args:
|
|
||||||
length: an integer scalar.
|
|
||||||
Returns:
|
|
||||||
a Tensor with shape [1, 1, length, length]
|
|
||||||
"""
|
|
||||||
r = torch.arange(length, dtype=torch.float32)
|
|
||||||
diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
|
|
||||||
return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
|
|
||||||
|
|
||||||
|
|
||||||
class FFN(nn.Module):
|
|
||||||
def __init__(self, in_channels, out_channels, filter_channels, kernel_size, p_dropout=0., activation=None, causal=False):
|
|
||||||
super().__init__()
|
|
||||||
self.in_channels = in_channels
|
|
||||||
self.out_channels = out_channels
|
|
||||||
self.filter_channels = filter_channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
self.activation = activation
|
|
||||||
self.causal = causal
|
|
||||||
|
|
||||||
if causal:
|
|
||||||
self.padding = self._causal_padding
|
|
||||||
else:
|
|
||||||
self.padding = self._same_padding
|
|
||||||
|
|
||||||
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
|
|
||||||
self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
|
|
||||||
self.drop = nn.Dropout(p_dropout)
|
|
||||||
|
|
||||||
def forward(self, x, x_mask):
|
|
||||||
x = self.conv_1(self.padding(x * x_mask))
|
|
||||||
if self.activation == "gelu":
|
|
||||||
x = x * torch.sigmoid(1.702 * x)
|
|
||||||
else:
|
|
||||||
x = torch.relu(x)
|
|
||||||
x = self.drop(x)
|
|
||||||
x = self.conv_2(self.padding(x * x_mask))
|
|
||||||
return x * x_mask
|
|
||||||
|
|
||||||
def _causal_padding(self, x):
|
|
||||||
if self.kernel_size == 1:
|
|
||||||
return x
|
|
||||||
pad_l = self.kernel_size - 1
|
|
||||||
pad_r = 0
|
|
||||||
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
|
||||||
x = F.pad(x, convert_pad_shape(padding))
|
|
||||||
return x
|
|
||||||
|
|
||||||
def _same_padding(self, x):
|
|
||||||
if self.kernel_size == 1:
|
|
||||||
return x
|
|
||||||
pad_l = (self.kernel_size - 1) // 2
|
|
||||||
pad_r = self.kernel_size // 2
|
|
||||||
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
|
||||||
x = F.pad(x, convert_pad_shape(padding))
|
|
||||||
return x
|
|
||||||
@@ -1,298 +0,0 @@
|
|||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
from .sublayer.global_style_token import GlobalStyleToken
|
|
||||||
from .sublayer.pre_net import PreNet
|
|
||||||
from .sublayer.cbhg import CBHG
|
|
||||||
from .sublayer.lsa import LSA
|
|
||||||
from .base import Base
|
|
||||||
from models.synthesizer.gst_hyperparameters import GSTHyperparameters as gst_hp
|
|
||||||
from models.synthesizer.hparams import hparams
|
|
||||||
|
|
||||||
class Encoder(nn.Module):
|
|
||||||
def __init__(self, num_chars, embed_dims=512, encoder_dims=256, K=5, num_highways=4, dropout=0.5):
|
|
||||||
""" Encoder for SV2TTS
|
|
||||||
|
|
||||||
Args:
|
|
||||||
num_chars (int): length of symbols
|
|
||||||
embed_dims (int, optional): embedding dim for input texts. Defaults to 512.
|
|
||||||
encoder_dims (int, optional): output dim for encoder. Defaults to 256.
|
|
||||||
K (int, optional): _description_. Defaults to 5.
|
|
||||||
num_highways (int, optional): _description_. Defaults to 4.
|
|
||||||
dropout (float, optional): _description_. Defaults to 0.5.
|
|
||||||
"""
|
|
||||||
super().__init__()
|
|
||||||
self.embedding = nn.Embedding(num_chars, embed_dims)
|
|
||||||
self.pre_net = PreNet(embed_dims, fc1_dims=encoder_dims, fc2_dims=encoder_dims,
|
|
||||||
dropout=dropout)
|
|
||||||
self.cbhg = CBHG(K=K, in_channels=encoder_dims, channels=encoder_dims,
|
|
||||||
proj_channels=[encoder_dims, encoder_dims],
|
|
||||||
num_highways=num_highways)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
"""forward pass for encoder
|
|
||||||
|
|
||||||
Args:
|
|
||||||
x (2D tensor with size `[batch_size, text_num_chars]`): input texts list
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
3D tensor with size `[batch_size, text_num_chars, encoder_dims]`
|
|
||||||
|
|
||||||
"""
|
|
||||||
x = self.embedding(x) # return: [batch_size, text_num_chars, tts_embed_dims]
|
|
||||||
x = self.pre_net(x) # return: [batch_size, text_num_chars, encoder_dims]
|
|
||||||
x.transpose_(1, 2) # return: [batch_size, encoder_dims, text_num_chars]
|
|
||||||
return self.cbhg(x) # return: [batch_size, text_num_chars, encoder_dims]
|
|
||||||
|
|
||||||
class Decoder(nn.Module):
|
|
||||||
# Class variable because its value doesn't change between classes
|
|
||||||
# yet ought to be scoped by class because its a property of a Decoder
|
|
||||||
max_r = 20
|
|
||||||
def __init__(self, n_mels, input_dims, decoder_dims, lstm_dims,
|
|
||||||
dropout, speaker_embedding_size):
|
|
||||||
super().__init__()
|
|
||||||
self.register_buffer("r", torch.tensor(1, dtype=torch.int))
|
|
||||||
self.n_mels = n_mels
|
|
||||||
self.prenet = PreNet(n_mels, fc1_dims=decoder_dims * 2, fc2_dims=decoder_dims * 2,
|
|
||||||
dropout=dropout)
|
|
||||||
self.attn_net = LSA(decoder_dims)
|
|
||||||
if hparams.use_gst:
|
|
||||||
speaker_embedding_size += gst_hp.E
|
|
||||||
self.attn_rnn = nn.GRUCell(input_dims + decoder_dims * 2, decoder_dims)
|
|
||||||
self.rnn_input = nn.Linear(input_dims + decoder_dims, lstm_dims)
|
|
||||||
self.res_rnn1 = nn.LSTMCell(lstm_dims, lstm_dims)
|
|
||||||
self.res_rnn2 = nn.LSTMCell(lstm_dims, lstm_dims)
|
|
||||||
self.mel_proj = nn.Linear(lstm_dims, n_mels * self.max_r, bias=False)
|
|
||||||
self.stop_proj = nn.Linear(input_dims + lstm_dims, 1)
|
|
||||||
|
|
||||||
def zoneout(self, prev, current, device, p=0.1):
|
|
||||||
mask = torch.zeros(prev.size(),device=device).bernoulli_(p)
|
|
||||||
return prev * mask + current * (1 - mask)
|
|
||||||
|
|
||||||
def forward(self, encoder_seq, encoder_seq_proj, prenet_in,
|
|
||||||
hidden_states, cell_states, context_vec, times, chars):
|
|
||||||
"""_summary_
|
|
||||||
|
|
||||||
Args:
|
|
||||||
encoder_seq (3D tensor `[batch_size, text_num_chars, project_dim(default to 512)]`): _description_
|
|
||||||
encoder_seq_proj (3D tensor `[batch_size, text_num_chars, decoder_dims(default to 128)]`): _description_
|
|
||||||
prenet_in (2D tensor `[batch_size, n_mels]`): _description_
|
|
||||||
hidden_states (_type_): _description_
|
|
||||||
cell_states (_type_): _description_
|
|
||||||
context_vec (2D tensor `[batch_size, project_dim(default to 512)]`): _description_
|
|
||||||
times (int): the number of times runned
|
|
||||||
chars (2D tensor with size `[batch_size, text_num_chars]`): original texts list input
|
|
||||||
|
|
||||||
"""
|
|
||||||
# Need this for reshaping mels
|
|
||||||
batch_size = encoder_seq.size(0)
|
|
||||||
device = encoder_seq.device
|
|
||||||
# Unpack the hidden and cell states
|
|
||||||
attn_hidden, rnn1_hidden, rnn2_hidden = hidden_states
|
|
||||||
rnn1_cell, rnn2_cell = cell_states
|
|
||||||
|
|
||||||
# PreNet for the Attention RNN
|
|
||||||
prenet_out = self.prenet(prenet_in) # return: `[batch_size, decoder_dims * 2(256)]`
|
|
||||||
|
|
||||||
# Compute the Attention RNN hidden state
|
|
||||||
attn_rnn_in = torch.cat([context_vec, prenet_out], dim=-1) # `[batch_size, project_dim + decoder_dims * 2 (768)]`
|
|
||||||
attn_hidden = self.attn_rnn(attn_rnn_in.squeeze(1), attn_hidden) # `[batch_size, decoder_dims (128)]`
|
|
||||||
|
|
||||||
# Compute the attention scores
|
|
||||||
scores = self.attn_net(encoder_seq_proj, attn_hidden, times, chars)
|
|
||||||
|
|
||||||
# Dot product to create the context vector
|
|
||||||
context_vec = scores @ encoder_seq
|
|
||||||
context_vec = context_vec.squeeze(1)
|
|
||||||
|
|
||||||
# Concat Attention RNN output w. Context Vector & project
|
|
||||||
x = torch.cat([context_vec, attn_hidden], dim=1) # `[batch_size, project_dim + decoder_dims (630)]`
|
|
||||||
x = self.rnn_input(x) # `[batch_size, lstm_dims(1024)]`
|
|
||||||
|
|
||||||
# Compute first Residual RNN, training with fixed zoneout rate 0.1
|
|
||||||
rnn1_hidden_next, rnn1_cell = self.res_rnn1(x, (rnn1_hidden, rnn1_cell)) # `[batch_size, lstm_dims(1024)]`
|
|
||||||
if self.training:
|
|
||||||
rnn1_hidden = self.zoneout(rnn1_hidden, rnn1_hidden_next,device=device)
|
|
||||||
else:
|
|
||||||
rnn1_hidden = rnn1_hidden_next
|
|
||||||
x = x + rnn1_hidden
|
|
||||||
|
|
||||||
# Compute second Residual RNN
|
|
||||||
rnn2_hidden_next, rnn2_cell = self.res_rnn2(x, (rnn2_hidden, rnn2_cell)) # `[batch_size, lstm_dims(1024)]`
|
|
||||||
if self.training:
|
|
||||||
rnn2_hidden = self.zoneout(rnn2_hidden, rnn2_hidden_next, device=device)
|
|
||||||
else:
|
|
||||||
rnn2_hidden = rnn2_hidden_next
|
|
||||||
x = x + rnn2_hidden
|
|
||||||
|
|
||||||
# Project Mels
|
|
||||||
mels = self.mel_proj(x) # `[batch_size, 1600]`
|
|
||||||
mels = mels.view(batch_size, self.n_mels, self.max_r)[:, :, :self.r] # `[batch_size, n_mels, r]`
|
|
||||||
hidden_states = (attn_hidden, rnn1_hidden, rnn2_hidden)
|
|
||||||
cell_states = (rnn1_cell, rnn2_cell)
|
|
||||||
|
|
||||||
# Stop token prediction
|
|
||||||
s = torch.cat((x, context_vec), dim=1)
|
|
||||||
s = self.stop_proj(s)
|
|
||||||
stop_tokens = torch.sigmoid(s)
|
|
||||||
|
|
||||||
return mels, scores, hidden_states, cell_states, context_vec, stop_tokens
|
|
||||||
|
|
||||||
class Tacotron(Base):
|
|
||||||
def __init__(self, embed_dims, num_chars, encoder_dims, decoder_dims, n_mels,
|
|
||||||
fft_bins, postnet_dims, encoder_K, lstm_dims, postnet_K, num_highways,
|
|
||||||
dropout, stop_threshold, speaker_embedding_size):
|
|
||||||
super().__init__(stop_threshold)
|
|
||||||
self.n_mels = n_mels
|
|
||||||
self.lstm_dims = lstm_dims
|
|
||||||
self.encoder_dims = encoder_dims
|
|
||||||
self.decoder_dims = decoder_dims
|
|
||||||
self.speaker_embedding_size = speaker_embedding_size
|
|
||||||
self.encoder = Encoder(num_chars, embed_dims, encoder_dims,
|
|
||||||
encoder_K, num_highways, dropout)
|
|
||||||
self.project_dims = encoder_dims + speaker_embedding_size
|
|
||||||
if hparams.use_gst:
|
|
||||||
self.project_dims += gst_hp.E
|
|
||||||
self.encoder_proj = nn.Linear(self.project_dims, decoder_dims, bias=False)
|
|
||||||
if hparams.use_gst:
|
|
||||||
self.gst = GlobalStyleToken(speaker_embedding_size)
|
|
||||||
self.decoder = Decoder(n_mels, self.project_dims, decoder_dims, lstm_dims,
|
|
||||||
dropout, speaker_embedding_size)
|
|
||||||
self.postnet = CBHG(postnet_K, n_mels, postnet_dims,
|
|
||||||
[postnet_dims, fft_bins], num_highways)
|
|
||||||
self.post_proj = nn.Linear(postnet_dims, fft_bins, bias=False)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _concat_speaker_embedding(outputs, speaker_embeddings):
|
|
||||||
speaker_embeddings_ = speaker_embeddings.expand(
|
|
||||||
outputs.size(0), outputs.size(1), -1)
|
|
||||||
outputs = torch.cat([outputs, speaker_embeddings_], dim=-1)
|
|
||||||
return outputs
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _add_speaker_embedding(x, speaker_embedding):
|
|
||||||
"""Add speaker embedding
|
|
||||||
This concats the speaker embedding for each char in the encoder output
|
|
||||||
Args:
|
|
||||||
x (3D tensor with size `[batch_size, text_num_chars, encoder_dims]`): the encoder output
|
|
||||||
speaker_embedding (2D tensor `[batch_size, speaker_embedding_size]`): the speaker embedding
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
3D tensor with size `[batch_size, text_num_chars, encoder_dims+speaker_embedding_size]`
|
|
||||||
"""
|
|
||||||
# Save the dimensions as human-readable names
|
|
||||||
batch_size = x.size()[0]
|
|
||||||
text_num_chars = x.size()[1]
|
|
||||||
|
|
||||||
# Start by making a copy of each speaker embedding to match the input text length
|
|
||||||
# The output of this has size (batch_size, text_num_chars * speaker_embedding_size)
|
|
||||||
speaker_embedding_size = speaker_embedding.size()[1]
|
|
||||||
e = speaker_embedding.repeat_interleave(text_num_chars, dim=1)
|
|
||||||
|
|
||||||
# Reshape it and transpose
|
|
||||||
e = e.reshape(batch_size, speaker_embedding_size, text_num_chars)
|
|
||||||
e = e.transpose(1, 2)
|
|
||||||
|
|
||||||
# Concatenate the tiled speaker embedding with the encoder output
|
|
||||||
x = torch.cat((x, e), 2)
|
|
||||||
return x
|
|
||||||
|
|
||||||
def forward(self, texts, mels, speaker_embedding, steps=2000, style_idx=0, min_stop_token=5):
|
|
||||||
"""Forward pass for Tacotron
|
|
||||||
|
|
||||||
Args:
|
|
||||||
texts (`[batch_size, text_num_chars]`): input texts list
|
|
||||||
mels (`[batch_size, varied_mel_lengths, steps]`): mels for comparison (training only)
|
|
||||||
speaker_embedding (`[batch_size, speaker_embedding_size(default to 256)]`): referring embedding.
|
|
||||||
steps (int, optional): . Defaults to 2000.
|
|
||||||
style_idx (int, optional): GST style selected. Defaults to 0.
|
|
||||||
min_stop_token (int, optional): decoder min_stop_token. Defaults to 5.
|
|
||||||
"""
|
|
||||||
device = texts.device # use same device as parameters
|
|
||||||
|
|
||||||
if self.training:
|
|
||||||
self.step += 1
|
|
||||||
batch_size, _, steps = mels.size()
|
|
||||||
else:
|
|
||||||
batch_size, _ = texts.size()
|
|
||||||
|
|
||||||
# Initialise all hidden states and pack into tuple
|
|
||||||
attn_hidden = torch.zeros(batch_size, self.decoder_dims, device=device)
|
|
||||||
rnn1_hidden = torch.zeros(batch_size, self.lstm_dims, device=device)
|
|
||||||
rnn2_hidden = torch.zeros(batch_size, self.lstm_dims, device=device)
|
|
||||||
hidden_states = (attn_hidden, rnn1_hidden, rnn2_hidden)
|
|
||||||
|
|
||||||
# Initialise all lstm cell states and pack into tuple
|
|
||||||
rnn1_cell = torch.zeros(batch_size, self.lstm_dims, device=device)
|
|
||||||
rnn2_cell = torch.zeros(batch_size, self.lstm_dims, device=device)
|
|
||||||
cell_states = (rnn1_cell, rnn2_cell)
|
|
||||||
|
|
||||||
# <GO> Frame for start of decoder loop
|
|
||||||
go_frame = torch.zeros(batch_size, self.n_mels, device=device)
|
|
||||||
|
|
||||||
# SV2TTS: Run the encoder with the speaker embedding
|
|
||||||
# The projection avoids unnecessary matmuls in the decoder loop
|
|
||||||
encoder_seq = self.encoder(texts)
|
|
||||||
|
|
||||||
encoder_seq = self._add_speaker_embedding(encoder_seq, speaker_embedding)
|
|
||||||
|
|
||||||
if hparams.use_gst and self.gst is not None:
|
|
||||||
if self.training:
|
|
||||||
style_embed = self.gst(speaker_embedding, speaker_embedding) # for training, speaker embedding can represent both style inputs and referenced
|
|
||||||
# style_embed = style_embed.expand_as(encoder_seq)
|
|
||||||
# encoder_seq = torch.cat((encoder_seq, style_embed), 2)
|
|
||||||
elif style_idx >= 0 and style_idx < 10:
|
|
||||||
query = torch.zeros(1, 1, self.gst.stl.attention.num_units)
|
|
||||||
if device.type == 'cuda':
|
|
||||||
query = query.cuda()
|
|
||||||
gst_embed = torch.tanh(self.gst.stl.embed)
|
|
||||||
key = gst_embed[style_idx].unsqueeze(0).expand(1, -1, -1)
|
|
||||||
style_embed = self.gst.stl.attention(query, key)
|
|
||||||
else:
|
|
||||||
speaker_embedding_style = torch.zeros(speaker_embedding.size()[0], 1, self.speaker_embedding_size).to(device)
|
|
||||||
style_embed = self.gst(speaker_embedding_style, speaker_embedding)
|
|
||||||
encoder_seq = self._concat_speaker_embedding(encoder_seq, style_embed) # return: [batch_size, text_num_chars, project_dims]
|
|
||||||
|
|
||||||
encoder_seq_proj = self.encoder_proj(encoder_seq) # return: [batch_size, text_num_chars, decoder_dims]
|
|
||||||
|
|
||||||
# Need a couple of lists for outputs
|
|
||||||
mel_outputs, attn_scores, stop_outputs = [], [], []
|
|
||||||
|
|
||||||
# Need an initial context vector
|
|
||||||
context_vec = torch.zeros(batch_size, self.project_dims, device=device)
|
|
||||||
|
|
||||||
# Run the decoder loop
|
|
||||||
for t in range(0, steps, self.r):
|
|
||||||
if self.training:
|
|
||||||
prenet_in = mels[:, :, t -1] if t > 0 else go_frame
|
|
||||||
else:
|
|
||||||
prenet_in = mel_outputs[-1][:, :, -1] if t > 0 else go_frame
|
|
||||||
mel_frames, scores, hidden_states, cell_states, context_vec, stop_tokens = \
|
|
||||||
self.decoder(encoder_seq, encoder_seq_proj, prenet_in,
|
|
||||||
hidden_states, cell_states, context_vec, t, texts)
|
|
||||||
mel_outputs.append(mel_frames)
|
|
||||||
attn_scores.append(scores)
|
|
||||||
stop_outputs.extend([stop_tokens] * self.r)
|
|
||||||
if not self.training and (stop_tokens * 10 > min_stop_token).all() and t > 10: break
|
|
||||||
|
|
||||||
# Concat the mel outputs into sequence
|
|
||||||
mel_outputs = torch.cat(mel_outputs, dim=2)
|
|
||||||
|
|
||||||
# Post-Process for Linear Spectrograms
|
|
||||||
postnet_out = self.postnet(mel_outputs)
|
|
||||||
linear = self.post_proj(postnet_out)
|
|
||||||
linear = linear.transpose(1, 2)
|
|
||||||
|
|
||||||
# For easy visualisation
|
|
||||||
attn_scores = torch.cat(attn_scores, 1)
|
|
||||||
# attn_scores = attn_scores.cpu().data.numpy()
|
|
||||||
stop_outputs = torch.cat(stop_outputs, 1)
|
|
||||||
|
|
||||||
if self.training:
|
|
||||||
self.train()
|
|
||||||
|
|
||||||
return mel_outputs, linear, attn_scores, stop_outputs
|
|
||||||
|
|
||||||
def generate(self, x, speaker_embedding, steps=2000, style_idx=0, min_stop_token=5):
|
|
||||||
self.eval()
|
|
||||||
mel_outputs, linear, attn_scores, _ = self.forward(x, None, speaker_embedding, steps, style_idx, min_stop_token)
|
|
||||||
return mel_outputs, linear, attn_scores
|
|
||||||
@@ -1,524 +0,0 @@
|
|||||||
import math
|
|
||||||
import torch
|
|
||||||
from torch import nn
|
|
||||||
from torch.nn import functional as F
|
|
||||||
|
|
||||||
from .sublayer.vits_modules import *
|
|
||||||
import monotonic_align
|
|
||||||
|
|
||||||
from .base import Base
|
|
||||||
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
|
||||||
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
|
||||||
from utils.util import init_weights, get_padding, sequence_mask, rand_slice_segments, generate_path
|
|
||||||
|
|
||||||
|
|
||||||
class StochasticDurationPredictor(nn.Module):
|
|
||||||
def __init__(self, in_channels, filter_channels, kernel_size, p_dropout, n_flows=4, gin_channels=0):
|
|
||||||
super().__init__()
|
|
||||||
filter_channels = in_channels # it needs to be removed from future version.
|
|
||||||
self.in_channels = in_channels
|
|
||||||
self.filter_channels = filter_channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
self.n_flows = n_flows
|
|
||||||
self.gin_channels = gin_channels
|
|
||||||
|
|
||||||
self.log_flow = Log()
|
|
||||||
self.flows = nn.ModuleList()
|
|
||||||
self.flows.append(ElementwiseAffine(2))
|
|
||||||
for i in range(n_flows):
|
|
||||||
self.flows.append(ConvFlow(2, filter_channels, kernel_size, n_layers=3))
|
|
||||||
self.flows.append(Flip())
|
|
||||||
|
|
||||||
self.post_pre = nn.Conv1d(1, filter_channels, 1)
|
|
||||||
self.post_proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
|
||||||
self.post_convs = DDSConv(filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout)
|
|
||||||
self.post_flows = nn.ModuleList()
|
|
||||||
self.post_flows.append(ElementwiseAffine(2))
|
|
||||||
for i in range(4):
|
|
||||||
self.post_flows.append(ConvFlow(2, filter_channels, kernel_size, n_layers=3))
|
|
||||||
self.post_flows.append(Flip())
|
|
||||||
|
|
||||||
self.pre = nn.Conv1d(in_channels, filter_channels, 1)
|
|
||||||
self.proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
|
||||||
self.convs = DDSConv(filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout)
|
|
||||||
if gin_channels != 0:
|
|
||||||
self.cond = nn.Conv1d(gin_channels, filter_channels, 1)
|
|
||||||
|
|
||||||
def forward(self, x, x_mask, w=None, g=None, reverse=False, noise_scale=1.0):
|
|
||||||
x = torch.detach(x)
|
|
||||||
x = self.pre(x)
|
|
||||||
if g is not None:
|
|
||||||
g = torch.detach(g)
|
|
||||||
x = x + self.cond(g)
|
|
||||||
x = self.convs(x, x_mask)
|
|
||||||
x = self.proj(x) * x_mask
|
|
||||||
|
|
||||||
if not reverse:
|
|
||||||
flows = self.flows
|
|
||||||
assert w is not None
|
|
||||||
|
|
||||||
logdet_tot_q = 0
|
|
||||||
h_w = self.post_pre(w)
|
|
||||||
h_w = self.post_convs(h_w, x_mask)
|
|
||||||
h_w = self.post_proj(h_w) * x_mask
|
|
||||||
e_q = torch.randn(w.size(0), 2, w.size(2)).to(device=x.device, dtype=x.dtype) * x_mask
|
|
||||||
z_q = e_q
|
|
||||||
for flow in self.post_flows:
|
|
||||||
z_q, logdet_q = flow(z_q, x_mask, g=(x + h_w))
|
|
||||||
logdet_tot_q += logdet_q
|
|
||||||
z_u, z1 = torch.split(z_q, [1, 1], 1)
|
|
||||||
u = torch.sigmoid(z_u) * x_mask
|
|
||||||
z0 = (w - u) * x_mask
|
|
||||||
logdet_tot_q += torch.sum((F.logsigmoid(z_u) + F.logsigmoid(-z_u)) * x_mask, [1,2])
|
|
||||||
logq = torch.sum(-0.5 * (math.log(2*math.pi) + (e_q**2)) * x_mask, [1,2]) - logdet_tot_q
|
|
||||||
|
|
||||||
logdet_tot = 0
|
|
||||||
z0, logdet = self.log_flow(z0, x_mask)
|
|
||||||
logdet_tot += logdet
|
|
||||||
z = torch.cat([z0, z1], 1)
|
|
||||||
for flow in flows:
|
|
||||||
z, logdet = flow(z, x_mask, g=x, reverse=reverse)
|
|
||||||
logdet_tot = logdet_tot + logdet
|
|
||||||
nll = torch.sum(0.5 * (math.log(2*math.pi) + (z**2)) * x_mask, [1,2]) - logdet_tot
|
|
||||||
return nll + logq # [b]
|
|
||||||
else:
|
|
||||||
flows = list(reversed(self.flows))
|
|
||||||
flows = flows[:-2] + [flows[-1]] # remove a useless vflow
|
|
||||||
z = torch.randn(x.size(0), 2, x.size(2)).to(device=x.device, dtype=x.dtype) * noise_scale
|
|
||||||
for flow in flows:
|
|
||||||
z = flow(z, x_mask, g=x, reverse=reverse)
|
|
||||||
z0, z1 = torch.split(z, [1, 1], 1)
|
|
||||||
logw = z0
|
|
||||||
return logw
|
|
||||||
|
|
||||||
|
|
||||||
class DurationPredictor(nn.Module):
|
|
||||||
def __init__(self, in_channels, filter_channels, kernel_size, p_dropout, gin_channels=0):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
self.in_channels = in_channels
|
|
||||||
self.filter_channels = filter_channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
self.gin_channels = gin_channels
|
|
||||||
|
|
||||||
self.drop = nn.Dropout(p_dropout)
|
|
||||||
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size, padding=kernel_size//2)
|
|
||||||
self.norm_1 = LayerNorm(filter_channels)
|
|
||||||
self.conv_2 = nn.Conv1d(filter_channels, filter_channels, kernel_size, padding=kernel_size//2)
|
|
||||||
self.norm_2 = LayerNorm(filter_channels)
|
|
||||||
self.proj = nn.Conv1d(filter_channels, 1, 1)
|
|
||||||
|
|
||||||
if gin_channels != 0:
|
|
||||||
self.cond = nn.Conv1d(gin_channels, in_channels, 1)
|
|
||||||
|
|
||||||
def forward(self, x, x_mask, g=None):
|
|
||||||
x = torch.detach(x)
|
|
||||||
if g is not None:
|
|
||||||
g = torch.detach(g)
|
|
||||||
x = x + self.cond(g)
|
|
||||||
x = self.conv_1(x * x_mask)
|
|
||||||
x = torch.relu(x)
|
|
||||||
x = self.norm_1(x)
|
|
||||||
x = self.drop(x)
|
|
||||||
x = self.conv_2(x * x_mask)
|
|
||||||
x = torch.relu(x)
|
|
||||||
x = self.norm_2(x)
|
|
||||||
x = self.drop(x)
|
|
||||||
x = self.proj(x * x_mask)
|
|
||||||
return x * x_mask
|
|
||||||
|
|
||||||
|
|
||||||
class TextEncoder(nn.Module):
|
|
||||||
def __init__(self,
|
|
||||||
n_vocab,
|
|
||||||
out_channels,
|
|
||||||
hidden_channels,
|
|
||||||
filter_channels,
|
|
||||||
n_heads,
|
|
||||||
n_layers,
|
|
||||||
kernel_size,
|
|
||||||
p_dropout):
|
|
||||||
super().__init__()
|
|
||||||
self.n_vocab = n_vocab
|
|
||||||
self.out_channels = out_channels
|
|
||||||
self.hidden_channels = hidden_channels
|
|
||||||
self.filter_channels = filter_channels
|
|
||||||
self.n_heads = n_heads
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
|
|
||||||
self.emb = nn.Embedding(n_vocab, hidden_channels)
|
|
||||||
self.emo_proj = nn.Linear(1024, hidden_channels)
|
|
||||||
|
|
||||||
nn.init.normal_(self.emb.weight, 0.0, hidden_channels**-0.5)
|
|
||||||
|
|
||||||
self.encoder = Encoder(
|
|
||||||
hidden_channels,
|
|
||||||
filter_channels,
|
|
||||||
n_heads,
|
|
||||||
n_layers,
|
|
||||||
kernel_size,
|
|
||||||
p_dropout)
|
|
||||||
self.proj= nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
|
||||||
|
|
||||||
def forward(self, x, x_lengths, emo):
|
|
||||||
x = self.emb(x) * math.sqrt(self.hidden_channels) # [b, t, h]
|
|
||||||
x = x + self.emo_proj(emo.unsqueeze(1))
|
|
||||||
x = torch.transpose(x, 1, -1) # [b, h, t]
|
|
||||||
x_mask = torch.unsqueeze(sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
|
|
||||||
|
|
||||||
x = self.encoder(x * x_mask, x_mask)
|
|
||||||
stats = self.proj(x) * x_mask
|
|
||||||
|
|
||||||
m, logs = torch.split(stats, self.out_channels, dim=1)
|
|
||||||
return x, m, logs, x_mask
|
|
||||||
|
|
||||||
|
|
||||||
class ResidualCouplingBlock(nn.Module):
|
|
||||||
def __init__(self,
|
|
||||||
channels,
|
|
||||||
hidden_channels,
|
|
||||||
kernel_size,
|
|
||||||
dilation_rate,
|
|
||||||
n_layers,
|
|
||||||
n_flows=4,
|
|
||||||
gin_channels=0):
|
|
||||||
super().__init__()
|
|
||||||
self.channels = channels
|
|
||||||
self.hidden_channels = hidden_channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.dilation_rate = dilation_rate
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.n_flows = n_flows
|
|
||||||
self.gin_channels = gin_channels
|
|
||||||
|
|
||||||
self.flows = nn.ModuleList()
|
|
||||||
for i in range(n_flows):
|
|
||||||
self.flows.append(ResidualCouplingLayer(channels, hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels, mean_only=True))
|
|
||||||
self.flows.append(Flip())
|
|
||||||
|
|
||||||
def forward(self, x, x_mask, g=None, reverse=False):
|
|
||||||
if not reverse:
|
|
||||||
for flow in self.flows:
|
|
||||||
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
|
||||||
else:
|
|
||||||
for flow in reversed(self.flows):
|
|
||||||
x = flow(x, x_mask, g=g, reverse=reverse)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class PosteriorEncoder(nn.Module):
|
|
||||||
def __init__(self,
|
|
||||||
in_channels,
|
|
||||||
out_channels,
|
|
||||||
hidden_channels,
|
|
||||||
kernel_size,
|
|
||||||
dilation_rate,
|
|
||||||
n_layers,
|
|
||||||
gin_channels=0):
|
|
||||||
super().__init__()
|
|
||||||
self.in_channels = in_channels
|
|
||||||
self.out_channels = out_channels
|
|
||||||
self.hidden_channels = hidden_channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.dilation_rate = dilation_rate
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.gin_channels = gin_channels
|
|
||||||
|
|
||||||
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
|
||||||
self.enc = WN(hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels)
|
|
||||||
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
|
||||||
|
|
||||||
def forward(self, x, x_lengths, g=None):
|
|
||||||
x_mask = torch.unsqueeze(sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
|
|
||||||
x = self.pre(x) * x_mask
|
|
||||||
x = self.enc(x, x_mask, g=g)
|
|
||||||
stats = self.proj(x) * x_mask
|
|
||||||
m, logs = torch.split(stats, self.out_channels, dim=1)
|
|
||||||
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
|
||||||
return z, m, logs, x_mask
|
|
||||||
|
|
||||||
|
|
||||||
class Generator(torch.nn.Module):
|
|
||||||
def __init__(self, initial_channel, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates, upsample_initial_channel, upsample_kernel_sizes, gin_channels=0):
|
|
||||||
super(Generator, self).__init__()
|
|
||||||
self.num_kernels = len(resblock_kernel_sizes)
|
|
||||||
self.num_upsamples = len(upsample_rates)
|
|
||||||
self.conv_pre = Conv1d(initial_channel, upsample_initial_channel, 7, 1, padding=3)
|
|
||||||
resblock = ResBlock1 if resblock == '1' else ResBlock2
|
|
||||||
|
|
||||||
self.ups = nn.ModuleList()
|
|
||||||
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
|
||||||
self.ups.append(weight_norm(
|
|
||||||
ConvTranspose1d(upsample_initial_channel//(2**i), upsample_initial_channel//(2**(i+1)),
|
|
||||||
k, u, padding=(k-u)//2)))
|
|
||||||
|
|
||||||
self.resblocks = nn.ModuleList()
|
|
||||||
for i in range(len(self.ups)):
|
|
||||||
ch = upsample_initial_channel//(2**(i+1))
|
|
||||||
for j, (k, d) in enumerate(zip(resblock_kernel_sizes, resblock_dilation_sizes)):
|
|
||||||
self.resblocks.append(resblock(ch, k, d))
|
|
||||||
|
|
||||||
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
|
||||||
self.ups.apply(init_weights)
|
|
||||||
|
|
||||||
if gin_channels != 0:
|
|
||||||
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
|
||||||
|
|
||||||
def forward(self, x, g=None):
|
|
||||||
x = self.conv_pre(x)
|
|
||||||
if g is not None:
|
|
||||||
x = x + self.cond(g)
|
|
||||||
|
|
||||||
for i in range(self.num_upsamples):
|
|
||||||
x = F.leaky_relu(x, LRELU_SLOPE)
|
|
||||||
x = self.ups[i](x)
|
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
class DiscriminatorP(torch.nn.Module):
|
|
||||||
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
|
||||||
super(DiscriminatorP, self).__init__()
|
|
||||||
self.period = period
|
|
||||||
self.use_spectral_norm = use_spectral_norm
|
|
||||||
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
|
||||||
self.convs = nn.ModuleList([
|
|
||||||
norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
|
||||||
norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
|
||||||
norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
|
||||||
norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
|
||||||
norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(get_padding(kernel_size, 1), 0))),
|
|
||||||
])
|
|
||||||
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
fmap = []
|
|
||||||
|
|
||||||
# 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 DiscriminatorS(torch.nn.Module):
|
|
||||||
def __init__(self, use_spectral_norm=False):
|
|
||||||
super(DiscriminatorS, self).__init__()
|
|
||||||
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
|
||||||
self.convs = nn.ModuleList([
|
|
||||||
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
|
||||||
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
|
||||||
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
|
||||||
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
|
||||||
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, 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):
|
|
||||||
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 MultiPeriodDiscriminator(torch.nn.Module):
|
|
||||||
def __init__(self, use_spectral_norm=False):
|
|
||||||
super(MultiPeriodDiscriminator, self).__init__()
|
|
||||||
periods = [2,3,5,7,11]
|
|
||||||
|
|
||||||
discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
|
|
||||||
discs = discs + [DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods]
|
|
||||||
self.discriminators = nn.ModuleList(discs)
|
|
||||||
|
|
||||||
def forward(self, y, y_hat):
|
|
||||||
y_d_rs = []
|
|
||||||
y_d_gs = []
|
|
||||||
fmap_rs = []
|
|
||||||
fmap_gs = []
|
|
||||||
for i, d in enumerate(self.discriminators):
|
|
||||||
y_d_r, fmap_r = d(y)
|
|
||||||
y_d_g, fmap_g = d(y_hat)
|
|
||||||
y_d_rs.append(y_d_r)
|
|
||||||
y_d_gs.append(y_d_g)
|
|
||||||
fmap_rs.append(fmap_r)
|
|
||||||
fmap_gs.append(fmap_g)
|
|
||||||
|
|
||||||
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
|
||||||
|
|
||||||
class Vits(Base):
|
|
||||||
"""
|
|
||||||
Synthesizer of Vits
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
n_vocab,
|
|
||||||
spec_channels,
|
|
||||||
segment_size,
|
|
||||||
inter_channels,
|
|
||||||
hidden_channels,
|
|
||||||
filter_channels,
|
|
||||||
n_heads,
|
|
||||||
n_layers,
|
|
||||||
kernel_size,
|
|
||||||
p_dropout,
|
|
||||||
resblock,
|
|
||||||
resblock_kernel_sizes,
|
|
||||||
resblock_dilation_sizes,
|
|
||||||
upsample_rates,
|
|
||||||
upsample_initial_channel,
|
|
||||||
upsample_kernel_sizes,
|
|
||||||
stop_threshold,
|
|
||||||
n_speakers=0,
|
|
||||||
gin_channels=0,
|
|
||||||
use_sdp=True,
|
|
||||||
**kwargs):
|
|
||||||
|
|
||||||
super().__init__(stop_threshold)
|
|
||||||
self.n_vocab = n_vocab
|
|
||||||
self.spec_channels = spec_channels
|
|
||||||
self.inter_channels = inter_channels
|
|
||||||
self.hidden_channels = hidden_channels
|
|
||||||
self.filter_channels = filter_channels
|
|
||||||
self.n_heads = n_heads
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.p_dropout = p_dropout
|
|
||||||
self.resblock = resblock
|
|
||||||
self.resblock_kernel_sizes = resblock_kernel_sizes
|
|
||||||
self.resblock_dilation_sizes = resblock_dilation_sizes
|
|
||||||
self.upsample_rates = upsample_rates
|
|
||||||
self.upsample_initial_channel = upsample_initial_channel
|
|
||||||
self.upsample_kernel_sizes = upsample_kernel_sizes
|
|
||||||
self.segment_size = segment_size
|
|
||||||
self.n_speakers = n_speakers
|
|
||||||
self.gin_channels = gin_channels
|
|
||||||
|
|
||||||
self.use_sdp = use_sdp
|
|
||||||
|
|
||||||
self.enc_p = TextEncoder(n_vocab,
|
|
||||||
inter_channels,
|
|
||||||
hidden_channels,
|
|
||||||
filter_channels,
|
|
||||||
n_heads,
|
|
||||||
n_layers,
|
|
||||||
kernel_size,
|
|
||||||
p_dropout)
|
|
||||||
self.dec = Generator(inter_channels, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates, upsample_initial_channel, upsample_kernel_sizes, gin_channels=gin_channels)
|
|
||||||
self.enc_q = PosteriorEncoder(spec_channels, inter_channels, hidden_channels, 5, 1, 16, gin_channels=gin_channels)
|
|
||||||
self.flow = ResidualCouplingBlock(inter_channels, hidden_channels, 5, 1, 4, gin_channels=gin_channels)
|
|
||||||
|
|
||||||
if use_sdp:
|
|
||||||
self.dp = StochasticDurationPredictor(hidden_channels, 192, 3, 0.5, 4, gin_channels=gin_channels)
|
|
||||||
else:
|
|
||||||
self.dp = DurationPredictor(hidden_channels, 256, 3, 0.5, gin_channels=gin_channels)
|
|
||||||
|
|
||||||
if n_speakers > 1:
|
|
||||||
self.emb_g = nn.Embedding(n_speakers, gin_channels)
|
|
||||||
|
|
||||||
def forward(self, x, x_lengths, y, y_lengths, sid=None, emo=None):
|
|
||||||
|
|
||||||
x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths, emo)
|
|
||||||
if self.n_speakers > 0:
|
|
||||||
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
|
||||||
else:
|
|
||||||
g = None
|
|
||||||
|
|
||||||
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g)
|
|
||||||
z_p = self.flow(z, y_mask, g=g)
|
|
||||||
|
|
||||||
with torch.no_grad():
|
|
||||||
# negative cross-entropy
|
|
||||||
s_p_sq_r = torch.exp(-2 * logs_p) # [b, d, t]
|
|
||||||
neg_cent1 = torch.sum(-0.5 * math.log(2 * math.pi) - logs_p, [1], keepdim=True) # [b, 1, t_s]
|
|
||||||
neg_cent2 = torch.matmul(-0.5 * (z_p ** 2).transpose(1, 2), s_p_sq_r) # [b, t_t, d] x [b, d, t_s] = [b, t_t, t_s]
|
|
||||||
neg_cent3 = torch.matmul(z_p.transpose(1, 2), (m_p * s_p_sq_r)) # [b, t_t, d] x [b, d, t_s] = [b, t_t, t_s]
|
|
||||||
neg_cent4 = torch.sum(-0.5 * (m_p ** 2) * s_p_sq_r, [1], keepdim=True) # [b, 1, t_s]
|
|
||||||
neg_cent = neg_cent1 + neg_cent2 + neg_cent3 + neg_cent4
|
|
||||||
|
|
||||||
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1)
|
|
||||||
attn = monotonic_align.maximum_path(neg_cent, attn_mask.squeeze(1)).unsqueeze(1).detach()
|
|
||||||
|
|
||||||
w = attn.sum(2)
|
|
||||||
if self.use_sdp:
|
|
||||||
l_length = self.dp(x, x_mask, w, g=g)
|
|
||||||
l_length = l_length / torch.sum(x_mask)
|
|
||||||
else:
|
|
||||||
logw_ = torch.log(w + 1e-6) * x_mask
|
|
||||||
logw = self.dp(x, x_mask, g=g)
|
|
||||||
l_length = torch.sum((logw - logw_)**2, [1,2]) / torch.sum(x_mask) # for averaging
|
|
||||||
|
|
||||||
# expand prior
|
|
||||||
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2)
|
|
||||||
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1, 2)
|
|
||||||
|
|
||||||
z_slice, ids_slice = rand_slice_segments(z, y_lengths, self.segment_size)
|
|
||||||
o = self.dec(z_slice, g=g)
|
|
||||||
return o, l_length, attn, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q)
|
|
||||||
|
|
||||||
def infer(self, x, x_lengths, sid=None, emo=None, noise_scale=1, length_scale=1, noise_scale_w=1., max_len=None):
|
|
||||||
x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths,emo)
|
|
||||||
if self.n_speakers > 0:
|
|
||||||
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
|
||||||
else:
|
|
||||||
g = None
|
|
||||||
|
|
||||||
if self.use_sdp:
|
|
||||||
logw = self.dp(x, x_mask, g=g, reverse=True, noise_scale=noise_scale_w)
|
|
||||||
else:
|
|
||||||
logw = self.dp(x, x_mask, g=g)
|
|
||||||
w = torch.exp(logw) * x_mask * length_scale
|
|
||||||
w_ceil = torch.ceil(w)
|
|
||||||
y_lengths = torch.clamp_min(torch.sum(w_ceil, [1, 2]), 1).long()
|
|
||||||
y_mask = torch.unsqueeze(sequence_mask(y_lengths, None), 1).to(x_mask.dtype)
|
|
||||||
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1)
|
|
||||||
attn = generate_path(w_ceil, attn_mask)
|
|
||||||
|
|
||||||
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
|
||||||
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
|
||||||
|
|
||||||
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
|
|
||||||
z = self.flow(z_p, y_mask, g=g, reverse=True)
|
|
||||||
o = self.dec((z * y_mask)[:,:,:max_len], g=g)
|
|
||||||
return o, attn, y_mask, (z, z_p, m_p, logs_p)
|
|
||||||
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
from transformers.models.wav2vec2.modeling_wav2vec2 import (
|
|
||||||
Wav2Vec2Model,
|
|
||||||
Wav2Vec2PreTrainedModel,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class RegressionHead(nn.Module):
|
|
||||||
r"""Classification head."""
|
|
||||||
|
|
||||||
def __init__(self, config):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
|
|
||||||
self.dropout = nn.Dropout(config.final_dropout)
|
|
||||||
self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
|
|
||||||
|
|
||||||
def forward(self, features, **kwargs):
|
|
||||||
x = features
|
|
||||||
x = self.dropout(x)
|
|
||||||
x = self.dense(x)
|
|
||||||
x = torch.tanh(x)
|
|
||||||
x = self.dropout(x)
|
|
||||||
x = self.out_proj(x)
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class EmotionExtractorModel(Wav2Vec2PreTrainedModel):
|
|
||||||
r"""Speech emotion classifier."""
|
|
||||||
|
|
||||||
def __init__(self, config):
|
|
||||||
super().__init__(config)
|
|
||||||
|
|
||||||
self.config = config
|
|
||||||
self.wav2vec2 = Wav2Vec2Model(config)
|
|
||||||
self.classifier = RegressionHead(config)
|
|
||||||
self.init_weights()
|
|
||||||
|
|
||||||
def forward(
|
|
||||||
self,
|
|
||||||
input_values,
|
|
||||||
):
|
|
||||||
outputs = self.wav2vec2(input_values)
|
|
||||||
hidden_states = outputs[0]
|
|
||||||
hidden_states = torch.mean(hidden_states, dim=1)
|
|
||||||
logits = self.classifier(hidden_states)
|
|
||||||
|
|
||||||
return hidden_states, logits
|
|
||||||
@@ -1,389 +0,0 @@
|
|||||||
import os
|
|
||||||
from loguru import logger
|
|
||||||
import torch
|
|
||||||
import glob
|
|
||||||
from torch.nn import functional as F
|
|
||||||
from torch.utils.data import DataLoader
|
|
||||||
from torch.utils.tensorboard import SummaryWriter
|
|
||||||
import torch.distributed as dist
|
|
||||||
from torch.nn.parallel import DistributedDataParallel as DDP
|
|
||||||
from torch.cuda.amp import autocast, GradScaler
|
|
||||||
from utils.audio_utils import mel_spectrogram, spec_to_mel
|
|
||||||
from utils.loss import feature_loss, generator_loss, discriminator_loss, kl_loss
|
|
||||||
from utils.util import slice_segments, clip_grad_value_
|
|
||||||
from models.synthesizer.vits_dataset import (
|
|
||||||
VitsDataset,
|
|
||||||
VitsDatasetCollate,
|
|
||||||
DistributedBucketSampler
|
|
||||||
)
|
|
||||||
from models.synthesizer.models.vits import (
|
|
||||||
Vits,
|
|
||||||
MultiPeriodDiscriminator,
|
|
||||||
)
|
|
||||||
from models.synthesizer.utils.symbols import symbols
|
|
||||||
from models.synthesizer.utils.plot import plot_spectrogram_to_numpy, plot_alignment_to_numpy
|
|
||||||
from pathlib import Path
|
|
||||||
from utils.hparams import HParams
|
|
||||||
import torch.multiprocessing as mp
|
|
||||||
import argparse
|
|
||||||
|
|
||||||
# torch.backends.cudnn.benchmark = True
|
|
||||||
global_step = 0
|
|
||||||
|
|
||||||
|
|
||||||
def new_train():
|
|
||||||
"""Assume Single Node Multi GPUs Training Only"""
|
|
||||||
assert torch.cuda.is_available(), "CPU training is not allowed."
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument("--syn_dir", type=str, default="../audiodata/SV2TTS/synthesizer", help= \
|
|
||||||
"Path to the synthesizer directory that contains the ground truth mel spectrograms, "
|
|
||||||
"the wavs, the emos and the embeds.")
|
|
||||||
parser.add_argument("-m", "--model_dir", type=str, default="data/ckpt/synthesizer/vits", help=\
|
|
||||||
"Path to the output directory that will contain the saved model weights and the logs.")
|
|
||||||
parser.add_argument('--ckptG', type=str, required=False,
|
|
||||||
help='original VITS G checkpoint path')
|
|
||||||
parser.add_argument('--ckptD', type=str, required=False,
|
|
||||||
help='original VITS D checkpoint path')
|
|
||||||
args, _ = parser.parse_known_args()
|
|
||||||
|
|
||||||
datasets_root = Path(args.syn_dir)
|
|
||||||
hparams= HParams(
|
|
||||||
model_dir = args.model_dir,
|
|
||||||
)
|
|
||||||
hparams.loadJson(Path(hparams.model_dir).joinpath("config.json"))
|
|
||||||
hparams.data["training_files"] = str(datasets_root.joinpath("train.txt"))
|
|
||||||
hparams.data["validation_files"] = str(datasets_root.joinpath("train.txt"))
|
|
||||||
hparams.data["datasets_root"] = str(datasets_root)
|
|
||||||
hparams.ckptG = args.ckptG
|
|
||||||
hparams.ckptD = args.ckptD
|
|
||||||
n_gpus = torch.cuda.device_count()
|
|
||||||
# for spawn
|
|
||||||
os.environ['MASTER_ADDR'] = 'localhost'
|
|
||||||
os.environ['MASTER_PORT'] = '8899'
|
|
||||||
# mp.spawn(run, nprocs=n_gpus, args=(n_gpus, hparams))
|
|
||||||
run(0, 1, hparams)
|
|
||||||
|
|
||||||
|
|
||||||
def load_checkpoint(checkpoint_path, model, optimizer=None, is_old=False):
|
|
||||||
assert os.path.isfile(checkpoint_path)
|
|
||||||
checkpoint_dict = torch.load(checkpoint_path, map_location='cpu')
|
|
||||||
iteration = checkpoint_dict['iteration']
|
|
||||||
learning_rate = checkpoint_dict['learning_rate']
|
|
||||||
if optimizer is not None:
|
|
||||||
if not is_old:
|
|
||||||
optimizer.load_state_dict(checkpoint_dict['optimizer'])
|
|
||||||
else:
|
|
||||||
new_opt_dict = optimizer.state_dict()
|
|
||||||
new_opt_dict_params = new_opt_dict['param_groups'][0]['params']
|
|
||||||
new_opt_dict['param_groups'] = checkpoint_dict['optimizer']['param_groups']
|
|
||||||
new_opt_dict['param_groups'][0]['params'] = new_opt_dict_params
|
|
||||||
optimizer.load_state_dict(new_opt_dict)
|
|
||||||
saved_state_dict = checkpoint_dict['model']
|
|
||||||
if hasattr(model, 'module'):
|
|
||||||
state_dict = model.module.state_dict()
|
|
||||||
else:
|
|
||||||
state_dict = model.state_dict()
|
|
||||||
new_state_dict= {}
|
|
||||||
for k, v in state_dict.items():
|
|
||||||
try:
|
|
||||||
new_state_dict[k] = saved_state_dict[k]
|
|
||||||
except:
|
|
||||||
logger.info("%s is not in the checkpoint" % k)
|
|
||||||
new_state_dict[k] = v
|
|
||||||
if hasattr(model, 'module'):
|
|
||||||
model.module.load_state_dict(new_state_dict, strict=False)
|
|
||||||
else:
|
|
||||||
model.load_state_dict(new_state_dict, strict=False)
|
|
||||||
logger.info("Loaded checkpoint '{}' (iteration {})" .format(
|
|
||||||
checkpoint_path, iteration))
|
|
||||||
return model, optimizer, learning_rate, iteration
|
|
||||||
|
|
||||||
|
|
||||||
def save_checkpoint(model, optimizer, learning_rate, iteration, checkpoint_path):
|
|
||||||
logger.info("Saving model and optimizer state at iteration {} to {}".format(
|
|
||||||
iteration, checkpoint_path))
|
|
||||||
if hasattr(model, 'module'):
|
|
||||||
state_dict = model.module.state_dict()
|
|
||||||
else:
|
|
||||||
state_dict = model.state_dict()
|
|
||||||
torch.save({'model': state_dict,
|
|
||||||
'iteration': iteration,
|
|
||||||
'optimizer': optimizer.state_dict(),
|
|
||||||
'learning_rate': learning_rate}, checkpoint_path)
|
|
||||||
|
|
||||||
def latest_checkpoint_path(dir_path, regex="G_*.pth"):
|
|
||||||
f_list = glob.glob(os.path.join(dir_path, regex))
|
|
||||||
f_list.sort(key=lambda f: int("".join(filter(str.isdigit, f))))
|
|
||||||
x = f_list[-1]
|
|
||||||
print(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
def run(rank, n_gpus, hps):
|
|
||||||
global global_step
|
|
||||||
if rank == 0:
|
|
||||||
logger.info(hps)
|
|
||||||
writer = SummaryWriter(log_dir=hps.model_dir)
|
|
||||||
writer_eval = SummaryWriter(log_dir=os.path.join(hps.model_dir, "eval"))
|
|
||||||
|
|
||||||
dist.init_process_group(backend='gloo', init_method='env://', world_size=n_gpus, rank=rank)
|
|
||||||
torch.manual_seed(hps.train.seed)
|
|
||||||
torch.cuda.set_device(rank)
|
|
||||||
train_dataset = VitsDataset(hps.data.training_files, hps.data)
|
|
||||||
train_sampler = DistributedBucketSampler(
|
|
||||||
train_dataset,
|
|
||||||
hps.train.batch_size,
|
|
||||||
[32, 300, 400, 500, 600, 700, 800, 900, 1000],
|
|
||||||
num_replicas=n_gpus,
|
|
||||||
rank=rank,
|
|
||||||
shuffle=True)
|
|
||||||
collate_fn = VitsDatasetCollate()
|
|
||||||
train_loader = DataLoader(train_dataset, num_workers=8, shuffle=False, pin_memory=True,
|
|
||||||
collate_fn=collate_fn, batch_sampler=train_sampler)
|
|
||||||
if rank == 0:
|
|
||||||
eval_dataset = VitsDataset(hps.data.validation_files, hps.data)
|
|
||||||
eval_loader = DataLoader(eval_dataset, num_workers=8, shuffle=False,
|
|
||||||
batch_size=hps.train.batch_size, pin_memory=True,
|
|
||||||
drop_last=False, collate_fn=collate_fn)
|
|
||||||
|
|
||||||
net_g = Vits(
|
|
||||||
len(symbols),
|
|
||||||
hps.data.filter_length // 2 + 1,
|
|
||||||
hps.train.segment_size // hps.data.hop_length,
|
|
||||||
n_speakers=hps.data.n_speakers,
|
|
||||||
**hps.model).cuda(rank)
|
|
||||||
net_d = MultiPeriodDiscriminator(hps.model.use_spectral_norm).cuda(rank)
|
|
||||||
optim_g = torch.optim.AdamW(
|
|
||||||
net_g.parameters(),
|
|
||||||
hps.train.learning_rate,
|
|
||||||
betas=hps.train.betas,
|
|
||||||
eps=hps.train.eps)
|
|
||||||
optim_d = torch.optim.AdamW(
|
|
||||||
net_d.parameters(),
|
|
||||||
hps.train.learning_rate,
|
|
||||||
betas=hps.train.betas,
|
|
||||||
eps=hps.train.eps)
|
|
||||||
net_g = DDP(net_g, device_ids=[rank])
|
|
||||||
net_d = DDP(net_d, device_ids=[rank])
|
|
||||||
ckptG = hps.ckptG
|
|
||||||
ckptD = hps.ckptD
|
|
||||||
try:
|
|
||||||
if ckptG is not None:
|
|
||||||
_, _, _, epoch_str = load_checkpoint(ckptG, net_g, optim_g, is_old=True)
|
|
||||||
print("加载原版VITS模型G记录点成功")
|
|
||||||
else:
|
|
||||||
_, _, _, epoch_str = load_checkpoint(latest_checkpoint_path(hps.model_dir, "G_*.pth"), net_g,
|
|
||||||
optim_g)
|
|
||||||
if ckptD is not None:
|
|
||||||
_, _, _, epoch_str = load_checkpoint(ckptG, net_g, optim_g, is_old=True)
|
|
||||||
print("加载原版VITS模型D记录点成功")
|
|
||||||
else:
|
|
||||||
_, _, _, epoch_str = load_checkpoint(latest_checkpoint_path(hps.model_dir, "D_*.pth"), net_d,
|
|
||||||
optim_d)
|
|
||||||
global_step = (epoch_str - 1) * len(train_loader)
|
|
||||||
except:
|
|
||||||
epoch_str = 1
|
|
||||||
global_step = 0
|
|
||||||
if ckptG is not None or ckptD is not None:
|
|
||||||
epoch_str = 1
|
|
||||||
global_step = 0
|
|
||||||
scheduler_g = torch.optim.lr_scheduler.ExponentialLR(optim_g, gamma=hps.train.lr_decay, last_epoch=epoch_str - 2)
|
|
||||||
scheduler_d = torch.optim.lr_scheduler.ExponentialLR(optim_d, gamma=hps.train.lr_decay, last_epoch=epoch_str - 2)
|
|
||||||
|
|
||||||
scaler = GradScaler(enabled=hps.train.fp16_run)
|
|
||||||
|
|
||||||
for epoch in range(epoch_str, hps.train.epochs + 1):
|
|
||||||
if rank == 0:
|
|
||||||
train_and_evaluate(rank, epoch, hps, [net_g, net_d], [optim_g, optim_d], [scheduler_g, scheduler_d], scaler,
|
|
||||||
[train_loader, eval_loader], logger, [writer, writer_eval])
|
|
||||||
else:
|
|
||||||
train_and_evaluate(rank, epoch, hps, [net_g, net_d], [optim_g, optim_d], [scheduler_g, scheduler_d], scaler,
|
|
||||||
[train_loader, None], None, None)
|
|
||||||
scheduler_g.step()
|
|
||||||
scheduler_d.step()
|
|
||||||
|
|
||||||
|
|
||||||
def train_and_evaluate(rank, epoch, hps, nets, optims, schedulers, scaler, loaders, logger, writers):
|
|
||||||
net_g, net_d = nets
|
|
||||||
optim_g, optim_d = optims
|
|
||||||
scheduler_g, scheduler_d = schedulers
|
|
||||||
train_loader, eval_loader = loaders
|
|
||||||
if writers is not None:
|
|
||||||
writer, writer_eval = writers
|
|
||||||
train_loader.batch_sampler.set_epoch(epoch)
|
|
||||||
global global_step
|
|
||||||
|
|
||||||
net_g.train()
|
|
||||||
net_d.train()
|
|
||||||
for batch_idx, (x, x_lengths, spec, spec_lengths, y, y_lengths, speakers, emo) in enumerate(train_loader):
|
|
||||||
logger.info(f'====> Step: 1 {batch_idx}')
|
|
||||||
x, x_lengths = x.cuda(rank, non_blocking=True), x_lengths.cuda(rank, non_blocking=True)
|
|
||||||
spec, spec_lengths = spec.cuda(rank, non_blocking=True), spec_lengths.cuda(rank, non_blocking=True)
|
|
||||||
y, y_lengths = y.cuda(rank, non_blocking=True), y_lengths.cuda(rank, non_blocking=True)
|
|
||||||
speakers = speakers.cuda(rank, non_blocking=True)
|
|
||||||
emo = emo.cuda(rank, non_blocking=True)
|
|
||||||
|
|
||||||
with autocast(enabled=hps.train.fp16_run):
|
|
||||||
y_hat, l_length, attn, ids_slice, x_mask, z_mask, \
|
|
||||||
(z, z_p, m_p, logs_p, m_q, logs_q) = net_g(x, x_lengths, spec, spec_lengths, speakers, emo)
|
|
||||||
|
|
||||||
mel = spec_to_mel(
|
|
||||||
spec,
|
|
||||||
hps.data.filter_length,
|
|
||||||
hps.data.n_mel_channels,
|
|
||||||
hps.data.sampling_rate,
|
|
||||||
hps.data.mel_fmin,
|
|
||||||
hps.data.mel_fmax)
|
|
||||||
y_mel = slice_segments(mel, ids_slice, hps.train.segment_size // hps.data.hop_length)
|
|
||||||
y_hat_mel = mel_spectrogram(
|
|
||||||
y_hat.squeeze(1),
|
|
||||||
hps.data.filter_length,
|
|
||||||
hps.data.n_mel_channels,
|
|
||||||
hps.data.sampling_rate,
|
|
||||||
hps.data.hop_length,
|
|
||||||
hps.data.win_length,
|
|
||||||
hps.data.mel_fmin,
|
|
||||||
hps.data.mel_fmax
|
|
||||||
)
|
|
||||||
|
|
||||||
y = slice_segments(y, ids_slice * hps.data.hop_length, hps.train.segment_size) # slice
|
|
||||||
|
|
||||||
# Discriminator
|
|
||||||
y_d_hat_r, y_d_hat_g, _, _ = net_d(y, y_hat.detach())
|
|
||||||
with autocast(enabled=False):
|
|
||||||
loss_disc, losses_disc_r, losses_disc_g = discriminator_loss(y_d_hat_r, y_d_hat_g)
|
|
||||||
loss_disc_all = loss_disc
|
|
||||||
optim_d.zero_grad()
|
|
||||||
scaler.scale(loss_disc_all).backward()
|
|
||||||
scaler.unscale_(optim_d)
|
|
||||||
grad_norm_d = clip_grad_value_(net_d.parameters(), None)
|
|
||||||
scaler.step(optim_d)
|
|
||||||
logger.info(f'====> Step: 2 {batch_idx}')
|
|
||||||
|
|
||||||
with autocast(enabled=hps.train.fp16_run):
|
|
||||||
# Generator
|
|
||||||
y_d_hat_r, y_d_hat_g, fmap_r, fmap_g = net_d(y, y_hat)
|
|
||||||
with autocast(enabled=False):
|
|
||||||
loss_dur = torch.sum(l_length.float())
|
|
||||||
loss_mel = F.l1_loss(y_mel, y_hat_mel) * hps.train.c_mel
|
|
||||||
loss_kl = kl_loss(z_p, logs_q, m_p, logs_p, z_mask) * hps.train.c_kl
|
|
||||||
|
|
||||||
loss_fm = feature_loss(fmap_r, fmap_g)
|
|
||||||
loss_gen, losses_gen = generator_loss(y_d_hat_g)
|
|
||||||
loss_gen_all = loss_gen + loss_fm + loss_mel + loss_dur + loss_kl
|
|
||||||
optim_g.zero_grad()
|
|
||||||
scaler.scale(loss_gen_all.float()).backward()
|
|
||||||
scaler.unscale_(optim_g)
|
|
||||||
grad_norm_g = clip_grad_value_(net_g.parameters(), None)
|
|
||||||
scaler.step(optim_g)
|
|
||||||
scaler.update()
|
|
||||||
# logger.info(f'====> Step: 3 {batch_idx}')
|
|
||||||
if rank == 0:
|
|
||||||
if global_step % hps.train.log_interval == 0:
|
|
||||||
lr = optim_g.param_groups[0]['lr']
|
|
||||||
losses = [loss_disc, loss_gen, loss_fm, loss_mel, loss_dur, loss_kl]
|
|
||||||
logger.info('Train Epoch: {} [{:.0f}%]'.format(
|
|
||||||
epoch,
|
|
||||||
100. * batch_idx / len(train_loader)))
|
|
||||||
logger.info([x.item() for x in losses] + [global_step, lr])
|
|
||||||
|
|
||||||
scalar_dict = {"loss/g/total": loss_gen_all, "loss/d/total": loss_disc_all, "learning_rate": lr,
|
|
||||||
"grad_norm_d": grad_norm_d, "grad_norm_g": grad_norm_g}
|
|
||||||
scalar_dict.update(
|
|
||||||
{"loss/g/fm": loss_fm, "loss/g/mel": loss_mel, "loss/g/dur": loss_dur, "loss/g/kl": loss_kl})
|
|
||||||
|
|
||||||
scalar_dict.update({"loss/g/{}".format(i): v for i, v in enumerate(losses_gen)})
|
|
||||||
scalar_dict.update({"loss/d_r/{}".format(i): v for i, v in enumerate(losses_disc_r)})
|
|
||||||
scalar_dict.update({"loss/d_g/{}".format(i): v for i, v in enumerate(losses_disc_g)})
|
|
||||||
image_dict = {
|
|
||||||
"slice/mel_org": plot_spectrogram_to_numpy(y_mel[0].data.cpu().numpy()),
|
|
||||||
"slice/mel_gen": plot_spectrogram_to_numpy(y_hat_mel[0].data.cpu().numpy()),
|
|
||||||
"all/mel": plot_spectrogram_to_numpy(mel[0].data.cpu().numpy()),
|
|
||||||
"all/attn": plot_alignment_to_numpy(attn[0, 0].data.cpu().numpy())
|
|
||||||
}
|
|
||||||
summarize(
|
|
||||||
writer=writer,
|
|
||||||
global_step=global_step,
|
|
||||||
images=image_dict,
|
|
||||||
scalars=scalar_dict)
|
|
||||||
|
|
||||||
if global_step % hps.train.eval_interval == 0:
|
|
||||||
evaluate(hps, net_g, eval_loader, writer_eval)
|
|
||||||
save_checkpoint(net_g, optim_g, hps.train.learning_rate, epoch,
|
|
||||||
os.path.join(hps.model_dir, "G_{}.pth".format(global_step)))
|
|
||||||
save_checkpoint(net_d, optim_d, hps.train.learning_rate, epoch,
|
|
||||||
os.path.join(hps.model_dir, "D_{}.pth".format(global_step)))
|
|
||||||
global_step += 1
|
|
||||||
|
|
||||||
if rank == 0:
|
|
||||||
logger.info('====> Epoch: {}'.format(epoch))
|
|
||||||
|
|
||||||
|
|
||||||
def evaluate(hps, generator, eval_loader, writer_eval):
|
|
||||||
generator.eval()
|
|
||||||
with torch.no_grad():
|
|
||||||
for batch_idx, (x, x_lengths, spec, spec_lengths, y, y_lengths, speakers, emo) in enumerate(eval_loader):
|
|
||||||
x, x_lengths = x.cuda(0), x_lengths.cuda(0)
|
|
||||||
spec, spec_lengths = spec.cuda(0), spec_lengths.cuda(0)
|
|
||||||
y, y_lengths = y.cuda(0), y_lengths.cuda(0)
|
|
||||||
speakers = speakers.cuda(0)
|
|
||||||
emo = emo.cuda(0)
|
|
||||||
# remove else
|
|
||||||
x = x[:1]
|
|
||||||
x_lengths = x_lengths[:1]
|
|
||||||
spec = spec[:1]
|
|
||||||
spec_lengths = spec_lengths[:1]
|
|
||||||
y = y[:1]
|
|
||||||
y_lengths = y_lengths[:1]
|
|
||||||
speakers = speakers[:1]
|
|
||||||
emo = emo[:1]
|
|
||||||
break
|
|
||||||
y_hat, attn, mask, *_ = generator.module.infer(x, x_lengths, speakers, emo, max_len=1000)
|
|
||||||
y_hat_lengths = mask.sum([1, 2]).long() * hps.data.hop_length
|
|
||||||
|
|
||||||
mel = spec_to_mel(
|
|
||||||
spec,
|
|
||||||
hps.data.filter_length,
|
|
||||||
hps.data.n_mel_channels,
|
|
||||||
hps.data.sampling_rate,
|
|
||||||
hps.data.mel_fmin,
|
|
||||||
hps.data.mel_fmax)
|
|
||||||
y_hat_mel = mel_spectrogram(
|
|
||||||
y_hat.squeeze(1).float(),
|
|
||||||
hps.data.filter_length,
|
|
||||||
hps.data.n_mel_channels,
|
|
||||||
hps.data.sampling_rate,
|
|
||||||
hps.data.hop_length,
|
|
||||||
hps.data.win_length,
|
|
||||||
hps.data.mel_fmin,
|
|
||||||
hps.data.mel_fmax
|
|
||||||
)
|
|
||||||
image_dict = {
|
|
||||||
"gen/mel": plot_spectrogram_to_numpy(y_hat_mel[0].cpu().numpy())
|
|
||||||
}
|
|
||||||
audio_dict = {
|
|
||||||
"gen/audio": y_hat[0, :, :y_hat_lengths[0]]
|
|
||||||
}
|
|
||||||
if global_step == 0:
|
|
||||||
image_dict.update({"gt/mel": plot_spectrogram_to_numpy(mel[0].cpu().numpy())})
|
|
||||||
audio_dict.update({"gt/audio": y[0, :, :y_lengths[0]]})
|
|
||||||
|
|
||||||
summarize(
|
|
||||||
writer=writer_eval,
|
|
||||||
global_step=global_step,
|
|
||||||
images=image_dict,
|
|
||||||
audios=audio_dict,
|
|
||||||
audio_sampling_rate=hps.data.sampling_rate
|
|
||||||
)
|
|
||||||
generator.train()
|
|
||||||
|
|
||||||
def summarize(writer, global_step, scalars={}, histograms={}, images={}, audios={}, audio_sampling_rate=22050):
|
|
||||||
for k, v in scalars.items():
|
|
||||||
writer.add_scalar(k, v, global_step)
|
|
||||||
for k, v in histograms.items():
|
|
||||||
writer.add_histogram(k, v, global_step)
|
|
||||||
for k, v in images.items():
|
|
||||||
writer.add_image(k, v, global_step, dataformats='HWC')
|
|
||||||
for k, v in audios.items():
|
|
||||||
writer.add_audio(k, v, global_step, audio_sampling_rate)
|
|
||||||
|
|
||||||
@@ -1,280 +0,0 @@
|
|||||||
import os
|
|
||||||
import random
|
|
||||||
import numpy as np
|
|
||||||
import torch
|
|
||||||
import torch.utils.data
|
|
||||||
|
|
||||||
from utils.audio_utils import spectrogram, load_wav
|
|
||||||
from utils.util import intersperse
|
|
||||||
from models.synthesizer.utils.text import text_to_sequence
|
|
||||||
|
|
||||||
|
|
||||||
"""Multi speaker version"""
|
|
||||||
class VitsDataset(torch.utils.data.Dataset):
|
|
||||||
"""
|
|
||||||
1) loads audio, speaker_id, text pairs
|
|
||||||
2) normalizes text and converts them to sequences of integers
|
|
||||||
3) computes spectrograms from audio files.
|
|
||||||
"""
|
|
||||||
def __init__(self, audio_file_path, hparams):
|
|
||||||
with open(audio_file_path, encoding='utf-8') as f:
|
|
||||||
self.audio_metadata = [line.strip().split('|') for line in f]
|
|
||||||
self.text_cleaners = hparams.text_cleaners
|
|
||||||
self.max_wav_value = hparams.max_wav_value
|
|
||||||
self.sampling_rate = hparams.sampling_rate
|
|
||||||
self.filter_length = hparams.filter_length
|
|
||||||
self.hop_length = hparams.hop_length
|
|
||||||
self.win_length = hparams.win_length
|
|
||||||
self.sampling_rate = hparams.sampling_rate
|
|
||||||
|
|
||||||
self.cleaned_text = getattr(hparams, "cleaned_text", False)
|
|
||||||
|
|
||||||
self.add_blank = hparams.add_blank
|
|
||||||
self.datasets_root = hparams.datasets_root
|
|
||||||
|
|
||||||
self.min_text_len = getattr(hparams, "min_text_len", 1)
|
|
||||||
self.max_text_len = getattr(hparams, "max_text_len", 190)
|
|
||||||
|
|
||||||
random.seed(1234)
|
|
||||||
random.shuffle(self.audio_metadata)
|
|
||||||
self._filter()
|
|
||||||
|
|
||||||
def _filter(self):
|
|
||||||
"""
|
|
||||||
Filter text & store spec lengths
|
|
||||||
"""
|
|
||||||
# Store spectrogram lengths for Bucketing
|
|
||||||
# wav_length ~= file_size / (wav_channels * Bytes per dim) = file_size / (1 * 2)
|
|
||||||
# spec_length = wav_length // hop_length
|
|
||||||
|
|
||||||
audio_metadata_new = []
|
|
||||||
lengths = []
|
|
||||||
|
|
||||||
# for audiopath, sid, text in self.audio_metadata:
|
|
||||||
sid = 0
|
|
||||||
spk_to_sid = {}
|
|
||||||
for wav_fpath, mel_fpath, embed_path, wav_length, mel_frames, text in self.audio_metadata:
|
|
||||||
if self.min_text_len <= len(text) and len(text) <= self.max_text_len:
|
|
||||||
# TODO: for magic data only
|
|
||||||
speaker_name = wav_fpath.split("_")[1]
|
|
||||||
if speaker_name not in spk_to_sid:
|
|
||||||
sid += 1
|
|
||||||
spk_to_sid[speaker_name] = sid
|
|
||||||
|
|
||||||
audio_metadata_new.append([wav_fpath, mel_fpath, embed_path, wav_length, mel_frames, text, spk_to_sid[speaker_name]])
|
|
||||||
lengths.append(os.path.getsize(f'{self.datasets_root}{os.sep}audio{os.sep}{wav_fpath}') // (2 * self.hop_length))
|
|
||||||
print("found sid:%d", sid)
|
|
||||||
self.audio_metadata = audio_metadata_new
|
|
||||||
self.lengths = lengths
|
|
||||||
|
|
||||||
def get_audio_text_speaker_pair(self, audio_metadata):
|
|
||||||
# separate filename, speaker_id and text
|
|
||||||
wav_fpath, text, sid = audio_metadata[0], audio_metadata[5], audio_metadata[6]
|
|
||||||
text = self.get_text(text)
|
|
||||||
|
|
||||||
spec, wav = self.get_audio(f'{self.datasets_root}{os.sep}audio{os.sep}{wav_fpath}')
|
|
||||||
sid = self.get_sid(sid)
|
|
||||||
emo = torch.FloatTensor(np.load(f'{self.datasets_root}{os.sep}emo{os.sep}{wav_fpath.replace("audio", "emo")}'))
|
|
||||||
return (text, spec, wav, sid, emo)
|
|
||||||
|
|
||||||
def get_audio(self, filename):
|
|
||||||
# audio, sampling_rate = load_wav(filename)
|
|
||||||
|
|
||||||
# if sampling_rate != self.sampling_rate:
|
|
||||||
# raise ValueError("{} {} SR doesn't match target {} SR".format(
|
|
||||||
# sampling_rate, self.sampling_rate))
|
|
||||||
# audio = torch.load(filename)
|
|
||||||
audio = torch.FloatTensor(np.load(filename).astype(np.float32))
|
|
||||||
audio = audio.unsqueeze(0)
|
|
||||||
# audio_norm = audio / self.max_wav_value
|
|
||||||
# audio_norm = audio_norm.unsqueeze(0)
|
|
||||||
# spec_filename = filename.replace(".wav", ".spec.pt")
|
|
||||||
# if os.path.exists(spec_filename):
|
|
||||||
# spec = torch.load(spec_filename)
|
|
||||||
# else:
|
|
||||||
# spec = spectrogram(audio, self.filter_length,
|
|
||||||
# self.sampling_rate, self.hop_length, self.win_length,
|
|
||||||
# center=False)
|
|
||||||
# spec = torch.squeeze(spec, 0)
|
|
||||||
# torch.save(spec, spec_filename)
|
|
||||||
spec = spectrogram(audio, self.filter_length, self.hop_length, self.win_length,
|
|
||||||
center=False)
|
|
||||||
spec = torch.squeeze(spec, 0)
|
|
||||||
return spec, audio
|
|
||||||
|
|
||||||
def get_text(self, text):
|
|
||||||
if self.cleaned_text:
|
|
||||||
text_norm = text_to_sequence(text, self.text_cleaners)
|
|
||||||
if self.add_blank:
|
|
||||||
text_norm = intersperse(text_norm, 0)
|
|
||||||
text_norm = torch.LongTensor(text_norm)
|
|
||||||
return text_norm
|
|
||||||
|
|
||||||
def get_sid(self, sid):
|
|
||||||
sid = torch.LongTensor([int(sid)])
|
|
||||||
return sid
|
|
||||||
|
|
||||||
def __getitem__(self, index):
|
|
||||||
return self.get_audio_text_speaker_pair(self.audio_metadata[index])
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self.audio_metadata)
|
|
||||||
|
|
||||||
|
|
||||||
class VitsDatasetCollate():
|
|
||||||
""" Zero-pads model inputs and targets
|
|
||||||
"""
|
|
||||||
def __init__(self, return_ids=False):
|
|
||||||
self.return_ids = return_ids
|
|
||||||
|
|
||||||
def __call__(self, batch):
|
|
||||||
"""Collate's training batch from normalized text, audio and speaker identities
|
|
||||||
PARAMS
|
|
||||||
------
|
|
||||||
batch: [text_normalized, spec_normalized, wav_normalized, sid]
|
|
||||||
"""
|
|
||||||
# Right zero-pad all one-hot text sequences to max input length
|
|
||||||
_, ids_sorted_decreasing = torch.sort(
|
|
||||||
torch.LongTensor([x[1].size(1) for x in batch]),
|
|
||||||
dim=0, descending=True)
|
|
||||||
|
|
||||||
max_text_len = max([len(x[0]) for x in batch])
|
|
||||||
max_spec_len = max([x[1].size(1) for x in batch])
|
|
||||||
max_wav_len = max([x[2].size(1) for x in batch])
|
|
||||||
|
|
||||||
text_lengths = torch.LongTensor(len(batch))
|
|
||||||
spec_lengths = torch.LongTensor(len(batch))
|
|
||||||
wav_lengths = torch.LongTensor(len(batch))
|
|
||||||
sid = torch.LongTensor(len(batch))
|
|
||||||
|
|
||||||
text_padded = torch.LongTensor(len(batch), max_text_len)
|
|
||||||
spec_padded = torch.FloatTensor(len(batch), batch[0][1].size(0), max_spec_len)
|
|
||||||
wav_padded = torch.FloatTensor(len(batch), 1, max_wav_len)
|
|
||||||
emo = torch.FloatTensor(len(batch), 1024)
|
|
||||||
|
|
||||||
text_padded.zero_()
|
|
||||||
spec_padded.zero_()
|
|
||||||
wav_padded.zero_()
|
|
||||||
emo.zero_()
|
|
||||||
|
|
||||||
for i in range(len(ids_sorted_decreasing)):
|
|
||||||
row = batch[ids_sorted_decreasing[i]]
|
|
||||||
|
|
||||||
text = row[0]
|
|
||||||
text_padded[i, :text.size(0)] = text
|
|
||||||
text_lengths[i] = text.size(0)
|
|
||||||
|
|
||||||
spec = row[1]
|
|
||||||
spec_padded[i, :, :spec.size(1)] = spec
|
|
||||||
spec_lengths[i] = spec.size(1)
|
|
||||||
|
|
||||||
wav = row[2]
|
|
||||||
wav_padded[i, :, :wav.size(1)] = wav
|
|
||||||
wav_lengths[i] = wav.size(1)
|
|
||||||
|
|
||||||
sid[i] = row[3]
|
|
||||||
|
|
||||||
emo[i, :] = row[4]
|
|
||||||
|
|
||||||
if self.return_ids:
|
|
||||||
return text_padded, text_lengths, spec_padded, spec_lengths, wav_padded, wav_lengths, sid, ids_sorted_decreasing
|
|
||||||
return text_padded, text_lengths, spec_padded, spec_lengths, wav_padded, wav_lengths, sid, emo
|
|
||||||
|
|
||||||
|
|
||||||
class DistributedBucketSampler(torch.utils.data.distributed.DistributedSampler):
|
|
||||||
"""
|
|
||||||
Maintain similar input lengths in a batch.
|
|
||||||
Length groups are specified by boundaries.
|
|
||||||
Ex) boundaries = [b1, b2, b3] -> any batch is included either {x | b1 < length(x) <=b2} or {x | b2 < length(x) <= b3}.
|
|
||||||
|
|
||||||
It removes samples which are not included in the boundaries.
|
|
||||||
Ex) boundaries = [b1, b2, b3] -> any x s.t. length(x) <= b1 or length(x) > b3 are discarded.
|
|
||||||
"""
|
|
||||||
def __init__(self, dataset, batch_size, boundaries, num_replicas=None, rank=None, shuffle=True):
|
|
||||||
super().__init__(dataset, num_replicas=num_replicas, rank=rank, shuffle=shuffle)
|
|
||||||
self.lengths = dataset.lengths
|
|
||||||
self.batch_size = batch_size
|
|
||||||
self.boundaries = boundaries
|
|
||||||
|
|
||||||
self.buckets, self.num_samples_per_bucket = self._create_buckets()
|
|
||||||
self.total_size = sum(self.num_samples_per_bucket)
|
|
||||||
self.num_samples = self.total_size // self.num_replicas
|
|
||||||
|
|
||||||
def _create_buckets(self):
|
|
||||||
buckets = [[] for _ in range(len(self.boundaries) - 1)]
|
|
||||||
for i in range(len(self.lengths)):
|
|
||||||
length = self.lengths[i]
|
|
||||||
idx_bucket = self._bisect(length)
|
|
||||||
if idx_bucket != -1:
|
|
||||||
buckets[idx_bucket].append(i)
|
|
||||||
|
|
||||||
for i in range(len(buckets) - 1, 0, -1):
|
|
||||||
if len(buckets[i]) == 0:
|
|
||||||
buckets.pop(i)
|
|
||||||
self.boundaries.pop(i+1)
|
|
||||||
|
|
||||||
num_samples_per_bucket = []
|
|
||||||
for i in range(len(buckets)):
|
|
||||||
len_bucket = len(buckets[i])
|
|
||||||
total_batch_size = self.num_replicas * self.batch_size
|
|
||||||
rem = (total_batch_size - (len_bucket % total_batch_size)) % total_batch_size
|
|
||||||
num_samples_per_bucket.append(len_bucket + rem)
|
|
||||||
return buckets, num_samples_per_bucket
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
# deterministically shuffle based on epoch
|
|
||||||
g = torch.Generator()
|
|
||||||
g.manual_seed(self.epoch)
|
|
||||||
|
|
||||||
indices = []
|
|
||||||
if self.shuffle:
|
|
||||||
for bucket in self.buckets:
|
|
||||||
indices.append(torch.randperm(len(bucket), generator=g).tolist())
|
|
||||||
else:
|
|
||||||
for bucket in self.buckets:
|
|
||||||
indices.append(list(range(len(bucket))))
|
|
||||||
|
|
||||||
batches = []
|
|
||||||
for i in range(len(self.buckets)):
|
|
||||||
bucket = self.buckets[i]
|
|
||||||
len_bucket = len(bucket)
|
|
||||||
ids_bucket = indices[i]
|
|
||||||
num_samples_bucket = self.num_samples_per_bucket[i]
|
|
||||||
|
|
||||||
# add extra samples to make it evenly divisible
|
|
||||||
rem = num_samples_bucket - len_bucket
|
|
||||||
ids_bucket = ids_bucket + ids_bucket * (rem // len_bucket) + ids_bucket[:(rem % len_bucket)]
|
|
||||||
|
|
||||||
# subsample
|
|
||||||
ids_bucket = ids_bucket[self.rank::self.num_replicas]
|
|
||||||
|
|
||||||
# batching
|
|
||||||
for j in range(len(ids_bucket) // self.batch_size):
|
|
||||||
batch = [bucket[idx] for idx in ids_bucket[j*self.batch_size:(j+1)*self.batch_size]]
|
|
||||||
batches.append(batch)
|
|
||||||
|
|
||||||
if self.shuffle:
|
|
||||||
batch_ids = torch.randperm(len(batches), generator=g).tolist()
|
|
||||||
batches = [batches[i] for i in batch_ids]
|
|
||||||
self.batches = batches
|
|
||||||
|
|
||||||
assert len(self.batches) * self.batch_size == self.num_samples
|
|
||||||
return iter(self.batches)
|
|
||||||
|
|
||||||
def _bisect(self, x, lo=0, hi=None):
|
|
||||||
if hi is None:
|
|
||||||
hi = len(self.boundaries) - 1
|
|
||||||
|
|
||||||
if hi > lo:
|
|
||||||
mid = (hi + lo) // 2
|
|
||||||
if self.boundaries[mid] < x and x <= self.boundaries[mid+1]:
|
|
||||||
return mid
|
|
||||||
elif x <= self.boundaries[mid]:
|
|
||||||
return self._bisect(x, lo, mid)
|
|
||||||
else:
|
|
||||||
return self._bisect(x, mid + 1, hi)
|
|
||||||
else:
|
|
||||||
return -1
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return self.num_samples // self.batch_size
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
#
|
|
||||||
129
models/vocoder/fregan/.gitignore
vendored
129
models/vocoder/fregan/.gitignore
vendored
@@ -1,129 +0,0 @@
|
|||||||
# Byte-compiled / optimized / DLL files
|
|
||||||
__pycache__/
|
|
||||||
*.py[cod]
|
|
||||||
*$py.class
|
|
||||||
|
|
||||||
# C extensions
|
|
||||||
*.so
|
|
||||||
|
|
||||||
# Distribution / packaging
|
|
||||||
.Python
|
|
||||||
build/
|
|
||||||
develop-eggs/
|
|
||||||
dist/
|
|
||||||
downloads/
|
|
||||||
eggs/
|
|
||||||
.eggs/
|
|
||||||
lib/
|
|
||||||
lib64/
|
|
||||||
parts/
|
|
||||||
sdist/
|
|
||||||
var/
|
|
||||||
wheels/
|
|
||||||
pip-wheel-metadata/
|
|
||||||
share/python-wheels/
|
|
||||||
*.egg-info/
|
|
||||||
.installed.cfg
|
|
||||||
*.egg
|
|
||||||
MANIFEST
|
|
||||||
|
|
||||||
# PyInstaller
|
|
||||||
# Usually these files are written by a python script from a template
|
|
||||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
||||||
*.manifest
|
|
||||||
*.spec
|
|
||||||
|
|
||||||
# Installer logs
|
|
||||||
pip-log.txt
|
|
||||||
pip-delete-this-directory.txt
|
|
||||||
|
|
||||||
# Unit test / coverage reports
|
|
||||||
htmlcov/
|
|
||||||
.tox/
|
|
||||||
.nox/
|
|
||||||
.coverage
|
|
||||||
.coverage.*
|
|
||||||
.cache
|
|
||||||
nosetests.xml
|
|
||||||
coverage.xml
|
|
||||||
*.cover
|
|
||||||
*.py,cover
|
|
||||||
.hypothesis/
|
|
||||||
.pytest_cache/
|
|
||||||
|
|
||||||
# Translations
|
|
||||||
*.mo
|
|
||||||
*.pot
|
|
||||||
|
|
||||||
# Django stuff:
|
|
||||||
*.log
|
|
||||||
local_settings.py
|
|
||||||
db.sqlite3
|
|
||||||
db.sqlite3-journal
|
|
||||||
|
|
||||||
# Flask stuff:
|
|
||||||
instance/
|
|
||||||
.webassets-cache
|
|
||||||
|
|
||||||
# Scrapy stuff:
|
|
||||||
.scrapy
|
|
||||||
|
|
||||||
# Sphinx documentation
|
|
||||||
docs/_build/
|
|
||||||
|
|
||||||
# PyBuilder
|
|
||||||
target/
|
|
||||||
|
|
||||||
# Jupyter Notebook
|
|
||||||
.ipynb_checkpoints
|
|
||||||
|
|
||||||
# IPython
|
|
||||||
profile_default/
|
|
||||||
ipython_config.py
|
|
||||||
|
|
||||||
# pyenv
|
|
||||||
.python-version
|
|
||||||
|
|
||||||
# pipenv
|
|
||||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
||||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
||||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
||||||
# install all needed dependencies.
|
|
||||||
#Pipfile.lock
|
|
||||||
|
|
||||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
||||||
__pypackages__/
|
|
||||||
|
|
||||||
# Celery stuff
|
|
||||||
celerybeat-schedule
|
|
||||||
celerybeat.pid
|
|
||||||
|
|
||||||
# SageMath parsed files
|
|
||||||
*.sage.py
|
|
||||||
|
|
||||||
# Environments
|
|
||||||
.env
|
|
||||||
.venv
|
|
||||||
env/
|
|
||||||
venv/
|
|
||||||
ENV/
|
|
||||||
env.bak/
|
|
||||||
venv.bak/
|
|
||||||
|
|
||||||
# Spyder project settings
|
|
||||||
.spyderproject
|
|
||||||
.spyproject
|
|
||||||
|
|
||||||
# Rope project settings
|
|
||||||
.ropeproject
|
|
||||||
|
|
||||||
# mkdocs documentation
|
|
||||||
/site
|
|
||||||
|
|
||||||
# mypy
|
|
||||||
.mypy_cache/
|
|
||||||
.dmypy.json
|
|
||||||
dmypy.json
|
|
||||||
|
|
||||||
# Pyre type checker
|
|
||||||
.pyre/
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2021 Rishikesh (ऋषिकेश)
|
|
||||||
|
|
||||||
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.
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
#
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
{
|
|
||||||
"resblock": "1",
|
|
||||||
"num_gpus": 0,
|
|
||||||
"batch_size": 16,
|
|
||||||
"learning_rate": 0.0002,
|
|
||||||
"adam_b1": 0.8,
|
|
||||||
"adam_b2": 0.99,
|
|
||||||
"lr_decay": 0.999,
|
|
||||||
"seed": 1234,
|
|
||||||
"disc_start_step":0,
|
|
||||||
|
|
||||||
|
|
||||||
"upsample_rates": [5,5,2,2,2],
|
|
||||||
"upsample_kernel_sizes": [10,10,4,4,4],
|
|
||||||
"upsample_initial_channel": 512,
|
|
||||||
"resblock_kernel_sizes": [3,7,11],
|
|
||||||
"resblock_dilation_sizes": [[1, 3, 5, 7], [1,3,5,7], [1,3,5,7]],
|
|
||||||
|
|
||||||
"segment_size": 6400,
|
|
||||||
"num_mels": 80,
|
|
||||||
"num_freq": 1025,
|
|
||||||
"n_fft": 1024,
|
|
||||||
"hop_size": 200,
|
|
||||||
"win_size": 800,
|
|
||||||
|
|
||||||
"sampling_rate": 16000,
|
|
||||||
|
|
||||||
"fmin": 0,
|
|
||||||
"fmax": 7600,
|
|
||||||
"fmax_for_loss": null,
|
|
||||||
|
|
||||||
"num_workers": 4,
|
|
||||||
|
|
||||||
"dist_config": {
|
|
||||||
"dist_backend": "nccl",
|
|
||||||
"dist_url": "tcp://localhost:54321",
|
|
||||||
"world_size": 1
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,303 +0,0 @@
|
|||||||
import torch
|
|
||||||
import torch.nn.functional as F
|
|
||||||
import torch.nn as nn
|
|
||||||
from torch.nn import Conv1d, AvgPool1d, Conv2d
|
|
||||||
from torch.nn.utils import weight_norm, spectral_norm
|
|
||||||
from models.vocoder.fregan.utils import get_padding
|
|
||||||
from models.vocoder.fregan.stft_loss import stft
|
|
||||||
from models.vocoder.fregan.dwt import DWT_1D
|
|
||||||
LRELU_SLOPE = 0.1
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SpecDiscriminator(nn.Module):
|
|
||||||
"""docstring for Discriminator."""
|
|
||||||
|
|
||||||
def __init__(self, fft_size=1024, shift_size=120, win_length=600, window="hann_window", use_spectral_norm=False):
|
|
||||||
super(SpecDiscriminator, self).__init__()
|
|
||||||
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
|
||||||
self.fft_size = fft_size
|
|
||||||
self.shift_size = shift_size
|
|
||||||
self.win_length = win_length
|
|
||||||
self.window = getattr(torch, window)(win_length)
|
|
||||||
self.discriminators = nn.ModuleList([
|
|
||||||
norm_f(nn.Conv2d(1, 32, kernel_size=(3, 9), padding=(1, 4))),
|
|
||||||
norm_f(nn.Conv2d(32, 32, kernel_size=(3, 9), stride=(1,2), padding=(1, 4))),
|
|
||||||
norm_f(nn.Conv2d(32, 32, kernel_size=(3, 9), stride=(1,2), padding=(1, 4))),
|
|
||||||
norm_f(nn.Conv2d(32, 32, kernel_size=(3, 9), stride=(1,2), padding=(1, 4))),
|
|
||||||
norm_f(nn.Conv2d(32, 32, kernel_size=(3, 3), stride=(1,1), padding=(1, 1))),
|
|
||||||
])
|
|
||||||
|
|
||||||
self.out = norm_f(nn.Conv2d(32, 1, 3, 1, 1))
|
|
||||||
|
|
||||||
def forward(self, y):
|
|
||||||
|
|
||||||
fmap = []
|
|
||||||
with torch.no_grad():
|
|
||||||
y = y.squeeze(1)
|
|
||||||
y = stft(y, self.fft_size, self.shift_size, self.win_length, self.window.to(y.get_device()))
|
|
||||||
y = y.unsqueeze(1)
|
|
||||||
for i, d in enumerate(self.discriminators):
|
|
||||||
y = d(y)
|
|
||||||
y = F.leaky_relu(y, LRELU_SLOPE)
|
|
||||||
fmap.append(y)
|
|
||||||
|
|
||||||
y = self.out(y)
|
|
||||||
fmap.append(y)
|
|
||||||
|
|
||||||
return torch.flatten(y, 1, -1), fmap
|
|
||||||
|
|
||||||
class MultiResSpecDiscriminator(torch.nn.Module):
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
fft_sizes=[1024, 2048, 512],
|
|
||||||
hop_sizes=[120, 240, 50],
|
|
||||||
win_lengths=[600, 1200, 240],
|
|
||||||
window="hann_window"):
|
|
||||||
|
|
||||||
super(MultiResSpecDiscriminator, self).__init__()
|
|
||||||
self.discriminators = nn.ModuleList([
|
|
||||||
SpecDiscriminator(fft_sizes[0], hop_sizes[0], win_lengths[0], window),
|
|
||||||
SpecDiscriminator(fft_sizes[1], hop_sizes[1], win_lengths[1], window),
|
|
||||||
SpecDiscriminator(fft_sizes[2], hop_sizes[2], win_lengths[2], window)
|
|
||||||
])
|
|
||||||
|
|
||||||
def forward(self, y, y_hat):
|
|
||||||
y_d_rs = []
|
|
||||||
y_d_gs = []
|
|
||||||
fmap_rs = []
|
|
||||||
fmap_gs = []
|
|
||||||
for i, d in enumerate(self.discriminators):
|
|
||||||
y_d_r, fmap_r = d(y)
|
|
||||||
y_d_g, fmap_g = d(y_hat)
|
|
||||||
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 DiscriminatorP(torch.nn.Module):
|
|
||||||
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
|
||||||
super(DiscriminatorP, self).__init__()
|
|
||||||
self.period = period
|
|
||||||
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
|
||||||
self.dwt1d = DWT_1D()
|
|
||||||
self.dwt_conv1 = norm_f(Conv1d(2, 1, 1))
|
|
||||||
self.dwt_proj1 = norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0)))
|
|
||||||
self.dwt_conv2 = norm_f(Conv1d(4, 1, 1))
|
|
||||||
self.dwt_proj2 = norm_f(Conv2d(1, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0)))
|
|
||||||
self.dwt_conv3 = norm_f(Conv1d(8, 1, 1))
|
|
||||||
self.dwt_proj3 = norm_f(Conv2d(1, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0)))
|
|
||||||
self.convs = nn.ModuleList([
|
|
||||||
norm_f(Conv2d(1, 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):
|
|
||||||
fmap = []
|
|
||||||
|
|
||||||
# DWT 1
|
|
||||||
x_d1_high1, x_d1_low1 = self.dwt1d(x)
|
|
||||||
x_d1 = self.dwt_conv1(torch.cat([x_d1_high1, x_d1_low1], dim=1))
|
|
||||||
# 1d to 2d
|
|
||||||
b, c, t = x_d1.shape
|
|
||||||
if t % self.period != 0: # pad first
|
|
||||||
n_pad = self.period - (t % self.period)
|
|
||||||
x_d1 = F.pad(x_d1, (0, n_pad), "reflect")
|
|
||||||
t = t + n_pad
|
|
||||||
x_d1 = x_d1.view(b, c, t // self.period, self.period)
|
|
||||||
|
|
||||||
x_d1 = self.dwt_proj1(x_d1)
|
|
||||||
|
|
||||||
# DWT 2
|
|
||||||
x_d2_high1, x_d2_low1 = self.dwt1d(x_d1_high1)
|
|
||||||
x_d2_high2, x_d2_low2 = self.dwt1d(x_d1_low1)
|
|
||||||
x_d2 = self.dwt_conv2(torch.cat([x_d2_high1, x_d2_low1, x_d2_high2, x_d2_low2], dim=1))
|
|
||||||
# 1d to 2d
|
|
||||||
b, c, t = x_d2.shape
|
|
||||||
if t % self.period != 0: # pad first
|
|
||||||
n_pad = self.period - (t % self.period)
|
|
||||||
x_d2 = F.pad(x_d2, (0, n_pad), "reflect")
|
|
||||||
t = t + n_pad
|
|
||||||
x_d2 = x_d2.view(b, c, t // self.period, self.period)
|
|
||||||
|
|
||||||
x_d2 = self.dwt_proj2(x_d2)
|
|
||||||
|
|
||||||
# DWT 3
|
|
||||||
|
|
||||||
x_d3_high1, x_d3_low1 = self.dwt1d(x_d2_high1)
|
|
||||||
x_d3_high2, x_d3_low2 = self.dwt1d(x_d2_low1)
|
|
||||||
x_d3_high3, x_d3_low3 = self.dwt1d(x_d2_high2)
|
|
||||||
x_d3_high4, x_d3_low4 = self.dwt1d(x_d2_low2)
|
|
||||||
x_d3 = self.dwt_conv3(
|
|
||||||
torch.cat([x_d3_high1, x_d3_low1, x_d3_high2, x_d3_low2, x_d3_high3, x_d3_low3, x_d3_high4, x_d3_low4],
|
|
||||||
dim=1))
|
|
||||||
# 1d to 2d
|
|
||||||
b, c, t = x_d3.shape
|
|
||||||
if t % self.period != 0: # pad first
|
|
||||||
n_pad = self.period - (t % self.period)
|
|
||||||
x_d3 = F.pad(x_d3, (0, n_pad), "reflect")
|
|
||||||
t = t + n_pad
|
|
||||||
x_d3 = x_d3.view(b, c, t // self.period, self.period)
|
|
||||||
|
|
||||||
x_d3 = self.dwt_proj3(x_d3)
|
|
||||||
|
|
||||||
# 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)
|
|
||||||
i = 0
|
|
||||||
for l in self.convs:
|
|
||||||
x = l(x)
|
|
||||||
x = F.leaky_relu(x, LRELU_SLOPE)
|
|
||||||
|
|
||||||
fmap.append(x)
|
|
||||||
if i == 0:
|
|
||||||
x = torch.cat([x, x_d1], dim=2)
|
|
||||||
elif i == 1:
|
|
||||||
x = torch.cat([x, x_d2], dim=2)
|
|
||||||
elif i == 2:
|
|
||||||
x = torch.cat([x, x_d3], dim=2)
|
|
||||||
else:
|
|
||||||
x = x
|
|
||||||
i = i + 1
|
|
||||||
x = self.conv_post(x)
|
|
||||||
fmap.append(x)
|
|
||||||
x = torch.flatten(x, 1, -1)
|
|
||||||
|
|
||||||
return x, fmap
|
|
||||||
|
|
||||||
|
|
||||||
class ResWiseMultiPeriodDiscriminator(torch.nn.Module):
|
|
||||||
def __init__(self):
|
|
||||||
super(ResWiseMultiPeriodDiscriminator, self).__init__()
|
|
||||||
self.discriminators = nn.ModuleList([
|
|
||||||
DiscriminatorP(2),
|
|
||||||
DiscriminatorP(3),
|
|
||||||
DiscriminatorP(5),
|
|
||||||
DiscriminatorP(7),
|
|
||||||
DiscriminatorP(11),
|
|
||||||
])
|
|
||||||
|
|
||||||
def forward(self, y, y_hat):
|
|
||||||
y_d_rs = []
|
|
||||||
y_d_gs = []
|
|
||||||
fmap_rs = []
|
|
||||||
fmap_gs = []
|
|
||||||
for i, d in enumerate(self.discriminators):
|
|
||||||
y_d_r, fmap_r = d(y)
|
|
||||||
y_d_g, fmap_g = d(y_hat)
|
|
||||||
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):
|
|
||||||
super(DiscriminatorS, self).__init__()
|
|
||||||
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
|
||||||
self.dwt1d = DWT_1D()
|
|
||||||
self.dwt_conv1 = norm_f(Conv1d(2, 128, 15, 1, padding=7))
|
|
||||||
self.dwt_conv2 = norm_f(Conv1d(4, 128, 41, 2, padding=20))
|
|
||||||
self.convs = nn.ModuleList([
|
|
||||||
norm_f(Conv1d(1, 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):
|
|
||||||
fmap = []
|
|
||||||
|
|
||||||
# DWT 1
|
|
||||||
x_d1_high1, x_d1_low1 = self.dwt1d(x)
|
|
||||||
x_d1 = self.dwt_conv1(torch.cat([x_d1_high1, x_d1_low1], dim=1))
|
|
||||||
|
|
||||||
# DWT 2
|
|
||||||
x_d2_high1, x_d2_low1 = self.dwt1d(x_d1_high1)
|
|
||||||
x_d2_high2, x_d2_low2 = self.dwt1d(x_d1_low1)
|
|
||||||
x_d2 = self.dwt_conv2(torch.cat([x_d2_high1, x_d2_low1, x_d2_high2, x_d2_low2], dim=1))
|
|
||||||
|
|
||||||
i = 0
|
|
||||||
for l in self.convs:
|
|
||||||
x = l(x)
|
|
||||||
x = F.leaky_relu(x, LRELU_SLOPE)
|
|
||||||
fmap.append(x)
|
|
||||||
if i == 0:
|
|
||||||
x = torch.cat([x, x_d1], dim=2)
|
|
||||||
if i == 1:
|
|
||||||
x = torch.cat([x, x_d2], dim=2)
|
|
||||||
i = i + 1
|
|
||||||
x = self.conv_post(x)
|
|
||||||
fmap.append(x)
|
|
||||||
x = torch.flatten(x, 1, -1)
|
|
||||||
|
|
||||||
return x, fmap
|
|
||||||
|
|
||||||
|
|
||||||
class ResWiseMultiScaleDiscriminator(torch.nn.Module):
|
|
||||||
def __init__(self, use_spectral_norm=False):
|
|
||||||
super(ResWiseMultiScaleDiscriminator, self).__init__()
|
|
||||||
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
|
||||||
self.dwt1d = DWT_1D()
|
|
||||||
self.dwt_conv1 = norm_f(Conv1d(2, 1, 1))
|
|
||||||
self.dwt_conv2 = norm_f(Conv1d(4, 1, 1))
|
|
||||||
self.discriminators = nn.ModuleList([
|
|
||||||
DiscriminatorS(use_spectral_norm=True),
|
|
||||||
DiscriminatorS(),
|
|
||||||
DiscriminatorS(),
|
|
||||||
])
|
|
||||||
|
|
||||||
def forward(self, y, y_hat):
|
|
||||||
y_d_rs = []
|
|
||||||
y_d_gs = []
|
|
||||||
fmap_rs = []
|
|
||||||
fmap_gs = []
|
|
||||||
# DWT 1
|
|
||||||
y_hi, y_lo = self.dwt1d(y)
|
|
||||||
y_1 = self.dwt_conv1(torch.cat([y_hi, y_lo], dim=1))
|
|
||||||
x_d1_high1, x_d1_low1 = self.dwt1d(y_hat)
|
|
||||||
y_hat_1 = self.dwt_conv1(torch.cat([x_d1_high1, x_d1_low1], dim=1))
|
|
||||||
|
|
||||||
# DWT 2
|
|
||||||
x_d2_high1, x_d2_low1 = self.dwt1d(y_hi)
|
|
||||||
x_d2_high2, x_d2_low2 = self.dwt1d(y_lo)
|
|
||||||
y_2 = self.dwt_conv2(torch.cat([x_d2_high1, x_d2_low1, x_d2_high2, x_d2_low2], dim=1))
|
|
||||||
|
|
||||||
x_d2_high1, x_d2_low1 = self.dwt1d(x_d1_high1)
|
|
||||||
x_d2_high2, x_d2_low2 = self.dwt1d(x_d1_low1)
|
|
||||||
y_hat_2 = self.dwt_conv2(torch.cat([x_d2_high1, x_d2_low1, x_d2_high2, x_d2_low2], dim=1))
|
|
||||||
|
|
||||||
for i, d in enumerate(self.discriminators):
|
|
||||||
|
|
||||||
if i == 1:
|
|
||||||
y = y_1
|
|
||||||
y_hat = y_hat_1
|
|
||||||
if i == 2:
|
|
||||||
y = y_2
|
|
||||||
y_hat = y_hat_2
|
|
||||||
|
|
||||||
y_d_r, fmap_r = d(y)
|
|
||||||
y_d_g, fmap_g = d(y_hat)
|
|
||||||
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
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
# Copyright (c) 2019, Adobe Inc. All rights reserved.
|
|
||||||
#
|
|
||||||
# This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike
|
|
||||||
# 4.0 International Public License. To view a copy of this license, visit
|
|
||||||
# https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode.
|
|
||||||
|
|
||||||
# DWT code borrow from https://github.com/LiQiufu/WaveSNet/blob/12cb9d24208c3d26917bf953618c30f0c6b0f03d/DWT_IDWT/DWT_IDWT_layer.py
|
|
||||||
|
|
||||||
|
|
||||||
import pywt
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
|
|
||||||
__all__ = ['DWT_1D']
|
|
||||||
Pad_Mode = ['constant', 'reflect', 'replicate', 'circular']
|
|
||||||
|
|
||||||
|
|
||||||
class DWT_1D(nn.Module):
|
|
||||||
def __init__(self, pad_type='reflect', wavename='haar',
|
|
||||||
stride=2, in_channels=1, out_channels=None, groups=None,
|
|
||||||
kernel_size=None, trainable=False):
|
|
||||||
|
|
||||||
super(DWT_1D, self).__init__()
|
|
||||||
self.trainable = trainable
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
if not self.trainable:
|
|
||||||
assert self.kernel_size == None
|
|
||||||
self.in_channels = in_channels
|
|
||||||
self.out_channels = self.in_channels if out_channels == None else out_channels
|
|
||||||
self.groups = self.in_channels if groups == None else groups
|
|
||||||
assert isinstance(self.groups, int) and self.in_channels % self.groups == 0
|
|
||||||
self.stride = stride
|
|
||||||
assert self.stride == 2
|
|
||||||
self.wavename = wavename
|
|
||||||
self.pad_type = pad_type
|
|
||||||
assert self.pad_type in Pad_Mode
|
|
||||||
self.get_filters()
|
|
||||||
self.initialization()
|
|
||||||
|
|
||||||
def get_filters(self):
|
|
||||||
wavelet = pywt.Wavelet(self.wavename)
|
|
||||||
band_low = torch.tensor(wavelet.rec_lo)
|
|
||||||
band_high = torch.tensor(wavelet.rec_hi)
|
|
||||||
length_band = band_low.size()[0]
|
|
||||||
self.kernel_size = length_band if self.kernel_size == None else self.kernel_size
|
|
||||||
assert self.kernel_size >= length_band
|
|
||||||
a = (self.kernel_size - length_band) // 2
|
|
||||||
b = - (self.kernel_size - length_band - a)
|
|
||||||
b = None if b == 0 else b
|
|
||||||
self.filt_low = torch.zeros(self.kernel_size)
|
|
||||||
self.filt_high = torch.zeros(self.kernel_size)
|
|
||||||
self.filt_low[a:b] = band_low
|
|
||||||
self.filt_high[a:b] = band_high
|
|
||||||
|
|
||||||
def initialization(self):
|
|
||||||
self.filter_low = self.filt_low[None, None, :].repeat((self.out_channels, self.in_channels // self.groups, 1))
|
|
||||||
self.filter_high = self.filt_high[None, None, :].repeat((self.out_channels, self.in_channels // self.groups, 1))
|
|
||||||
if torch.cuda.is_available():
|
|
||||||
self.filter_low = self.filter_low.cuda()
|
|
||||||
self.filter_high = self.filter_high.cuda()
|
|
||||||
if self.trainable:
|
|
||||||
self.filter_low = nn.Parameter(self.filter_low)
|
|
||||||
self.filter_high = nn.Parameter(self.filter_high)
|
|
||||||
if self.kernel_size % 2 == 0:
|
|
||||||
self.pad_sizes = [self.kernel_size // 2 - 1, self.kernel_size // 2 - 1]
|
|
||||||
else:
|
|
||||||
self.pad_sizes = [self.kernel_size // 2, self.kernel_size // 2]
|
|
||||||
|
|
||||||
def forward(self, input):
|
|
||||||
assert isinstance(input, torch.Tensor)
|
|
||||||
assert len(input.size()) == 3
|
|
||||||
assert input.size()[1] == self.in_channels
|
|
||||||
input = F.pad(input, pad=self.pad_sizes, mode=self.pad_type)
|
|
||||||
return F.conv1d(input, self.filter_low.to(input.device), stride=self.stride, groups=self.groups), \
|
|
||||||
F.conv1d(input, self.filter_high.to(input.device), stride=self.stride, groups=self.groups)
|
|
||||||
@@ -1,210 +0,0 @@
|
|||||||
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 utils.util import init_weights, get_padding
|
|
||||||
|
|
||||||
LRELU_SLOPE = 0.1
|
|
||||||
|
|
||||||
|
|
||||||
class ResBlock1(torch.nn.Module):
|
|
||||||
def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5, 7)):
|
|
||||||
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]))),
|
|
||||||
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[3],
|
|
||||||
padding=get_padding(kernel_size, dilation[3])))
|
|
||||||
])
|
|
||||||
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))),
|
|
||||||
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 FreGAN(torch.nn.Module):
|
|
||||||
def __init__(self, h, top_k=4):
|
|
||||||
super(FreGAN, self).__init__()
|
|
||||||
self.h = h
|
|
||||||
|
|
||||||
self.num_kernels = len(h.resblock_kernel_sizes)
|
|
||||||
self.num_upsamples = len(h.upsample_rates)
|
|
||||||
self.upsample_rates = h.upsample_rates
|
|
||||||
self.up_kernels = h.upsample_kernel_sizes
|
|
||||||
self.cond_level = self.num_upsamples - top_k
|
|
||||||
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()
|
|
||||||
self.cond_up = nn.ModuleList()
|
|
||||||
self.res_output = nn.ModuleList()
|
|
||||||
upsample_ = 1
|
|
||||||
kr = 80
|
|
||||||
|
|
||||||
for i, (u, k) in enumerate(zip(self.upsample_rates, self.up_kernels)):
|
|
||||||
# self.ups.append(weight_norm(
|
|
||||||
# ConvTranspose1d(h.upsample_initial_channel // (2 ** i), h.upsample_initial_channel // (2 ** (i + 1)),
|
|
||||||
# k, u, padding=(k - u) // 2)))
|
|
||||||
self.ups.append(weight_norm(ConvTranspose1d(h.upsample_initial_channel//(2**i),
|
|
||||||
h.upsample_initial_channel//(2**(i+1)),
|
|
||||||
k, u, padding=(u//2 + u%2), output_padding=u%2)))
|
|
||||||
|
|
||||||
if i > (self.num_upsamples - top_k):
|
|
||||||
self.res_output.append(
|
|
||||||
nn.Sequential(
|
|
||||||
nn.Upsample(scale_factor=u, mode='nearest'),
|
|
||||||
weight_norm(nn.Conv1d(h.upsample_initial_channel // (2 ** i),
|
|
||||||
h.upsample_initial_channel // (2 ** (i + 1)), 1))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if i >= (self.num_upsamples - top_k):
|
|
||||||
self.cond_up.append(
|
|
||||||
weight_norm(
|
|
||||||
ConvTranspose1d(kr, h.upsample_initial_channel // (2 ** i),
|
|
||||||
self.up_kernels[i - 1], self.upsample_rates[i - 1],
|
|
||||||
padding=(self.upsample_rates[i-1]//2+self.upsample_rates[i-1]%2), output_padding=self.upsample_rates[i-1]%2))
|
|
||||||
)
|
|
||||||
kr = h.upsample_initial_channel // (2 ** i)
|
|
||||||
|
|
||||||
upsample_ *= u
|
|
||||||
|
|
||||||
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, 1, 7, 1, padding=3))
|
|
||||||
self.ups.apply(init_weights)
|
|
||||||
self.conv_post.apply(init_weights)
|
|
||||||
self.cond_up.apply(init_weights)
|
|
||||||
self.res_output.apply(init_weights)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
mel = x
|
|
||||||
x = self.conv_pre(x)
|
|
||||||
output = None
|
|
||||||
for i in range(self.num_upsamples):
|
|
||||||
if i >= self.cond_level:
|
|
||||||
mel = self.cond_up[i - self.cond_level](mel)
|
|
||||||
x += mel
|
|
||||||
if i > self.cond_level:
|
|
||||||
if output is None:
|
|
||||||
output = self.res_output[i - self.cond_level - 1](x)
|
|
||||||
else:
|
|
||||||
output = self.res_output[i - self.cond_level - 1](output)
|
|
||||||
x = F.leaky_relu(x, LRELU_SLOPE)
|
|
||||||
x = self.ups[i](x)
|
|
||||||
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
|
|
||||||
if output is not None:
|
|
||||||
output = output + x
|
|
||||||
|
|
||||||
x = F.leaky_relu(output)
|
|
||||||
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()
|
|
||||||
for l in self.cond_up:
|
|
||||||
remove_weight_norm(l)
|
|
||||||
for l in self.res_output:
|
|
||||||
remove_weight_norm(l[1])
|
|
||||||
remove_weight_norm(self.conv_pre)
|
|
||||||
remove_weight_norm(self.conv_post)
|
|
||||||
|
|
||||||
|
|
||||||
'''
|
|
||||||
to run this, fix
|
|
||||||
from . import ResStack
|
|
||||||
into
|
|
||||||
from res_stack import ResStack
|
|
||||||
'''
|
|
||||||
if __name__ == '__main__':
|
|
||||||
'''
|
|
||||||
torch.Size([3, 80, 10])
|
|
||||||
torch.Size([3, 1, 2000])
|
|
||||||
4527362
|
|
||||||
'''
|
|
||||||
with open('config.json') as f:
|
|
||||||
data = f.read()
|
|
||||||
from utils import AttrDict
|
|
||||||
import json
|
|
||||||
json_config = json.loads(data)
|
|
||||||
h = AttrDict(json_config)
|
|
||||||
model = FreGAN(h)
|
|
||||||
|
|
||||||
c = torch.randn(3, 80, 10) # (B, channels, T).
|
|
||||||
print(c.shape)
|
|
||||||
|
|
||||||
y = model(c) # (B, 1, T ** prod(upsample_scales)
|
|
||||||
print(y.shape)
|
|
||||||
assert y.shape == torch.Size([3, 1, 2560]) # For normal melgan torch.Size([3, 1, 2560])
|
|
||||||
|
|
||||||
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
|
||||||
print(pytorch_total_params)
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
|
||||||
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
import torch
|
|
||||||
from utils.util import AttrDict
|
|
||||||
from models.vocoder.fregan.generator import FreGAN
|
|
||||||
|
|
||||||
generator = None # type: FreGAN
|
|
||||||
output_sample_rate = None
|
|
||||||
_device = None
|
|
||||||
|
|
||||||
|
|
||||||
def load_checkpoint(filepath, device):
|
|
||||||
assert os.path.isfile(filepath)
|
|
||||||
print("Loading '{}'".format(filepath))
|
|
||||||
checkpoint_dict = torch.load(filepath, map_location=device)
|
|
||||||
print("Complete.")
|
|
||||||
return checkpoint_dict
|
|
||||||
|
|
||||||
|
|
||||||
def load_model(weights_fpath, config_fpath=None, verbose=True):
|
|
||||||
global generator, _device, output_sample_rate
|
|
||||||
|
|
||||||
if verbose:
|
|
||||||
print("Building fregan")
|
|
||||||
|
|
||||||
if config_fpath == None:
|
|
||||||
model_config_fpaths = list(weights_fpath.parent.rglob("*.json"))
|
|
||||||
if len(model_config_fpaths) > 0:
|
|
||||||
config_fpath = model_config_fpaths[0]
|
|
||||||
else:
|
|
||||||
config_fpath = "./vocoder/fregan/config.json"
|
|
||||||
with open(config_fpath) as f:
|
|
||||||
data = f.read()
|
|
||||||
json_config = json.loads(data)
|
|
||||||
h = AttrDict(json_config)
|
|
||||||
output_sample_rate = h.sampling_rate
|
|
||||||
torch.manual_seed(h.seed)
|
|
||||||
|
|
||||||
if torch.cuda.is_available():
|
|
||||||
# _model = _model.cuda()
|
|
||||||
_device = torch.device('cuda')
|
|
||||||
else:
|
|
||||||
_device = torch.device('cpu')
|
|
||||||
|
|
||||||
generator = FreGAN(h).to(_device)
|
|
||||||
state_dict_g = load_checkpoint(
|
|
||||||
weights_fpath, _device
|
|
||||||
)
|
|
||||||
generator.load_state_dict(state_dict_g['generator'])
|
|
||||||
generator.eval()
|
|
||||||
generator.remove_weight_norm()
|
|
||||||
|
|
||||||
|
|
||||||
def is_loaded():
|
|
||||||
return generator is not None
|
|
||||||
|
|
||||||
|
|
||||||
def infer_waveform(mel, progress_callback=None):
|
|
||||||
|
|
||||||
if generator is None:
|
|
||||||
raise Exception("Please load fre-gan in memory before using it")
|
|
||||||
|
|
||||||
mel = torch.FloatTensor(mel).to(_device)
|
|
||||||
mel = mel.unsqueeze(0)
|
|
||||||
|
|
||||||
with torch.no_grad():
|
|
||||||
y_g_hat = generator(mel)
|
|
||||||
audio = y_g_hat.squeeze()
|
|
||||||
audio = audio.cpu().numpy()
|
|
||||||
|
|
||||||
return audio, output_sample_rate
|
|
||||||
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
import math
|
|
||||||
import os
|
|
||||||
import random
|
|
||||||
import torch
|
|
||||||
import torch.utils.data
|
|
||||||
import numpy as np
|
|
||||||
from librosa.util import normalize
|
|
||||||
from scipy.io.wavfile import read
|
|
||||||
from utils.audio_utils import mel_spectrogram
|
|
||||||
|
|
||||||
MAX_WAV_VALUE = 32768.0
|
|
||||||
|
|
||||||
|
|
||||||
def load_wav(full_path):
|
|
||||||
sampling_rate, data = read(full_path)
|
|
||||||
return data, sampling_rate
|
|
||||||
|
|
||||||
|
|
||||||
def get_dataset_filelist(a):
|
|
||||||
#with open(a.input_training_file, 'r', encoding='utf-8') as fi:
|
|
||||||
# training_files = [os.path.join(a.input_wavs_dir, x.split('|')[0] + '.wav')
|
|
||||||
# for x in fi.read().split('\n') if len(x) > 0]
|
|
||||||
|
|
||||||
#with open(a.input_validation_file, 'r', encoding='utf-8') as fi:
|
|
||||||
# validation_files = [os.path.join(a.input_wavs_dir, x.split('|')[0] + '.wav')
|
|
||||||
# for x in fi.read().split('\n') if len(x) > 0]
|
|
||||||
files = os.listdir(a.input_wavs_dir)
|
|
||||||
random.shuffle(files)
|
|
||||||
files = [os.path.join(a.input_wavs_dir, f) for f in files]
|
|
||||||
training_files = files[: -int(len(files) * 0.05)]
|
|
||||||
validation_files = files[-int(len(files) * 0.05):]
|
|
||||||
return training_files, validation_files
|
|
||||||
|
|
||||||
|
|
||||||
class MelDataset(torch.utils.data.Dataset):
|
|
||||||
def __init__(self, training_files, segment_size, n_fft, num_mels,
|
|
||||||
hop_size, win_size, sampling_rate, fmin, fmax, split=True, shuffle=True, n_cache_reuse=1,
|
|
||||||
device=None, fmax_loss=None, fine_tuning=False, base_mels_path=None):
|
|
||||||
self.audio_files = training_files
|
|
||||||
random.seed(1234)
|
|
||||||
if shuffle:
|
|
||||||
random.shuffle(self.audio_files)
|
|
||||||
self.segment_size = segment_size
|
|
||||||
self.sampling_rate = sampling_rate
|
|
||||||
self.split = split
|
|
||||||
self.n_fft = n_fft
|
|
||||||
self.num_mels = num_mels
|
|
||||||
self.hop_size = hop_size
|
|
||||||
self.win_size = win_size
|
|
||||||
self.fmin = fmin
|
|
||||||
self.fmax = fmax
|
|
||||||
self.fmax_loss = fmax_loss
|
|
||||||
self.cached_wav = None
|
|
||||||
self.n_cache_reuse = n_cache_reuse
|
|
||||||
self._cache_ref_count = 0
|
|
||||||
self.device = device
|
|
||||||
self.fine_tuning = fine_tuning
|
|
||||||
self.base_mels_path = base_mels_path
|
|
||||||
|
|
||||||
def __getitem__(self, index):
|
|
||||||
filename = self.audio_files[index]
|
|
||||||
if self._cache_ref_count == 0:
|
|
||||||
#audio, sampling_rate = load_wav(filename)
|
|
||||||
#audio = audio / MAX_WAV_VALUE
|
|
||||||
audio = np.load(filename)
|
|
||||||
if not self.fine_tuning:
|
|
||||||
audio = normalize(audio) * 0.95
|
|
||||||
self.cached_wav = audio
|
|
||||||
#if sampling_rate != self.sampling_rate:
|
|
||||||
# raise ValueError("{} SR doesn't match target {} SR".format(
|
|
||||||
# sampling_rate, self.sampling_rate))
|
|
||||||
self._cache_ref_count = self.n_cache_reuse
|
|
||||||
else:
|
|
||||||
audio = self.cached_wav
|
|
||||||
self._cache_ref_count -= 1
|
|
||||||
|
|
||||||
audio = torch.FloatTensor(audio)
|
|
||||||
audio = audio.unsqueeze(0)
|
|
||||||
|
|
||||||
if not self.fine_tuning:
|
|
||||||
if self.split:
|
|
||||||
if audio.size(1) >= self.segment_size:
|
|
||||||
max_audio_start = audio.size(1) - self.segment_size
|
|
||||||
audio_start = random.randint(0, max_audio_start)
|
|
||||||
audio = audio[:, audio_start:audio_start+self.segment_size]
|
|
||||||
else:
|
|
||||||
audio = torch.nn.functional.pad(audio, (0, self.segment_size - audio.size(1)), 'constant')
|
|
||||||
|
|
||||||
mel = mel_spectrogram(audio, self.n_fft, self.num_mels,
|
|
||||||
self.sampling_rate, self.hop_size, self.win_size, self.fmin, self.fmax,
|
|
||||||
center=False)
|
|
||||||
else:
|
|
||||||
mel_path = os.path.join(self.base_mels_path, "mel" + "-" + filename.split("/")[-1].split("-")[-1])
|
|
||||||
mel = np.load(mel_path).T
|
|
||||||
#mel = np.load(
|
|
||||||
# os.path.join(self.base_mels_path, os.path.splitext(os.path.split(filename)[-1])[0] + '.npy'))
|
|
||||||
mel = torch.from_numpy(mel)
|
|
||||||
|
|
||||||
if len(mel.shape) < 3:
|
|
||||||
mel = mel.unsqueeze(0)
|
|
||||||
|
|
||||||
if self.split:
|
|
||||||
frames_per_seg = math.ceil(self.segment_size / self.hop_size)
|
|
||||||
|
|
||||||
if audio.size(1) >= self.segment_size:
|
|
||||||
mel_start = random.randint(0, mel.size(2) - frames_per_seg - 1)
|
|
||||||
mel = mel[:, :, mel_start:mel_start + frames_per_seg]
|
|
||||||
audio = audio[:, mel_start * self.hop_size:(mel_start + frames_per_seg) * self.hop_size]
|
|
||||||
else:
|
|
||||||
mel = torch.nn.functional.pad(mel, (0, frames_per_seg - mel.size(2)), 'constant')
|
|
||||||
audio = torch.nn.functional.pad(audio, (0, self.segment_size - audio.size(1)), 'constant')
|
|
||||||
|
|
||||||
mel_loss = mel_spectrogram(audio, self.n_fft, self.num_mels,
|
|
||||||
self.sampling_rate, self.hop_size, self.win_size, self.fmin, self.fmax_loss,
|
|
||||||
center=False)
|
|
||||||
|
|
||||||
return (mel.squeeze(), audio.squeeze(0), filename, mel_loss.squeeze())
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self.audio_files)
|
|
||||||
@@ -1,201 +0,0 @@
|
|||||||
import torch
|
|
||||||
import torch.nn.functional as F
|
|
||||||
|
|
||||||
class KernelPredictor(torch.nn.Module):
|
|
||||||
''' Kernel predictor for the location-variable convolutions
|
|
||||||
'''
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
cond_channels,
|
|
||||||
conv_in_channels,
|
|
||||||
conv_out_channels,
|
|
||||||
conv_layers,
|
|
||||||
conv_kernel_size=3,
|
|
||||||
kpnet_hidden_channels=64,
|
|
||||||
kpnet_conv_size=3,
|
|
||||||
kpnet_dropout=0.0,
|
|
||||||
kpnet_nonlinear_activation="LeakyReLU",
|
|
||||||
kpnet_nonlinear_activation_params={"negative_slope": 0.1}
|
|
||||||
):
|
|
||||||
'''
|
|
||||||
Args:
|
|
||||||
cond_channels (int): number of channel for the conditioning sequence,
|
|
||||||
conv_in_channels (int): number of channel for the input sequence,
|
|
||||||
conv_out_channels (int): number of channel for the output sequence,
|
|
||||||
conv_layers (int):
|
|
||||||
kpnet_
|
|
||||||
'''
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
self.conv_in_channels = conv_in_channels
|
|
||||||
self.conv_out_channels = conv_out_channels
|
|
||||||
self.conv_kernel_size = conv_kernel_size
|
|
||||||
self.conv_layers = conv_layers
|
|
||||||
|
|
||||||
l_w = conv_in_channels * conv_out_channels * conv_kernel_size * conv_layers
|
|
||||||
l_b = conv_out_channels * conv_layers
|
|
||||||
|
|
||||||
padding = (kpnet_conv_size - 1) // 2
|
|
||||||
self.input_conv = torch.nn.Sequential(
|
|
||||||
torch.nn.Conv1d(cond_channels, kpnet_hidden_channels, 5, padding=(5 - 1) // 2, bias=True),
|
|
||||||
getattr(torch.nn, kpnet_nonlinear_activation)(**kpnet_nonlinear_activation_params),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.residual_conv = torch.nn.Sequential(
|
|
||||||
torch.nn.Dropout(kpnet_dropout),
|
|
||||||
torch.nn.Conv1d(kpnet_hidden_channels, kpnet_hidden_channels, kpnet_conv_size, padding=padding, bias=True),
|
|
||||||
getattr(torch.nn, kpnet_nonlinear_activation)(**kpnet_nonlinear_activation_params),
|
|
||||||
torch.nn.Conv1d(kpnet_hidden_channels, kpnet_hidden_channels, kpnet_conv_size, padding=padding, bias=True),
|
|
||||||
getattr(torch.nn, kpnet_nonlinear_activation)(**kpnet_nonlinear_activation_params),
|
|
||||||
torch.nn.Dropout(kpnet_dropout),
|
|
||||||
torch.nn.Conv1d(kpnet_hidden_channels, kpnet_hidden_channels, kpnet_conv_size, padding=padding, bias=True),
|
|
||||||
getattr(torch.nn, kpnet_nonlinear_activation)(**kpnet_nonlinear_activation_params),
|
|
||||||
torch.nn.Conv1d(kpnet_hidden_channels, kpnet_hidden_channels, kpnet_conv_size, padding=padding, bias=True),
|
|
||||||
getattr(torch.nn, kpnet_nonlinear_activation)(**kpnet_nonlinear_activation_params),
|
|
||||||
torch.nn.Dropout(kpnet_dropout),
|
|
||||||
torch.nn.Conv1d(kpnet_hidden_channels, kpnet_hidden_channels, kpnet_conv_size, padding=padding, bias=True),
|
|
||||||
getattr(torch.nn, kpnet_nonlinear_activation)(**kpnet_nonlinear_activation_params),
|
|
||||||
torch.nn.Conv1d(kpnet_hidden_channels, kpnet_hidden_channels, kpnet_conv_size, padding=padding, bias=True),
|
|
||||||
getattr(torch.nn, kpnet_nonlinear_activation)(**kpnet_nonlinear_activation_params),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.kernel_conv = torch.nn.Conv1d(kpnet_hidden_channels, l_w, kpnet_conv_size,
|
|
||||||
padding=padding, bias=True)
|
|
||||||
self.bias_conv = torch.nn.Conv1d(kpnet_hidden_channels, l_b, kpnet_conv_size, padding=padding,
|
|
||||||
bias=True)
|
|
||||||
|
|
||||||
def forward(self, c):
|
|
||||||
'''
|
|
||||||
Args:
|
|
||||||
c (Tensor): the conditioning sequence (batch, cond_channels, cond_length)
|
|
||||||
Returns:
|
|
||||||
'''
|
|
||||||
batch, cond_channels, cond_length = c.shape
|
|
||||||
|
|
||||||
c = self.input_conv(c)
|
|
||||||
c = c + self.residual_conv(c)
|
|
||||||
k = self.kernel_conv(c)
|
|
||||||
b = self.bias_conv(c)
|
|
||||||
|
|
||||||
kernels = k.contiguous().view(batch,
|
|
||||||
self.conv_layers,
|
|
||||||
self.conv_in_channels,
|
|
||||||
self.conv_out_channels,
|
|
||||||
self.conv_kernel_size,
|
|
||||||
cond_length)
|
|
||||||
bias = b.contiguous().view(batch,
|
|
||||||
self.conv_layers,
|
|
||||||
self.conv_out_channels,
|
|
||||||
cond_length)
|
|
||||||
return kernels, bias
|
|
||||||
|
|
||||||
|
|
||||||
class LVCBlock(torch.nn.Module):
|
|
||||||
''' the location-variable convolutions
|
|
||||||
'''
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
in_channels,
|
|
||||||
cond_channels,
|
|
||||||
upsample_ratio,
|
|
||||||
conv_layers=4,
|
|
||||||
conv_kernel_size=3,
|
|
||||||
cond_hop_length=256,
|
|
||||||
kpnet_hidden_channels=64,
|
|
||||||
kpnet_conv_size=3,
|
|
||||||
kpnet_dropout=0.0
|
|
||||||
):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
self.cond_hop_length = cond_hop_length
|
|
||||||
self.conv_layers = conv_layers
|
|
||||||
self.conv_kernel_size = conv_kernel_size
|
|
||||||
self.convs = torch.nn.ModuleList()
|
|
||||||
|
|
||||||
self.upsample = torch.nn.ConvTranspose1d(in_channels, in_channels,
|
|
||||||
kernel_size=upsample_ratio*2, stride=upsample_ratio,
|
|
||||||
padding=upsample_ratio // 2 + upsample_ratio % 2,
|
|
||||||
output_padding=upsample_ratio % 2)
|
|
||||||
|
|
||||||
|
|
||||||
self.kernel_predictor = KernelPredictor(
|
|
||||||
cond_channels=cond_channels,
|
|
||||||
conv_in_channels=in_channels,
|
|
||||||
conv_out_channels=2 * in_channels,
|
|
||||||
conv_layers=conv_layers,
|
|
||||||
conv_kernel_size=conv_kernel_size,
|
|
||||||
kpnet_hidden_channels=kpnet_hidden_channels,
|
|
||||||
kpnet_conv_size=kpnet_conv_size,
|
|
||||||
kpnet_dropout=kpnet_dropout
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
for i in range(conv_layers):
|
|
||||||
padding = (3 ** i) * int((conv_kernel_size - 1) / 2)
|
|
||||||
conv = torch.nn.Conv1d(in_channels, in_channels, kernel_size=conv_kernel_size, padding=padding, dilation=3 ** i)
|
|
||||||
|
|
||||||
self.convs.append(conv)
|
|
||||||
|
|
||||||
|
|
||||||
def forward(self, x, c):
|
|
||||||
''' forward propagation of the location-variable convolutions.
|
|
||||||
Args:
|
|
||||||
x (Tensor): the input sequence (batch, in_channels, in_length)
|
|
||||||
c (Tensor): the conditioning sequence (batch, cond_channels, cond_length)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Tensor: the output sequence (batch, in_channels, in_length)
|
|
||||||
'''
|
|
||||||
batch, in_channels, in_length = x.shape
|
|
||||||
|
|
||||||
|
|
||||||
kernels, bias = self.kernel_predictor(c)
|
|
||||||
|
|
||||||
x = F.leaky_relu(x, 0.2)
|
|
||||||
x = self.upsample(x)
|
|
||||||
|
|
||||||
for i in range(self.conv_layers):
|
|
||||||
y = F.leaky_relu(x, 0.2)
|
|
||||||
y = self.convs[i](y)
|
|
||||||
y = F.leaky_relu(y, 0.2)
|
|
||||||
|
|
||||||
k = kernels[:, i, :, :, :, :]
|
|
||||||
b = bias[:, i, :, :]
|
|
||||||
y = self.location_variable_convolution(y, k, b, 1, self.cond_hop_length)
|
|
||||||
x = x + torch.sigmoid(y[:, :in_channels, :]) * torch.tanh(y[:, in_channels:, :])
|
|
||||||
return x
|
|
||||||
|
|
||||||
def location_variable_convolution(self, x, kernel, bias, dilation, hop_size):
|
|
||||||
''' perform location-variable convolution operation on the input sequence (x) using the local convolution kernl.
|
|
||||||
Time: 414 μs ± 309 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each), test on NVIDIA V100.
|
|
||||||
Args:
|
|
||||||
x (Tensor): the input sequence (batch, in_channels, in_length).
|
|
||||||
kernel (Tensor): the local convolution kernel (batch, in_channel, out_channels, kernel_size, kernel_length)
|
|
||||||
bias (Tensor): the bias for the local convolution (batch, out_channels, kernel_length)
|
|
||||||
dilation (int): the dilation of convolution.
|
|
||||||
hop_size (int): the hop_size of the conditioning sequence.
|
|
||||||
Returns:
|
|
||||||
(Tensor): the output sequence after performing local convolution. (batch, out_channels, in_length).
|
|
||||||
'''
|
|
||||||
batch, in_channels, in_length = x.shape
|
|
||||||
batch, in_channels, out_channels, kernel_size, kernel_length = kernel.shape
|
|
||||||
|
|
||||||
|
|
||||||
assert in_length == (kernel_length * hop_size), "length of (x, kernel) is not matched"
|
|
||||||
|
|
||||||
padding = dilation * int((kernel_size - 1) / 2)
|
|
||||||
x = F.pad(x, (padding, padding), 'constant', 0) # (batch, in_channels, in_length + 2*padding)
|
|
||||||
x = x.unfold(2, hop_size + 2 * padding, hop_size) # (batch, in_channels, kernel_length, hop_size + 2*padding)
|
|
||||||
|
|
||||||
if hop_size < dilation:
|
|
||||||
x = F.pad(x, (0, dilation), 'constant', 0)
|
|
||||||
x = x.unfold(3, dilation,
|
|
||||||
dilation) # (batch, in_channels, kernel_length, (hop_size + 2*padding)/dilation, dilation)
|
|
||||||
x = x[:, :, :, :, :hop_size]
|
|
||||||
x = x.transpose(3, 4) # (batch, in_channels, kernel_length, dilation, (hop_size + 2*padding)/dilation)
|
|
||||||
x = x.unfold(4, kernel_size, 1) # (batch, in_channels, kernel_length, dilation, _, kernel_size)
|
|
||||||
|
|
||||||
o = torch.einsum('bildsk,biokl->bolsd', x, kernel)
|
|
||||||
o = o + bias.unsqueeze(-1).unsqueeze(-1)
|
|
||||||
o = o.contiguous().view(batch, out_channels, -1)
|
|
||||||
return o
|
|
||||||
@@ -1,136 +0,0 @@
|
|||||||
# -*- 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.to(x.get_device()))
|
|
||||||
y_mag = stft(y, self.fft_size, self.shift_size, self.win_length, self.window.to(x.get_device()))
|
|
||||||
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
|
|
||||||
@@ -1,246 +0,0 @@
|
|||||||
import warnings
|
|
||||||
|
|
||||||
warnings.simplefilter(action='ignore', category=FutureWarning)
|
|
||||||
import itertools
|
|
||||||
import os
|
|
||||||
import time
|
|
||||||
import torch
|
|
||||||
import torch.nn.functional as F
|
|
||||||
from torch.utils.tensorboard import SummaryWriter
|
|
||||||
from torch.utils.data import DistributedSampler, DataLoader
|
|
||||||
from torch.distributed import init_process_group
|
|
||||||
from torch.nn.parallel import DistributedDataParallel
|
|
||||||
from models.vocoder.fregan.meldataset import MelDataset, mel_spectrogram, get_dataset_filelist
|
|
||||||
from models.vocoder.fregan.generator import FreGAN
|
|
||||||
from models.vocoder.fregan.discriminator import ResWiseMultiPeriodDiscriminator, ResWiseMultiScaleDiscriminator
|
|
||||||
from utils.loss import feature_loss, generator_loss, discriminator_loss
|
|
||||||
from models.vocoder.fregan.utils import plot_spectrogram, scan_checkpoint, load_checkpoint, save_checkpoint
|
|
||||||
|
|
||||||
|
|
||||||
torch.backends.cudnn.benchmark = True
|
|
||||||
|
|
||||||
|
|
||||||
def train(rank, a, h):
|
|
||||||
|
|
||||||
a.checkpoint_path = a.models_dir.joinpath(a.run_id+'_fregan')
|
|
||||||
a.checkpoint_path.mkdir(exist_ok=True)
|
|
||||||
a.training_epochs = 3100
|
|
||||||
a.stdout_interval = 5
|
|
||||||
a.checkpoint_interval = a.backup_every
|
|
||||||
a.summary_interval = 5000
|
|
||||||
a.validation_interval = 1000
|
|
||||||
a.fine_tuning = True
|
|
||||||
|
|
||||||
a.input_wavs_dir = a.syn_dir.joinpath("audio")
|
|
||||||
a.input_mels_dir = a.syn_dir.joinpath("mels")
|
|
||||||
|
|
||||||
if h.num_gpus > 1:
|
|
||||||
init_process_group(backend=h.dist_config['dist_backend'], init_method=h.dist_config['dist_url'],
|
|
||||||
world_size=h.dist_config['world_size'] * h.num_gpus, rank=rank)
|
|
||||||
|
|
||||||
torch.cuda.manual_seed(h.seed)
|
|
||||||
device = torch.device('cuda:{:d}'.format(rank))
|
|
||||||
|
|
||||||
generator = FreGAN(h).to(device)
|
|
||||||
mpd = ResWiseMultiPeriodDiscriminator().to(device)
|
|
||||||
msd = ResWiseMultiScaleDiscriminator().to(device)
|
|
||||||
|
|
||||||
if rank == 0:
|
|
||||||
print(generator)
|
|
||||||
os.makedirs(a.checkpoint_path, exist_ok=True)
|
|
||||||
print("checkpoints directory : ", a.checkpoint_path)
|
|
||||||
|
|
||||||
if os.path.isdir(a.checkpoint_path):
|
|
||||||
cp_g = scan_checkpoint(a.checkpoint_path, 'g_fregan_')
|
|
||||||
cp_do = scan_checkpoint(a.checkpoint_path, 'do_fregan_')
|
|
||||||
|
|
||||||
steps = 0
|
|
||||||
if cp_g is None or cp_do is None:
|
|
||||||
state_dict_do = None
|
|
||||||
last_epoch = -1
|
|
||||||
else:
|
|
||||||
state_dict_g = load_checkpoint(cp_g, device)
|
|
||||||
state_dict_do = load_checkpoint(cp_do, device)
|
|
||||||
generator.load_state_dict(state_dict_g['generator'])
|
|
||||||
mpd.load_state_dict(state_dict_do['mpd'])
|
|
||||||
msd.load_state_dict(state_dict_do['msd'])
|
|
||||||
steps = state_dict_do['steps'] + 1
|
|
||||||
last_epoch = state_dict_do['epoch']
|
|
||||||
|
|
||||||
if h.num_gpus > 1:
|
|
||||||
generator = DistributedDataParallel(generator, device_ids=[rank]).to(device)
|
|
||||||
mpd = DistributedDataParallel(mpd, device_ids=[rank]).to(device)
|
|
||||||
msd = DistributedDataParallel(msd, device_ids=[rank]).to(device)
|
|
||||||
|
|
||||||
optim_g = torch.optim.AdamW(generator.parameters(), h.learning_rate, betas=[h.adam_b1, h.adam_b2])
|
|
||||||
optim_d = torch.optim.AdamW(itertools.chain(msd.parameters(), mpd.parameters()),
|
|
||||||
h.learning_rate, betas=[h.adam_b1, h.adam_b2])
|
|
||||||
|
|
||||||
if state_dict_do is not None:
|
|
||||||
optim_g.load_state_dict(state_dict_do['optim_g'])
|
|
||||||
optim_d.load_state_dict(state_dict_do['optim_d'])
|
|
||||||
|
|
||||||
scheduler_g = torch.optim.lr_scheduler.ExponentialLR(optim_g, gamma=h.lr_decay, last_epoch=last_epoch)
|
|
||||||
scheduler_d = torch.optim.lr_scheduler.ExponentialLR(optim_d, gamma=h.lr_decay, last_epoch=last_epoch)
|
|
||||||
|
|
||||||
training_filelist, validation_filelist = get_dataset_filelist(a)
|
|
||||||
|
|
||||||
trainset = MelDataset(training_filelist, h.segment_size, h.n_fft, h.num_mels,
|
|
||||||
h.hop_size, h.win_size, h.sampling_rate, h.fmin, h.fmax, n_cache_reuse=0,
|
|
||||||
shuffle=False if h.num_gpus > 1 else True, fmax_loss=h.fmax_for_loss, device=device,
|
|
||||||
fine_tuning=a.fine_tuning, base_mels_path=a.input_mels_dir)
|
|
||||||
|
|
||||||
train_sampler = DistributedSampler(trainset) if h.num_gpus > 1 else None
|
|
||||||
|
|
||||||
train_loader = DataLoader(trainset, num_workers=h.num_workers, shuffle=False,
|
|
||||||
sampler=train_sampler,
|
|
||||||
batch_size=h.batch_size,
|
|
||||||
pin_memory=True,
|
|
||||||
drop_last=True)
|
|
||||||
|
|
||||||
if rank == 0:
|
|
||||||
validset = MelDataset(validation_filelist, h.segment_size, h.n_fft, h.num_mels,
|
|
||||||
h.hop_size, h.win_size, h.sampling_rate, h.fmin, h.fmax, False, False, n_cache_reuse=0,
|
|
||||||
fmax_loss=h.fmax_for_loss, device=device, fine_tuning=a.fine_tuning,
|
|
||||||
base_mels_path=a.input_mels_dir)
|
|
||||||
validation_loader = DataLoader(validset, num_workers=1, shuffle=False,
|
|
||||||
sampler=None,
|
|
||||||
batch_size=1,
|
|
||||||
pin_memory=True,
|
|
||||||
drop_last=True)
|
|
||||||
|
|
||||||
sw = SummaryWriter(os.path.join(a.checkpoint_path, 'logs'))
|
|
||||||
|
|
||||||
generator.train()
|
|
||||||
mpd.train()
|
|
||||||
msd.train()
|
|
||||||
for epoch in range(max(0, last_epoch), a.training_epochs):
|
|
||||||
if rank == 0:
|
|
||||||
start = time.time()
|
|
||||||
print("Epoch: {}".format(epoch + 1))
|
|
||||||
|
|
||||||
if h.num_gpus > 1:
|
|
||||||
train_sampler.set_epoch(epoch)
|
|
||||||
|
|
||||||
for i, batch in enumerate(train_loader):
|
|
||||||
if rank == 0:
|
|
||||||
start_b = time.time()
|
|
||||||
x, y, _, y_mel = batch
|
|
||||||
x = torch.autograd.Variable(x.to(device, non_blocking=True))
|
|
||||||
y = torch.autograd.Variable(y.to(device, non_blocking=True))
|
|
||||||
y_mel = torch.autograd.Variable(y_mel.to(device, non_blocking=True))
|
|
||||||
y = y.unsqueeze(1)
|
|
||||||
y_g_hat = generator(x)
|
|
||||||
y_g_hat_mel = mel_spectrogram(y_g_hat.squeeze(1), h.n_fft, h.num_mels, h.sampling_rate, h.hop_size,
|
|
||||||
h.win_size,
|
|
||||||
h.fmin, h.fmax_for_loss)
|
|
||||||
|
|
||||||
if steps > h.disc_start_step:
|
|
||||||
optim_d.zero_grad()
|
|
||||||
|
|
||||||
# MPD
|
|
||||||
y_df_hat_r, y_df_hat_g, _, _ = mpd(y, y_g_hat.detach())
|
|
||||||
loss_disc_f, losses_disc_f_r, losses_disc_f_g = discriminator_loss(y_df_hat_r, y_df_hat_g)
|
|
||||||
|
|
||||||
# MSD
|
|
||||||
y_ds_hat_r, y_ds_hat_g, _, _ = msd(y, y_g_hat.detach())
|
|
||||||
loss_disc_s, losses_disc_s_r, losses_disc_s_g = discriminator_loss(y_ds_hat_r, y_ds_hat_g)
|
|
||||||
|
|
||||||
loss_disc_all = loss_disc_s + loss_disc_f
|
|
||||||
|
|
||||||
loss_disc_all.backward()
|
|
||||||
optim_d.step()
|
|
||||||
|
|
||||||
# Generator
|
|
||||||
optim_g.zero_grad()
|
|
||||||
|
|
||||||
|
|
||||||
# L1 Mel-Spectrogram Loss
|
|
||||||
loss_mel = F.l1_loss(y_mel, y_g_hat_mel) * 45
|
|
||||||
|
|
||||||
# sc_loss, mag_loss = stft_loss(y_g_hat[:, :, :y.size(2)].squeeze(1), y.squeeze(1))
|
|
||||||
# loss_mel = h.lambda_aux * (sc_loss + mag_loss) # STFT Loss
|
|
||||||
|
|
||||||
if steps > h.disc_start_step:
|
|
||||||
y_df_hat_r, y_df_hat_g, fmap_f_r, fmap_f_g = mpd(y, y_g_hat)
|
|
||||||
y_ds_hat_r, y_ds_hat_g, fmap_s_r, fmap_s_g = msd(y, y_g_hat)
|
|
||||||
loss_fm_f = feature_loss(fmap_f_r, fmap_f_g)
|
|
||||||
loss_fm_s = feature_loss(fmap_s_r, fmap_s_g)
|
|
||||||
loss_gen_f, losses_gen_f = generator_loss(y_df_hat_g)
|
|
||||||
loss_gen_s, losses_gen_s = generator_loss(y_ds_hat_g)
|
|
||||||
loss_gen_all = loss_gen_s + loss_gen_f + (2 * (loss_fm_s + loss_fm_f)) + loss_mel
|
|
||||||
else:
|
|
||||||
loss_gen_all = loss_mel
|
|
||||||
|
|
||||||
loss_gen_all.backward()
|
|
||||||
optim_g.step()
|
|
||||||
|
|
||||||
if rank == 0:
|
|
||||||
# STDOUT logging
|
|
||||||
if steps % a.stdout_interval == 0:
|
|
||||||
with torch.no_grad():
|
|
||||||
mel_error = F.l1_loss(y_mel, y_g_hat_mel).item()
|
|
||||||
|
|
||||||
print('Steps : {:d}, Gen Loss Total : {:4.3f}, Mel-Spec. Error : {:4.3f}, s/b : {:4.3f}'.
|
|
||||||
format(steps, loss_gen_all, mel_error, time.time() - start_b))
|
|
||||||
|
|
||||||
# checkpointing
|
|
||||||
if steps % a.checkpoint_interval == 0 and steps != 0:
|
|
||||||
checkpoint_path = "{}/g_fregan_{:08d}.pt".format(a.checkpoint_path, steps)
|
|
||||||
save_checkpoint(checkpoint_path,
|
|
||||||
{'generator': (generator.module if h.num_gpus > 1 else generator).state_dict()})
|
|
||||||
checkpoint_path = "{}/do_fregan_{:08d}.pt".format(a.checkpoint_path, steps)
|
|
||||||
save_checkpoint(checkpoint_path,
|
|
||||||
{'mpd': (mpd.module if h.num_gpus > 1
|
|
||||||
else mpd).state_dict(),
|
|
||||||
'msd': (msd.module if h.num_gpus > 1
|
|
||||||
else msd).state_dict(),
|
|
||||||
'optim_g': optim_g.state_dict(), 'optim_d': optim_d.state_dict(), 'steps': steps,
|
|
||||||
'epoch': epoch})
|
|
||||||
|
|
||||||
# Tensorboard summary logging
|
|
||||||
if steps % a.summary_interval == 0:
|
|
||||||
sw.add_scalar("training/gen_loss_total", loss_gen_all, steps)
|
|
||||||
sw.add_scalar("training/mel_spec_error", mel_error, steps)
|
|
||||||
|
|
||||||
# Validation
|
|
||||||
if steps % a.validation_interval == 0: # and steps != 0:
|
|
||||||
generator.eval()
|
|
||||||
torch.cuda.empty_cache()
|
|
||||||
val_err_tot = 0
|
|
||||||
with torch.no_grad():
|
|
||||||
for j, batch in enumerate(validation_loader):
|
|
||||||
x, y, _, y_mel = batch
|
|
||||||
y_g_hat = generator(x.to(device))
|
|
||||||
y_mel = torch.autograd.Variable(y_mel.to(device, non_blocking=True))
|
|
||||||
y_g_hat_mel = mel_spectrogram(y_g_hat.squeeze(1), h.n_fft, h.num_mels, h.sampling_rate,
|
|
||||||
h.hop_size, h.win_size,
|
|
||||||
h.fmin, h.fmax_for_loss)
|
|
||||||
#val_err_tot += F.l1_loss(y_mel, y_g_hat_mel).item()
|
|
||||||
|
|
||||||
if j <= 4:
|
|
||||||
if steps == 0:
|
|
||||||
sw.add_audio('gt/y_{}'.format(j), y[0], steps, h.sampling_rate)
|
|
||||||
sw.add_figure('gt/y_spec_{}'.format(j), plot_spectrogram(x[0]), steps)
|
|
||||||
|
|
||||||
sw.add_audio('generated/y_hat_{}'.format(j), y_g_hat[0], steps, h.sampling_rate)
|
|
||||||
y_hat_spec = mel_spectrogram(y_g_hat.squeeze(1), h.n_fft, h.num_mels,
|
|
||||||
h.sampling_rate, h.hop_size, h.win_size,
|
|
||||||
h.fmin, h.fmax)
|
|
||||||
sw.add_figure('generated/y_hat_spec_{}'.format(j),
|
|
||||||
plot_spectrogram(y_hat_spec.squeeze(0).cpu().numpy()), steps)
|
|
||||||
|
|
||||||
val_err = val_err_tot / (j + 1)
|
|
||||||
sw.add_scalar("validation/mel_spec_error", val_err, steps)
|
|
||||||
|
|
||||||
generator.train()
|
|
||||||
|
|
||||||
steps += 1
|
|
||||||
|
|
||||||
scheduler_g.step()
|
|
||||||
scheduler_d.step()
|
|
||||||
|
|
||||||
if rank == 0:
|
|
||||||
print('Time taken for epoch {} is {} sec\n'.format(epoch + 1, int(time.time() - start)))
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
import glob
|
|
||||||
import os
|
|
||||||
import matplotlib
|
|
||||||
import torch
|
|
||||||
from torch.nn.utils import weight_norm
|
|
||||||
matplotlib.use("Agg")
|
|
||||||
import matplotlib.pylab as plt
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
|
|
||||||
def build_env(config, config_name, path):
|
|
||||||
t_path = os.path.join(path, config_name)
|
|
||||||
if config != t_path:
|
|
||||||
os.makedirs(path, exist_ok=True)
|
|
||||||
shutil.copyfile(config, os.path.join(path, config_name))
|
|
||||||
|
|
||||||
|
|
||||||
def plot_spectrogram(spectrogram):
|
|
||||||
fig, ax = plt.subplots(figsize=(10, 2))
|
|
||||||
im = ax.imshow(spectrogram, aspect="auto", origin="lower",
|
|
||||||
interpolation='none')
|
|
||||||
plt.colorbar(im, ax=ax)
|
|
||||||
|
|
||||||
fig.canvas.draw()
|
|
||||||
plt.close()
|
|
||||||
|
|
||||||
return fig
|
|
||||||
|
|
||||||
|
|
||||||
def apply_weight_norm(m):
|
|
||||||
classname = m.__class__.__name__
|
|
||||||
if classname.find("Conv") != -1:
|
|
||||||
weight_norm(m)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def load_checkpoint(filepath, device):
|
|
||||||
assert os.path.isfile(filepath)
|
|
||||||
print("Loading '{}'".format(filepath))
|
|
||||||
checkpoint_dict = torch.load(filepath, map_location=device)
|
|
||||||
print("Complete.")
|
|
||||||
return checkpoint_dict
|
|
||||||
|
|
||||||
|
|
||||||
def save_checkpoint(filepath, obj):
|
|
||||||
print("Saving checkpoint to {}".format(filepath))
|
|
||||||
torch.save(obj, filepath)
|
|
||||||
print("Complete.")
|
|
||||||
|
|
||||||
|
|
||||||
def scan_checkpoint(cp_dir, prefix):
|
|
||||||
pattern = os.path.join(cp_dir, prefix + '????????.pt')
|
|
||||||
cp_list = glob.glob(pattern)
|
|
||||||
if len(cp_list) == 0:
|
|
||||||
return None
|
|
||||||
return sorted(cp_list)[-1]
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
#
|
|
||||||
@@ -15,7 +15,7 @@ from .rnn_decoder_mol import Decoder
|
|||||||
from .utils.cnn_postnet import Postnet
|
from .utils.cnn_postnet import Postnet
|
||||||
from .utils.vc_utils import get_mask_from_lengths
|
from .utils.vc_utils import get_mask_from_lengths
|
||||||
|
|
||||||
from utils.hparams import HpsYaml
|
from utils.load_yaml import HpsYaml
|
||||||
|
|
||||||
class MelDecoderMOLv2(AbsMelDecoder):
|
class MelDecoderMOLv2(AbsMelDecoder):
|
||||||
"""Use an encoder to preprocess ppg."""
|
"""Use an encoder to preprocess ppg."""
|
||||||
@@ -7,10 +7,10 @@ from pathlib import Path
|
|||||||
import soundfile
|
import soundfile
|
||||||
import resampy
|
import resampy
|
||||||
|
|
||||||
from models.ppg_extractor import load_model
|
from ppg_extractor import load_model
|
||||||
import encoder.inference as Encoder
|
import encoder.inference as Encoder
|
||||||
from models.encoder.audio import preprocess_wav
|
from encoder.audio import preprocess_wav
|
||||||
from models.encoder import audio
|
from encoder import audio
|
||||||
from utils.f0_utils import compute_f0
|
from utils.f0_utils import compute_f0
|
||||||
|
|
||||||
from torch.multiprocessing import Pool, cpu_count
|
from torch.multiprocessing import Pool, cpu_count
|
||||||
@@ -2,8 +2,8 @@ import sys
|
|||||||
import torch
|
import torch
|
||||||
import argparse
|
import argparse
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from utils.hparams import HpsYaml
|
from utils.load_yaml import HpsYaml
|
||||||
from models.ppg2mel.train.train_linglf02mel_seq2seq_oneshotvc import Solver
|
from ppg2mel.train.train_linglf02mel_seq2seq_oneshotvc import Solver
|
||||||
|
|
||||||
# For reproducibility, comment these may speed up training
|
# For reproducibility, comment these may speed up training
|
||||||
torch.backends.cudnn.deterministic = True
|
torch.backends.cudnn.deterministic = True
|
||||||
@@ -8,6 +8,7 @@ from torch.utils.tensorboard import SummaryWriter
|
|||||||
|
|
||||||
from .option import default_hparas
|
from .option import default_hparas
|
||||||
from utils.util import human_format, Timer
|
from utils.util import human_format, Timer
|
||||||
|
from utils.load_yaml import HpsYaml
|
||||||
|
|
||||||
|
|
||||||
class BaseSolver():
|
class BaseSolver():
|
||||||
@@ -14,7 +14,7 @@ from utils.data_load import OneshotVcDataset, MultiSpkVcCollate
|
|||||||
from .loss import MaskedMSELoss
|
from .loss import MaskedMSELoss
|
||||||
from .optim import Optimizer
|
from .optim import Optimizer
|
||||||
from utils.util import human_format
|
from utils.util import human_format
|
||||||
from models.ppg2mel import MelDecoderMOLv2
|
from ppg2mel import MelDecoderMOLv2
|
||||||
|
|
||||||
|
|
||||||
class Solver(BaseSolver):
|
class Solver(BaseSolver):
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user