Don’t waste your unlabeled data. Evaluate the weakness of your model and... | by Betty LD | Towards Data Science

In the last decade, the main trend in AI was focusing on building creative and complex models. But these last years, state-of-the-art architectures are made available to anyone in popular languages like Python and its libraries such as Pytorch, Tensorflow, JAX, etc. Now, AI's highest priority is to make these models valuable for each particular use case. This requires a deep understanding of available curated data, and unfortunately, data analysis and how to build a good training set are skills rarely taught in classical ML courses but learned with time and sweat.
So far, data was not thought to be a major issue: standard research datasets of variable size were available for free (ImageNet, COCO, GLUE, etc), as well as the pre-trained model’s weights (ModelZoo, Huggingface, etc) as prior knowledge for fine-tuning on a specific subset of data. Besides that, training locally models on small-scale datasets became a commodity. But in practice, making an ML model successful in a production stage is a hard problem.
One of the major causes of failure is the loss of accuracy between a model on research template and this model deployed in a real-world environment. In fact, 13% of models in the companies make it to production [7].
This seems quite surprising when we know that even model search has been automated with the rise of AutoML platforms. What is the bottleneck?
The bottleneck is the data. And the next challenge that needs a strong framework and standardization is the pipeline that massages the data. The AI in the industry may need to step back from the model-centric approach and shift to the data-centric approach [6]. The data-centric approach tries to reconcile the model development with the creation of the training dataset and the monitoring of the model performance.
“Not all data is created equal” — Jennifer Prendki
Because real-world data is messy, noisy, wrong, filled with outliers, and does not follow a normal distribution, quantifying uncertainty is crucial to building a trustworthy AI model. Choose well the data you let in. Kick out the suspicious data. Avoid the fatal garbage in, garbage out, and save time and money. A model prediction is almost useless unless we can trust it. To spot these sick data, we’ll explore the uncertainty of the model as a proxy for data quality. We want to identify where the data is confusing the model.
There are numerous causes for uncertainty. Uncertainties can appear in the inference when the distribution of the training and test dataset are unmatched. It also appears when the data classes overlap or when the data itself is noisy. In general, we should be aware of the original source of uncertainty in our dataset in order to create an efficient strategy to reduce it when possible.
In the active learning literature, these uncertainties are classified into two major types: epistemic and aleatoric uncertainty. On the one hand, aleatoric uncertainty is created by the noise of the data and will differ at every run of the same experiment. This uncertainty is irreducible because it is an inherent property of the data. On the other hand, epistemic uncertainty can be reduced by learning. By increasing the dataset size or making complexify the model.
By default, we often erroneously consider the probability for each label output by the softmax function as the model confidence into each label. This misconception can be avoided by using a simple technique: considering the relative value of each softmax output relative to other labels or other instances.
In this article, I’ll share standard techniques to scan all available data (labeled or unlabelled) to help make a data-centric conclusion on the current state of the data and model. These techniques rank each data point according to the confidence of the model on its predicted label. We can use different functions that can attribute an “uncertainty” score to each data point. High uncertainty means that the model was not sure about this data point and the inference is likely to be wrong.
leveraging both existing data and model
find edge cases in the dataset
find bad quality data to re-label or exclude from the training dataset
clarify labeling instructions by tracking down ambiguity
find the weakness of the current model
identify data drift with on new incoming data
error analysis to find which samples to label next
find outliers
Continuously improving the data is a necessary step given the evolving world where we put models in production. I’ve heard the story of a bowling picture classifier that was labeling empty toilet paper shelves images as bowling pictures when the COVID started because of social media trends on this matter. We never know what the future data will look like and we should always be aware of the data we let sneak into our model prediction.
In this article, we evaluate the images that feed a CNN model for classification. We’ll use EMNIST for experimentation.
We use softmax to create a probability distribution from the output logits computed by the last activation function. Softmax cannot provide the probability of certainty of the model and is losing information because it is using exponential so it loses the scale of the digits ([1,4,2,1] will produce the same distribution that [101, 104, 102, 101]). But we can still use softmax as a proxy for the model confidence and rank our data samples from high model certainty to low model certainty.
“No algorithm can survive bad data” — Robert Monarch
We want to label the data where our model is the most confused — uncertain. Therefore, for each data point, we will give a single uncertainty score for all unlabeled data points and rank them from the most uncertain to the least uncertain.
This method is the easiest and less expensive method for uncertainty quantification. You can identify the data points that lie near the decision boundary of your model.
It is the case when for one data point, your model:
give a similar softmax probability to different classes (for example, the input image is dog and output softmax are: dog 33.3%, cat 33.3%, bird 33.3%)
give a low probability to the highest probability (for example among ten classes the highest softmax is “frog” with a softmax at 10.5%)
We’ll list the scoring techniques from simplest to more complex.
We’ll call p_max the highest softmax score given to a class inferred for one image and p_2nd the second-highest confidence given to a class for one image. n is the number of classes for classification.
For each function below, the input prob_dist is the array of softmax output for one data point, and the output is an uncertainty score between 0 and 1 of the model for this data point. If the dataset is multiclass classification with three classes, prob_dist is of shape (3,).
least confidence (difference between the most confident prediction and 100% confidence)
We want to select the instance for which the confidence in the most likely class is the lowest. Let’s say we have two images A and B and the model's highest softmax logit is respectively 0.9 for A and 0.3 for B. In that case, we may want to understand how B looks like, process, (re-)label, or remove it.
We use a normalized “least” uncertainty score (formula (1)) computed for each data point. Since the minimum confidence level of the most likely instance cannot be lower than 1/n, we add the normalization score to scale the uncertainty score from 0 to 1.
This score will give a ranked order of the prediction where we want to sample the items with the lowest level of confidence for their predicted labels.
This scoring technique only depends on the confidence of the most confidence item, without caring about the second or nth confidence score.
margin of confidence
Margin confidence takes the difference between the two most confident predictions for one data point. We can convert this difference into a [0–1] range by subtracting the result to 1 (formula (2)). This time you care only about the difference between the two highest scores among the two highest categories. The smaller this difference, the closer to 1, and the less certain you are. This algorithm is less sensitive to the softmax base than the least confidence scoring but still sensitive.
ratio of confidence (ratio between the two most confident predictions)
The ratio scoring function is a variation of the margin of confidence scoring. This uncertainty score looks at the ratio of the two highest scores (3). The ratio is given as a multiplier of the base used for the softmax so, unlike the two previous methods, it is not sensitive to the softmax basis. It captures how much more likely is the first label compared to the second.
entropy-based (difference between all predictions)
The entropy score takes into account the prediction for all classes. It is computed by taking the negative sum of the probabilities multiplied by its log_2 probabilities.
We divide by log_2(n) to normalize the entropy between 0 and 1 (Equation (4)).
2 as a base for the log is an arbitrary base that does not change the ranking even if it will change monotonically the absolute uncertainty score.
We illustrate the results of two scoring functions (least and entropy). We first train a simple two-layers CNN on half of the EMNIST dataset (130,106 images). Then we predict the classes for all unseen data points and rank them according to their uncertainty score. We sample the top-1000 images given their uncertainty score.
Our CNN is made of two convolutional layers each one followed by RELU activation and then followed by two fully connected layers. We stop the training at the 6th epoch before the validation accuracy starts decreasing. We achieve an F1-score of 0.79 on the validation set.
You can find the code for training at: https://github.com/bledem/active-learning/blob/master/run_experiment.py
and for sampling the top-1000 images as a new dataset at: https://github.com/bledem/active-learning/blob/master/create_new_set.py
We look at the classes for which the per-class average F1 score is the highest and lowest on the validation set.
The classes with the lowest F1 score (F1<50%) are:
'k' 'o' 'i' 'r' 'Y' 'q' 'b' 'l' 'h' 'e' 'm'
and the classes with the highest F1 score (F1>95%) are:
'n' 'Z' 'A' 'a' '3'
Now let’s look at the top-1k images ranked after their uncertainty score. For comparison sake, we also sample a random 1k set.
We can see on the figure above, that we oversample characters that can be easily confused: “I”, “1”, “l” or “o”, “O”, “0”.
In the figure below, we compare the per-class F1 score with the distribution of instances between the different classes of the 1k sampled with the entropy uncertainty scoring function. We find a high peak in the “easily confused” classes that won’t happen in the randomly sampled dataset.
We show below some examples of the EMNIST images sampled as the top-1k uncertain inference. When we look at the “least” or “entropy” based samples, we can clearly see the noise in the labeling (e.g S labeled as 0) and the images themselves (blurry, non-readable characters).
Using uncertainty scores to sample data batches for new annotation tasks is an efficient way to label the most informative data point of the unseen dataset. However, as we saw, we should be careful not to systematically oversample similar data points in over-represented classes and their near-neighbor.
We list here the pro and cons of using the scoring score introduced above:
Pros:
use existing/standard models
fast and easy to compute and interpret.
create a new batch of informative data samples to label if your model uses traditional Machine Learning (not Deep Learning)
Cons:
these simple scoring functions should not be used to select data points for a new batch task with deep learning. Because of underlying sampling bias, the performance of the model after training on these sampled data may be worse than random sampling [5].
it is safer to use Bayesian Deep Learning to create a sampling strategy for high-dimensional data (image, text, etc.).
Using uncertainty quantification methods is very efficient to find in the unlabelled data the samples that lie on the boundary of your model. But, be careful when you select the data points to sample for your next annotation batch. This sampling assesses the informativeness of each unlabelled point individually. If you select only the highest uncertainty score data points, you will retrieve one informative data point and many near identical-copy. If you naively acquire the top-k most informative points, you may end up asking k near-identical points as well. This would make the contribution of your sample batch lower than a randomly sampled batch. For example, in the EMNIST experiment, if you look at the entropy sampling technique, you can see that the characters “i”, “1”, “l” were heavily sampled.
To avoid this duplicate sampling effect, some research papers propose complementary approaches like batch-aware sampling technique solutions such as BatchBALD [2]. In this paper, the author not only focuses on giving an uncertainty score to each sample but also on sampling for the next training batch that would provide the most informativeness.
Use wise sampling to build smart batch for annotation [1]:
minimize the sample size to ensure the most benefit is gained from each data point.
maximize the sample size to avoid retraining too often.
Another popular heuristic to assess the uncertainty of each unlabelled data point is called Query By Committee method. If you have an ensemble of models, you can also adapt the uncertainty scoring by taking all scores for each data point made by each model. You train several different models on your initial training set and you can look at their disagreement on the unlabelled data.
For example, I can train the same CNN with different hyperparameters on the same training set, then apply them on unlabelled data and use their disagreement to choose the next samples to label.
This method is particularly useful when you want to evaluate the uncertainty score for a regression task where softmax confidence in the prediction is not available.
Dropout strategy
If you don’t have an ensemble model, you can draw different predictions for each data point where each new prediction is made after dropping out a different random selection of neurons each time. Normally dropout is applied only during training, and all neurons are activated during testing. But if we apply dropout during testing too, we can infer a series of different predictions for each instance. The uncertainty for each sample can then be calculated as the variation across all predictions: the higher the disagreement, the more uncertainty [1]. This technique is often called the Monte Carlo dropout technique. Monte Carlo is a computational technique that randomly samples from a real distribution to obtain an estimate of this distribution. During the uncertainty estimation, we consider every prediction from the drop-out models as a Monte Carlo sample.
Open-source library for MC dropout and other active learning techniques: https://github.com/ElementAI/baal/ [paper]
You can use a generative Bayesian model to model the distribution of each class. In that case, you don’t need to use a probability proxy to infer the confidence of the model in its prediction since you can read directly the probability of each data point to belong to each class from the model output itself.
You can estimate the uncertainty of a Neural Net with a Bayesian Neural Network. In the Bayesian Neural Network framework, you don’t try learning single function f during the training but multiple functions similarly as ensemble models. After the training, we can take the average prediction for each data point, and compute the variance between these predictions. Bayesian NN can be built for various tasks from regression to segmentation. It considers the model parameters as normal distribution and the mean and variance of the distribution for each model parameter. These parameters are learned during training as usual.
I’ve been focusing on attributing an uncertainty score to each unlabeled sample. But, uncertainty quantification constitutes an infinitesimally small portion of the broader Active Learning field. AL is dedicated to obtaining as many performance gains as possible by labeling as few samples as possible [3]. In theory, an ideal system would query both the instances that lie on the decision boundary of the model and the instances that are far from the decision boundaries (outliers, diversity sampling, new classes to define). Unfortunately, there is no yet a standard way to build the best pipeline to query the smallest data sample for maximum performance.
Another very interesting research field tries to optimize learning by feeding the model with appropriate data points. It is not about focusing on unlabeled data but rather on already labeled data. Learning tasks require some time to feed the model with easy samples and to increase the difficulty of the samples little by little during the training, similarly to a human being. For the imbalanced dataset, the opposite strategy works well: we assign a higher penalty to hard (rare) instances to force the model to learn them. In practice, this easy-first and hard-first learning strategy is set by the choice of the loss function. I encourage you to read this article [4] to learn more about sampling techniques among labeled data.
https://github.com/bledem/active-learning
[1] Human-in-the-Loop Machine Learning: Active Learning and Annotation for Human-centered AI, Robert Monarch.
[2] BatchBALD: https://oatml.cs.ox.ac.uk/blog/2019/06/24/batchbald.html
[3] A Survey of Deep Active Learning. Pengzhen, Ren & Xiao, Yun & Chang, Xiaojun & Huang, Po-Yao & Li, Zhihui & Chen, Xiaojiang & Wang, Xin. (2020). https://arxiv.org/pdf/2009.00236.pdf
[4] Which Samples Should be Learned First: Easy or Hard? Xiaoling Zhou, Ou Wu, 2021. https://openreview.net/pdf?id=pSbqyZRKzbw
[5] Active Learning in CNNs via Expected Improvement Maximization. Nagpal, Udai & Knowles, David. (2020). https://openaccess.thecvf.com/content_WACV_2020/papers/Mayer_Adversarial_Sampling_for_Active_Learning_WACV_2020_paper.pdf