test_inference_cpp.sh 9.3 KB

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