Views
No views yet
| Model checkpoint | Semantic Model | Domain | Training Data |
|---|---|---|---|
| xcodec-hubert-librispeech | facebook/hubert-base-ls960 | Speech | Librispeech |
| xcodec-wavlm-mls | microsoft/wavlm-base-plus | Speech | MLS English |
| xcodec-wavlm-more-data | microsoft/wavlm-base-plus | Speech | MLS English + Internal data |
| xcodec-hubert-general (this model) | ZhenYe234/hubert_base_general_audio | General audio | 200k hours internal data |
| xcodec-hubert-general-balanced | ZhenYe234/hubert_base_general_audio | General audio | More balanced data |
xcodec_hubert_general_audio from this table.1
2from datasets import Audio, load_dataset
3from transformers import XcodecModel, AutoFeatureExtractor
4import torch
5import os
6from scipy.io.wavfile import write as write_wav
7
8
9model_id = "hf-audio/xcodec-hubert-general"
10torch_device = "cuda" if torch.cuda.is_available() else "cpu"
11available_bandwidths = [0.5, 1, 1.5, 2, 4]
12
13# load model
14model = XcodecModel.from_pretrained(model_id, device_map=torch_device)
15feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
16
17# load audio example
18librispeech_dummy = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
19librispeech_dummy = librispeech_dummy.cast_column(
20 "audio", Audio(sampling_rate=feature_extractor.sampling_rate)
21)
22audio_array = librispeech_dummy[0]["audio"]["array"]
23inputs = feature_extractor(
24 raw_audio=audio_array, sampling_rate=feature_extractor.sampling_rate, return_tensors="pt"
25).to(model.device)
26audio = inputs["input_values"]
27
28for bandwidth in available_bandwidths:
29 print(f"Encoding with bandwidth: {bandwidth} kbps")
30 # encode
31 audio_codes = model.encode(audio, bandwidth=bandwidth, return_dict=False)
32 print("Codebook shape", audio_codes.shape)
33 # 0.5 kbps -> torch.Size([1, 1, 293])
34 # 1.0 kbps -> torch.Size([1, 2, 293])
35 # 1.5 kbps -> torch.Size([1, 3, 293])
36 # 2.0 kbps -> torch.Size([1, 4, 293])
37 # 4.0 kbps -> torch.Size([1, 8, 293])
38
39 # decode
40 input_values_dec = model.decode(audio_codes).audio_values
41
42 # save audio to file
43 write_wav(f"{os.path.basename(model_id)}_{bandwidth}.wav", feature_extractor.sampling_rate, input_values_dec.squeeze().detach().cpu().numpy())
44
45write_wav("original.wav", feature_extractor.sampling_rate, audio.squeeze().detach().cpu().numpy())1
2from datasets import Audio, load_dataset
3from transformers import XcodecModel, AutoFeatureExtractor
4import torch
5
6
7model_id = "hf-audio/xcodec-hubert-general"
8torch_device = "cuda" if torch.cuda.is_available() else "cpu"
9bandwidth = 4
10n_audio = 2 # number of audio samples to process in a batch
11
12# load model
13model = XcodecModel.from_pretrained(model_id, device_map=torch_device)
14feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
15
16# load audio example
17ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
18ds = ds.cast_column(
19 "audio", Audio(sampling_rate=feature_extractor.sampling_rate)
20)
21audio = [audio_sample["array"] for audio_sample in ds[-n_audio:]["audio"]]
22print(f"Input audio shape: {[_sample.shape for _sample in audio]}")
23# Input audio shape: [(113840,), (71680,)]
24inputs = feature_extractor(
25 raw_audio=audio, sampling_rate=feature_extractor.sampling_rate, return_tensors="pt"
26).to(model.device)
27audio = inputs["input_values"]
28print(f"Padded audio shape: {audio.shape}")
29# Padded audio shape: torch.Size([2, 1, 113920])
30
31# encode
32audio_codes = model.encode(audio, bandwidth=bandwidth, return_dict=False)
33print("Codebook shape", audio_codes.shape)
34# Codebook shape torch.Size([2, 8, 356])
35
36# decode
37decoded_audio = model.decode(audio_codes).audio_values
38print("Decoded audio shape", decoded_audio.shape)
39# Decoded audio shape torch.Size([2, 1, 113920])