Path: blob/master/site/en-snapshot/model_optimization/guide/combine/pqat_example.ipynb
25118 views
Copyright 2021 The TensorFlow Authors.
Pruning preserving quantization aware training (PQAT) Keras example
Overview
This is an end to end example showing the usage of the pruning preserving quantization aware training (PQAT) API, part of the TensorFlow Model Optimization Toolkit's collaborative optimization pipeline.
Other pages
For an introduction to the pipeline and other available techniques, see the collaborative optimization overview page.
Contents
In the tutorial, you will:
Train a
tf.keras
model for the MNIST dataset from scratch.Fine-tune the model with pruning, using the sparsity API, and see the accuracy.
Apply QAT and observe the loss of sparsity.
Apply PQAT and observe that the sparsity applied earlier has been preserved.
Generate a TFLite model and observe the effects of applying PQAT on it.
Compare the achieved PQAT model accuracy with a model quantized using post-training quantization.
Setup
You can run this Jupyter Notebook in your local virtualenv or colab. For details of setting up dependencies, please refer to the installation guide.
Train a tf.keras model for MNIST without pruning
Evaluate the baseline model and save it for later usage
Prune and fine-tune the model to 50% sparsity
Apply the prune_low_magnitude()
API to prune the whole pre-trained model to demonstrate and observe its effectiveness in reducing the model size when applying zip, while maintaining accuracy. For how best to use the API to achieve the best compression rate while maintaining your target accuracy, refer to the pruning comprehensive guide.
Define the model and apply the sparsity API
The model needs to be pre-trained before using the sparsity API.
Fine-tune the model and evaluate the accuracy against baseline
Fine-tune the model with pruning for 3 epochs.
Define helper functions to calculate and print the sparsity of the model.
Check that the model was correctly pruned. We need to strip the pruning wrapper first.
For this example, there is minimal loss in test accuracy after pruning, compared to the baseline.
Apply QAT and PQAT and check effect on model sparsity in both cases
Next, we apply both QAT and pruning-preserving QAT (PQAT) on the pruned model and observe that PQAT preserves sparsity on your pruned model. Note that we stripped pruning wrappers from your pruned model with tfmot.sparsity.keras.strip_pruning
before applying PQAT API.
See compression benefits of PQAT model
Define helper function to get zipped model file.
Since this is a small model, the difference between the two models isn't very noticeable. Applying pruning and PQAT to a bigger production model would yield a more significant compression.
See the persistence of accuracy from TF to TFLite
Define a helper function to evaluate the TFLite model on the test dataset.
You evaluate the model, which has been pruned and quantized, and then see the accuracy from TensorFlow persists in the TFLite backend.
Apply post-training quantization and compare to PQAT model
Next, we use normal post-training quantization (no fine-tuning) on the pruned model and check its accuracy against the PQAT model. This demonstrates why you would need to use PQAT to improve the quantized model's accuracy.
First, define a generator for the callibration dataset from the first 1000 training images.
Quantize the model and compare accuracy to the previously acquired PQAT model. Note that the model quantized with fine-tuning achieves higher accuracy.
Conclusion
In this tutorial, you learned how to create a model, prune it using the sparsity API, and apply the sparsity-preserving quantization aware training (PQAT) to preserve sparsity while using QAT. The final PQAT model was compared to the QAT one to show that the sparsity is preserved in the former and lost in the latter. Next, the models were converted to TFLite to show the compression benefits of chaining pruning and PQAT model optimization techniques and the TFLite model was evaluated to ensure that the accuracy persists in the TFLite backend. Finally, the PQAT model was compared to a quantized pruned model achieved using the post-training quantization API to demonstrate the advantage of PQAT in recovering the accuracy loss from normal quantization.