From 12e3e760922357a5f7ace0fc0aa6b02c120502e6 Mon Sep 17 00:00:00 2001 From: TianHao Zhang <32243340+Zth9730@users.noreply.github.com> Date: Thu, 4 May 2023 16:45:29 +0800 Subject: [PATCH] [ASR] Support Hubert, fintuned on the librispeech dataset (#3088) * librispeech hubert, test=asr * librispeech hubert, test=asr * hubert decode * review * copyright, notes, example related * hubert cli * pre-commit format * fix conflicts * fix conflicts * doc related * doc and train config * librispeech.py * support hubert cli --- demos/speech_ssl/README.md | 4 +- demos/speech_ssl/README_cn.md | 4 +- docs/source/released_model.md | 2 + examples/librispeech/asr3/path.sh | 4 +- examples/librispeech/asr3/run.sh | 2 +- examples/librispeech/asr4/README.md | 197 ++ examples/librispeech/asr4/RESULTS.md | 9 + examples/librispeech/asr4/cmd.sh | 89 + examples/librispeech/asr4/conf/config.json | 77 + examples/librispeech/asr4/conf/hubertASR.yaml | 142 + .../librispeech/asr4/conf/preprocess.yaml | 3 + .../asr4/conf/preprocessor_config.json | 9 + .../librispeech/asr4/conf/tuning/decode.yaml | 4 + examples/librispeech/asr4/local/data.sh | 110 + examples/librispeech/asr4/local/test.sh | 83 + examples/librispeech/asr4/local/test_wav.sh | 58 + examples/librispeech/asr4/local/train.sh | 58 + examples/librispeech/asr4/path.sh | 13 + examples/librispeech/asr4/run.sh | 47 + examples/librispeech/asr4/utils | 1 + paddlespeech/cli/asr/infer.py | 6 +- paddlespeech/cli/ssl/infer.py | 50 +- paddlespeech/resource/model_alias.py | 2 + paddlespeech/resource/pretrained_models.py | 32 + paddlespeech/s2t/exps/hubert/__init__.py | 13 + paddlespeech/s2t/exps/hubert/bin/__init__.py | 13 + paddlespeech/s2t/exps/hubert/bin/test.py | 64 + paddlespeech/s2t/exps/hubert/bin/test_wav.py | 118 + paddlespeech/s2t/exps/hubert/bin/train.py | 55 + paddlespeech/s2t/exps/hubert/model.py | 918 ++++++ paddlespeech/s2t/models/hubert/__init__.py | 17 + paddlespeech/s2t/models/hubert/hubert_ASR.py | 368 +++ .../s2t/models/hubert/modules/__init__.py | 13 + .../s2t/models/hubert/modules/hubert_model.py | 586 ++++ .../models/wav2vec2/modules/wav2vec2_model.py | 2614 +++++++++++++++++ .../s2t/models/wav2vec2/wav2vec2_ASR.py | 3 + 36 files changed, 5753 insertions(+), 35 deletions(-) mode change 100644 => 100755 examples/librispeech/asr3/run.sh create mode 100644 examples/librispeech/asr4/README.md create mode 100644 examples/librispeech/asr4/RESULTS.md create mode 100644 examples/librispeech/asr4/cmd.sh create mode 100644 examples/librispeech/asr4/conf/config.json create mode 100644 examples/librispeech/asr4/conf/hubertASR.yaml create mode 100644 examples/librispeech/asr4/conf/preprocess.yaml create mode 100644 examples/librispeech/asr4/conf/preprocessor_config.json create mode 100644 examples/librispeech/asr4/conf/tuning/decode.yaml create mode 100755 examples/librispeech/asr4/local/data.sh create mode 100755 examples/librispeech/asr4/local/test.sh create mode 100755 examples/librispeech/asr4/local/test_wav.sh create mode 100755 examples/librispeech/asr4/local/train.sh create mode 100644 examples/librispeech/asr4/path.sh create mode 100755 examples/librispeech/asr4/run.sh create mode 120000 examples/librispeech/asr4/utils create mode 100644 paddlespeech/s2t/exps/hubert/__init__.py create mode 100644 paddlespeech/s2t/exps/hubert/bin/__init__.py create mode 100644 paddlespeech/s2t/exps/hubert/bin/test.py create mode 100644 paddlespeech/s2t/exps/hubert/bin/test_wav.py create mode 100644 paddlespeech/s2t/exps/hubert/bin/train.py create mode 100644 paddlespeech/s2t/exps/hubert/model.py create mode 100644 paddlespeech/s2t/models/hubert/__init__.py create mode 100644 paddlespeech/s2t/models/hubert/hubert_ASR.py create mode 100644 paddlespeech/s2t/models/hubert/modules/__init__.py create mode 100644 paddlespeech/s2t/models/hubert/modules/hubert_model.py create mode 100644 paddlespeech/s2t/models/wav2vec2/modules/wav2vec2_model.py diff --git a/demos/speech_ssl/README.md b/demos/speech_ssl/README.md index b98a7cc6..937cd95a 100644 --- a/demos/speech_ssl/README.md +++ b/demos/speech_ssl/README.md @@ -36,7 +36,7 @@ wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/en.wav ``` Arguments: - `input`(required): Audio file to recognize. - - `model`: Model type of asr task. Default: `wav2vec2ASR_librispeech`. + - `model`: Model type of asr task. Default: `wav2vec2`, choices: [wav2vec2, hubert]. - `task`: Output type. Default: `asr`. - `lang`: Model language. Default: `en`. - `sample_rate`: Sample rate of the model. Default: `16000`. @@ -56,7 +56,7 @@ wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/en.wav # to recognize text text = ssl_executor( - model='wav2vec2ASR_librispeech', + model='wav2vec2', task='asr', lang='en', sample_rate=16000, diff --git a/demos/speech_ssl/README_cn.md b/demos/speech_ssl/README_cn.md index 65961ce9..8455d2c7 100644 --- a/demos/speech_ssl/README_cn.md +++ b/demos/speech_ssl/README_cn.md @@ -36,7 +36,7 @@ wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/en.wav ``` 参数: - `input`(必须输入):用于识别的音频文件。 - - `model`:ASR 任务的模型,默认值:`wav2vec2ASR_librispeech`。 + - `model`:ASR 任务的模型,默认值:`wav2vec2`, 可选项:[wav2vec2, hubert]。 - `task`:输出类别,默认值:`asr`。 - `lang`:模型语言,默认值:`en`。 - `sample_rate`:音频采样率,默认值:`16000`。 @@ -56,7 +56,7 @@ wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/en.wav # 识别文本 text = ssl_executor( - model='wav2vec2ASR_librispeech', + model='wav2vec2, task='asr', lang='en', sample_rate=16000, diff --git a/docs/source/released_model.md b/docs/source/released_model.md index 02404684..03805b2b 100644 --- a/docs/source/released_model.md +++ b/docs/source/released_model.md @@ -26,6 +26,8 @@ Model | Pre-Train Method | Pre-Train Data | Finetune Data | Size | Descriptions [Wav2vec2ASR-large-960h-librispeech Model](https://paddlespeech.bj.bcebos.com/s2t/librispeech/asr3/wav2vec2ASR-large-960h-librispeech_ckpt_1.3.1.model.tar.gz) | wav2vec2 | Librispeech and LV-60k Dataset (5.3w h) | Librispeech (960 h) | 718 MB |Encoder: Wav2vec2.0, Decoder: CTC, Decoding method: Greedy search | - | 0.0189 | [Wav2vecASR Librispeech ASR3](../../examples/librispeech/asr3) | [Wav2vec2-large-wenetspeech-self Model](https://paddlespeech.bj.bcebos.com/s2t/aishell/asr3/wav2vec2-large-wenetspeech-self_ckpt_1.3.0.model.tar.gz) | wav2vec2 | Wenetspeech Dataset (1w h) | - | 714 MB |Pre-trained Wav2vec2.0 Model | - | - | - | [Wav2vec2ASR-large-aishell1 Model](https://paddlespeech.bj.bcebos.com/s2t/aishell/asr3/wav2vec2ASR-large-aishell1_ckpt_1.4.0.model.tar.gz) | wav2vec2 | Wenetspeech Dataset (1w h) | aishell1 (train set) | 1.18 GB |Encoder: Wav2vec2.0, Decoder: CTC, Decoding method: Greedy search | 0.0510 | - | - | +[Hubert-large-lv60 Model](https://paddlespeech.bj.bcebos.com/hubert/hubert-large-lv60.pdparams) | hubert | LV-60k Dataset | - | 1.18 GB |Pre-trained hubert Model | - | - | - | +[Hubert-large-100h-librispeech Model](https://paddlespeech.bj.bcebos.com/s2t/librispeech/asr4/hubertASR-large-100h-librispeech_ckpt_1.4.0.model.tar.gz) | hubert | LV-60k Dataset | librispeech train-clean-100 | 1.27 GB |Encoder: Hubert, Decoder: Linear + CTC, Decoding method: Greedy search | - | 0.0587 | [HubertASR Librispeech ASR4](../../examples/librispeech/asr4) | ### Whisper Model Demo Link | Training Data | Size | Descriptions | CER | Model diff --git a/examples/librispeech/asr3/path.sh b/examples/librispeech/asr3/path.sh index f4717838..d98171a8 100644 --- a/examples/librispeech/asr3/path.sh +++ b/examples/librispeech/asr3/path.sh @@ -10,6 +10,4 @@ export PYTHONPATH=${MAIN_ROOT}:${PYTHONPATH} export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib/ - -MODEL=wav2vec2 -export BIN_DIR=${MAIN_ROOT}/paddlespeech/s2t/exps/${MODEL}/bin +export BIN_DIR=${MAIN_ROOT}/paddlespeech/s2t/exps/wav2vec2/bin diff --git a/examples/librispeech/asr3/run.sh b/examples/librispeech/asr3/run.sh old mode 100644 new mode 100755 index f52266a1..c880c9cb --- a/examples/librispeech/asr3/run.sh +++ b/examples/librispeech/asr3/run.sh @@ -44,4 +44,4 @@ fi if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ]; then # test a single .wav file CUDA_VISIBLE_DEVICES=0 ./local/test_wav.sh ${conf_path} ${decode_conf_path} exp/${ckpt}/checkpoints/${avg_ckpt} ${audio_file} || exit -1 -fi +fi \ No newline at end of file diff --git a/examples/librispeech/asr4/README.md b/examples/librispeech/asr4/README.md new file mode 100644 index 00000000..064a7f16 --- /dev/null +++ b/examples/librispeech/asr4/README.md @@ -0,0 +1,197 @@ +# Hubert2ASR with Librispeech +This example contains code used to finetune [hubert](https://arxiv.org/abs/2106.07447) model with [Librispeech dataset](http://www.openslr.org/resources/12) +## Overview +All the scripts you need are in `run.sh`. There are several stages in `run.sh`, and each stage has its function. +| Stage | Function | +|:---- |:----------------------------------------------------------- | +| 0 | Process data. It includes:
(1) Download the dataset
(2) Calculate the CMVN of the train dataset
(3) Get the vocabulary file
(4) Get the manifest files of the train, development and test dataset
(5) Download the pretrained wav2vec2 model | +| 1 | Train the model | +| 2 | Get the final model by averaging the top-k models, set k = 1 means to choose the best model | +| 3 | Test the final model performance | +| 4 | Infer the single audio file | + + +You can choose to run a range of stages by setting `stage` and `stop_stage `. + +For example, if you want to execute the code in stage 2 and stage 3, you can run this script: +```bash +bash run.sh --stage 2 --stop_stage 3 +``` +Or you can set `stage` equal to `stop-stage` to only run one stage. +For example, if you only want to run `stage 0`, you can use the script below: +```bash +bash run.sh --stage 0 --stop_stage 0 +``` +The document below will describe the scripts in `run.sh` in detail. +## The Environment Variables +The path.sh contains the environment variables. +```bash +. ./path.sh +. ./cmd.sh +``` +This script needs to be run first. And another script is also needed: +```bash +source ${MAIN_ROOT}/utils/parse_options.sh +``` +It will support the way of using `--variable value` in the shell scripts. +## The Local Variables +Some local variables are set in `run.sh`. +`gpus` denotes the GPU number you want to use. If you set `gpus=`, it means you only use CPU. +`stage` denotes the number of stages you want to start from in the experiments. +`stop stage` denotes the number of the stage you want to end at in the experiments. +`conf_path` denotes the config path of the model. +`avg_num` denotes the number K of top-K models you want to average to get the final model. +`audio file` denotes the file path of the single file you want to infer in stage 5 +`ckpt` denotes the checkpoint prefix of the model, e.g. "hubertASR" + +You can set the local variables (except `ckpt`) when you use `run.sh` + +For example, you can set the `gpus` and `avg_num` when you use the command line: +```bash +bash run.sh --gpus 0,1 --avg_num 20 +``` +## Stage 0: Data Processing +To use this example, you need to process data firstly and you can use stage 0 in `run.sh` to do this. The code is shown below: +```bash + if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ]; then + # prepare data + bash ./local/data.sh || exit -1 + fi +``` +Stage 0 is for processing the data. + +If you only want to process the data. You can run +```bash +bash run.sh --stage 0 --stop_stage 0 +``` +You can also just run these scripts in your command line. +```bash +. ./path.sh +. ./cmd.sh +bash ./local/data.sh +``` +After processing the data, the `data` directory will look like this: +```bash +data/ +|-- dev.meta +|-- lang_char +| `-- bpe_unigram_5000.model +| `-- bpe_unigram_5000.vocab +| `-- vocab.txt +|-- manifest.dev +|-- manifest.dev.raw +|-- manifest.test +|-- manifest.test.raw +|-- manifest.train +|-- manifest.train.raw +|-- mean_std.json +|-- test.meta +`-- train.meta +``` + +Stage 0 also downloads the pre-trained [hubert](https://paddlespeech.bj.bcebos.com/hubert/hubert-large-lv60.pdparams) model. +```bash +mkdir -p exp/hubert +wget -P exp/hubert https://paddlespeech.bj.bcebos.com/hubert/hubert-large-lv60.pdparams +``` +## Stage 1: Model Training +If you want to train the model. you can use stage 1 in `run.sh`. The code is shown below. +```bash +if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then + # train model, all `ckpt` under `exp` dir + CUDA_VISIBLE_DEVICES=${gpus} ./local/train.sh ${conf_path} ${ckpt} + fi +``` +If you want to train the model, you can use the script below to execute stage 0 and stage 1: +```bash +bash run.sh --stage 0 --stop_stage 1 +``` +or you can run these scripts in the command line (only use CPU). +```bash +. ./path.sh +. ./cmd.sh +bash ./local/data.sh +CUDA_VISIBLE_DEVICES= ./local/train.sh conf/hubertASR.yaml hubertASR +``` +## Stage 2: Top-k Models Averaging +After training the model, we need to get the final model for testing and inference. In every epoch, the model checkpoint is saved, so we can choose the best model from them based on the validation loss or we can sort them and average the parameters of the top-k models to get the final model. We can use stage 2 to do this, and the code is shown below. Note: We only train one epoch for hubertASR, thus the `avg_num` is set to 1. +```bash + if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then + # avg n best model + avg.sh best exp/${ckpt}/checkpoints ${avg_num} + fi +``` +The `avg.sh` is in the `../../../utils/` which is define in the `path.sh`. +If you want to get the final model, you can use the script below to execute stage 0, stage 1, and stage 2: +```bash +bash run.sh --stage 0 --stop_stage 2 +``` +or you can run these scripts in the command line (only use CPU). + +```bash +. ./path.sh +. ./cmd.sh +bash ./local/data.sh +CUDA_VISIBLE_DEVICES= ./local/train.sh conf/hubertASR.yaml hubertASR +avg.sh best exp/hubertASR/checkpoints 1 +``` +## Stage 3: Model Testing +The test stage is to evaluate the model performance. The code of test stage is shown below: +```bash + if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then + # test ckpt avg_n + CUDA_VISIBLE_DEVICES=0 ./local/test.sh ${conf_path} ${decode_conf_path} exp/${ckpt}/checkpoints/${avg_ckpt} || exit -1 + fi +``` +If you want to train a model and test it, you can use the script below to execute stage 0, stage 1, stage 2, and stage 3 : +```bash +bash run.sh --stage 0 --stop_stage 3 +``` +or you can run these scripts in the command line (only use CPU). +```bash +. ./path.sh +. ./cmd.sh +bash ./local/data.sh +CUDA_VISIBLE_DEVICES= ./local/train.sh conf/hubertASR.yaml hubertASR +avg.sh best exp/hubertASR/checkpoints 1 +CUDA_VISIBLE_DEVICES= ./local/test.sh conf/hubertASR.yaml conf/tuning/decode.yaml exp/hubertASR/checkpoints/avg_1 +``` +## Pretrained Model +You can get the pretrained hubertASR from [this](../../../docs/source/released_model.md). + +using the `tar` scripts to unpack the model and then you can use the script to test the model. + +For example: +```bash +wget https://paddlespeech.bj.bcebos.com/hubert/hubertASR-large-100h-librispeech_ckpt_1.4.0.model.tar.gz +tar xzvf hubertASR-large-100h-librispeech_ckpt_1.4.0.model.tar.gz +source path.sh +# If you have process the data and get the manifest file, you can skip the following 2 steps +bash local/data.sh --stage -1 --stop_stage -1 +bash local/data.sh --stage 2 --stop_stage 2 +CUDA_VISIBLE_DEVICES= ./local/test.sh conf/hubertASR.yaml conf/tuning/decode.yaml exp/hubertASR/checkpoints/avg_1 +``` +The performance of the released models are shown in [here](./RESULTS.md). + + +## Stage 4: Single Audio File Inference +In some situations, you want to use the trained model to do the inference for the single audio file. You can use stage 5. The code is shown below +```bash + if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ]; then + # test a single .wav file + CUDA_VISIBLE_DEVICES=0 ./local/test_wav.sh ${conf_path} ${decode_conf_path} exp/${ckpt}/checkpoints/${avg_ckpt} ${audio_file} || exit -1 + fi +``` +you can train the model by yourself using ```bash run.sh --stage 0 --stop_stage 3```, or you can download the pretrained model through the script below: +```bash +wget https://paddlespeech.bj.bcebos.com/hubert/hubertASR-large-100h-librispeech_ckpt_1.4.0.model.tar.gz +tar xzvf hubertASR-large-100h-librispeech_ckpt_1.4.0.model.tar.gz +``` +You can download the audio demo: +```bash +wget -nc https://paddlespeech.bj.bcebos.com/datasets/single_wav/en/demo_002_en.wav -P data/ +``` +You need to prepare an audio file or use the audio demo above, please confirm the sample rate of the audio is 16K. You can get the result of the audio demo by running the script below. +```bash +CUDA_VISIBLE_DEVICES= ./local/test_wav.sh conf/hubertASR.yaml conf/tuning/decode.yaml exp/hubertASR/checkpoints/avg_1 data/demo_002_en.wav +``` diff --git a/examples/librispeech/asr4/RESULTS.md b/examples/librispeech/asr4/RESULTS.md new file mode 100644 index 00000000..81ce6ee9 --- /dev/null +++ b/examples/librispeech/asr4/RESULTS.md @@ -0,0 +1,9 @@ +# LibriSpeech + +## hubertASR +Fintuning on train-clean-100 +train: Epoch 3, 1*V100-32G, batchsize: 4, accum_grad: 8 + +| Model | Params | Config | Augmentation| Test set | Decode method | WER | +| --- | --- | --- | --- | --- | --- | --- | +| hubertASR | 326.16M | conf/hubertASR.yaml | spec_aug | test-clean | greedy search | 0.05868 | diff --git a/examples/librispeech/asr4/cmd.sh b/examples/librispeech/asr4/cmd.sh new file mode 100644 index 00000000..7b70ef5e --- /dev/null +++ b/examples/librispeech/asr4/cmd.sh @@ -0,0 +1,89 @@ +# ====== About run.pl, queue.pl, slurm.pl, and ssh.pl ====== +# Usage: .pl [options] JOB=1: +# e.g. +# run.pl --mem 4G JOB=1:10 echo.JOB.log echo JOB +# +# Options: +# --time