test_inference_cpp.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/bash
  2. source test_tipc/common_func.sh
  3. FILENAME=$1
  4. dataline=$(awk 'NR==1, NR==20{print}' $FILENAME)
  5. # parser params
  6. IFS=$'\n'
  7. lines=(${dataline})
  8. # parser cpp inference model
  9. model_name=$(func_parser_value "${lines[1]}")
  10. use_opencv=$(func_parser_value "${lines[2]}")
  11. cpp_infer_model_dir_list=$(func_parser_value "${lines[3]}")
  12. cpp_infer_is_quant=$(func_parser_value "${lines[4]}")
  13. # parser cpp inference
  14. inference_cmd=$(func_parser_value "${lines[5]}")
  15. cpp_use_gpu_key=$(func_parser_key "${lines[6]}")
  16. cpp_use_gpu_list=$(func_parser_value "${lines[6]}")
  17. cpp_use_mkldnn_key=$(func_parser_key "${lines[7]}")
  18. cpp_use_mkldnn_list=$(func_parser_value "${lines[7]}")
  19. cpp_cpu_threads_key=$(func_parser_key "${lines[8]}")
  20. cpp_cpu_threads_list=$(func_parser_value "${lines[8]}")
  21. cpp_batch_size_key=$(func_parser_key "${lines[9]}")
  22. cpp_batch_size_list=$(func_parser_value "${lines[9]}")
  23. cpp_use_trt_key=$(func_parser_key "${lines[10]}")
  24. cpp_use_trt_list=$(func_parser_value "${lines[10]}")
  25. cpp_precision_key=$(func_parser_key "${lines[11]}")
  26. cpp_precision_list=$(func_parser_value "${lines[11]}")
  27. cpp_infer_model_key=$(func_parser_key "${lines[12]}")
  28. cpp_image_dir_key=$(func_parser_key "${lines[13]}")
  29. cpp_infer_img_dir=$(func_parser_value "${lines[13]}")
  30. cpp_infer_key1=$(func_parser_key "${lines[14]}")
  31. cpp_infer_value1=$(func_parser_value "${lines[14]}")
  32. cpp_benchmark_key=$(func_parser_key "${lines[15]}")
  33. cpp_benchmark_value=$(func_parser_value "${lines[15]}")
  34. cpp_det_key=$(func_parser_key "${lines[16]}")
  35. cpp_det_value=$(func_parser_value "${lines[16]}")
  36. cpp_rec_key=$(func_parser_key "${lines[17]}")
  37. cpp_rec_value=$(func_parser_value "${lines[17]}")
  38. cpp_cls_key=$(func_parser_key "${lines[18]}")
  39. cpp_cls_value=$(func_parser_value "${lines[18]}")
  40. cpp_use_angle_cls_key=$(func_parser_key "${lines[19]}")
  41. cpp_use_angle_cls_value=$(func_parser_value "${lines[19]}")
  42. LOG_PATH="./test_tipc/output/${model_name}/cpp_infer"
  43. mkdir -p ${LOG_PATH}
  44. status_log="${LOG_PATH}/results_cpp.log"
  45. function func_cpp_inference(){
  46. IFS='|'
  47. _script=$1
  48. _model_dir=$2
  49. _log_path=$3
  50. _img_dir=$4
  51. _flag_quant=$5
  52. # inference
  53. for use_gpu in ${cpp_use_gpu_list[*]}; do
  54. if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then
  55. for use_mkldnn in ${cpp_use_mkldnn_list[*]}; do
  56. if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then
  57. continue
  58. fi
  59. for threads in ${cpp_cpu_threads_list[*]}; do
  60. for batch_size in ${cpp_batch_size_list[*]}; do
  61. precision="fp32"
  62. if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then
  63. precison="int8"
  64. fi
  65. _save_log_path="${_log_path}/cpp_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_${precision}_batchsize_${batch_size}.log"
  66. set_infer_data=$(func_set_params "${cpp_image_dir_key}" "${_img_dir}")
  67. set_benchmark=$(func_set_params "${cpp_benchmark_key}" "${cpp_benchmark_value}")
  68. set_batchsize=$(func_set_params "${cpp_batch_size_key}" "${batch_size}")
  69. set_mkldnn=$(func_set_params "${cpp_use_mkldnn_key}" "${use_mkldnn}")
  70. set_cpu_threads=$(func_set_params "${cpp_cpu_threads_key}" "${threads}")
  71. set_model_dir=$(func_set_params "${cpp_infer_model_key}" "${_model_dir}")
  72. set_infer_params1=$(func_set_params "${cpp_infer_key1}" "${cpp_infer_value1}")
  73. set_det=$(func_set_params "${cpp_det_key}" "${cpp_det_value}")
  74. set_rec=$(func_set_params "${cpp_rec_key}" "${cpp_rec_value}")
  75. set_cls=$(func_set_params "${cpp_cls_key}" "${cpp_cls_value}")
  76. set_use_angle_cls=$(func_set_params "${cpp_use_angle_cls_key}" "${cpp_use_angle_cls_value}")
  77. command="${_script} ${cpp_use_gpu_key}=${use_gpu} ${set_mkldnn} ${set_cpu_threads} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_det} ${set_rec} ${set_cls} ${set_use_angle_cls} ${set_infer_params1} > ${_save_log_path} 2>&1 "
  78. eval $command
  79. last_status=${PIPESTATUS[0]}
  80. eval "cat ${_save_log_path}"
  81. status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
  82. done
  83. done
  84. done
  85. elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
  86. for use_trt in ${cpp_use_trt_list[*]}; do
  87. for precision in ${cpp_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 ${cpp_batch_size_list[*]}; do
  98. _save_log_path="${_log_path}/cpp_infer_gpu_usetrt_${use_trt}_precision_${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_tensorrt=$(func_set_params "${cpp_use_trt_key}" "${use_trt}")
  103. set_precision=$(func_set_params "${cpp_precision_key}" "${precision}")
  104. set_model_dir=$(func_set_params "${cpp_infer_model_key}" "${_model_dir}")
  105. set_infer_params1=$(func_set_params "${cpp_infer_key1}" "${cpp_infer_value1}")
  106. set_det=$(func_set_params "${cpp_det_key}" "${cpp_det_value}")
  107. set_rec=$(func_set_params "${cpp_rec_key}" "${cpp_rec_value}")
  108. set_cls=$(func_set_params "${cpp_cls_key}" "${cpp_cls_value}")
  109. set_use_angle_cls=$(func_set_params "${cpp_use_angle_cls_key}" "${cpp_use_angle_cls_value}")
  110. command="${_script} ${cpp_use_gpu_key}=${use_gpu} ${set_tensorrt} ${set_precision} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_det} ${set_rec} ${set_cls} ${set_use_angle_cls} ${set_infer_params1} > ${_save_log_path} 2>&1 "
  111. eval $command
  112. last_status=${PIPESTATUS[0]}
  113. eval "cat ${_save_log_path}"
  114. status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
  115. done
  116. done
  117. done
  118. else
  119. echo "Does not support hardware other than CPU and GPU Currently!"
  120. fi
  121. done
  122. }
  123. cd deploy/cpp_infer
  124. if [ ${use_opencv} = "True" ]; then
  125. if [ -d "opencv-3.4.7/opencv3/" ] && [ $(md5sum opencv-3.4.7.tar.gz | awk -F ' ' '{print $1}') = "faa2b5950f8bee3f03118e600c74746a" ];then
  126. echo "################### build opencv skipped ###################"
  127. else
  128. echo "################### build opencv ###################"
  129. rm -rf opencv-3.4.7.tar.gz opencv-3.4.7/
  130. wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/test/opencv-3.4.7.tar.gz
  131. tar -xf opencv-3.4.7.tar.gz
  132. cd opencv-3.4.7/
  133. install_path=$(pwd)/opencv3
  134. rm -rf build
  135. mkdir build
  136. cd build
  137. cmake .. \
  138. -DCMAKE_INSTALL_PREFIX=${install_path} \
  139. -DCMAKE_BUILD_TYPE=Release \
  140. -DBUILD_SHARED_LIBS=OFF \
  141. -DWITH_IPP=OFF \
  142. -DBUILD_IPP_IW=OFF \
  143. -DWITH_LAPACK=OFF \
  144. -DWITH_EIGEN=OFF \
  145. -DCMAKE_INSTALL_LIBDIR=lib64 \
  146. -DWITH_ZLIB=ON \
  147. -DBUILD_ZLIB=ON \
  148. -DWITH_JPEG=ON \
  149. -DBUILD_JPEG=ON \
  150. -DWITH_PNG=ON \
  151. -DBUILD_PNG=ON \
  152. -DWITH_TIFF=ON \
  153. -DBUILD_TIFF=ON
  154. make -j
  155. make install
  156. cd ../..
  157. echo "################### build opencv finished ###################"
  158. fi
  159. fi
  160. echo "################### build PaddleOCR demo ####################"
  161. if [ ${use_opencv} = "True" ]; then
  162. OPENCV_DIR=$(pwd)/opencv-3.4.7/opencv3/
  163. else
  164. OPENCV_DIR=''
  165. fi
  166. if [ -d "paddle_inference/" ] ;then
  167. echo "################### download paddle inference skipped ###################"
  168. else
  169. echo "################### download paddle inference ###################"
  170. PADDLEInfer=$3
  171. if [ "" = "$PADDLEInfer" ];then
  172. wget -nc https://paddle-inference-lib.bj.bcebos.com/2.3.0/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
  173. else
  174. wget -nc $PADDLEInfer --no-check-certificate
  175. fi
  176. tar zxf paddle_inference.tgz
  177. if [ ! -d "paddle_inference" ]; then
  178. ln -s paddle_inference_install_dir paddle_inference
  179. fi
  180. echo "################### download paddle inference finished ###################"
  181. fi
  182. LIB_DIR=$(pwd)/paddle_inference/
  183. CUDA_LIB_DIR=$(dirname `find /usr -name libcudart.so`)
  184. CUDNN_LIB_DIR=$(dirname `find /usr -name libcudnn.so`)
  185. BUILD_DIR=build
  186. rm -rf ${BUILD_DIR}
  187. mkdir ${BUILD_DIR}
  188. cd ${BUILD_DIR}
  189. cmake .. \
  190. -DPADDLE_LIB=${LIB_DIR} \
  191. -DWITH_MKL=ON \
  192. -DWITH_GPU=OFF \
  193. -DWITH_STATIC_LIB=OFF \
  194. -DWITH_TENSORRT=OFF \
  195. -DOPENCV_DIR=${OPENCV_DIR} \
  196. -DCUDNN_LIB=${CUDNN_LIB_DIR} \
  197. -DCUDA_LIB=${CUDA_LIB_DIR} \
  198. -DTENSORRT_DIR=${TENSORRT_DIR} \
  199. make -j
  200. cd ../../../
  201. echo "################### build PaddleOCR demo finished ###################"
  202. # set cuda device
  203. GPUID=$2
  204. if [ ${#GPUID} -le 0 ];then
  205. env="export CUDA_VISIBLE_DEVICES=0"
  206. else
  207. env="export CUDA_VISIBLE_DEVICES=${GPUID}"
  208. fi
  209. eval $env
  210. echo "################### run test ###################"
  211. export Count=0
  212. IFS="|"
  213. infer_quant_flag=(${cpp_infer_is_quant})
  214. for infer_model in ${cpp_infer_model_dir_list[*]}; do
  215. #run inference
  216. is_quant=${infer_quant_flag[Count]}
  217. func_cpp_inference "${inference_cmd}" "${infer_model}" "${LOG_PATH}" "${cpp_infer_img_dir}" ${is_quant}
  218. Count=$(($Count + 1))
  219. done