run.sh 6.3 KB

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