Skip to content
Katsuya Hyodo edited this page Jun 5, 2019 · 54 revisions

Quantization-aware training

Environment

  • Ubuntu 16.04 x86_64
  • Tensorflow v1.13.1

Reference article

https://github.com/NobuoTsukamoto/edge_tpu_mnist/blob/master/train/MNIST_fully_connected_feed.ipynb

Procedure

mkdir dataset;cd dataset

curl -sc /tmp/cookie "https://drive.google.com/uc?export=download&id=1llUu6071hd0QY2DY5vs_7VPaceI7EVst" > /dev/null
CODE="$(awk '/_warning_/ {print $NF}' /tmp/cookie)"
curl -Lb /tmp/cookie "https://drive.google.com/uc?export=download&confirm=${CODE}&id=1llUu6071hd0QY2DY5vs_7VPaceI7EVst" -o train2017.zip
unzip train2017.zip
rm train2017.zip

curl -sc /tmp/cookie "https://drive.google.com/uc?export=download&id=1bB8M-WG2LJwmB6YpLMsxUTWwIX3BwYkr" > /dev/null
CODE="$(awk '/_warning_/ {print $NF}' /tmp/cookie)"
curl -Lb /tmp/cookie "https://drive.google.com/uc?export=download&confirm=${CODE}&id=1bB8M-WG2LJwmB6YpLMsxUTWwIX3BwYkr" -o val2017.zip
unzip val2017.zip
rm val2017.zip

curl -sc /tmp/cookie "https://drive.google.com/uc?export=download&id=1P5pir4LVhev_S1Yvu_K3q_hGERAcFpg6" > /dev/null
CODE="$(awk '/_warning_/ {print $NF}' /tmp/cookie)"
curl -Lb /tmp/cookie "https://drive.google.com/uc?export=download&confirm=${CODE}&id=1P5pir4LVhev_S1Yvu_K3q_hGERAcFpg6" -o annotations_trainval2017.zip
unzip annotations_trainval2017.zip
rm annotations_trainval2017.zip
cd ..
mkdir -p models/train
mkdir -p models/pretrained/mobilenet_v2_1.4_224;cd models/pretrained/mobilenet_v2_1.4_224
wget https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz
tar -xzvf mobilenet_v2_1.4_224.tgz;rm mobilenet_v2_1.4_224.tgz

unsupported operand type(s) for +: 'NoneType' and 'float' measures

sed -i s/"(slim.batch_norm,): {'center': True, 'scale': True, 'is_training': False},"/"(slim.batch_norm,): {'center': True, 'scale': True},"/ tf_pose/mobilenet/mobilenet_v2.py
cd ../../..

### 1 Step Training
python3 tf_pose/train2.py \
--model=mobilenet_v2_1.4 \
--datapath=dataset/annotations/ \
--imgpath=dataset/ \
--batchsize=1 \
--gpus=1 \
--maxepoch=1 \
--lr=0.001 \
--quantdelay=0

### Full Step Training
python3 tf_pose/train.py \
--model=mobilenet_v2_1.4 \
--datapath=dataset/annotations/ \
--imgpath=dataset/ \
--batchsize=4 \
--gpus=1 \
--maxepoch=1 \
--lr=0.001 \
--quantdelay=25000
sed -i s/"(slim.batch_norm,): {'center': True, 'scale': True},"/"(slim.batch_norm,): {'center': True, 'scale': True, 'is_training': False},"/ tf_pose/mobilenet/mobilenet_v2.py
python3 model_compression_quantize.py
sed -i s/"(slim.batch_norm,): {'center': True, 'scale': True, 'is_training': False},"/"(slim.batch_norm,): {'center': True, 'scale': True},"/ tf_pose/mobilenet/mobilenet_v2.py
python3 freeze_graph.py \
  --input_graph=models/train/test/model-finalquant.pb \
  --input_checkpoint=models/train/test/model-finalquant-1 \
  --output_graph=models/train/test/model-finalquantfrozen.pb \
  --output_node_names=Openpose/concat_stage7 \
  --input_binary=True \
  --clear_devices=True

