validate_gcn.sh 3.5 KB
Newer Older
L
liuqi 已提交
1 2
#!/bin/bash
# Must run at root dir of mace project.
Y
Yin Li 已提交
3
set +x
L
liuqi 已提交
4
Usage() {
L
liuqi 已提交
5
  echo 'Usage: bash tools/validate_gcn.sh tf_model_path image_size'
L
liuqi 已提交
6 7
}

L
liuqi 已提交
8
if [ $# != 2 ];then
L
liuqi 已提交
9 10 11 12 13 14
  Usage
  exit -1
fi

TF_MODEL_FILE_PATH=$1
MODEL_DIR=$(dirname ${TF_MODEL_FILE_PATH})
15
MACE_SOURCE_DIR=`/bin/pwd`
L
liuqi 已提交
16 17 18
MACE_MODEL_NAME='mace_model.pb'
INPUT_FILE_NAME='model_input'
OUTPUT_FILE_NAME='gcn.out'
L
liuqi 已提交
19
OUTPUT_LIST_FILE='gcn.list'
L
liuqi 已提交
20 21
PHONE_DATA_DIR="/data/local/tmp/${MACE_MODEL_NAME}"
KERNEL_DIR="${PHONE_DATA_DIR}/cl/"
L
liuqi 已提交
22
IMAGE_SIZE=$2
23
MODEL_TAG=GCN${IMAGE_SIZE}
L
liuqi 已提交
24

L
liuqi 已提交
25 26 27 28
# Step 1: Generate input data
echo "Step 1: Generate input data"
python tools/validate.py --generate_data true --random_seed 1 \
 --input_file=${MODEL_DIR}/${INPUT_FILE_NAME} \
L
liuqi 已提交
29
 --input_shape="${IMAGE_SIZE},${IMAGE_SIZE},3"
L
liuqi 已提交
30 31

# Step 2: convert tf model to mace model
32 33
echo "Step 2: convert tf model to mace model and optimize memory"
bazel build //mace/python/tools:tf_converter
34
rm -rf ${MACE_SOURCE_DIR}/mace/examples/models/gcn
35 36 37 38 39 40 41 42 43 44 45
mkdir -p ${MACE_SOURCE_DIR}/mace/examples/models/gcn
bazel-bin/mace/python/tools/tf_converter --input=${TF_MODEL_FILE_PATH} \
                                         --output=${MACE_SOURCE_DIR}/mace/examples/models/gcn/mace_gcn.cc \
                                         --input_node=input \
                                         --output_node=GCN/br_result_2/fcn_br \
                                         --data_type=DT_HALF \
                                         --runtime=gpu \
                                         --output_type=source \
                                         --template=${MACE_SOURCE_DIR}/mace/python/tools/model.template \
                                         --model_tag=${MODEL_TAG} \
                                         --confuse=True
L
liuqi 已提交
46 47 48

# Step 3: Run model on the phone
echo "Step 3: Run model on the phone"
49
echo Create${MODEL_TAG}
50
bazel build -c opt --strip always mace/examples:mace_run \
L
liuqi 已提交
51 52
    --crosstool_top=//external:android/crosstool \
    --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
53 54
    --cpu=arm64-v8a \
    --copt=-DMACE_MODEL_FUNCTION=Create${MODEL_TAG}
L
liuqi 已提交
55 56 57

adb shell "mkdir -p ${PHONE_DATA_DIR}"
adb shell "mkdir -p ${KERNEL_DIR}"
58
adb push mace/kernels/opencl/cl/ ${KERNEL_DIR}
L
liuqi 已提交
59 60 61
adb push ${MODEL_DIR}/${INPUT_FILE_NAME} ${PHONE_DATA_DIR}
adb push bazel-bin/mace/examples/mace_run ${PHONE_DATA_DIR}

L
liuqi 已提交
62
num_threads=${1:-4}
L
liuqi 已提交
63

L
liuqi 已提交
64 65
adb </dev/null shell MACE_CPP_MIN_VLOG_LEVEL=0 \
        MACE_RUN_PARAMETER_PATH=${PHONE_DATA_DIR}/mace_run.config \
L
liuqi 已提交
66 67 68
        MACE_KERNEL_PATH=$KERNEL_DIR \
        OMP_NUM_THREADS=$num_threads \
        ${PHONE_DATA_DIR}/mace_run \
69
          --model=${PHONE_DATA_DIR}/${MACE_MODEL_NAME} \
L
liuqi 已提交
70 71
          --input=mace_input_node \
          --output=mace_output_node \
L
liuqi 已提交
72
          --input_shape="1,${IMAGE_SIZE},${IMAGE_SIZE},3"\
L
liuqi 已提交
73 74
          --input_file=${PHONE_DATA_DIR}/${INPUT_FILE_NAME} \
          --output_file=${PHONE_DATA_DIR}/${OUTPUT_FILE_NAME} \
Y
Yin Li 已提交
75 76
          --device=OPENCL   \
          --round=1
L
liuqi 已提交
77 78 79

# Step 4: pull the mace run result.
echo "Step 4: pull the mace run result."
L
liuqi 已提交
80 81
rm -rf ${MODEL_DIR}/${OUTPUT_FILE_NAME}
adb </dev/null pull ${PHONE_DATA_DIR}/${OUTPUT_FILE_NAME} ${MODEL_DIR}
L
liuqi 已提交
82 83 84 85

# Step 5: validate the result
echo "Step 5: validate the result"
python tools/validate.py --model_file ${TF_MODEL_FILE_PATH} \
L
liuqi 已提交
86 87 88 89
    --input_file ${MODEL_DIR}/${INPUT_FILE_NAME} \
    --mace_out_file ${MODEL_DIR}/${OUTPUT_FILE_NAME} \
    --input_node input \
    --output_node GCN/br_result_2/fcn_br\
L
liuqi 已提交
90 91
    --input_shape "${IMAGE_SIZE},${IMAGE_SIZE},3" \
    --output_shape "1,${IMAGE_SIZE},${IMAGE_SIZE},2"