test_paddle2onnx.sh 5.9 KB
Newer Older
1 2 3 4
#!/bin/bash
source test_tipc/utils_func.sh

FILENAME=$1
5
MODE="paddle2onnx_infer"
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

# 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]}")
28 29
export_param_key=$(func_parser_key "${lines[12]}")
export_param_value=$(func_parser_value "${lines[12]}")
30 31
kl_quant_export=$(func_parser_value "${lines[13]}")

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# 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]}")
50 51

# parser onnx inference 
52 53 54 55 56 57 58
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]}")
59

60
LOG_PATH="./test_tipc/output/${model_name}/${MODE}"
61 62 63
mkdir -p ${LOG_PATH}
status_log="${LOG_PATH}/results_paddle2onnx.log"

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

    # paddle2onnx
71 72
    echo "################### run paddle2onnx ###################"
    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
    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}")
79
    trans_log_path="${_log_path}/trans_model.log"
80
    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}"
81
    eval "${trans_model_cmd} > ${trans_log_path} 2>&1"
82
    last_status=${PIPESTATUS[0]}
83
    cat ${trans_log_path}
Z
zhengya01 已提交
84
    status_check $last_status "${trans_model_cmd}" "${status_log}" "${model_name}" "${trans_log_path}"
85

86
    # python inference
87 88 89 90 91
    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}")
92 93 94
    _save_log_path="${_log_path}/paddle2onnx_infer_cpu.log"
    infer_model_cmd="${python} ${inference_py} ${set_infer_cfg} ${set_onnx_file} ${set_infer_image_file} ${set_infer_param1}"
    eval "${infer_model_cmd} > ${_save_log_path} 2>&1"
95
    last_status=${PIPESTATUS[0]}
96
    cat ${_save_log_path}
Z
zhengya01 已提交
97
    status_check $last_status "${infer_model_cmd}" "${status_log}" "${model_name}" "${_save_log_path}"
98 99 100 101
}

export Count=0
IFS="|"
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
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}")
117
        export_log_path="${LOG_PATH}/export.log"
118 119
        export_cmd="${python} ${run_export} ${set_export_weight} ${set_filename} ${set_export_param} ${set_save_export_dir} "
        echo  $export_cmd
120
        eval "${export_cmd} > ${export_log_path} 2>&1"
121
        status_export=$?
122
        cat ${export_log_path}
Z
zhengya01 已提交
123
        status_check $status_export "${export_cmd}" "${status_log}" "${model_name}" "${export_log_path}"
124
    fi
125 126 127 128 129

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