Commit
·
8dd1c70
verified
·
0
Parent(s):
Duplicate from HKUSTAudio/xcodec2
Browse filesCo-authored-by: HKUST Audio <HKUST-Audio@users.noreply.huggingface.co>
This view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +35 -0
- README.md +69 -0
- __init__.py +0 -0
- __pycache__/configuration_bigcodec.cpython-39.pyc +0 -0
- __pycache__/modeling_bigcodec.cpython-39.pyc +0 -0
- __pycache__/modeling_xcodec2.cpython-39.pyc +0 -0
- ckpt/epoch=4-step=1400000.ckpt +3 -0
- config.json +11 -0
- configuration_bigcodec.py +19 -0
- model.safetensors +3 -0
- modeling_xcodec2.py +165 -0
- module.py +0 -0
- pytorch_model.bin +3 -0
- reconstructed.wav +0 -0
- test.flac +0 -0
- test.py +21 -0
- vq/__init__.py +4 -0
- vq/__pycache__/__init__.cpython-310.pyc +0 -0
- vq/__pycache__/__init__.cpython-311.pyc +0 -0
- vq/__pycache__/__init__.cpython-312.pyc +0 -0
- vq/__pycache__/__init__.cpython-38.pyc +0 -0
- vq/__pycache__/__init__.cpython-39.pyc +0 -0
- vq/__pycache__/activations.cpython-310.pyc +0 -0
- vq/__pycache__/activations.cpython-311.pyc +0 -0
- vq/__pycache__/activations.cpython-312.pyc +0 -0
- vq/__pycache__/activations.cpython-38.pyc +0 -0
- vq/__pycache__/activations.cpython-39.pyc +0 -0
- vq/__pycache__/blocks.cpython-310.pyc +0 -0
- vq/__pycache__/blocks.cpython-39.pyc +0 -0
- vq/__pycache__/bs_roformer5.cpython-310.pyc +0 -0
- vq/__pycache__/bs_roformer5.cpython-38.pyc +0 -0
- vq/__pycache__/bs_roformer5.cpython-39.pyc +0 -0
- vq/__pycache__/codec_decoder.cpython-310.pyc +0 -0
- vq/__pycache__/codec_decoder.cpython-311.pyc +0 -0
- vq/__pycache__/codec_decoder.cpython-312.pyc +0 -0
- vq/__pycache__/codec_decoder.cpython-39.pyc +0 -0
- vq/__pycache__/codec_decoder_vocos.cpython-310.pyc +0 -0
- vq/__pycache__/codec_decoder_vocos.cpython-311.pyc +0 -0
- vq/__pycache__/codec_decoder_vocos.cpython-312.pyc +0 -0
- vq/__pycache__/codec_decoder_vocos.cpython-39.pyc +0 -0
- vq/__pycache__/codec_encoder.cpython-310.pyc +0 -0
- vq/__pycache__/codec_encoder.cpython-311.pyc +0 -0
- vq/__pycache__/codec_encoder.cpython-312.pyc +0 -0
- vq/__pycache__/codec_encoder.cpython-38.pyc +0 -0
- vq/__pycache__/codec_encoder.cpython-39.pyc +0 -0
- vq/__pycache__/factorized_vector_quantize.cpython-310.pyc +0 -0
- vq/__pycache__/factorized_vector_quantize.cpython-311.pyc +0 -0
- vq/__pycache__/factorized_vector_quantize.cpython-312.pyc +0 -0
- vq/__pycache__/factorized_vector_quantize.cpython-39.pyc +0 -0
- vq/__pycache__/module.cpython-310.pyc +0 -0
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc-by-nc-4.0
|
| 3 |
+
tags:
|
| 4 |
+
- audio-to-audio
|
| 5 |
+
pipeline_tag: audio-to-audio
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
[](https://arxiv.org/abs/2502.04128)
|
| 9 |
+
**Update (2025-02-13):** Add [Llasa finetune instruction](https://github.com/zhenye234/LLaSA_training/tree/main/finetune).
|
| 10 |
+
|
| 11 |
+
**Update (2025-02-07):** Our paper has been released!
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
## Paper
|
| 15 |
+
|
| 16 |
+
LLaSA: Scaling Train Time and Inference Time Compute for LLaMA based Speech Synthesis
|
| 17 |
+
|
| 18 |
+
Codec Does Matter: Exploring the Semantic Shortcoming of Codec for Audio Language Model (AAAI 2025, xcodec 1.0)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Getting Started with XCodec2 on Hugging Face
|
| 22 |
+
XCodec2 is a speech tokenizer that offers the following key features:
|
| 23 |
+
|
| 24 |
+
1. **Single Vector Quantization**
|
| 25 |
+
2. **50 Tokens per Second**
|
| 26 |
+
3. **Multilingual Speech Semantic Support and High-Quality Speech Reconstruction**
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
To use `xcodec2`, ensure you have it installed. You can install it using the following command:
|
| 30 |
+
|
| 31 |
+
```bash
|
| 32 |
+
conda create -n xcodec2 python=3.9
|
| 33 |
+
conda activate xcodec2
|
| 34 |
+
pip install xcodec2 (Use `xcodec2==0.1.5` for codec inference and llasa fine-tuning. I’ve removed unnecessary dependencies, and it works fine in my testing. However, I’m not sure if other problems may arise. If you prefer more stability, I recommend using `xcodec2==0.1.3` which accurately aligns during my codec training.)
|
| 35 |
+
|
| 36 |
+
```
|
| 37 |
+
Then,
|
| 38 |
+
```python
|
| 39 |
+
import torch
|
| 40 |
+
import soundfile as sf
|
| 41 |
+
from transformers import AutoConfig
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
from xcodec2.modeling_xcodec2 import XCodec2Model
|
| 45 |
+
|
| 46 |
+
model_path = "HKUSTAudio/xcodec2"
|
| 47 |
+
|
| 48 |
+
model = XCodec2Model.from_pretrained(model_path)
|
| 49 |
+
model.eval().cuda()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
wav, sr = sf.read("test.wav")
|
| 53 |
+
wav_tensor = torch.from_numpy(wav).float().unsqueeze(0) # Shape: (1, T)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
# Only 16khz speech
|
| 58 |
+
# Only supports single input. For batch inference, please refer to the link below.
|
| 59 |
+
vq_code = model.encode_code(input_waveform=wav_tensor)
|
| 60 |
+
print("Code:", vq_code )
|
| 61 |
+
|
| 62 |
+
recon_wav = model.decode_code(vq_code).cpu() # Shape: (1, 1, T')
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
sf.write("reconstructed.wav", recon_wav[0, 0, :].numpy(), sr)
|
| 66 |
+
print("Done! Check reconstructed.wav")
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
# If you want to train your own xcodec2, batch inference, or large-scale code extraction, the code is released [here](https://github.com/zhenye234/X-Codec-2.0).
|
__init__.py
ADDED
|
File without changes
|
__pycache__/configuration_bigcodec.cpython-39.pyc
ADDED
|
Binary file (743 Bytes). View file
|
|
|
__pycache__/modeling_bigcodec.cpython-39.pyc
ADDED
|
Binary file (2.6 kB). View file
|
|
|
__pycache__/modeling_xcodec2.cpython-39.pyc
ADDED
|
Binary file (4.07 kB). View file
|
|
|
ckpt/epoch=4-step=1400000.ckpt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:02d8c58b95ac84de701a94e351b6981523bd106cbd05863a09cc56dd148c689e
|
| 3 |
+
size 8306367217
|
config.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "xcodec2",
|
| 3 |
+
"semantic_hidden_size": 1024,
|
| 4 |
+
"codec_encoder_hidden_size": 1024,
|
| 5 |
+
"codec_decoder_hidden_size": 1024,
|
| 6 |
+
"use_vocos": true,
|
| 7 |
+
"architectures": [
|
| 8 |
+
"XCodec2Model"
|
| 9 |
+
]
|
| 10 |
+
}
|
| 11 |
+
|
configuration_bigcodec.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
class BigCodecConfig(PretrainedConfig):
|
| 4 |
+
model_type = "bigcodec"
|
| 5 |
+
|
| 6 |
+
def __init__(
|
| 7 |
+
self,
|
| 8 |
+
# 下面这些只是示例超参
|
| 9 |
+
semantic_hidden_size=1024,
|
| 10 |
+
codec_encoder_hidden_size=1024,
|
| 11 |
+
codec_decoder_hidden_size=1024,
|
| 12 |
+
use_vocos=True,
|
| 13 |
+
**kwargs
|
| 14 |
+
):
|
| 15 |
+
super().__init__(**kwargs)
|
| 16 |
+
self.semantic_hidden_size = semantic_hidden_size
|
| 17 |
+
self.codec_encoder_hidden_size = codec_encoder_hidden_size
|
| 18 |
+
self.codec_decoder_hidden_size = codec_decoder_hidden_size
|
| 19 |
+
self.use_vocos = use_vocos
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a27b1544a394b9aff722da55366281f00480a5479fe5cbfc84f2e2e74eab4db0
|
| 3 |
+
size 3291104560
|
modeling_xcodec2.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import PreTrainedModel
|
| 4 |
+
from configuration_bigcodec import BigCodecConfig
|
| 5 |
+
|
| 6 |
+
# 请确保这些模块路径是正确的
|
| 7 |
+
from vq.codec_encoder import CodecEncoder_Transformer
|
| 8 |
+
from vq.codec_decoder_vocos import CodecDecoderVocos
|
| 9 |
+
from vq.module import SemanticEncoder
|
| 10 |
+
from transformers import AutoFeatureExtractor, Wav2Vec2BertModel
|
| 11 |
+
|
| 12 |
+
class XCodec2Model(PreTrainedModel):
|
| 13 |
+
config_class = BigCodecConfig
|
| 14 |
+
|
| 15 |
+
def __init__(self, config: BigCodecConfig):
|
| 16 |
+
super().__init__(config)
|
| 17 |
+
|
| 18 |
+
# 1) 语义模型
|
| 19 |
+
self.semantic_model = Wav2Vec2BertModel.from_pretrained(
|
| 20 |
+
"facebook/w2v-bert-2.0",
|
| 21 |
+
output_hidden_states=True
|
| 22 |
+
)
|
| 23 |
+
self.semantic_model.eval()
|
| 24 |
+
|
| 25 |
+
self.SemanticEncoder_module = SemanticEncoder(
|
| 26 |
+
config.semantic_hidden_size,
|
| 27 |
+
config.semantic_hidden_size,
|
| 28 |
+
config.semantic_hidden_size
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# 2) Codec Encoder
|
| 32 |
+
self.CodecEnc = CodecEncoder_Transformer()
|
| 33 |
+
|
| 34 |
+
# 3) Codec Decoder
|
| 35 |
+
self.generator = CodecDecoderVocos()
|
| 36 |
+
|
| 37 |
+
# 4) 两个全连接层
|
| 38 |
+
self.fc_prior = nn.Linear(2048, 2048)
|
| 39 |
+
self.fc_post_a = nn.Linear(2048, 1024)
|
| 40 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/w2v-bert-2.0")
|
| 41 |
+
self.feature_extractor = feature_extractor
|
| 42 |
+
|
| 43 |
+
def forward(self, input_waveform, sample_rate=16000):
|
| 44 |
+
"""
|
| 45 |
+
这里的 forward 不一定要叫 forward,也可以拆成别的方法;
|
| 46 |
+
但是如果想兼容 pipeline,需要在 forward 里给出核心逻辑。
|
| 47 |
+
|
| 48 |
+
参数:
|
| 49 |
+
input_waveform: [batch_size, waveform_length]
|
| 50 |
+
sample_rate: 默认 16000
|
| 51 |
+
返回:
|
| 52 |
+
重构后的语音音频 (Tensor)
|
| 53 |
+
"""
|
| 54 |
+
# 1) 特征提取
|
| 55 |
+
# 如果需要 padding,可以在这里做
|
| 56 |
+
input_features = self.feature_extractor(
|
| 57 |
+
input_waveform,
|
| 58 |
+
sampling_rate=sample_rate,
|
| 59 |
+
return_tensors="pt"
|
| 60 |
+
).input_features.to(self.device) # [batch, frames, feat_dim]
|
| 61 |
+
|
| 62 |
+
# 2) 语义层
|
| 63 |
+
semantic_output = self.semantic_model(input_features)
|
| 64 |
+
semantic_hidden_16 = semantic_output.hidden_states[16] # 取第16层
|
| 65 |
+
semantic_hidden_16 = semantic_hidden_16.transpose(1, 2) # [batch, hidden_dim, frames]
|
| 66 |
+
semantic_encoded = self.SemanticEncoder_module(semantic_hidden_16)
|
| 67 |
+
|
| 68 |
+
# 3) codec encoder
|
| 69 |
+
wav = input_waveform.unsqueeze(1).to(self.device) # shape: [batch, 1, time]
|
| 70 |
+
vq_emb = self.CodecEnc(wav) # [batch, time//down, 1024] 只是示例
|
| 71 |
+
vq_emb = vq_emb.transpose(1, 2) # -> [batch, 1024, frames]
|
| 72 |
+
|
| 73 |
+
# 对齐语义向量的时间帧数,这里只做示例处理
|
| 74 |
+
# 真实做法里可能要先对齐维度
|
| 75 |
+
if vq_emb.shape[-1] != semantic_encoded.shape[-1]:
|
| 76 |
+
# 简单强行截断或补零都行,需要你自己决定
|
| 77 |
+
min_len = min(vq_emb.shape[-1], semantic_encoded.shape[-1])
|
| 78 |
+
vq_emb = vq_emb[:, :, :min_len]
|
| 79 |
+
semantic_encoded = semantic_encoded[:, :, :min_len]
|
| 80 |
+
|
| 81 |
+
# 4) 拼接
|
| 82 |
+
concat_emb = torch.cat([semantic_encoded, vq_emb], dim=1) # [batch, 1024 + 1024, frames]
|
| 83 |
+
|
| 84 |
+
# 5) fc_prior
|
| 85 |
+
concat_emb = self.fc_prior(concat_emb.transpose(1, 2)).transpose(1, 2)
|
| 86 |
+
|
| 87 |
+
# 6) decoder 的量化部分
|
| 88 |
+
_, vq_code, _ = self.generator(concat_emb, vq=True)
|
| 89 |
+
vq_post_emb = self.generator.quantizer.get_output_from_indices(vq_code.transpose(1, 2))
|
| 90 |
+
vq_post_emb = vq_post_emb.transpose(1, 2)
|
| 91 |
+
|
| 92 |
+
# 7) fc_post_a
|
| 93 |
+
vq_post_emb = self.fc_post_a(vq_post_emb.transpose(1, 2)).transpose(1, 2)
|
| 94 |
+
|
| 95 |
+
# 8) 最后解码成波形
|
| 96 |
+
recon_audio = self.generator(vq_post_emb.transpose(1, 2), vq=False)[0]
|
| 97 |
+
# recon_audio: [batch, time]
|
| 98 |
+
return recon_audio
|
| 99 |
+
|
| 100 |
+
def encode_code(self, input_waveform, sample_rate=16000):
|
| 101 |
+
"""
|
| 102 |
+
将输入的音频编码为代码表示。
|
| 103 |
+
|
| 104 |
+
参数:
|
| 105 |
+
input_waveform: [batch_size, waveform_length]
|
| 106 |
+
sample_rate: 默认 16000
|
| 107 |
+
返回:
|
| 108 |
+
编码后的代码 (Tensor)
|
| 109 |
+
"""
|
| 110 |
+
with torch.no_grad():
|
| 111 |
+
# 1) 特征提取
|
| 112 |
+
input_features = self.feature_extractor(
|
| 113 |
+
input_waveform,
|
| 114 |
+
sampling_rate=sample_rate,
|
| 115 |
+
return_tensors="pt"
|
| 116 |
+
).input_features.to(self.device) # [batch, frames, feat_dim]
|
| 117 |
+
|
| 118 |
+
# 2) 语义层
|
| 119 |
+
semantic_output = self.semantic_model(input_features)
|
| 120 |
+
semantic_hidden_16 = semantic_output.hidden_states[16] # 取第16层
|
| 121 |
+
semantic_hidden_16 = semantic_hidden_16.transpose(1, 2) # [batch, hidden_dim, frames]
|
| 122 |
+
semantic_encoded = self.SemanticEncoder_module(semantic_hidden_16)
|
| 123 |
+
|
| 124 |
+
# 3) codec encoder
|
| 125 |
+
wav = input_waveform.unsqueeze(1).to(self.device) # shape: [batch, 1, time]
|
| 126 |
+
vq_emb = self.CodecEnc(wav) # [batch, time//down, 1024] 只是示例
|
| 127 |
+
vq_emb = vq_emb.transpose(1, 2) # -> [batch, 1024, frames]
|
| 128 |
+
|
| 129 |
+
# 对齐语义向量的时间帧数,这里只做示例处理
|
| 130 |
+
if vq_emb.shape[-1] != semantic_encoded.shape[-1]:
|
| 131 |
+
min_len = min(vq_emb.shape[-1], semantic_encoded.shape[-1])
|
| 132 |
+
vq_emb = vq_emb[:, :, :min_len]
|
| 133 |
+
semantic_encoded = semantic_encoded[:, :, :min_len]
|
| 134 |
+
|
| 135 |
+
# 4) 拼接
|
| 136 |
+
concat_emb = torch.cat([semantic_encoded, vq_emb], dim=1) # [batch, 2048, frames]
|
| 137 |
+
|
| 138 |
+
# 5) fc_prior
|
| 139 |
+
concat_emb = self.fc_prior(concat_emb.transpose(1, 2)).transpose(1, 2)
|
| 140 |
+
|
| 141 |
+
# 6) decoder 的量化部分,获取code
|
| 142 |
+
_, vq_code, _ = self.generator(concat_emb, vq=True)
|
| 143 |
+
# vq_code: [batch, frames]
|
| 144 |
+
return vq_code
|
| 145 |
+
|
| 146 |
+
def decode_code(self, vq_code):
|
| 147 |
+
"""
|
| 148 |
+
将编码后的代码解码回音频。
|
| 149 |
+
|
| 150 |
+
参数:
|
| 151 |
+
vq_code: 编码后的代码 (Tensor) [batch, frames]
|
| 152 |
+
返回:
|
| 153 |
+
解码后的音频 (Tensor) [batch, waveform_length]
|
| 154 |
+
"""
|
| 155 |
+
with torch.no_grad():
|
| 156 |
+
# 获取量化后的嵌入
|
| 157 |
+
vq_post_emb = self.generator.quantizer.get_output_from_indices(vq_code.transpose(1, 2))
|
| 158 |
+
vq_post_emb = vq_post_emb.transpose(1, 2) # [batch, 1024, frames]
|
| 159 |
+
|
| 160 |
+
# 7) fc_post_a
|
| 161 |
+
vq_post_emb = self.fc_post_a(vq_post_emb.transpose(1, 2)).transpose(1, 2) # [batch, 1024, frames]
|
| 162 |
+
|
| 163 |
+
# 8) 最后解码成波形
|
| 164 |
+
recon_audio = self.generator(vq_post_emb.transpose(1, 2), vq=False)[0] # [batch, time]
|
| 165 |
+
return recon_audio
|
module.py
ADDED
|
File without changes
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8cb939062f3930e56ff22082f49c95461aedc8ceade7ff7b16a1b10f1e92e0be
|
| 3 |
+
size 3291343655
|
reconstructed.wav
ADDED
|
Binary file (157 kB). View file
|
|
|
test.flac
ADDED
|
Binary file (97.2 kB). View file
|
|
|
test.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import soundfile as sf
|
| 3 |
+
from transformers import AutoConfig
|
| 4 |
+
|
| 5 |
+
from modeling_xcodec2 import XCodec2Model
|
| 6 |
+
|
| 7 |
+
model_path = "/data/zheny/xcodec2" # 这是你在 huggingface 上的仓库名
|
| 8 |
+
|
| 9 |
+
model = XCodec2Model.from_pretrained(model_path)
|
| 10 |
+
model.eval().cuda()
|
| 11 |
+
|
| 12 |
+
# 准备一段音频
|
| 13 |
+
wav, sr = sf.read("test.flac")
|
| 14 |
+
wav_tensor = torch.from_numpy(wav).float().unsqueeze(0) # [1, time]
|
| 15 |
+
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
vq_code = model.encode_code(input_waveform=wav_tensor )
|
| 18 |
+
print(vq_code)
|
| 19 |
+
recon_wav = model.decode_code(vq_code).cpu()
|
| 20 |
+
|
| 21 |
+
sf.write("reconstructed.wav", recon_wav[0,0,:].numpy(), sr)
|
vq/__init__.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from vq.codec_encoder import CodecEncoder
|
| 2 |
+
from vq.codec_decoder import CodecDecoder
|
| 3 |
+
from vq.codec_decoder_vocos import CodecDecoderVocos
|
| 4 |
+
from vq.codec_encoder import CodecEncoder_Transformer,CodecEncoder_only_Transformer
|
vq/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (389 Bytes). View file
|
|
|
vq/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (371 Bytes). View file
|
|
|
vq/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (318 Bytes). View file
|
|
|
vq/__pycache__/__init__.cpython-38.pyc
ADDED
|
Binary file (414 Bytes). View file
|
|
|
vq/__pycache__/__init__.cpython-39.pyc
ADDED
|
Binary file (383 Bytes). View file
|
|
|
vq/__pycache__/activations.cpython-310.pyc
ADDED
|
Binary file (4 kB). View file
|
|
|
vq/__pycache__/activations.cpython-311.pyc
ADDED
|
Binary file (6.07 kB). View file
|
|
|
vq/__pycache__/activations.cpython-312.pyc
ADDED
|
Binary file (5.65 kB). View file
|
|
|
vq/__pycache__/activations.cpython-38.pyc
ADDED
|
Binary file (4.07 kB). View file
|
|
|
vq/__pycache__/activations.cpython-39.pyc
ADDED
|
Binary file (4.04 kB). View file
|
|
|
vq/__pycache__/blocks.cpython-310.pyc
ADDED
|
Binary file (6.03 kB). View file
|
|
|
vq/__pycache__/blocks.cpython-39.pyc
ADDED
|
Binary file (6.29 kB). View file
|
|
|
vq/__pycache__/bs_roformer5.cpython-310.pyc
ADDED
|
Binary file (3.91 kB). View file
|
|
|
vq/__pycache__/bs_roformer5.cpython-38.pyc
ADDED
|
Binary file (3.91 kB). View file
|
|
|
vq/__pycache__/bs_roformer5.cpython-39.pyc
ADDED
|
Binary file (3.86 kB). View file
|
|
|
vq/__pycache__/codec_decoder.cpython-310.pyc
ADDED
|
Binary file (9.09 kB). View file
|
|
|
vq/__pycache__/codec_decoder.cpython-311.pyc
ADDED
|
Binary file (8.78 kB). View file
|
|
|
vq/__pycache__/codec_decoder.cpython-312.pyc
ADDED
|
Binary file (7.76 kB). View file
|
|
|
vq/__pycache__/codec_decoder.cpython-39.pyc
ADDED
|
Binary file (9.22 kB). View file
|
|
|
vq/__pycache__/codec_decoder_vocos.cpython-310.pyc
ADDED
|
Binary file (18.1 kB). View file
|
|
|
vq/__pycache__/codec_decoder_vocos.cpython-311.pyc
ADDED
|
Binary file (27.7 kB). View file
|
|
|
vq/__pycache__/codec_decoder_vocos.cpython-312.pyc
ADDED
|
Binary file (25.2 kB). View file
|
|
|
vq/__pycache__/codec_decoder_vocos.cpython-39.pyc
ADDED
|
Binary file (18.4 kB). View file
|
|
|
vq/__pycache__/codec_encoder.cpython-310.pyc
ADDED
|
Binary file (9.97 kB). View file
|
|
|
vq/__pycache__/codec_encoder.cpython-311.pyc
ADDED
|
Binary file (4.91 kB). View file
|
|
|
vq/__pycache__/codec_encoder.cpython-312.pyc
ADDED
|
Binary file (4.39 kB). View file
|
|
|
vq/__pycache__/codec_encoder.cpython-38.pyc
ADDED
|
Binary file (7.45 kB). View file
|
|
|
vq/__pycache__/codec_encoder.cpython-39.pyc
ADDED
|
Binary file (10.2 kB). View file
|
|
|
vq/__pycache__/factorized_vector_quantize.cpython-310.pyc
ADDED
|
Binary file (3.71 kB). View file
|
|
|
vq/__pycache__/factorized_vector_quantize.cpython-311.pyc
ADDED
|
Binary file (6.3 kB). View file
|
|
|
vq/__pycache__/factorized_vector_quantize.cpython-312.pyc
ADDED
|
Binary file (5.82 kB). View file
|
|
|
vq/__pycache__/factorized_vector_quantize.cpython-39.pyc
ADDED
|
Binary file (3.64 kB). View file
|
|
|
vq/__pycache__/module.cpython-310.pyc
ADDED
|
Binary file (12 kB). View file
|
|
|