run.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. asr_config=train_asr_paraformer_conformer_12e_6d_2048_256.yaml
  33. model_dir="baseline_$(basename "${asr_config}" .yaml)_${lang}_${token_type}_${tag}"
  34. #inference_config=conf/decode_asr_transformer_noctc_1best.yaml
  35. #inference_asr_model=valid.acc.ave_10best.pb
  36. ## you can set gpu num for decoding here
  37. #gpuid_list=$CUDA_VISIBLE_DEVICES # set gpus for decoding, the same as training stage by default
  38. #ngpu=$(echo $gpuid_list | awk -F "," '{print NF}')
  39. #
  40. #if ${gpu_inference}; then
  41. # inference_nj=$[${ngpu}*${njob}]
  42. # _ngpu=1
  43. #else
  44. # inference_nj=$njob
  45. # _ngpu=0
  46. #fi
  47. if [ ${stage} -le -1 ] && [ ${stop_stage} -ge -1 ]; then
  48. echo "stage -1: Data Download"
  49. local/download_and_untar.sh ${raw_data} ${data_url} data_aishell
  50. local/download_and_untar.sh ${raw_data} ${data_url} resource_aishell
  51. fi
  52. if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ]; then
  53. echo "stage 0: Data preparation"
  54. # Data preparation
  55. local/aishell_data_prep.sh ${raw_data}/data_aishell/wav ${raw_data}/data_aishell/transcript ${feats_dir}
  56. for x in train dev test; do
  57. cp ${feats_dir}/data/${x}/text ${feats_dir}/data/${x}/text.org
  58. paste -d " " <(cut -f 1 -d" " ${feats_dir}/data/${x}/text.org) <(cut -f 2- -d" " ${feats_dir}/data/${x}/text.org | tr -d " ") \
  59. > ${feats_dir}/data/${x}/text
  60. utils/text2token.py -n 1 -s 1 ${feats_dir}/data/${x}/text > ${feats_dir}/data/${x}/text.org
  61. mv ${feats_dir}/data/${x}/text.org ${feats_dir}/data/${x}/text
  62. # convert wav.scp text to jsonl
  63. scp_file_list_arg="++scp_file_list='[\"${feats_dir}/data/${x}/wav.scp\",\"${feats_dir}/data/${x}/text\"]'"
  64. python ../../../funasr/datasets/audio_datasets/scp2jsonl.py \
  65. ++data_type_list='["source", "target"]' \
  66. ++jsonl_file_out=${feats_dir}/data/${x}/audio_datasets.jsonl \
  67. ${scp_file_list_arg}
  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. python ../../../funasr/bin/compute_audio_cmvn.py \
  74. --config-path "${workspace}" \
  75. --config-name "${asr_config}" \
  76. ++train_data_set_list="${feats_dir}/data/${train_set}/audio_datasets.jsonl" \
  77. ++cmvn_file="${feats_dir}/data/${train_set}/cmvn.json" \
  78. ++dataset_conf.num_workers=$nj
  79. fi
  80. token_list=${feats_dir}/data/${lang}_token_list/$token_type/tokens.txt
  81. echo "dictionary: ${token_list}"
  82. if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then
  83. echo "stage 2: Dictionary Preparation"
  84. mkdir -p ${feats_dir}/data/${lang}_token_list/$token_type/
  85. echo "make a dictionary"
  86. echo "<blank>" > ${token_list}
  87. echo "<s>" >> ${token_list}
  88. echo "</s>" >> ${token_list}
  89. utils/text2token.py -s 1 -n 1 --space "" ${feats_dir}/data/$train_set/text | cut -f 2- -d" " | tr " " "\n" \
  90. | sort | uniq | grep -a -v -e '^\s*$' | awk '{print $0}' >> ${token_list}
  91. echo "<unk>" >> ${token_list}
  92. fi
  93. # LM Training Stage
  94. if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then
  95. echo "stage 3: LM Training"
  96. fi
  97. # ASR Training Stage
  98. if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ]; then
  99. echo "stage 4: ASR Training"
  100. torchrun \
  101. --nnodes 1 \
  102. --nproc_per_node ${gpu_num} \
  103. ../../../funasr/bin/train.py \
  104. --config-path "${workspace}" \
  105. --config-name "${asr_config}" \
  106. ++train_data_set_list="${feats_dir}/data/${train_set}/audio_datasets.jsonl" \
  107. ++cmvn_file="${feats_dir}/data/${train_set}/am.mvn" \
  108. ++token_list="${token_list}" \
  109. ++output_dir="${exp_dir}/exp/${model_dir}"
  110. fi
  111. #
  112. ## Testing Stage
  113. #if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then
  114. # echo "stage 5: Inference"
  115. # for dset in ${test_sets}; do
  116. # asr_exp=${exp_dir}/exp/${model_dir}
  117. # inference_tag="$(basename "${inference_config}" .yaml)"
  118. # _dir="${asr_exp}/${inference_tag}/${inference_asr_model}/${dset}"
  119. # _logdir="${_dir}/logdir"
  120. # if [ -d ${_dir} ]; then
  121. # echo "${_dir} is already exists. if you want to decode again, please delete this dir first."
  122. # exit 0
  123. # fi
  124. # mkdir -p "${_logdir}"
  125. # _data="${feats_dir}/data/${dset}"
  126. # key_file=${_data}/${scp}
  127. # num_scp_file="$(<${key_file} wc -l)"
  128. # _nj=$([ $inference_nj -le $num_scp_file ] && echo "$inference_nj" || echo "$num_scp_file")
  129. # split_scps=
  130. # for n in $(seq "${_nj}"); do
  131. # split_scps+=" ${_logdir}/keys.${n}.scp"
  132. # done
  133. # # shellcheck disable=SC2086
  134. # utils/split_scp.pl "${key_file}" ${split_scps}
  135. # _opts=
  136. # if [ -n "${inference_config}" ]; then
  137. # _opts+="--config ${inference_config} "
  138. # fi
  139. # ${infer_cmd} --gpu "${_ngpu}" --max-jobs-run "${_nj}" JOB=1:"${_nj}" "${_logdir}"/asr_inference.JOB.log \
  140. # python -m funasr.bin.asr_inference_launch \
  141. # --batch_size 1 \
  142. # --ngpu "${_ngpu}" \
  143. # --njob ${njob} \
  144. # --gpuid_list ${gpuid_list} \
  145. # --data_path_and_name_and_type "${_data}/${scp},speech,${type}" \
  146. # --cmvn_file ${feats_dir}/data/${train_set}/cmvn/am.mvn \
  147. # --key_file "${_logdir}"/keys.JOB.scp \
  148. # --asr_train_config "${asr_exp}"/config.yaml \
  149. # --asr_model_file "${asr_exp}"/"${inference_asr_model}" \
  150. # --output_dir "${_logdir}"/output.JOB \
  151. # --mode paraformer \
  152. # ${_opts}
  153. #
  154. # for f in token token_int score text; do
  155. # if [ -f "${_logdir}/output.1/1best_recog/${f}" ]; then
  156. # for i in $(seq "${_nj}"); do
  157. # cat "${_logdir}/output.${i}/1best_recog/${f}"
  158. # done | sort -k1 >"${_dir}/${f}"
  159. # fi
  160. # done
  161. # python utils/proce_text.py ${_dir}/text ${_dir}/text.proc
  162. # python utils/proce_text.py ${_data}/text ${_data}/text.proc
  163. # python utils/compute_wer.py ${_data}/text.proc ${_dir}/text.proc ${_dir}/text.cer
  164. # tail -n 3 ${_dir}/text.cer > ${_dir}/text.cer.txt
  165. # cat ${_dir}/text.cer.txt
  166. # done
  167. #fi
  168. #
  169. ## Prepare files for ModelScope fine-tuning and inference
  170. #if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then
  171. # echo "stage 6: ModelScope Preparation"
  172. # cp ${feats_dir}/data/${train_set}/cmvn/am.mvn ${exp_dir}/exp/${model_dir}/am.mvn
  173. # vocab_size=$(cat ${token_list} | wc -l)
  174. # python utils/gen_modelscope_configuration.py \
  175. # --am_model_name $inference_asr_model \
  176. # --mode paraformer \
  177. # --model_name paraformer \
  178. # --dataset aishell \
  179. # --output_dir $exp_dir/exp/$model_dir \
  180. # --vocab_size $vocab_size \
  181. # --nat _nat \
  182. # --tag $tag
  183. #fi