Views
No views yet
git clone https://github.com/open-mmlab/Amphion.git1from Amphion.models.codec.ns3_codec import FACodecEncoder, FACodecDecoder
2from huggingface_hub import hf_hub_download
3
4fa_encoder = FACodecEncoder(
5 ngf=32,
6 up_ratios=[2, 4, 5, 5],
7 out_channels=256,
8)
9
10fa_decoder = FACodecDecoder(
11 in_channels=256,
12 upsample_initial_channel=1024,
13 ngf=32,
14 up_ratios=[5, 5, 4, 2],
15 vq_num_q_c=2,
16 vq_num_q_p=1,
17 vq_num_q_r=3,
18 vq_dim=256,
19 codebook_dim=8,
20 codebook_size_prosody=10,
21 codebook_size_content=10,
22 codebook_size_residual=10,
23 use_gr_x_timbre=True,
24 use_gr_residual_f0=True,
25 use_gr_residual_phone=True,
26)
27
28encoder_ckpt = hf_hub_download(repo_id="amphion/naturalspeech3_facodec", filename="ns3_facodec_encoder.bin")
29decoder_ckpt = hf_hub_download(repo_id="amphion/naturalspeech3_facodec", filename="ns3_facodec_decoder.bin")
30
31fa_encoder.load_state_dict(torch.load(encoder_ckpt))
32fa_decoder.load_state_dict(torch.load(decoder_ckpt))
33
34fa_encoder.eval()
35fa_decoder.eval()
361test_wav_path = "test.wav"
2test_wav = librosa.load(test_wav_path, sr=16000)[0]
3test_wav = torch.from_numpy(test_wav).float()
4test_wav = test_wav.unsqueeze(0).unsqueeze(0)
5
6with torch.no_grad():
7
8 # encode
9 enc_out = fa_encoder(test_wav)
10 print(enc_out.shape)
11
12 # quantize
13 vq_post_emb, vq_id, _, quantized, spk_embs = fa_decoder(enc_out, eval_vq=False, vq=True)
14
15 # latent after quantization
16 print(vq_post_emb.shape)
17
18 # codes
19 print("vq id shape:", vq_id.shape)
20
21 # get prosody code
22 prosody_code = vq_id[:1]
23 print("prosody code shape:", prosody_code.shape)
24
25 # get content code
26 cotent_code = vq_id[1:3]
27 print("content code shape:", cotent_code.shape)
28
29 # get residual code (acoustic detail codes)
30 residual_code = vq_id[3:]
31 print("residual code shape:", residual_code.shape)
32
33 # speaker embedding
34 print("speaker embedding shape:", spk_embs.shape)
35
36 # decode (recommand)
37 recon_wav = fa_decoder.inference(vq_post_emb, spk_embs)
38 print(recon_wav.shape)
39 sf.write("recon.wav", recon_wav[0][0].cpu().numpy(), 16000)1from Amphion.models.codec.ns3_codec import FACodecEncoderV2, FACodecDecoderV2
2
3# Same parameters as FACodecEncoder/FACodecDecoder
4fa_encoder_v2 = FACodecEncoderV2(...)
5fa_decoder_v2 = FACodecDecoderV2(...)
6
7encoder_v2_ckpt = hf_hub_download(repo_id="amphion/naturalspeech3_facodec", filename="ns3_facodec_encoder_v2.bin")
8decoder_v2_ckpt = hf_hub_download(repo_id="amphion/naturalspeech3_facodec", filename="ns3_facodec_decoder_v2.bin")
9
10fa_encoder_v2.load_state_dict(torch.load(encoder_v2_ckpt))
11fa_decoder_v2.load_state_dict(torch.load(decoder_v2_ckpt))
12
13with torch.no_grad():
14 enc_out_a = fa_encoder_v2(wav_a)
15 prosody_a = fa_encoder_v2.get_prosody_feature(wav_a)
16 enc_out_b = fa_encoder_v2(wav_b)
17 prosody_b = fa_encoder_v2.get_prosody_feature(wav_b)
18
19 vq_post_emb_a, vq_id_a, _, quantized, spk_embs_a = fa_decoder_v2(
20 enc_out_a, prosody_a, eval_vq=False, vq=True
21 )
22 vq_post_emb_b, vq_id_b, _, quantized, spk_embs_b = fa_decoder_v2(
23 enc_out_b, prosody_b, eval_vq=False, vq=True
24 )
25
26 vq_post_emb_a_to_b = fa_decoder_v2.vq2emb(vq_id_a, use_residual=False)
27 recon_wav_a_to_b = fa_decoder_v2.inference(vq_post_emb_a_to_b, spk_embs_b)1from Amphion.models.codec.ns3_codec import FACodecRedecoder
2
3fa_redecoder = FACodecRedecoder()
4
5redecoder_ckpt = hf_hub_download(repo_id="amphion/naturalspeech3_facodec", filename="ns3_facodec_redecoder.bin")
6
7fa_redecoder.load_state_dict(torch.load(redecoder_ckpt))
8
9with torch.no_grad():
10 enc_out_a = fa_encoder(wav_a)
11 enc_out_b = fa_encoder(wav_b)
12
13 vq_post_emb_a, vq_id_a, _, quantized_a, spk_embs_a = fa_decoder(enc_out_a, eval_vq=False, vq=True)
14 vq_post_emb_b, vq_id_b, _, quantized_b, spk_embs_b = fa_decoder(enc_out_b, eval_vq=False, vq=True)
15
16 # convert speaker
17 vq_post_emb_a_to_b = fa_redecoder.vq2emb(vq_id_a, spk_embs_b, use_residual=False)
18 recon_wav_a_to_b = fa_redecoder.inference(vq_post_emb_a_to_b, spk_embs_b)
19
20 sf.write("recon_a_to_b.wav", recon_wav_a_to_b[0][0].cpu().numpy(), 16000)1@article{ju2024naturalspeech,
2 title={NaturalSpeech 3: Zero-Shot Speech Synthesis with Factorized Codec and Diffusion Models},
3 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},
4 journal={arXiv preprint arXiv:2403.03100},
5 year={2024}
6}
7
8@article{zhang2023amphion,
9 title={Amphion: An Open-Source Audio, Music and Speech Generation Toolkit},
10 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},
11 journal={arXiv},
12 year={2024},
13 volume={abs/2312.09911}
14}