Path: blob/master/site/en-snapshot/model_optimization/guide/combine/cqat_example.ipynb
25118 views
Copyright 2021 The TensorFlow Authors.
Cluster preserving quantization aware training (CQAT) Keras example
Overview
This is an end to end example showing the usage of the cluster preserving quantization aware training (CQAT) 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 clustering and see the accuracy.
Apply QAT and observe the loss of clusters.
Apply CQAT and observe that the clustering applied earlier has been preserved.
Generate a TFLite model and observe the effects of applying CQAT on it.
Compare the achieved CQAT 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 clustering
Evaluate the baseline model and save it for later usage
Cluster and fine-tune the model with 8 clusters
Apply the cluster_weights()
API to cluster 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 clustering comprehensive guide.
Define the model and apply the clustering API
The model needs to be pre-trained before using the clustering API.
Fine-tune the model and evaluate the accuracy against baseline
Fine-tune the model with clustering for 3 epochs.
Define helper functions to calculate and print the number of clustering in each kernel of the model.
Check that the model kernels were correctly clustered. We need to strip the clustering wrapper first.
For this example, there is minimal loss in test accuracy after clustering, compared to the baseline.
Apply QAT and CQAT and check effect on model clusters in both cases
Next, we apply both QAT and cluster preserving QAT (CQAT) on the clustered model and observe that CQAT preserves weight clusters in your clustered model. Note that we stripped clustering wrappers from your model with tfmot.clustering.keras.strip_clustering
before applying CQAT API.
See compression benefits of CQAT model
Define helper function to get zipped model file.
Note that this is a small model. Applying clustering and CQAT 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 clustered and quantized, and then see the accuracy from TensorFlow persists in the TFLite backend.
Apply post-training quantization and compare to CQAT model
Next, we use post-training quantization (no fine-tuning) on the clustered model and check its accuracy against the CQAT model. This demonstrates why you would need to use CQAT to improve the quantized model's accuracy. The difference may not be very visible, because the MNIST model is quite small and overparametrized.
First, define a generator for the callibration dataset from the first 1000 training images.
Quantize the model and compare accuracy to the previously acquired CQAT model. Note that the model quantized with fine-tuning achieves higher accuracy.
Conclusion
In this tutorial, you learned how to create a model, cluster it using the cluster_weights()
API, and apply the cluster preserving quantization aware training (CQAT) to preserve clusters while using QAT. The final CQAT model was compared to the QAT one to show that the clusters are preserved in the former and lost in the latter. Next, the models were converted to TFLite to show the compression benefits of chaining clustering and CQAT model optimization techniques and the TFLite model was evaluated to ensure that the accuracy persists in the TFLite backend. Finally, the CQAT model was compared to a quantized clustered model achieved using the post-training quantization API to demonstrate the advantage of CQAT in recovering the accuracy loss from normal quantization.