run.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env bash
  2. workspace=`pwd`
  3. # machines configuration
  4. CUDA_VISIBLE_DEVICES="0,1"
  5. gpu_num=2
  6. gpu_inference=true # Whether to perform gpu decoding, set false for cpu decoding
  7. # for gpu decoding, inference_nj=ngpu*njob; for cpu decoding, inference_nj=njob
  8. njob=1
  9. # general configuration
  10. feats_dir="../DATA" #feature output dictionary
  11. exp_dir="."
  12. lang=zh
  13. token_type=char
  14. stage=0
  15. stop_stage=5
  16. # feature configuration
  17. nj=64
  18. # data
  19. raw_data=../raw_data
  20. data_url=www.openslr.org/resources/33
  21. # exp tag
  22. tag="exp1"
  23. . utils/parse_options.sh || exit 1;
  24. # Set bash to 'debug' mode, it will exit on :
  25. # -e 'error', -u 'undefined variable', -o ... 'error in pipeline', -x 'print commands',
  26. set -e
  27. set -u
  28. set -o pipefail
  29. train_set=train
  30. valid_set=dev
  31. test_sets="dev test"
  32. config=train_asr_paraformer_conformer_12e_6d_2048_256.yaml
  33. model_dir="baseline_$(basename "${config}" .yaml)_${lang}_${token_type}_${tag}"
  34. inference_device="cuda" #"cpu"
  35. inference_checkpoint="model.pt"
  36. inference_scp="wav.scp"
  37. if [ ${stage} -le -1 ] && [ ${stop_stage} -ge -1 ]; then
  38. echo "stage -1: Data Download"
  39. local/download_and_untar.sh ${raw_data} ${data_url} data_aishell
  40. local/download_and_untar.sh ${raw_data} ${data_url} resource_aishell
  41. fi
  42. if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ]; then
  43. echo "stage 0: Data preparation"
  44. # Data preparation
  45. local/aishell_data_prep.sh ${raw_data}/data_aishell/wav ${raw_data}/data_aishell/transcript ${feats_dir}
  46. for x in train dev test; do
  47. cp ${feats_dir}/data/${x}/text ${feats_dir}/data/${x}/text.org
  48. paste -d " " <(cut -f 1 -d" " ${feats_dir}/data/${x}/text.org) <(cut -f 2- -d" " ${feats_dir}/data/${x}/text.org | tr -d " ") \
  49. > ${feats_dir}/data/${x}/text
  50. utils/text2token.py -n 1 -s 1 ${feats_dir}/data/${x}/text > ${feats_dir}/data/${x}/text.org
  51. mv ${feats_dir}/data/${x}/text.org ${feats_dir}/data/${x}/text
  52. # convert wav.scp text to jsonl
  53. scp_file_list_arg="++scp_file_list='[\"${feats_dir}/data/${x}/wav.scp\",\"${feats_dir}/data/${x}/text\"]'"
  54. python ../../../funasr/datasets/audio_datasets/scp2jsonl.py \
  55. ++data_type_list='["source", "target"]' \
  56. ++jsonl_file_out=${feats_dir}/data/${x}/audio_datasets.jsonl \
  57. ${scp_file_list_arg}
  58. done
  59. fi
  60. if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then
  61. echo "stage 1: Feature and CMVN Generation"
  62. # utils/compute_cmvn.sh --fbankdir ${feats_dir}/data/${train_set} --cmd "$train_cmd" --nj $nj --feats_dim ${feats_dim} --config_file "$config" --scale 1.0
  63. python ../../../funasr/bin/compute_audio_cmvn.py \
  64. --config-path "${workspace}" \
  65. --config-name "${config}" \
  66. ++train_data_set_list="${feats_dir}/data/${train_set}/audio_datasets.jsonl" \
  67. ++cmvn_file="${feats_dir}/data/${train_set}/cmvn.json" \
  68. ++dataset_conf.num_workers=$nj
  69. fi
  70. token_list=${feats_dir}/data/${lang}_token_list/$token_type/tokens.txt
  71. echo "dictionary: ${token_list}"
  72. if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then
  73. echo "stage 2: Dictionary Preparation"
  74. mkdir -p ${feats_dir}/data/${lang}_token_list/$token_type/
  75. echo "make a dictionary"
  76. echo "<blank>" > ${token_list}
  77. echo "<s>" >> ${token_list}
  78. echo "</s>" >> ${token_list}
  79. utils/text2token.py -s 1 -n 1 --space "" ${feats_dir}/data/$train_set/text | cut -f 2- -d" " | tr " " "\n" \
  80. | sort | uniq | grep -a -v -e '^\s*$' | awk '{print $0}' >> ${token_list}
  81. echo "<unk>" >> ${token_list}
  82. fi
  83. # LM Training Stage
  84. if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then
  85. echo "stage 3: LM Training"
  86. fi
  87. # ASR Training Stage
  88. if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ]; then
  89. echo "stage 4: ASR Training"
  90. log_file="${exp_dir}/exp/${model_dir}/train.log.txt"
  91. echo "log_file: ${log_file}"
  92. torchrun \
  93. --nnodes 1 \
  94. --nproc_per_node ${gpu_num} \
  95. ../../../funasr/bin/train.py \
  96. --config-path "${workspace}" \
  97. --config-name "${config}" \
  98. ++train_data_set_list="${feats_dir}/data/${train_set}/audio_datasets.jsonl" \
  99. ++tokenizer_conf.token_list="${token_list}" \
  100. ++frontend_conf.cmvn_file="${feats_dir}/data/${train_set}/am.mvn" \
  101. ++output_dir="${exp_dir}/exp/${model_dir}" &> ${log_file}
  102. fi
  103. # Testing Stage
  104. if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then
  105. echo "stage 5: Inference"
  106. if ${inference_device} == "cuda"; then
  107. nj=$(echo CUDA_VISIBLE_DEVICES | awk -F "," '{print NF}')
  108. else
  109. nj=$njob
  110. batch_size=1
  111. gpuid_list=""
  112. for JOB in $(seq ${nj}); do
  113. gpuid_list=CUDA_VISIBLE_DEVICES"-1,"
  114. done
  115. fi
  116. for dset in ${test_sets}; do
  117. inference_dir="${asr_exp}/${inference_checkpoint}/${dset}"
  118. _logdir="${inference_dir}/logdir"
  119. mkdir -p "${_logdir}"
  120. data_dir="${feats_dir}/data/${dset}"
  121. key_file=${data_dir}/${inference_scp}
  122. split_scps=
  123. for JOB in $(seq "${nj}"); do
  124. split_scps+=" ${_logdir}/keys.${JOB}.scp"
  125. done
  126. utils/split_scp.pl "${key_file}" ${split_scps}
  127. for JOB in $(seq ${nj}); do
  128. {
  129. python ../../../funasr/bin/inference.py \
  130. --config-path="${exp_dir}/exp/${model_dir}" \
  131. --config-name="config.yaml" \
  132. ++init_param="${exp_dir}/exp/${model_dir}/${inference_checkpoint}" \
  133. ++tokenizer_conf.token_list="${token_list}" \
  134. ++frontend_conf.cmvn_file="${feats_dir}/data/${train_set}/am.mvn" \
  135. ++input="${_logdir}/keys.${JOB}.scp" \
  136. ++output_dir="${inference_dir}/${JOB}" \
  137. ++device="${inference_device}"
  138. }&
  139. done
  140. wait
  141. mkdir -p ${inference_dir}/1best_recog
  142. for f in token score text; do
  143. if [ -f "${inference_dir}/${JOB}/1best_recog/${f}" ]; then
  144. for JOB in $(seq "${nj}"); do
  145. cat "${inference_dir}/${JOB}/1best_recog/${f}"
  146. done | sort -k1 >"${inference_dir}/1best_recog/${f}"
  147. fi
  148. done
  149. echo "Computing WER ..."
  150. cp ${inference_dir}/1best_recog/text ${inference_dir}/1best_recog/text.proc
  151. cp ${data_dir}/text ${inference_dir}/1best_recog/text.ref
  152. python utils/compute_wer.py ${inference_dir}/1best_recog/text.ref ${inference_dir}/1best_recog/text.proc ${inference_dir}/1best_recog/text.cer
  153. tail -n 3 ${inference_dir}/1best_recog/text.cer
  154. done
  155. fi