graph_image - model-finalquantfrozen.pb

mkdir -p models/train/test/tflite

tflite_convert \
  --output_file="models/train/test/tflite/output_tflite_graph.tflite" \
  --graph_def_file="models/train/test/model-finalquantfrozen.pb" \
  --inference_type=QUANTIZED_UINT8 \
  --input_arrays="image" \
  --output_arrays="Openpose/concat_stage7" \
  --mean_values=128 \
  --std_dev_values=128 \
  --input_shapes=1,368,432,3 \
  --change_concat_input_ranges=false \
  --allow_nudging_weights_to_use_fast_gemm_kernel=true
edgetpu_compiler \
--out_dir models/train/test/tflite \
models/train/test/tflite/output_tflite_graph.tflite
wget https://github.com/PINTO0309/Bazel_bin/raw/master/0.19.2/Ubuntu1604_x86_64/install.sh
./install.sh
git clone -b v1.13.1 https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout -b v1.13.1

sudo bazel run tensorflow/lite/tools:visualize -- \
${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/tflite/output_tflite_graph.tflite \
${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/tflite/model_viz_tflite.html

sudo bazel run tensorflow/lite/tools:visualize -- \
${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/tflite/output_tflite_graph_edgetpu.tflite \
${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/tflite/model_viz_tpu.html

TFLite model - Graph structure

TFLitestructure

TPU model - Graph image

TPUmodel

TPU model - Graph structure

TPUstructure

Apendix

### Tensorflow v1.13.1

git clone -b v1.13.1 https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout -b v1.13.1

sudo bazel build tensorflow/tools/graph_transforms:transform_graph

#RealDiv= p/256, Sub= p-0.5
sudo bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
  --in_graph="${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/checkpoints_no_quantization/cmu-model-finalfrozen.pb" \
  --out_graph="${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/checkpoints_no_quantization/cmu-model-finalfrozenc.pb" \
  --inputs="image" \
  --outputs="Openpose/concat_stage7" \
  --transforms='
  strip_unused_nodes(type=float, shape="1,368,656,3")
  remove_nodes(op=RealDiv, op=Sub)'

#RealDiv= p/256, Sub= p-0.5
sudo bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
  --in_graph="${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/checkpoints_no_quantization/cmu-model-finalfrozen.pb" \
  --out_graph="${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/checkpoints_no_quantization/cmu-model-finalfrozenc.pb" \
  --inputs="image" \
  --outputs="Openpose/concat_stage7" \
  --transforms='remove_nodes(op=RealDiv, op=Sub)'

#RealDiv= p/256, Sub= p-0.5
sudo bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
  --in_graph="${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/checkpoints_no_quantization/cmu-model-finalfrozen.pb" \
  --out_graph="${HOME}/git/MobileNetV2-PoseEstimation/models/train/test/checkpoints_no_quantization/cmu-model-finalfrozenc.pb" \
  --inputs="image" \
  --outputs="Openpose/concat_stage7" \
  --transforms='
  strip_unused_nodes(type=float, shape="1,368,656,3")
  remove_nodes(op=RealDiv, op=Sub)
  fold_constants(ignore_errors=true)
  fold_batch_norms
  fold_old_batch_norms
  quantize_weights
  quantize_nodes'

tflite_convert \
  --output_file="models/train/test/tflite/output_tflite_graph.tflite" \
  --graph_def_file="models/train/test/checkpoints_no_quantization/cmu-model-finalfrozen.pb" \
  --inference_type=QUANTIZED_UINT8 \
  --input_arrays="image" \
  --output_arrays="Openpose/concat_stage7" \
  --default_ranges_min=0 \
  --default_ranges_max=6 \
  --mean_values=128 \
  --std_dev_values=128 \
  --input_shapes=1,368,656,3 \
  --change_concat_input_ranges=false \
  --allow_nudging_weights_to_use_fast_gemm_kernel=true