Path: blob/master/examples/vision/ipynb/consistency_training.ipynb
3236 views
Consistency training with supervision
Author: Sayak Paul
Date created: 2021/04/13
Last modified: 2021/04/19
Description: Training with consistency regularization for robustness against data distribution shifts.
Deep learning models excel in many image recognition tasks when the data is independent and identically distributed (i.i.d.). However, they can suffer from performance degradation caused by subtle distribution shifts in the input data (such as random noise, contrast change, and blurring). So, naturally, there arises a question of why. As discussed in A Fourier Perspective on Model Robustness in Computer Vision), there's no reason for deep learning models to be robust against such shifts. Standard model training procedures (such as standard image classification training workflows) don't enable a model to learn beyond what's fed to it in the form of training data.
In this example, we will be training an image classification model enforcing a sense of consistency inside it by doing the following:
Train a standard image classification model.
Train an equal or larger model on a noisy version of the dataset (augmented using RandAugment).
To do this, we will first obtain predictions of the previous model on the clean images of the dataset.
We will then use these predictions and train the second model to match these predictions on the noisy variant of the same images. This is identical to the workflow of Knowledge Distillation but since the student model is equal or larger in size this process is also referred to as Self-Training.
This overall training workflow finds its roots in works like FixMatch, Unsupervised Data Augmentation for Consistency Training, and Noisy Student Training. Since this training process encourages a model yield consistent predictions for clean as well as noisy images, it's often referred to as consistency training or training with consistency regularization. Although the example focuses on using consistency training to enhance the robustness of models to common corruptions this example can also serve a template for performing weakly supervised learning.
This example requires TensorFlow 2.4 or higher, as well as TensorFlow Hub and TensorFlow Models, which can be installed using the following command:
Imports and setup
Define hyperparameters
Load the CIFAR-10 dataset
Create TensorFlow Dataset
objects
For training the teacher model, we will only be using two geometric augmentation transforms: random horizontal flip and random crop.
We make sure train_clean_ds
and train_noisy_ds
are shuffled using the same seed to ensure their orders are exactly the same. This will be helpful during training the student model.
Visualize the datasets
Define a model building utility function
We now define our model building utility. Our model is based on the ResNet50V2 architecture.
In the interest of reproducibility, we serialize the initial random weights of the teacher network.
Train the teacher model
As noted in Noisy Student Training, if the teacher model is trained with geometric ensembling and when the student model is forced to mimic that, it leads to better performance. The original work uses Stochastic Depth and Dropout to bring in the ensembling part but for this example, we will use Stochastic Weight Averaging (SWA) which also resembles geometric ensembling.
Define a self-training utility
For this part, we will borrow the Distiller
class from this Keras Example.
The only difference in this implementation is the way loss is being calculated. Instead of weighted the distillation loss and student loss differently we are taking their average following Noisy Student Training.
Train the student model
Assess the robustness of the models
A standard benchmark of assessing the robustness of vision models is to record their performance on corrupted datasets like ImageNet-C and CIFAR-10-C both of which were proposed in Benchmarking Neural Network Robustness to Common Corruptions and Perturbations. For this example, we will be using the CIFAR-10-C dataset which has 19 different corruptions on 5 different severity levels. To assess the robustness of the models on this dataset, we will do the following:
Run the pre-trained models on the highest level of severities and obtain the top-1 accuracies.
Compute the mean top-1 accuracy.
For the purpose of this example, we won't be going through these steps. This is why we trained the models for only 5 epochs. You can check out this repository that demonstrates the full-scale training experiments and also the aforementioned assessment. The figure below presents an executive summary of that assessment:
Mean Top-1 results stand for the CIFAR-10-C dataset and Test Top-1 results stand for the CIFAR-10 test set. It's clear that consistency training has an advantage on not only enhancing the model robustness but also on improving the standard test performance.