This example contains code used to train a DeepSpeech2 offline or online model with Aishell dataset
All the scripts you need are in the run.sh. There are several stages in the 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 |
| 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 | Export the static graph model |
| 5 | Test the static graph model |
| 6 | Infer the single audio file |
You can choose to run a range of stages by setting the 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 run.sh --stage 2 --stop_stage 3Or 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 run.sh --stage 0 --stop_stage 0The document below will describe the scripts in the run.sh in detail.
The path.sh contains the environment variable.
source path.shThis script needs to be run first.
And another script is also needed:
source ${MAIN_ROOT}/utils/parse_options.shIt will support the way of using --variable value in the shell scripts.
Some local variables are set in the 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 the stage 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.
model_type denotes the model type: offline or online
audio file denotes the file path of the single file you want to infer in stage 6
ckpt denotes the checkpoint prefix of the model, e.g. "deepspeech2"
You can set the local variables (except ckpt) when you use the run.sh
For example, you can set the gpus and avg_num when you use the command line.:
bash run.sh --gpus 0,1 --avg_num 1To use this example, you need to process data firstly and you can use stage 0 in the run.sh to do this. The code is shown below:
if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ]; then
# prepare data
bash ./local/data.sh || exit -1
fiStage 0 is for processing the data. If you only want to process the data. You can run
bash run.sh --stage 0 --stop_stage 0You can also just run these scripts in your command line.
source path.sh
bash ./local/data.shAfter processing the data, the data directory will look like this:
data/
|-- dev.meta
|-- lang_char
| `-- vocab.txt
|-- manifest.dev
|-- manifest.dev.raw
|-- manifest.test
|-- manifest.test.raw
|-- manifest.train
|-- manifest.train.raw
|-- mean_std.json
|-- test.meta
`-- train.metaIf you want to train the model. you can use stage 1 in the run.sh. The code is shown below.
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}
fiIf you want to train the model, you can use the script below to execute stage 0 and stage 1:
bash run.sh --stage 0 --stop_stage 1Or you can run these scripts in the command line (only use CPU).
source path.sh
bash ./local/data.sh
CUDA_VISIBLE_DEVICES= ./local/train.sh conf/deepspeech2.yaml deepspeech2If you want to use GPU, you can run these scripts in the command line (suppose you have only 1 GPU).
source path.sh
bash ./local/data.sh
CUDA_VISIBLE_DEVICES=0 ./local/train.sh conf/deepspeech2.yaml deepspeech2After 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:
if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then
# avg n best model
avg.sh best exp/${ckpt}/checkpoints ${avg_num}
fiThe 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 run.sh --stage 0 --stop_stage 2or you can run these scripts in the command line (only use CPU).
source path.sh
bash ./local/data.sh
CUDA_VISIBLE_DEVICES= ./local/train.sh conf/deepspeech2.yaml deepspeech2
avg.sh best exp/deepspeech2/checkpoints 1The test stage is to evaluate the model performance. The code of the test stage is shown below:
if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then
# test ckpt avg_n
CUDA_VISIBLE_DEVICES=0 ./local/test.sh ${conf_path} exp/${ckpt}/checkpoints/${avg_ckpt} || exit -1
fiIf 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 run.sh --stage 0 --stop_stage 3or you can run these scripts in the command line (only use CPU).
source path.sh
bash ./local/data.sh
CUDA_VISIBLE_DEVICES= ./local/train.sh conf/deepspeech2.yaml deepspeech2
avg.sh best exp/deepspeech2/checkpoints 1
CUDA_VISIBLE_DEVICES= ./local/test.sh conf/deepspeech2.yaml conf/tuning/decode.yaml exp/deepspeech2/checkpoints/avg_10You can get the pretrained models from this.
using the tar scripts to unpack the model and then you can use the script to test the model.
For example:
wget https://paddlespeech.cdn.bcebos.com/s2t/aishell/asr0/asr0_deepspeech2_offline_aishell_ckpt_1.0.1.model.tar.gz
tar xzvf asr0_deepspeech2_offline_aishell_ckpt_1.0.1.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/deepspeech2.yaml exp/deepspeech2/checkpoints/avg_10
The performance of the released models are shown in this
This stage is to transform dygraph to static graph.
if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ]; then
# export ckpt avg_n
CUDA_VISIBLE_DEVICES=0 ./local/export.sh ${conf_path} exp/${ckpt}/checkpoints/${avg_ckpt} exp/${ckpt}/checkpoints/${avg_ckpt}.jit ${model_type}
fiIf you already have a dynamic graph model, you can run this script:
source path.sh
./local/export.sh conf/deepspeech2.yaml exp/deepspeech2/checkpoints/avg_10 exp/deepspeech2/checkpoints/avg_10.jitSimilar to stage 3, the static graph model can also be tested.
if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then
# test export ckpt avg_n
CUDA_VISIBLE_DEVICES=0 ./local/test_export.sh ${conf_path} exp/${ckpt}/checkpoints/${avg_ckpt}.jit ${model_type}|| exit -1
fiIf you already have exported the static graph, you can run this script:
CUDA_VISIBLE_DEVICES= ./local/test_export.sh conf/deepspeech2.yaml conf/tuning/decode.yaml exp/deepspeech2/checkpoints/avg_10.jitIn 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
if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then
# test a single .wav file
CUDA_VISIBLE_DEVICES=0 ./local/test_wav.sh ${conf_path} ${decode_conf_path} exp/${ckpt}/checkpoints/${avg_ckpt} ${model_type} ${audio_file}
fiyou can train the model by yourself, or you can download the pretrained model by the script below:
wget https://paddlespeech.cdn.bcebos.com/s2t/aishell/asr0/asr0_deepspeech2_offline_aishell_ckpt_1.0.1.model.tar.gz
tar xzvf asr0_deepspeech2_offline_aishell_ckpt_1.0.1.model.tar.gzYou can download the audio demo:
wget -nc https://paddlespeech.cdn.bcebos.com/datasets/single_wav/zh/demo_01_03.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.
CUDA_VISIBLE_DEVICES= ./local/test_wav.sh conf/deepspeech2.yaml conf/tuning/decode.yaml exp/deepspeech2/checkpoints/avg_10 data/demo_01_03.wav