netrans/examples/tensorflow/quantize.sh

77 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
if [ -z "$NETRANS_PATH" ]; then
echo "Need to set enviroment variable NETRANS_PATH"
exit 1
fi
TENSORZONEX=$NETRANS_PATH/pnnacc
TENSORZONEX="$TENSORZONEX quantize"
DATASET=./dataset.txt
function quantize_network()
{
NAME=$1
pushd $NAME
QUANTIZED=$2
if [ ${QUANTIZED} = 'float' ]; then
echo "=========== do not need quantied==========="
exit -1
elif [ ${QUANTIZED} = 'uint8' ]; then
quantization_type="asymmetric_affine"
elif [ ${QUANTIZED} = 'int8' ]; then
quantization_type="dynamic_fixed_point-8"
elif [ ${QUANTIZED} = 'int16' ]; then
quantization_type="dynamic_fixed_point-16"
else
echo "=========== wrong quantization_type ! ( uint8 / int8 / int16 )==========="
exit -1
fi
echo " ======================================================================="
echo " ==== Start Quantizing $NAME model with type of ${quantization_type} ==="
echo " ======================================================================="
if [ -f ${NAME}_${quantization_type}.quantize ]; then
echo -e "\033[31m rm ${NAME}_${quantization_type}.quantize \033[0m"
rm ${NAME}_${quantization_type}.quantize
fi
cmd="$TENSORZONEX \
--batch-size 1 \
--qtype ${QUANTIZED} \
--rebuild \
--quantizer ${quantization_type%-*} \
--model-quantize ${NAME}_${quantization_type}.quantize \
--model ${NAME}.json \
--model-data ${NAME}.data \
--with-input-meta ${NAME}_inputmeta.yml \
--device CPU"
echo $cmd
eval $cmd
if [ -f ${NAME}_${quantization_type}.quantize ]; then
echo -e "\033[31m SUCCESS \033[0m"
else
echo -e "\033[31m ERROR ! \033[0m"
fi
popd
}
if [ "$#" -lt 2 ]; then
echo "Input a network name and quantized type ( uint8 / int8 / int16 )"
exit -1
fi
if [ ! -e "${1%/}" ]; then
echo "Directory ${1%/} does not exist !"
exit -2
fi
quantize_network ${1%/} ${2%/}