-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NaturalSpeech3 FACodec Release (#152)
NaturalSpeech3 FACodec: code and pretrained models
- Loading branch information
1 parent
e62c46e
commit 8064cdd
Showing
15 changed files
with
1,625 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
## FACodec: Speech Codec with Attribute Factorization used for NaturalSpeech 3 | ||
|
||
[![arXiv](https://img.shields.io/badge/arXiv-Paper-<COLOR>.svg)](https://arxiv.org/pdf/2403.03100.pdf) | ||
[![demo](https://img.shields.io/badge/FACodec-Demo-red)](https://speechresearch.github.io/naturalspeech3/) | ||
[![model](https://img.shields.io/badge/%F0%9F%A4%97%20HuggingFace-Models-pink)](https://huggingface.co/amphion/naturalspeech3_facodec) | ||
[![hf](https://img.shields.io/badge/%F0%9F%A4%97%20HuggingFace-Spaces-yellow)](https://huggingface.co/spaces/amphion/naturalspeech3_facodec) | ||
|
||
## Overview | ||
|
||
FACodec is a core component of the advanced text-to-speech (TTS) model NaturalSpeech 3. FACodec converts complex speech waveform into disentangled subspaces representing speech attributes of content, prosody, timbre, and acoustic details and reconstruct high-quality speech waveform from these attributes. FACodec decomposes complex speech into subspaces representing different attributes, thus simplifying the modeling of speech representation. | ||
|
||
Research can use FACodec to develop different modes of TTS models, such as non-autoregressive based discrete diffusion (NaturalSpeech 3) or autoregressive models (like VALL-E). | ||
|
||
<br> | ||
<div align="center"> | ||
<img src="../../imgs/ns3/ns3_overview.png" width="65%"> | ||
</div> | ||
<br> | ||
|
||
<br> | ||
<div align="center"> | ||
<img src="../../imgs/ns3/ns3_facodec.png" width="100%"> | ||
</div> | ||
<br> | ||
|
||
## Useage | ||
|
||
Download the pre-trained FACodec model from HuggingFace: [Pretrained FACodec checkpoint](https://huggingface.co/amphion/naturalspeech3_facodec) | ||
|
||
Install Amphion | ||
```bash | ||
git clone https://github.com/open-mmlab/Amphion.git | ||
``` | ||
|
||
Few lines of code to use the pre-trained FACodec model | ||
```python | ||
from Amphion.models.codec.ns3_codec import FACodecEncoder, FACodecDecoder | ||
from huggingface_hub import hf_hub_download | ||
|
||
fa_encoder = FACodecEncoder( | ||
ngf=32, | ||
up_ratios=[2, 4, 5, 5], | ||
out_channels=256, | ||
) | ||
|
||
fa_decoder = FACodecDecoder( | ||
in_channels=256, | ||
upsample_initial_channel=1024, | ||
ngf=32, | ||
up_ratios=[5, 5, 4, 2], | ||
vq_num_q_c=2, | ||
vq_num_q_p=1, | ||
vq_num_q_r=3, | ||
vq_dim=256, | ||
codebook_dim=8, | ||
codebook_size_prosody=10, | ||
codebook_size_content=10, | ||
codebook_size_residual=10, | ||
use_gr_x_timbre=True, | ||
use_gr_residual_f0=True, | ||
use_gr_residual_phone=True, | ||
) | ||
|
||
encoder_ckpt = hf_hub_download(repo_id="amphion/naturalspeech3_facodec", filename="ns3_facodec_encoder.bin") | ||
decoder_ckpt = hf_hub_download(repo_id="amphion/naturalspeech3_facodec", filename="ns3_facodec_decoder.bin") | ||
|
||
fa_encoder.load_state_dict(torch.load(encoder_ckpt)) | ||
fa_decoder.load_state_dict(torch.load(decoder_ckpt)) | ||
|
||
fa_encoder.eval() | ||
fa_decoder.eval() | ||
|
||
``` | ||
|
||
Inference | ||
```python | ||
test_wav_path = "test.wav" | ||
test_wav = librosa.load(test_wav_path, sr=16000)[0] | ||
test_wav = torch.from_numpy(test_wav).float() | ||
test_wav = test_wav.unsqueeze(0).unsqueeze(0) | ||
|
||
with torch.no_grad(): | ||
|
||
# encode | ||
enc_out = fa_encoder(test_wav) | ||
print(enc_out.shape) | ||
|
||
# quantize | ||
vq_post_emb, vq_id, _, quantized, spk_embs = fa_decoder(enc_out, eval_vq=False, vq=True) | ||
|
||
# latent after quantization | ||
print(vq_post_emb.shape) | ||
|
||
# codes | ||
print("vq id shape:", vq_id.shape) | ||
|
||
# get prosody code | ||
prosody_code = vq_id[:1] | ||
print("prosody code shape:", prosody_code.shape) | ||
|
||
# get content code | ||
cotent_code = vq_id[1:3] | ||
print("content code shape:", cotent_code.shape) | ||
|
||
# get residual code (acoustic detail codes) | ||
residual_code = vq_id[3:] | ||
print("residual code shape:", residual_code.shape) | ||
|
||
# speaker embedding | ||
print("speaker embedding shape:", spk_embs.shape) | ||
|
||
# decode (recommand) | ||
recon_wav = fa_decoder.inference(vq_post_emb, spk_embs) | ||
print(recon_wav.shape) | ||
sf.write("recon.wav", recon_wav[0][0].cpu().numpy(), 16000) | ||
``` | ||
|
||
FACodec can achieve zero-shot voice conversion with FACodecRedecoder | ||
```python | ||
from Amphion.models.codec.ns3_codec import FACodecRedecoder | ||
|
||
fa_redecoder = FACodecRedecoder() | ||
|
||
redecoder_ckpt = hf_hub_download(repo_id="amphion/naturalspeech3_facodec", filename="ns3_facodec_redecoder.bin") | ||
|
||
fa_redecoder.load_state_dict(torch.load(redecoder_ckpt)) | ||
|
||
with torch.no_grad(): | ||
enc_out_a = fa_encoder(wav_a) | ||
enc_out_b = fa_encoder(wav_b) | ||
|
||
vq_post_emb_a, vq_id_a, _, quantized_a, spk_embs_a = fa_decoder(enc_out_a, eval_vq=False, vq=True) | ||
vq_post_emb_b, vq_id_b, _, quantized_b, spk_embs_b = fa_decoder(enc_out_b, eval_vq=False, vq=True) | ||
|
||
# convert speaker | ||
vq_post_emb_a_to_b = fa_redecoder.vq2emb(vq_id_a, spk_embs_b, use_residual=False) | ||
recon_wav_a_to_b = fa_redecoder.inference(vq_post_emb_a_to_b, spk_embs_b) | ||
|
||
sf.write("recon_a_to_b.wav", recon_wav_a_to_b[0][0].cpu().numpy(), 16000) | ||
``` | ||
|
||
## Q&A | ||
|
||
Q1: What audio sample rate does FACodec support? What is the hop size? How many codes will be generated for each frame? | ||
|
||
A1: FACodec supports 16KHz speech audio. The hop size is 200 samples, and (16000/200) * 6 (total number of codebooks) codes will be generated for each frame. | ||
|
||
Q2: Is it possible to train an autoregressive TTS model like VALL-E using FACodec? | ||
|
||
A2: Yes. In fact, the authors of NaturalSpeech 3 have already employ explore the autoregressive generative model for discrete token generation with FACodec. They use an autoregressive language model to generate prosody codes, followed by a non-autoregressive model to generate the remaining content and acoustic details codes. | ||
|
||
Q3: Is it possible to train a latent diffusion TTS model like NaturalSpeech2 using FACodec? | ||
|
||
A3: Yes. You can use the latent getted after quanzaition as the modelling target for the latent diffusion model. | ||
|
||
Q4: Can FACodec compress and reconstruct audio from other domains? Such as sound effects, music, etc. | ||
|
||
A4: Since FACodec is designed for speech, it may not be suitable for other audio domains. However, it is possible to use the FACodec model to compress and reconstruct audio from other domains, but the quality may not be as good as the original audio. | ||
|
||
Q5: Can FACodec be used for content feature for some other tasks like voice conversion? | ||
|
||
A5: I think the answer is yes. Researchers can use the content code of FACodec as the content feature for voice conversion. We hope to see more research in this direction. | ||
|
||
## Citations | ||
|
||
If you use our FACodec model, please cite the following paper: | ||
|
||
```bibtex | ||
@article{ju2024naturalspeech, | ||
title={NaturalSpeech 3: Zero-Shot Speech Synthesis with Factorized Codec and Diffusion Models}, | ||
author={Ju, Zeqian and Wang, Yuancheng and Shen, Kai and Tan, Xu and Xin, Detai and Yang, Dongchao and Liu, Yanqing and Leng, Yichong and Song, Kaitao and Tang, Siliang and others}, | ||
journal={arXiv preprint arXiv:2403.03100}, | ||
year={2024} | ||
} | ||
@article{zhang2023amphion, | ||
title={Amphion: An Open-Source Audio, Music and Speech Generation Toolkit}, | ||
author={Xueyao Zhang and Liumeng Xue and Yicheng Gu and Yuancheng Wang and Haorui He and Chaoren Wang and Xi Chen and Zihao Fang and Haopeng Chen and Junan Zhang and Tze Ying Tang and Lexiao Zou and Mingxuan Wang and Jun Han and Kai Chen and Haizhou Li and Zhizheng Wu}, | ||
journal={arXiv}, | ||
year={2024}, | ||
volume={abs/2312.09911} | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Copyright (c) 2023 Amphion. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from .facodec import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0 | ||
|
||
from .filter import * | ||
from .resample import * | ||
from .act import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0 | ||
|
||
import torch.nn as nn | ||
from .resample import UpSample1d, DownSample1d | ||
|
||
|
||
class Activation1d(nn.Module): | ||
def __init__( | ||
self, | ||
activation, | ||
up_ratio: int = 2, | ||
down_ratio: int = 2, | ||
up_kernel_size: int = 12, | ||
down_kernel_size: int = 12, | ||
): | ||
super().__init__() | ||
self.up_ratio = up_ratio | ||
self.down_ratio = down_ratio | ||
self.act = activation | ||
self.upsample = UpSample1d(up_ratio, up_kernel_size) | ||
self.downsample = DownSample1d(down_ratio, down_kernel_size) | ||
|
||
# x: [B,C,T] | ||
def forward(self, x): | ||
x = self.upsample(x) | ||
x = self.act(x) | ||
x = self.downsample(x) | ||
|
||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0 | ||
|
||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
import math | ||
|
||
if "sinc" in dir(torch): | ||
sinc = torch.sinc | ||
else: | ||
# This code is adopted from adefossez's julius.core.sinc under the MIT License | ||
# https://adefossez.github.io/julius/julius/core.html | ||
def sinc(x: torch.Tensor): | ||
""" | ||
Implementation of sinc, i.e. sin(pi * x) / (pi * x) | ||
__Warning__: Different to julius.sinc, the input is multiplied by `pi`! | ||
""" | ||
return torch.where( | ||
x == 0, | ||
torch.tensor(1.0, device=x.device, dtype=x.dtype), | ||
torch.sin(math.pi * x) / math.pi / x, | ||
) | ||
|
||
|
||
# This code is adopted from adefossez's julius.lowpass.LowPassFilters under the MIT License | ||
# https://adefossez.github.io/julius/julius/lowpass.html | ||
def kaiser_sinc_filter1d( | ||
cutoff, half_width, kernel_size | ||
): # return filter [1,1,kernel_size] | ||
even = kernel_size % 2 == 0 | ||
half_size = kernel_size // 2 | ||
|
||
# For kaiser window | ||
delta_f = 4 * half_width | ||
A = 2.285 * (half_size - 1) * math.pi * delta_f + 7.95 | ||
if A > 50.0: | ||
beta = 0.1102 * (A - 8.7) | ||
elif A >= 21.0: | ||
beta = 0.5842 * (A - 21) ** 0.4 + 0.07886 * (A - 21.0) | ||
else: | ||
beta = 0.0 | ||
window = torch.kaiser_window(kernel_size, beta=beta, periodic=False) | ||
|
||
# ratio = 0.5/cutoff -> 2 * cutoff = 1 / ratio | ||
if even: | ||
time = torch.arange(-half_size, half_size) + 0.5 | ||
else: | ||
time = torch.arange(kernel_size) - half_size | ||
if cutoff == 0: | ||
filter_ = torch.zeros_like(time) | ||
else: | ||
filter_ = 2 * cutoff * window * sinc(2 * cutoff * time) | ||
# Normalize filter to have sum = 1, otherwise we will have a small leakage | ||
# of the constant component in the input signal. | ||
filter_ /= filter_.sum() | ||
filter = filter_.view(1, 1, kernel_size) | ||
|
||
return filter | ||
|
||
|
||
class LowPassFilter1d(nn.Module): | ||
def __init__( | ||
self, | ||
cutoff=0.5, | ||
half_width=0.6, | ||
stride: int = 1, | ||
padding: bool = True, | ||
padding_mode: str = "replicate", | ||
kernel_size: int = 12, | ||
): | ||
# kernel_size should be even number for stylegan3 setup, | ||
# in this implementation, odd number is also possible. | ||
super().__init__() | ||
if cutoff < -0.0: | ||
raise ValueError("Minimum cutoff must be larger than zero.") | ||
if cutoff > 0.5: | ||
raise ValueError("A cutoff above 0.5 does not make sense.") | ||
self.kernel_size = kernel_size | ||
self.even = kernel_size % 2 == 0 | ||
self.pad_left = kernel_size // 2 - int(self.even) | ||
self.pad_right = kernel_size // 2 | ||
self.stride = stride | ||
self.padding = padding | ||
self.padding_mode = padding_mode | ||
filter = kaiser_sinc_filter1d(cutoff, half_width, kernel_size) | ||
self.register_buffer("filter", filter) | ||
|
||
# input [B, C, T] | ||
def forward(self, x): | ||
_, C, _ = x.shape | ||
|
||
if self.padding: | ||
x = F.pad(x, (self.pad_left, self.pad_right), mode=self.padding_mode) | ||
out = F.conv1d(x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C) | ||
|
||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0 | ||
|
||
import torch.nn as nn | ||
from torch.nn import functional as F | ||
from .filter import LowPassFilter1d | ||
from .filter import kaiser_sinc_filter1d | ||
|
||
|
||
class UpSample1d(nn.Module): | ||
def __init__(self, ratio=2, kernel_size=None): | ||
super().__init__() | ||
self.ratio = ratio | ||
self.kernel_size = ( | ||
int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size | ||
) | ||
self.stride = ratio | ||
self.pad = self.kernel_size // ratio - 1 | ||
self.pad_left = self.pad * self.stride + (self.kernel_size - self.stride) // 2 | ||
self.pad_right = ( | ||
self.pad * self.stride + (self.kernel_size - self.stride + 1) // 2 | ||
) | ||
filter = kaiser_sinc_filter1d( | ||
cutoff=0.5 / ratio, half_width=0.6 / ratio, kernel_size=self.kernel_size | ||
) | ||
self.register_buffer("filter", filter) | ||
|
||
# x: [B, C, T] | ||
def forward(self, x): | ||
_, C, _ = x.shape | ||
|
||
x = F.pad(x, (self.pad, self.pad), mode="replicate") | ||
x = self.ratio * F.conv_transpose1d( | ||
x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C | ||
) | ||
x = x[..., self.pad_left : -self.pad_right] | ||
|
||
return x | ||
|
||
|
||
class DownSample1d(nn.Module): | ||
def __init__(self, ratio=2, kernel_size=None): | ||
super().__init__() | ||
self.ratio = ratio | ||
self.kernel_size = ( | ||
int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size | ||
) | ||
self.lowpass = LowPassFilter1d( | ||
cutoff=0.5 / ratio, | ||
half_width=0.6 / ratio, | ||
stride=ratio, | ||
kernel_size=self.kernel_size, | ||
) | ||
|
||
def forward(self, x): | ||
xx = self.lowpass(x) | ||
|
||
return xx |
Oops, something went wrong.