Solve the problem of tensorflow model compression _ step on countless pits and finally get it done

  • 2021-09-12 01:38:34
  • OfStack

1. Install bazel, download the linux version of. sh file from github, and then install

2. Download the latest TensorFlow source from GitHub

3. Enter the TensorFlow source folder and enter the command


bazel build tensorflow/tools/graph_transforms:transform_graph

There will be various pits here, such as

ERROR:/opt/tf/tensorflow-master/tensorflow/core/kernels/BUILD: 3044: 1: C + + compilation of rule '//tensorflow/core/kernels: matrix_square_root_op' failed (Exit 4)

gcc: internal compiler error: Killed (program cc1plus)

This error is due to the heavy load of cpu, which requires an additional line of code


#  Generate swap Mirror file 
sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512
#  Formatting the image file 
sudo mkswap /mnt/512Mb.swap
#  Mount the image file  
sudo swapon /mnt/512Mb.swap

Or this @ aws Error downloading

I think some bloggers of csdn solve the problem by deleting files from temporary folders and downloading them again, but I find it useless here. My solution here is to enter a command before running bazel:


sed -i '\@https://github.com/aws/aws-sdk-cpp/archive/1.5.8.tar.gz@aws' tensorflow/workspace.bzl

The URL in the command is the address of the file to be downloaded, because some addresses may have been changed

Compiling bazel here is complete

4. After compilation, the model can be compressed, which is also a line of code. in_graph is the input model path, outputs does not move, out_graph is the output model path, and transforms can be filled with an quantize_weights. This is the most effective step of this method to convert 32bit into 8bit; I see some bloggers compile summary first, then print out the input and output nodes, then input a lot of parameters, and delete a few nodes. I tried them all here, and finally I didn't reduce the size of the model, so that's it.


bazel-bin/tensorflow/tools/graph_transforms/transform_graph --in_graph=../model/ctpn.pb  --outputs='output_node_name'  --out_graph=../model/quantized_ctpn.pb  --transforms='quantize_weights'

Finally, it was reduced from 68m to 17m, with a reduction ratio of 75%. The measured effect is basically the same, and this method is still very useful.

Added: Model Compression 123 tensorflow View the parameters and values in the ckpt model

View ckpt model parameters and values


import os
from tensorflow.python import pywrap_tensorflow
checkpoint_path = os.path.join("< Directory of your model >", "./model.ckpt-11000") 
# Read data from checkpoint file
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
# Print tensor name and values
for key in var_to_shape_map:
 print("tensor_name: ", key)
 print(reader.get_tensor(key))

Note:

1. " < Your model directory > "Refers to your meta, ckpt model storage path.

For example, the path "/models/model. ckpt-11000. meta", then " < Your model directory > "Is"/models "

2. When there are more than one ckpt in the directory, take the latest model name to ckpt- < Maximum number > That's fine, not in the back.


Related articles: