run.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/usr/bin/env bash
  2. . ./path.sh || exit 1;
  3. # machines configuration
  4. CUDA_VISIBLE_DEVICES="0,1"
  5. gpu_num=2
  6. count=1
  7. gpu_inference=true # Whether to perform gpu decoding, set false for cpu decoding
  8. # for gpu decoding, inference_nj=ngpu*njob; for cpu decoding, inference_nj=njob
  9. njob=1
  10. train_cmd=utils/run.pl
  11. infer_cmd=utils/run.pl
  12. # general configuration
  13. feats_dir="../DATA" #feature output dictionary, for large data
  14. exp_dir="."
  15. lang=zh
  16. token_type=char
  17. type=sound
  18. scp=wav.scp
  19. speed_perturb="0.9 1.0 1.1"
  20. stage=0
  21. stop_stage=5
  22. skip_extract_embed=false
  23. bert_model_name="bert-base-chinese"
  24. # feature configuration
  25. feats_dim=80
  26. nj=64
  27. # data
  28. raw_data=../raw_data
  29. data_url=www.openslr.org/resources/33
  30. # exp tag
  31. tag="exp1"
  32. . utils/parse_options.sh || exit 1;
  33. # Set bash to 'debug' mode, it will exit on :
  34. # -e 'error', -u 'undefined variable', -o ... 'error in pipeline', -x 'print commands',
  35. set -e
  36. set -u
  37. set -o pipefail
  38. train_set=train
  39. valid_set=dev
  40. test_sets="dev test"
  41. asr_config=conf/train_asr_paraformerbert_conformer_12e_6d_2048_256.yaml
  42. model_dir="baseline_$(basename "${asr_config}" .yaml)_${lang}_${token_type}_${tag}"
  43. inference_config=conf/decode_asr_transformer_noctc_1best.yaml
  44. inference_asr_model=valid.acc.ave_10best.pb
  45. # you can set gpu num for decoding here
  46. gpuid_list=$CUDA_VISIBLE_DEVICES # set gpus for decoding, the same as training stage by default
  47. ngpu=$(echo $gpuid_list | awk -F "," '{print NF}')
  48. if ${gpu_inference}; then
  49. inference_nj=$[${ngpu}*${njob}]
  50. _ngpu=1
  51. else
  52. inference_nj=$njob
  53. _ngpu=0
  54. fi
  55. if [ ${stage} -le -1 ] && [ ${stop_stage} -ge -1 ]; then
  56. echo "stage -1: Data Download"
  57. local/download_and_untar.sh ${raw_data} ${data_url} data_aishell
  58. local/download_and_untar.sh ${raw_data} ${data_url} resource_aishell
  59. fi
  60. if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ]; then
  61. echo "stage 0: Data preparation"
  62. # Data preparation
  63. local/aishell_data_prep.sh ${raw_data}/data_aishell/wav ${raw_data}/data_aishell/transcript ${feats_dir}
  64. for x in train dev test; do
  65. cp ${feats_dir}/data/${x}/text ${feats_dir}/data/${x}/text.org
  66. paste -d " " <(cut -f 1 -d" " ${feats_dir}/data/${x}/text.org) <(cut -f 2- -d" " ${feats_dir}/data/${x}/text.org | tr -d " ") \
  67. > ${feats_dir}/data/${x}/text
  68. utils/text2token.py -n 1 -s 1 ${feats_dir}/data/${x}/text > ${feats_dir}/data/${x}/text.org
  69. mv ${feats_dir}/data/${x}/text.org ${feats_dir}/data/${x}/text
  70. done
  71. fi
  72. if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then
  73. echo "stage 1: Feature and CMVN Generation"
  74. utils/compute_cmvn.sh --fbankdir ${feats_dir}/data/${train_set} --cmd "$train_cmd" --nj $nj --feats_dim ${feats_dim} --config_file "$asr_config" --scale 1.0
  75. fi
  76. token_list=${feats_dir}/data/${lang}_token_list/$token_type/tokens.txt
  77. echo "dictionary: ${token_list}"
  78. if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then
  79. echo "stage 2: Dictionary Preparation"
  80. mkdir -p ${feats_dir}/data/${lang}_token_list/$token_type/
  81. echo "make a dictionary"
  82. echo "<blank>" > ${token_list}
  83. echo "<s>" >> ${token_list}
  84. echo "</s>" >> ${token_list}
  85. utils/text2token.py -s 1 -n 1 --space "" ${feats_dir}/data/$train_set/text | cut -f 2- -d" " | tr " " "\n" \
  86. | sort | uniq | grep -a -v -e '^\s*$' | awk '{print $0}' >> ${token_list}
  87. echo "<unk>" >> ${token_list}
  88. fi
  89. # LM Training Stage
  90. world_size=$gpu_num # run on one machine
  91. if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then
  92. echo "stage 3: LM Training"
  93. fi
  94. # ASR Training Stage
  95. world_size=$gpu_num # run on one machine
  96. if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ]; then
  97. echo "stage 4: ASR Training"
  98. if ! "${skip_extract_embed}"; then
  99. echo "extract embeddings..."
  100. local/extract_embeds.sh \
  101. --bert_model_name ${bert_model_name} \
  102. --raw_dataset_path ${feats_dir} \
  103. --nj $nj
  104. fi
  105. mkdir -p ${exp_dir}/exp/${model_dir}
  106. mkdir -p ${exp_dir}/exp/${model_dir}/log
  107. INIT_FILE=${exp_dir}/exp/${model_dir}/ddp_init
  108. if [ -f $INIT_FILE ];then
  109. rm -f $INIT_FILE
  110. fi
  111. init_method=file://$(readlink -f $INIT_FILE)
  112. echo "$0: init method is $init_method"
  113. for ((i = 0; i < $gpu_num; ++i)); do
  114. {
  115. rank=$i
  116. local_rank=$i
  117. gpu_id=$(echo $CUDA_VISIBLE_DEVICES | cut -d',' -f$[$i+1])
  118. train.py \
  119. --task_name asr \
  120. --gpu_id $gpu_id \
  121. --use_preprocessor true \
  122. --token_type $token_type \
  123. --token_list $token_list \
  124. --data_dir ${feats_dir}/data \
  125. --train_set ${train_set} \
  126. --valid_set ${valid_set} \
  127. --data_file_names "wav.scp,text,embeds.scp" \
  128. --cmvn_file ${feats_dir}/data/${train_set}/cmvn/am.mvn \
  129. --speed_perturb ${speed_perturb} \
  130. --resume true \
  131. --output_dir ${exp_dir}/exp/${model_dir} \
  132. --config $asr_config \
  133. --ngpu $gpu_num \
  134. --num_worker_count $count \
  135. --dist_init_method $init_method \
  136. --dist_world_size $world_size \
  137. --dist_rank $rank \
  138. --local_rank $local_rank 1> ${exp_dir}/exp/${model_dir}/log/train.log.$i 2>&1
  139. } &
  140. done
  141. wait
  142. fi
  143. # Testing Stage
  144. if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then
  145. echo "stage 5: Inference"
  146. for dset in ${test_sets}; do
  147. asr_exp=${exp_dir}/exp/${model_dir}
  148. inference_tag="$(basename "${inference_config}" .yaml)"
  149. _dir="${asr_exp}/${inference_tag}/${inference_asr_model}/${dset}"
  150. _logdir="${_dir}/logdir"
  151. if [ -d ${_dir} ]; then
  152. echo "${_dir} is already exists. if you want to decode again, please delete this dir first."
  153. exit 0
  154. fi
  155. mkdir -p "${_logdir}"
  156. _data="${feats_dir}/data/${dset}"
  157. key_file=${_data}/${scp}
  158. num_scp_file="$(<${key_file} wc -l)"
  159. _nj=$([ $inference_nj -le $num_scp_file ] && echo "$inference_nj" || echo "$num_scp_file")
  160. split_scps=
  161. for n in $(seq "${_nj}"); do
  162. split_scps+=" ${_logdir}/keys.${n}.scp"
  163. done
  164. # shellcheck disable=SC2086
  165. utils/split_scp.pl "${key_file}" ${split_scps}
  166. _opts=
  167. if [ -n "${inference_config}" ]; then
  168. _opts+="--config ${inference_config} "
  169. fi
  170. ${infer_cmd} --gpu "${_ngpu}" --max-jobs-run "${_nj}" JOB=1:"${_nj}" "${_logdir}"/asr_inference.JOB.log \
  171. python -m funasr.bin.asr_inference_launch \
  172. --batch_size 1 \
  173. --ngpu "${_ngpu}" \
  174. --njob ${njob} \
  175. --gpuid_list ${gpuid_list} \
  176. --data_path_and_name_and_type "${_data}/${scp},speech,${type}" \
  177. --cmvn_file ${feats_dir}/data/${train_set}/cmvn/am.mvn \
  178. --key_file "${_logdir}"/keys.JOB.scp \
  179. --asr_train_config "${asr_exp}"/config.yaml \
  180. --asr_model_file "${asr_exp}"/"${inference_asr_model}" \
  181. --output_dir "${_logdir}"/output.JOB \
  182. --mode paraformer \
  183. ${_opts}
  184. for f in token token_int score text; do
  185. if [ -f "${_logdir}/output.1/1best_recog/${f}" ]; then
  186. for i in $(seq "${_nj}"); do
  187. cat "${_logdir}/output.${i}/1best_recog/${f}"
  188. done | sort -k1 >"${_dir}/${f}"
  189. fi
  190. done
  191. python utils/proce_text.py ${_dir}/text ${_dir}/text.proc
  192. python utils/proce_text.py ${_data}/text ${_data}/text.proc
  193. python utils/compute_wer.py ${_data}/text.proc ${_dir}/text.proc ${_dir}/text.cer
  194. tail -n 3 ${_dir}/text.cer > ${_dir}/text.cer.txt
  195. cat ${_dir}/text.cer.txt
  196. done
  197. fi
  198. # Prepare files for ModelScope fine-tuning and inference
  199. if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then
  200. echo "stage 6: ModelScope Preparation"
  201. cp ${feats_dir}/data/${train_set}/cmvn/am.mvn ${exp_dir}/exp/${model_dir}/am.mvn
  202. vocab_size=$(cat ${token_list} | wc -l)
  203. python utils/gen_modelscope_configuration.py \
  204. --am_model_name $inference_asr_model \
  205. --mode paraformer \
  206. --model_name paraformer_bert \
  207. --dataset aishell \
  208. --output_dir $exp_dir/exp/$model_dir \
  209. --vocab_size $vocab_size \
  210. --nat _nat \
  211. --tag $tag
  212. fi