test_inference_cpp.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/bin/bash
  2. source test_tipc/utils_func.sh
  3. FILENAME=$1
  4. # parser model_name
  5. dataline=$(cat ${FILENAME})
  6. IFS=$'\n'
  7. lines=(${dataline})
  8. model_name=$(func_parser_value "${lines[1]}")
  9. echo "ppdet cpp_infer: ${model_name}"
  10. python=$(func_parser_value "${lines[2]}")
  11. filename_key=$(func_parser_key "${lines[3]}")
  12. filename_value=$(func_parser_value "${lines[3]}")
  13. # export params
  14. save_export_key=$(func_parser_key "${lines[5]}")
  15. save_export_value=$(func_parser_value "${lines[5]}")
  16. export_weight_key=$(func_parser_key "${lines[6]}")
  17. export_weight_value=$(func_parser_value "${lines[6]}")
  18. norm_export=$(func_parser_value "${lines[7]}")
  19. pact_export=$(func_parser_value "${lines[8]}")
  20. fpgm_export=$(func_parser_value "${lines[9]}")
  21. distill_export=$(func_parser_value "${lines[10]}")
  22. export_key1=$(func_parser_key "${lines[11]}")
  23. export_value1=$(func_parser_value "${lines[11]}")
  24. export_key2=$(func_parser_key "${lines[12]}")
  25. export_value2=$(func_parser_value "${lines[12]}")
  26. kl_quant_export=$(func_parser_value "${lines[13]}")
  27. # parser cpp inference model
  28. opencv_dir=$(func_parser_value "${lines[15]}")
  29. cpp_infer_mode_list=$(func_parser_value "${lines[16]}")
  30. cpp_infer_is_quant_list=$(func_parser_value "${lines[17]}")
  31. # parser cpp inference
  32. inference_cmd=$(func_parser_value "${lines[18]}")
  33. cpp_use_gpu_key=$(func_parser_key "${lines[19]}")
  34. cpp_use_gpu_list=$(func_parser_value "${lines[19]}")
  35. cpp_use_mkldnn_key=$(func_parser_key "${lines[20]}")
  36. cpp_use_mkldnn_list=$(func_parser_value "${lines[20]}")
  37. cpp_cpu_threads_key=$(func_parser_key "${lines[21]}")
  38. cpp_cpu_threads_list=$(func_parser_value "${lines[21]}")
  39. cpp_batch_size_key=$(func_parser_key "${lines[22]}")
  40. cpp_batch_size_list=$(func_parser_value "${lines[22]}")
  41. cpp_use_trt_key=$(func_parser_key "${lines[23]}")
  42. cpp_use_trt_list=$(func_parser_value "${lines[23]}")
  43. cpp_precision_key=$(func_parser_key "${lines[24]}")
  44. cpp_precision_list=$(func_parser_value "${lines[24]}")
  45. cpp_infer_model_key=$(func_parser_key "${lines[25]}")
  46. cpp_image_dir_key=$(func_parser_key "${lines[26]}")
  47. cpp_infer_img_dir=$(func_parser_value "${lines[26]}")
  48. cpp_benchmark_key=$(func_parser_key "${lines[27]}")
  49. cpp_benchmark_value=$(func_parser_value "${lines[27]}")
  50. cpp_infer_key1=$(func_parser_key "${lines[28]}")
  51. cpp_infer_value1=$(func_parser_value "${lines[28]}")
  52. LOG_PATH="./test_tipc/output"
  53. mkdir -p ${LOG_PATH}
  54. status_log="${LOG_PATH}/results_cpp.log"
  55. function func_cpp_inference(){
  56. IFS='|'
  57. _script=$1
  58. _model_dir=$2
  59. _log_path=$3
  60. _img_dir=$4
  61. _flag_quant=$5
  62. # inference
  63. for use_gpu in ${cpp_use_gpu_list[*]}; do
  64. if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then
  65. for use_mkldnn in ${cpp_use_mkldnn_list[*]}; do
  66. if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then
  67. continue
  68. fi
  69. for threads in ${cpp_cpu_threads_list[*]}; do
  70. for batch_size in ${cpp_batch_size_list[*]}; do
  71. _save_log_path="${_log_path}/cpp_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_fluid_batchsize_${batch_size}.log"
  72. set_infer_data=$(func_set_params "${cpp_image_dir_key}" "${_img_dir}")
  73. set_benchmark=$(func_set_params "${cpp_benchmark_key}" "${cpp_benchmark_value}")
  74. set_batchsize=$(func_set_params "${cpp_batch_size_key}" "${batch_size}")
  75. set_cpu_threads=$(func_set_params "${cpp_cpu_threads_key}" "${threads}")
  76. set_model_dir=$(func_set_params "${cpp_infer_model_key}" "${_model_dir}")
  77. set_infer_params1=$(func_set_params "${cpp_infer_key1}" "${cpp_infer_value1}")
  78. command="${_script} ${cpp_use_gpu_key}=${use_gpu} ${cpp_use_mkldnn_key}=${use_mkldnn} ${set_cpu_threads} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_infer_params1} > ${_save_log_path} 2>&1 "
  79. eval $command
  80. last_status=${PIPESTATUS[0]}
  81. eval "cat ${_save_log_path}"
  82. status_check $last_status "${command}" "${status_log}"
  83. done
  84. done
  85. done
  86. elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
  87. for precision in ${cpp_precision_list[*]}; do
  88. if [[ ${precision} != "fluid" ]]; then
  89. if [[ ${_flag_quant} = "False" ]] && [[ ${precision} = "trt_int8" ]]; then
  90. continue
  91. fi
  92. if [[ ${_flag_quant} = "True" ]] && [[ ${precision} != "trt_int8" ]]; then
  93. continue
  94. fi
  95. fi
  96. for batch_size in ${cpp_batch_size_list[*]}; do
  97. _save_log_path="${_log_path}/cpp_infer_gpu_precision_${precision}_batchsize_${batch_size}.log"
  98. set_infer_data=$(func_set_params "${cpp_image_dir_key}" "${_img_dir}")
  99. set_benchmark=$(func_set_params "${cpp_benchmark_key}" "${cpp_benchmark_value}")
  100. set_batchsize=$(func_set_params "${cpp_batch_size_key}" "${batch_size}")
  101. set_precision=$(func_set_params "${cpp_precision_key}" "${precision}")
  102. set_model_dir=$(func_set_params "${cpp_infer_model_key}" "${_model_dir}")
  103. set_infer_params1=$(func_set_params "${cpp_infer_key1}" "${cpp_infer_value1}")
  104. command="${_script} ${cpp_use_gpu_key}=${use_gpu} ${set_precision} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_infer_params1} > ${_save_log_path} 2>&1 "
  105. eval $command
  106. last_status=${PIPESTATUS[0]}
  107. eval "cat ${_save_log_path}"
  108. status_check $last_status "${command}" "${status_log}"
  109. done
  110. done
  111. else
  112. echo "Does not support hardware other than CPU and GPU Currently!"
  113. fi
  114. done
  115. }
  116. cd ./deploy/cpp
  117. # set OPENCV_DIR
  118. if [ ${opencv_dir} = "default" ] || [ ${opencv_dir} = "null" ]; then
  119. OPENCV_DIR=$(pwd)/deps/opencv-3.4.16_gcc8.2_ffmpeg
  120. else
  121. OPENCV_DIR=${opencv_dir}
  122. fi
  123. # build program
  124. # TODO: set PADDLE_DIR and TENSORRT_ROOT
  125. if [ -z $PADDLE_DIR ]; then
  126. PADDLE_DIR=/paddle/Paddle/build/paddle_inference_install_dir/
  127. fi
  128. if [ -z $TENSORRT_ROOT ]; then
  129. TENSORRT_ROOT=/usr/local/TensorRT6-cuda10.1-cudnn7
  130. fi
  131. CUDA_LIB=$(dirname `find /usr -name libcudart.so`)
  132. CUDNN_LIB=$(dirname `find /usr -name libcudnn.so`)
  133. TENSORRT_LIB_DIR="${TENSORRT_ROOT}/lib"
  134. TENSORRT_INC_DIR="${TENSORRT_ROOT}/include"
  135. rm -rf build
  136. mkdir -p build
  137. cd ./build
  138. cmake .. \
  139. -DWITH_GPU=ON \
  140. -DWITH_MKL=ON \
  141. -DWITH_TENSORRT=ON \
  142. -DPADDLE_LIB_NAME=libpaddle_inference \
  143. -DPADDLE_DIR=${PADDLE_DIR} \
  144. -DCUDA_LIB=${CUDA_LIB} \
  145. -DCUDNN_LIB=${CUDNN_LIB} \
  146. -DTENSORRT_LIB_DIR=${TENSORRT_LIB_DIR} \
  147. -DTENSORRT_INC_DIR=${TENSORRT_INC_DIR} \
  148. -DOPENCV_DIR=${OPENCV_DIR} \
  149. -DWITH_KEYPOINT=ON \
  150. -DWITH_MOT=ON
  151. make -j4
  152. cd ../../../
  153. echo "################### build finished! ###################"
  154. # set cuda device
  155. GPUID=$2
  156. if [ ${#GPUID} -le 0 ];then
  157. env=" "
  158. else
  159. env="export CUDA_VISIBLE_DEVICES=${GPUID}"
  160. fi
  161. eval $env
  162. # run cpp infer
  163. Count=0
  164. IFS="|"
  165. infer_quant_flag=(${cpp_infer_is_quant_list})
  166. for infer_mode in ${cpp_infer_mode_list[*]}; do
  167. # run export
  168. case ${infer_mode} in
  169. norm) run_export=${norm_export} ;;
  170. quant) run_export=${pact_export} ;;
  171. fpgm) run_export=${fpgm_export} ;;
  172. distill) run_export=${distill_export} ;;
  173. kl_quant) run_export=${kl_quant_export} ;;
  174. *) echo "Undefined infer_mode!"; exit 1;
  175. esac
  176. if [ ${run_export} = "null" ]; then
  177. continue
  178. fi
  179. set_export_weight=$(func_set_params "${export_weight_key}" "${export_weight_value}")
  180. set_save_export_dir=$(func_set_params "${save_export_key}" "${save_export_value}")
  181. set_filename=$(func_set_params "${filename_key}" "${filename_value}")
  182. export_cmd="${python} ${run_export} ${set_export_weight} ${set_filename} ${set_save_export_dir} "
  183. echo $export_cmd
  184. eval $export_cmd
  185. status_export=$?
  186. status_check $status_export "${export_cmd}" "${status_log}"
  187. #run inference
  188. save_export_model_dir="${save_export_value}/${filename_value}"
  189. is_quant=${infer_quant_flag[Count]}
  190. func_cpp_inference "${inference_cmd}" "${save_export_model_dir}" "${LOG_PATH}" "${cpp_infer_img_dir}" ${is_quant}
  191. Count=$(($Count + 1))
  192. done
  193. eval "unset CUDA_VISIBLE_DEVICES"