netrans/test/netrans_cli/run_examples_test.sh

235 lines
6.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 自动化测试脚本循环处理examples目录下的模型
# 使用方法: ./run_examples_test.sh [模型类型] [测试步骤]
# 示例: ./run_examples_test.sh onnx all
# ./run_examples_test.sh all 01_load
set -e # 遇到错误时退出
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 默认参数
MODEL_TYPE=${1:-"onnx"} # 默认测试ONNX模型
TEST_STEP=${2:-"all"} # 默认运行所有步骤
# 基础路径
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
EXAMPLES_DIR="$REPO_ROOT/examples"
NETRANS_SCRIPT="$REPO_ROOT/script/netrans"
TEST_DIR="$REPO_ROOT/test/netrans_cli"
# 检查netrans脚本是否存在
if [ ! -f "$NETRANS_SCRIPT" ]; then
echo -e "${RED}错误: netrans脚本不存在: $NETRANS_SCRIPT${NC}"
exit 1
fi
# 检查测试目录是否存在
if [ ! -d "$TEST_DIR" ]; then
echo -e "${RED}错误: 测试目录不存在: $TEST_DIR${NC}"
exit 1
fi
# 获取指定类型的模型列表
get_models() {
local type=$1
case $type in
"onnx")
find "$EXAMPLES_DIR" -name "*.onnx" -type f | grep -v quantize_hybrid | head -5
;;
"caffe")
find "$EXAMPLES_DIR" -name "*.caffemodel" -type f | head -5
;;
"tensorflow")
find "$EXAMPLES_DIR" -name "*.pb" -type f | head -5
;;
"paddle")
find "$EXAMPLES_DIR" -name "*.pdmodel" -type f | head -5
;;
"all")
find "$EXAMPLES_DIR" -name "*.onnx" -o -name "*.caffemodel" -o -name "*.pb" -o -name "*.pdmodel" | head -10
;;
*)
echo -e "${RED}不支持的模型类型: $type${NC}"
echo "支持的类型: onnx, caffe, tensorflow, paddle, all"
exit 1
;;
esac
}
# 获取模型对应的目录
get_model_dir() {
local model_path=$1
dirname "$model_path"
}
# 获取模型名称(不含路径和扩展名)
get_model_name() {
local model_path=$1
basename "$model_path" | sed 's/\..*$//'
}
# 运行指定步骤
run_step() {
local step_script=$1
local work_dir=$2
local model_name=$3
echo -e "${YELLOW}运行 $step_script - 模型: $model_name${NC}"
echo -e "${YELLOW}工作目录: $work_dir${NC}"
local full_script_path="$TEST_DIR/$step_script"
if [ -f "$full_script_path" ]; then
if bash "$full_script_path" "$work_dir"; then
echo -e "${GREEN}$step_script 完成${NC}"
return 0
else
echo -e "${RED}$step_script 失败${NC}"
return 1
fi
else
echo -e "${RED}步骤脚本不存在: $full_script_path${NC}"
return 1
fi
}
# 运行完整测试流程
run_full_test() {
local work_dir=$1
local model_name=$2
echo -e "${YELLOW}=== 开始完整测试流程: $model_name ===${NC}"
# 定义步骤顺序
local steps=(
"01_load.sh"
"02_quantize.sh"
"03_quantize_hybrid.sh"
"04_add_pre_post.sh"
"05_export.sh"
"06_inference.sh"
"07_inference_hybrid.sh"
"08_dump.sh"
"09_add_pre_post.sh"
)
for step in "${steps[@]}"; do
if ! run_step "$step" "$work_dir" "$model_name"; then
echo -e "${RED}测试流程在 $step 中断${NC}"
return 1
fi
echo
done
echo -e "${GREEN}=== 完整测试流程完成: $model_name ===${NC}"
return 0
}
# 运行指定步骤
run_single_step() {
local step_name=$1
local work_dir=$2
local model_name=$3
local step_script="${step_name}.sh"
run_step "$step_script" "$work_dir" "$model_name"
}
# 主函数
main() {
echo -e "${YELLOW}=== Netrans Examples 自动化测试开始 ===${NC}"
echo -e "测试模型类型: $MODEL_TYPE"
echo -e "测试步骤: $TEST_STEP"
echo
# 获取模型列表
models=$(get_models "$MODEL_TYPE")
if [ -z "$models" ]; then
echo -e "${RED}未找到任何$MODEL_TYPE类型的模型${NC}"
exit 1
fi
echo -e "${GREEN}找到以下模型:${NC}"
echo "$models" | nl
echo
# 统计结果
local total=0
local success=0
local failed=0
# 对每个模型进行测试
while IFS= read -r model_path; do
if [ -z "$model_path" ]; then
continue
fi
total=$((total + 1))
local model_dir=$(get_model_dir "$model_path")
local model_name=$(get_model_name "$model_path")
echo -e "${YELLOW}--- 处理第 $total 个模型 ---${NC}"
if [ "$TEST_STEP" = "all" ]; then
if run_full_test "$model_dir" "$model_name"; then
success=$((success + 1))
else
failed=$((failed + 1))
fi
else
if run_single_step "$TEST_STEP" "$model_dir" "$model_name"; then
success=$((success + 1))
else
failed=$((failed + 1))
fi
fi
echo
echo "----------------------------------------"
echo
done <<< "$models"
# 输出统计结果
echo -e "${YELLOW}=== 测试统计 ===${NC}"
echo -e "总模型数: $total"
echo -e "${GREEN}成功: $success${NC}"
echo -e "${RED}失败: $failed${NC}"
if [ $failed -eq 0 ]; then
echo -e "${GREEN}所有测试都通过了!${NC}"
exit 0
else
echo -e "${RED}部分测试失败${NC}"
exit 1
fi
}
# 显示帮助信息
show_help() {
echo "使用方法: $0 [模型类型] [测试步骤]"
echo
echo "参数:"
echo " 模型类型: onnx, caffe, tensorflow, paddle, all (默认: onnx)"
echo " 测试步骤: 01_load, 02_quantize, 03_quantize_hybrid, 04_add_pre_post,"
echo " 05_export, 06_inference, 07_inference_hybrid, 08_dump,"
echo " 09_add_pre_post, all (默认: all)"
echo
echo "示例:"
echo " $0 onnx all # 测试所有ONNX模型运行完整流程"
echo " $0 all 01_load # 所有模型类型只运行load步骤"
echo " $0 tensorflow 05_export # 测试TensorFlow模型只运行export步骤"
}
# 处理特殊参数
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
exit 0
fi
# 运行主函数
main