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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# RepCodec: A Speech Representation Codec for Speech Tokenization
|
2 |
+
|
3 |
+
> [**RepCodec: A Speech Representation Codec for Speech Tokenization**](https://arxiv.org/abs/2309.00169)
|
4 |
+
|
5 |
+
## Introduction
|
6 |
+
|
7 |
+
**RepCodec** is a speech tokenization method for converting a speech waveform into a sequence of discrete semantic
|
8 |
+
tokens.
|
9 |
+
The main idea is to train a representation codec which learns a vector quantization codebook through reconstructing the
|
10 |
+
input speech representations from speech encoders like HuBERT or data2vec.
|
11 |
+
Extensive experiments show that RepCodec significantly outperforms the widely used k-means clustering approach in both
|
12 |
+
speech understanding and generation.
|
13 |
+
Also, RepCodec generalizes well across various speech encoders and languages.
|
14 |
+
|
15 |
+
<img src="images/RepCodec.png" alt="se" width="1000" />
|
16 |
+
|
17 |
+
## RepCodec Models
|
18 |
+
|
19 |
+
| Feature Type | Speech Data | RepCodec Model |
|
20 |
+
|-----------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------|----------------------------------------------------------------------------------------------------------|
|
21 |
+
| [HuBERT base](https://github.com/facebookresearch/fairseq/tree/main/examples/hubert#pre-trained-and-fine-tuned-asr-models) layer 9 | [Librispeech](http://www.openslr.org/12) train-clean-100 | [hubert_base_l9](https://drive.google.com/file/d/1XD0HKl607FFjri2-VJT7lHQeSpxsCCFO/view?usp=sharing) |
|
22 |
+
| [HuBERT large](https://github.com/facebookresearch/fairseq/tree/main/examples/hubert#pre-trained-and-fine-tuned-asr-models) layer 18 | [Librispeech](http://www.openslr.org/12) train-clean-100 | [hubert_large_l18](https://drive.google.com/file/d/1mTbm5GeJ7gp_5L3QLP-JGXdf8RnRw5n6/view?usp=sharing) |
|
23 |
+
| [data2vec base](https://github.com/facebookresearch/fairseq/blob/main/examples/data2vec/README.md#speech-2) layer 6 | [Librispeech](http://www.openslr.org/12) train-clean-100 | [data2vec_base_l6](https://drive.google.com/file/d/1d8sf3Ko_fYM9zlaiwxK_4xusLRKV5EMd/view?usp=sharing) |
|
24 |
+
| [data2vec large](https://github.com/facebookresearch/fairseq/blob/main/examples/data2vec/README.md#speech-2) layer 18 | [Librispeech](http://www.openslr.org/12) train-clean-100 | [data2vec_large_l18](https://drive.google.com/file/d/1nuRIHaejT-uVi4cluftbT8o_JZqar5SU/view?usp=sharing) |
|
25 |
+
| [Whisper medium](https://github.com/openai/whisper/tree/main#available-models-and-languages) layer 24 | [Librispeech](http://www.openslr.org/12) train-clean-100 | [whisper_medium_l24](https://drive.google.com/file/d/1V6YJSA2V4iywXrecJAN0oqsa3aHowexZ/view?usp=sharing) |
|
26 |
+
| [Whisper large-v2](https://github.com/openai/whisper/tree/main#available-models-and-languages) layer 32 | [Librispeech](http://www.openslr.org/12) train-clean-100 | [whisper_large_l32](https://drive.google.com/file/d/1k_X7ZMPg8iOeDrIJe70v6CHfFygzufXC/view?usp=sharing) |
|
27 |
+
|
28 |
+
## Speech Tokenization Using Pre-Trained Models
|
29 |
+
|
30 |
+
### Installation
|
31 |
+
|
32 |
+
Please first install RepCodec by
|
33 |
+
|
34 |
+
```
|
35 |
+
git clone https://github.com/mct10/RepCodec.git
|
36 |
+
cd RepCodec
|
37 |
+
pip install .
|
38 |
+
```
|
39 |
+
|
40 |
+
We used Python 3.9.18 and PyTorch 1.12.1 to test the usage, but the code should be compatible with other recent Python
|
41 |
+
and PyTorch versions.
|
42 |
+
|
43 |
+
### Representation Preparation
|
44 |
+
|
45 |
+
We adapt the `dump_hubert_feature.py` script
|
46 |
+
from [fairseq](https://github.com/facebookresearch/fairseq/tree/main/examples/hubert/simple_kmeans#hubert-feature)
|
47 |
+
to support dumping representations from **data2vec**, **HuBERT**, or **Whisper** encoders.
|
48 |
+
|
49 |
+
If you use our script (`examples/dump_feature.py`), please also install the following packages:
|
50 |
+
|
51 |
+
```
|
52 |
+
pip install npy_append_array soundfile
|
53 |
+
```
|
54 |
+
|
55 |
+
Additionally, if you want to dump representations from
|
56 |
+
|
57 |
+
- **data2vec** or **HuBERT**: please
|
58 |
+
follow [fairseq's instruction](https://github.com/facebookresearch/fairseq#requirements-and-installation) to install
|
59 |
+
the latest fairseq.
|
60 |
+
|
61 |
+
- **Whisper**: please follow [Whispers'instruction](https://github.com/openai/whisper/tree/main#setup) to install the
|
62 |
+
latest
|
63 |
+
Whisper.
|
64 |
+
|
65 |
+
Then, you can follow the given examples to dump representations:
|
66 |
+
|
67 |
+
```
|
68 |
+
# Example 1: dump from HuBERT base layer 9
|
69 |
+
# (for data2vec, simply change "model_type" to data2vec and "ckpt_path" to the path of data2vec model)
|
70 |
+
|
71 |
+
layer=9
|
72 |
+
|
73 |
+
python3 examples/dump_feature.py \
|
74 |
+
--model_type hubert \
|
75 |
+
--tsv_path /path/to/tsv/file \
|
76 |
+
--ckpt_path /path/to/HuBERT/model \
|
77 |
+
--layer ${layer} \
|
78 |
+
--feat_dir /dir/to/save/representations
|
79 |
+
|
80 |
+
|
81 |
+
# Example 2: dump from Whisper medium layer 24
|
82 |
+
|
83 |
+
layer=24
|
84 |
+
|
85 |
+
python3 examples/dump_feature.py \
|
86 |
+
--model_type whisper \
|
87 |
+
--tsv_path /path/to/tsv/file \
|
88 |
+
--whisper_root /directory/to/save/whisper/model \
|
89 |
+
--whisper_name medium \
|
90 |
+
--layer ${layer} \
|
91 |
+
--feat_dir /dir/to/save/representations
|
92 |
+
```
|
93 |
+
|
94 |
+
Explanations about the args:
|
95 |
+
|
96 |
+
- **model_type:** choose from `data2vec`, `hubert`, and `whisper`.
|
97 |
+
|
98 |
+
- **tsv_path:** path of the tsv file.
|
99 |
+
Should have the format of
|
100 |
+
|
101 |
+
```
|
102 |
+
/dir/to/dataset
|
103 |
+
path_of_utterance_1 number_of_frames
|
104 |
+
path_of_utterance_2 number_of_frames
|
105 |
+
```
|
106 |
+
|
107 |
+
You can follow [this script](https://github.com/facebookresearch/fairseq/blob/main/examples/wav2vec/wav2vec_manifest.py)
|
108 |
+
to generate the tsv file.
|
109 |
+
|
110 |
+
For example, by running
|
111 |
+
|
112 |
+
```
|
113 |
+
python wav2vec_manifest.py \
|
114 |
+
/dir/to/LibriSpeech/dev-clean \
|
115 |
+
--dest /dir/to/manifest \
|
116 |
+
--ext flac \
|
117 |
+
--valid-percent 0
|
118 |
+
```
|
119 |
+
|
120 |
+
you can obtain the `dev-clean.tsv` in `/dir/to/manifest` for LibriSpeech. (By default, the output file name
|
121 |
+
is `train.tsv`. Remember to rename the file.)
|
122 |
+
|
123 |
+
It should be similar to:
|
124 |
+
|
125 |
+
```
|
126 |
+
/dir/to/LibriSpeech/dev-clean
|
127 |
+
2277/149896/2277-149896-0026.flac 78720
|
128 |
+
2277/149896/2277-149896-0005.flac 89600
|
129 |
+
2277/149896/2277-149896-0033.flac 45520
|
130 |
+
```
|
131 |
+
|
132 |
+
- **ckpt_path**:
|
133 |
+
must provide for data2vec and HuBERT.
|
134 |
+
You need to download the model
|
135 |
+
from [data2vec website](https://github.com/facebookresearch/fairseq/blob/main/examples/data2vec/README.md#speech-2)
|
136 |
+
or [HuBERT website](https://github.com/facebookresearch/fairseq/tree/main/examples/hubert#pre-trained-and-fine-tuned-asr-models)
|
137 |
+
yourself.
|
138 |
+
`--ckpt_path` is the path of the data2vec/HuBERT model.
|
139 |
+
- **whisper_root** and **whisper_name**:
|
140 |
+
must provide **BOTH** `--whisper_root` and `--whisper_name` for Whisper.
|
141 |
+
If there is no corresponding model in `--whisper_root`, the script will download for you.
|
142 |
+
|
143 |
+
- **layer**:
|
144 |
+
which Transformer encoder layer of the model should the representations be extracted from.
|
145 |
+
It is **1-based**.
|
146 |
+
For example, if layer=9, then the outputs from the 9<sup>th</sup> Transformer encoder layer are dumped.
|
147 |
+
Range: [1, number of Transformer encoder layers]
|
148 |
+
|
149 |
+
- **feat_dir**: The output representations will be saved to `${feat_dir}/0_1.npy`
|
150 |
+
and `${feat_dir}/0_1.len`.
|
151 |
+
|
152 |
+
For other useful functionalities (e.g., sharding), please check the argument list in `examples/dump_feature.py`.
|
153 |
+
|
154 |
+
### Command Line Usage
|
155 |
+
|
156 |
+
We expect to have `${feat_dir}/0_1.npy` and `${feat_dir}/0_1.len` in the provided
|
157 |
+
directory `/dir/to/representaitons`.
|
158 |
+
|
159 |
+
Also, the tsv file should be the **same** as the one used in [Representation Preparation](#representation-preparation).
|
160 |
+
|
161 |
+
```
|
162 |
+
repcodec /dir/to/representaitons \
|
163 |
+
--model /path/to/repcodec/model \
|
164 |
+
--tsv_path /path/to/tsv/file \
|
165 |
+
[--model_config_path /path/to/train/config] \
|
166 |
+
[--use_gpu] \
|
167 |
+
[--out_dir /path/to/output]
|
168 |
+
```
|
169 |
+
|
170 |
+
If you trained the model yourself following [Training New RepCodec Models](#training-new-repcodec-models),
|
171 |
+
please provide the training config file using `--model_config_path`.
|
172 |
+
If you use the model we provide [here](#repcodec-models), then you do not have to provide that.
|
173 |
+
|
174 |
+
This command will tokenize the representations and the output discrete tokens will be saved to `${out_dir}/tokens`.
|
175 |
+
The tokens are in the same order as the provided tsv file.
|
176 |
+
|
177 |
+
An example of the output file:
|
178 |
+
|
179 |
+
```
|
180 |
+
/dir/to/LibriSpeech/dev-clean
|
181 |
+
2277/149896/2277-149896-0026.flac 696 696 198 198 198 498 ...
|
182 |
+
2277/149896/2277-149896-0005.flac 696 696 198 198 198 907 ...
|
183 |
+
2277/149896/2277-149896-0033.flac 696 696 198 198 198 696 ...
|
184 |
+
```
|
185 |
+
|
186 |
+
Under `examples/tokens`, we provide some token files as references. They are obtained from LibriSpeech dev-clean subset
|
187 |
+
using the 6 types of representations and corresponding [RepCodec Models](#repcodec-models).
|
188 |
+
Your results should be very similar to ours.
|
189 |
+
|
190 |
+
### Python Usage
|
191 |
+
|
192 |
+
```python
|
193 |
+
import torch
|
194 |
+
import yaml
|
195 |
+
|
196 |
+
from repcodec.RepCodec import RepCodec
|
197 |
+
|
198 |
+
# for feature types of HubERT base & data2vec base, please use repcodec_dim768.yaml;
|
199 |
+
# for feature types of HuBERT large & data2vec large & Whisper medium, please use repcodec_dim1024.yaml;
|
200 |
+
# for feature types of Whisper large-v2, please use repcodec_dim1280.yaml
|
201 |
+
config = "repcodec/configs/repcodec_dim768.yaml"
|
202 |
+
with open(config) as fp:
|
203 |
+
conf = yaml.load(fp, Loader=yaml.FullLoader)
|
204 |
+
|
205 |
+
model = RepCodec(**conf)
|
206 |
+
model.load_state_dict(torch.load("./hubert_base_l9.pkl", map_location="cpu")["model"]["repcodec"])
|
207 |
+
model.quantizer.initial()
|
208 |
+
model.eval()
|
209 |
+
|
210 |
+
# input shape: (batch size, hidden dim, sequence length)
|
211 |
+
random_features = torch.randn(size=(1, 768, 100))
|
212 |
+
with torch.no_grad():
|
213 |
+
x = model.encoder(random_features)
|
214 |
+
z = model.projector(x)
|
215 |
+
_, idx = model.quantizer.codebook.forward_index(z.transpose(2, 1))
|
216 |
+
tokens = idx.cpu().data.numpy().tolist()[0]
|
217 |
+
```
|
218 |
+
|
219 |
+
## Training New RepCodec Models
|
220 |
+
|
221 |
+
We use a config file to set up all the training configurations, e.g., data, model architecture,
|
222 |
+
optimizer, scheduler.
|
223 |
+
We provide an example [here](./train_configs/ex_dim768_mse.yaml).
|
224 |
+
|
225 |
+
Please first install required packages following [Installation](#installation)
|
226 |
+
and prepare the representations following [Representation Preparation](#representation-preparation).
|
227 |
+
|
228 |
+
The input data directory is expected to have the following structure
|
229 |
+
```
|
230 |
+
/dir/to/representations/
|
231 |
+
train_set_name/
|
232 |
+
0_1.npy
|
233 |
+
0_1.len
|
234 |
+
valid_set_name/
|
235 |
+
0_1.npy
|
236 |
+
0_1.len
|
237 |
+
test_set_name/
|
238 |
+
0_1.npy
|
239 |
+
0_1.len
|
240 |
+
```
|
241 |
+
|
242 |
+
The names of subsets should be the same as the fields in the config file.
|
243 |
+
|
244 |
+
Then, you can run training by
|
245 |
+
```
|
246 |
+
python train.py \
|
247 |
+
-c /path/to/config/file \
|
248 |
+
--tag $tag \
|
249 |
+
--exp_root exp
|
250 |
+
```
|
251 |
+
|
252 |
+
`tag` is the name of the output folder.
|
253 |
+
All outputs will be saved to `exp_root/tag/`.
|
254 |
+
|
255 |
+
## Acknowledge
|
256 |
+
|
257 |
+
Our implementation is based on [facebookresearch/AudioDec](https://github.com/facebookresearch/AudioDec).
|
258 |
+
We thank them for open-sourcing their code!
|
259 |
+
|
260 |
+
## Citation
|
261 |
+
|
262 |
+
If you find our work useful, please cite the following article.
|
263 |
+
|
264 |
+
```
|
265 |
+
@misc{huang2023repcodec,
|
266 |
+
title={RepCodec: A Speech Representation Codec for Speech Tokenization},
|
267 |
+
author={Zhichao Huang and Chutong Meng and Tom Ko},
|
268 |
+
year={2023},
|
269 |
+
eprint={2309.00169},
|
270 |
+
archivePrefix={arXiv},
|
271 |
+
primaryClass={eess.AS}
|
272 |
+
}
|
273 |
+
```
|