run.sh 7.9 KB

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