test_paddle2onnx.sh 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/bin/bash
source test_tipc/utils_func.sh

FILENAME=$1

# parser model_name
dataline=$(cat ${FILENAME})
IFS=$'\n'
lines=(${dataline})
model_name=$(func_parser_value "${lines[1]}")
echo "ppdet onnx_infer: ${model_name}"
python=$(func_parser_value "${lines[2]}")
filename_key=$(func_parser_key "${lines[3]}")
filename_value=$(func_parser_value "${lines[3]}")

# export params
save_export_key=$(func_parser_key "${lines[5]}")
save_export_value=$(func_parser_value "${lines[5]}")
export_weight_key=$(func_parser_key "${lines[6]}")
export_weight_value=$(func_parser_value "${lines[6]}")
norm_export=$(func_parser_value "${lines[7]}")
pact_export=$(func_parser_value "${lines[8]}")
fpgm_export=$(func_parser_value "${lines[9]}")
distill_export=$(func_parser_value "${lines[10]}")
export_key1=$(func_parser_key "${lines[11]}")
export_value1=$(func_parser_value "${lines[11]}")
27 28
export_param_key=$(func_parser_key "${lines[12]}")
export_param_value=$(func_parser_value "${lines[12]}")
29 30
kl_quant_export=$(func_parser_value "${lines[13]}")

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# parser paddle2onnx params
infer_mode_list=$(func_parser_value "${lines[15]}")
infer_is_quant_list=$(func_parser_value "${lines[16]}")

padlle2onnx_cmd=$(func_parser_value "${lines[17]}")
model_dir_key=$(func_parser_key "${lines[18]}")
model_filename_key=$(func_parser_key "${lines[19]}")
model_filename_value=$(func_parser_value "${lines[19]}")
params_filename_key=$(func_parser_key "${lines[20]}")
params_filename_value=$(func_parser_value "${lines[20]}")
save_file_key=$(func_parser_key "${lines[21]}")
save_file_value=$(func_parser_value "${lines[21]}")
opset_version_key=$(func_parser_key "${lines[22]}")
opset_version_value=$(func_parser_value "${lines[22]}")
enable_onnx_checker_key=$(func_parser_key "${lines[23]}")
enable_onnx_checker_value=$(func_parser_value "${lines[23]}")
paddle2onnx_params1_key=$(func_parser_key "${lines[24]}")
paddle2onnx_params1_value=$(func_parser_value "${lines[24]}")
49 50

# parser onnx inference 
51 52 53 54 55 56 57
inference_py=$(func_parser_value "${lines[25]}")
infer_cfg_key=$(func_parser_key "${lines[26]}")
onnx_file_key=$(func_parser_key "${lines[27]}")
infer_image_key=$(func_parser_key "${lines[28]}")
infer_image_value=$(func_parser_value "${lines[28]}")
infer_param1_key=$(func_parser_key "${lines[29]}")
infer_param1_value=$(func_parser_value "${lines[29]}")
58 59 60 61 62

LOG_PATH="./test_tipc/output"
mkdir -p ${LOG_PATH}
status_log="${LOG_PATH}/results_paddle2onnx.log"

63
function func_paddle2onnx_inference(){
64
    IFS='|'
65 66 67
    _python=$1
    _log_path=$2
    _export_model_dir=$3
68 69

    # paddle2onnx
70
    echo "################### run paddle2onnx ###################"
71
    _save_log_path="${LOG_PATH}/paddle2onnx_infer_cpu.log"
72
    set_dirname=$(func_set_params "${model_dir_key}" "${_export_model_dir}")
73 74
    set_model_filename=$(func_set_params "${model_filename_key}" "${model_filename_value}")
    set_params_filename=$(func_set_params "${params_filename_key}" "${params_filename_value}")
75
    set_save_model=$(func_set_params "${save_file_key}" "${_export_model_dir}/${save_file_value}")
76
    set_opset_version=$(func_set_params "${opset_version_key}" "${opset_version_value}")
77 78 79
    set_enable_onnx_checker=$(func_set_params "${enable_onnx_checker_key}" "${enable_onnx_checker_value}")
    set_paddle2onnx_params1=$(func_set_params "${paddle2onnx_params1_key}" "${paddle2onnx_params1_value}")
    trans_model_cmd="${padlle2onnx_cmd} ${set_dirname} ${set_model_filename} ${set_params_filename} ${set_save_model} ${set_opset_version} ${set_enable_onnx_checker} ${set_paddle2onnx_params1}"
80 81
    eval $trans_model_cmd
    last_status=${PIPESTATUS[0]}
82 83
    status_check $last_status "${trans_model_cmd}" "${status_log}" "${model_name}"

84
    # python inference
85 86 87 88 89 90
    echo "################### run onnx infer ###################"
    set_infer_cfg=$(func_set_params "${infer_cfg_key}" "${_export_model_dir}/infer_cfg.yml")
    set_onnx_file=$(func_set_params "${onnx_file_key}" "${_export_model_dir}/${save_file_value}")
    set_infer_image_file=$(func_set_params "${infer_image_key}" "${infer_image_value}")
    set_infer_param1=$(func_set_params "${infer_param1_key}" "${infer_param1_value}")
    infer_model_cmd="${python} ${inference_py} ${set_infer_cfg} ${set_onnx_file} ${set_infer_image_file} ${set_infer_param1} > ${_save_log_path} 2>&1 "
91 92
    eval $infer_model_cmd
    last_status=${PIPESTATUS[0]}
93
    status_check $last_status "${infer_model_cmd}" "${status_log}" "${model_name}"
94 95 96 97
}

export Count=0
IFS="|"
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
for infer_mode in ${infer_mode_list[*]}; do
    if [ ${infer_mode} != "null" ]; then
        # run export
        case ${infer_mode} in
            norm) run_export=${norm_export} ;;
            quant) run_export=${pact_export} ;;
            fpgm) run_export=${fpgm_export} ;;
            distill) run_export=${distill_export} ;;
            kl_quant) run_export=${kl_quant_export} ;;
            *) echo "Undefined infer_mode!"; exit 1;
        esac
        set_export_weight=$(func_set_params "${export_weight_key}" "${export_weight_value}")
        set_save_export_dir=$(func_set_params "${save_export_key}" "${save_export_value}")
        set_filename=$(func_set_params "${filename_key}" "${model_name}")
        set_export_param=$(func_set_params "${export_param_key}" "${export_param_value}")
        export_cmd="${python} ${run_export} ${set_export_weight} ${set_filename} ${set_export_param} ${set_save_export_dir} "
        echo  $export_cmd
        eval $export_cmd
        status_export=$?
        status_check $status_export "${export_cmd}" "${status_log}" "${model_name}"
118
    fi
119 120 121 122 123

    #run inference
    export_model_dir="${save_export_value}/${model_name}"
    func_paddle2onnx_inference "${python}" "${LOG_PATH}" "${export_model_dir}"
    Count=$(($Count + 1))
124
done