test_inference_python.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/bash
  2. source test_tipc/common_func.sh
  3. #source test_tipc/test_train_inference_python.sh
  4. FILENAME=$1
  5. # MODE be one of ['whole_infer']
  6. MODE=$2
  7. dataline=$(awk 'NR==1, NR==20{print}' $FILENAME)
  8. # parser params
  9. IFS=$'\n'
  10. lines=(${dataline})
  11. model_name=$(func_parser_value "${lines[1]}")
  12. python=$(func_parser_value "${lines[2]}")
  13. infer_model_dir_list=$(func_parser_value "${lines[3]}")
  14. infer_export_list=$(func_parser_value "${lines[4]}")
  15. infer_is_quant=$(func_parser_value "${lines[5]}")
  16. # parser inference
  17. inference_py=$(func_parser_value "${lines[6]}")
  18. use_gpu_key=$(func_parser_key "${lines[7]}")
  19. use_gpu_list=$(func_parser_value "${lines[7]}")
  20. use_mkldnn_key=$(func_parser_key "${lines[8]}")
  21. use_mkldnn_list=$(func_parser_value "${lines[8]}")
  22. cpu_threads_key=$(func_parser_key "${lines[9]}")
  23. cpu_threads_list=$(func_parser_value "${lines[9]}")
  24. batch_size_key=$(func_parser_key "${lines[10]}")
  25. batch_size_list=$(func_parser_value "${lines[10]}")
  26. use_trt_key=$(func_parser_key "${lines[11]}")
  27. use_trt_list=$(func_parser_value "${lines[11]}")
  28. precision_key=$(func_parser_key "${lines[12]}")
  29. precision_list=$(func_parser_value "${lines[12]}")
  30. infer_model_key=$(func_parser_key "${lines[13]}")
  31. image_dir_key=$(func_parser_key "${lines[14]}")
  32. infer_img_dir=$(func_parser_value "${lines[14]}")
  33. rec_model_key=$(func_parser_key "${lines[15]}")
  34. rec_model_value=$(func_parser_value "${lines[15]}")
  35. benchmark_key=$(func_parser_key "${lines[16]}")
  36. benchmark_value=$(func_parser_value "${lines[16]}")
  37. infer_key1=$(func_parser_key "${lines[17]}")
  38. infer_value1=$(func_parser_value "${lines[17]}")
  39. LOG_PATH="./test_tipc/output/${model_name}/${MODE}"
  40. mkdir -p ${LOG_PATH}
  41. status_log="${LOG_PATH}/results_python.log"
  42. function func_inference(){
  43. IFS='|'
  44. _python=$1
  45. _script=$2
  46. _model_dir=$3
  47. _log_path=$4
  48. _img_dir=$5
  49. _flag_quant=$6
  50. # inference
  51. for use_gpu in ${use_gpu_list[*]}; do
  52. if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then
  53. for use_mkldnn in ${use_mkldnn_list[*]}; do
  54. if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then
  55. continue
  56. fi
  57. for threads in ${cpu_threads_list[*]}; do
  58. for batch_size in ${batch_size_list[*]}; do
  59. for precision in ${precision_list[*]}; do
  60. if [ ${use_mkldnn} = "False" ] && [ ${precision} = "fp16" ]; then
  61. continue
  62. fi # skip when enable fp16 but disable mkldnn
  63. if [ ${_flag_quant} = "True" ] && [ ${precision} != "int8" ]; then
  64. continue
  65. fi # skip when quant model inference but precision is not int8
  66. set_precision=$(func_set_params "${precision_key}" "${precision}")
  67. _save_log_path="${_log_path}/python_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_${precision}_batchsize_${batch_size}.log"
  68. set_infer_data=$(func_set_params "${image_dir_key}" "${_img_dir}")
  69. set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
  70. set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  71. set_mkldnn=$(func_set_params "${use_mkldnn_key}" "${use_mkldnn}")
  72. set_cpu_threads=$(func_set_params "${cpu_threads_key}" "${threads}")
  73. set_model_dir=$(func_set_params "${infer_model_key}" "${_model_dir}")
  74. set_infer_params0=$(func_set_params "${rec_model_key}" "${rec_model_value}")
  75. set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
  76. command="${_python} ${_script} ${use_gpu_key}=${use_gpu} ${set_mkldnn} ${set_cpu_threads} ${set_model_dir} ${set_batchsize} ${set_infer_params0} ${set_infer_data} ${set_benchmark} ${set_precision} ${set_infer_params1} > ${_save_log_path} 2>&1 "
  77. eval $command
  78. last_status=${PIPESTATUS[0]}
  79. eval "cat ${_save_log_path}"
  80. status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
  81. done
  82. done
  83. done
  84. done
  85. elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
  86. for use_trt in ${use_trt_list[*]}; do
  87. for precision in ${precision_list[*]}; do
  88. if [[ ${_flag_quant} = "False" ]] && [[ ${precision} =~ "int8" ]]; then
  89. continue
  90. fi
  91. if [[ ${precision} =~ "fp16" || ${precision} =~ "int8" ]] && [ ${use_trt} = "False" ]; then
  92. continue
  93. fi
  94. if [[ ${use_trt} = "False" || ${precision} =~ "int8" ]] && [ ${_flag_quant} = "True" ]; then
  95. continue
  96. fi
  97. for batch_size in ${batch_size_list[*]}; do
  98. _save_log_path="${_log_path}/python_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}.log"
  99. set_infer_data=$(func_set_params "${image_dir_key}" "${_img_dir}")
  100. set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
  101. set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  102. set_tensorrt=$(func_set_params "${use_trt_key}" "${use_trt}")
  103. set_precision=$(func_set_params "${precision_key}" "${precision}")
  104. set_model_dir=$(func_set_params "${infer_model_key}" "${_model_dir}")
  105. set_infer_params0=$(func_set_params "${rec_model_key}" "${rec_model_value}")
  106. set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
  107. command="${_python} ${_script} ${use_gpu_key}=${use_gpu} ${set_tensorrt} ${set_precision} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_infer_params1} ${set_infer_params0} > ${_save_log_path} 2>&1 "
  108. eval $command
  109. last_status=${PIPESTATUS[0]}
  110. eval "cat ${_save_log_path}"
  111. status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
  112. done
  113. done
  114. done
  115. else
  116. echo "Does not support hardware other than CPU and GPU Currently!"
  117. fi
  118. done
  119. }
  120. if [ ${MODE} = "whole_infer" ]; then
  121. GPUID=$3
  122. if [ ${#GPUID} -le 0 ];then
  123. env=" "
  124. else
  125. env="export CUDA_VISIBLE_DEVICES=${GPUID}"
  126. fi
  127. # set CUDA_VISIBLE_DEVICES
  128. eval $env
  129. export Count=0
  130. IFS="|"
  131. infer_run_exports=(${infer_export_list})
  132. infer_quant_flag=(${infer_is_quant})
  133. for infer_model in ${infer_model_dir_list[*]}; do
  134. # run export
  135. if [ ${infer_run_exports[Count]} != "null" ];then
  136. _save_log_path="${_log_path}/python_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}_infermodel_${infer_model}.log"
  137. save_infer_dir=$(dirname $infer_model)
  138. set_export_weight=$(func_set_params "${export_weight}" "${infer_model}")
  139. set_save_infer_key=$(func_set_params "${save_infer_key}" "${save_infer_dir}")
  140. export_cmd="${python} ${infer_run_exports[Count]} ${set_export_weight} ${set_save_infer_key} > ${_save_log_path} 2>&1 "
  141. echo ${infer_run_exports[Count]}
  142. eval $export_cmd
  143. status_export=$?
  144. status_check $status_export "${export_cmd}" "${status_log}" "${model_name}" "${_save_log_path}"
  145. else
  146. save_infer_dir=${infer_model}
  147. fi
  148. #run inference
  149. is_quant=${infer_quant_flag[Count]}
  150. if [ ${MODE} = "klquant_infer" ]; then
  151. is_quant="True"
  152. fi
  153. func_inference "${python}" "${inference_py}" "${save_infer_dir}" "${LOG_PATH}" "${infer_img_dir}" ${is_quant}
  154. Count=$(($Count + 1))
  155. done
  156. fi