E NABLING C ALIBRATION I N T HE Z ERO -S HOT I NFERENCE OF L ARGE V ISION -L ANGUAGE M ODELS
A BSTRACT
Calibration of deep learning models is crucial to their trustworthiness and safe usage, and as such, has been extensively studied in supervised classification models, with methods crafted to decrease miscalibration.
However, there has yet to be a comprehensive study of the calibration of vision-language models that are used for zero-shot inference, like CLIP.
We measure calibration across relevant variables like prompt, dataset and architecture and find that zero-shot inference with CLIP is miscalibrated.
Furthermore, we propose a modified version of temperature scaling that is aligned with the common use cases of CLIP as a zero-shot inference model and show that a single learned temperature generalizes for each specific CLIP model (defined by a chosen pre-training dataset and architecture) across inference dataset and prompt choice.
I NTRODUCTION
Interpretability is one of the main hurdles in the trust, safety, and reliability of deep learning models.
One specific area of concern is the miscalibration of these models, where the confidences of the model predictions do not reflect the probabilities of being correct.
There exists many studies on the calibration and corresponding interpretability of classification models (Guo et al., 2017; Kull et al., 2019; Rajendran & LeVine, 2019) that are trained and tested on in a traditional fashion - a given dataset of one modality (like images) is split into a train, validation, and test set, with a known, fixed number of classes.
The model is trained on the train set, tuned with the validation set, and evaluated on the test set.
However, the use of vision-language models for zero-shot inference, like CLIP (Radford et al., 2021), is becoming increasingly popular.
In this setting, the dataset is multimodal, and the inference paradigm allows for zero-shot inference, where the class being predicted was not explicitly defined as a class of interest during training.
There has yet to be either an extensive study of the calibration of CLIP as a zero-shot inference model or applications of calibration methods to CLIP’s zero-shot inference setting.
We therefore propose “Zero-Shot-Enabled Temperature Scaling,” a method based on Temperature Scaling (TS) that enables zero-shot inference for models like CLIP.
Our main contributions are
• An extensive analytical study of the calibration of CLIP stratified by architecture, dataset (pre-training and inference), and input prompt.
There exists literature that briefly mentions the calibration of CLIP (Minderer et al., 2021), but not one that studies the calibration across any of the previously mentioned experimental variables.
• The novel application of Temperature Scaling to CLIP in a way that preserves the ability for zero-shot inference with an exposition on its robustness to changes in inference dataset and prompt.
We note that the only modification to inference is to perform Temperature Scaling on the text-image similarity with temperature T .
We show that this parameter varies solely with changes in underlying architecture and pre-training dataset (identical to the axes of variation allowed in the parameters of CLIP), and thus can be used at inference time for any arbitrary set of prompts or inference datasets.
Therefore, to perform inference on a given dataset of interest requires no training, tuning, or calibration - meaning our method matches the zero-shot inference paradigm as used in CLIP.
P RELIMINARIES 2. P ROBLEM S ETUP
Let X : Ω → X ⊂ R D be the input (random) variable and let Y : Ω → { 1 , 2 , ..., C − 1 , C } ⊂ N (where C is the number of output classes) be the response (random) variable.
Typically X has some information about Y and we’d like to make inferences about Y given X .
A common situation is trying to compute argmax c
E ( Y = c
| X = x ) with a model
ˆ f : R d → [0 , 1] C .
That is, ˆ f predicts the most likely class on inference example x i among the output classes as argmax c
ˆ f c ( x i ) .
Typically, f uses an intermediate logit function L : R D → R C .
That is, the logit function returns a real number per class.
For a given class index c , the logit corresponding to that class L c ( x i ) ideally increases as the resemblance increases between the inference example x i and the training inputs of class c (formally { x j s.t.
y j = c } Nj =1 ).
2. CLIP
Introduced in Radford et al.
(2021), CLIP is trained to align image/text pairs.
This enables zero-shot inference, where the output classes of Y are chosen at inference time based on natural language prompts.
CLIP accomplishes this by calculating its logit function as the cosine similarity between the embedding of a given input image x i - which we denote as E im ( x i ) - and the language embeddings of the natural language corresponding to the output classes.
We denote the language embedding of the natural language corresponding to a given output class y c as E lang ( y c ) .
More formally, the logit function of CLIP is
[Equation]
For CLIP and non-CLIP models, the softmax function is typically used to convert these logits into class probabilities.
That is
[Equation]
2. C ALIBRATION
Ideally, we would like the confidence estimate ˆ p ( x i , ˆ f ) = max c ˆ f c ( x i ) to be in alignment with the accuracy of ˆ f on x i and points with confidences similar to that of x i .
As an example noted in Guo et al.
(2017), given a set of 100 predictions with confidences of 0 .
8 , we would hope that 80 of these predictions would be correctly classified.
If so, we would consider the model to be calibrated .
Let D test =
{ ( x i , y i ) }
Ni =1 be the test set on which ˆ f is evaluated, where each x i and y i are examples drawn from ( X, Y ) (or a subset thereof), respectively.
Further let
D testp = { ( x i , y i )
∈
D test s.t.
ˆ p ( x i , ˆ f ) = p } .
Formally, a model is calibrated if
[Equation]
We further note as in Guo et al.
(2017) that the probability in 1 cannot be computed on a single sample, since an accuracy is computed on a set of examples rather than a single sample.
Thus, there is a need for empirical approximations that can capture the essence of 1, which we describe below.
2.3. V ISUALIZING M ISCALIBRATION V IA R ELIABILITY D IAGRAMS
We visualize the calibration of our estimator through reliability diagrams (DeGroot & Fienberg, 1983; Niculescu-Mizil & Caruana, 2005).
These diagrams group points by their predicted confidence scores into M equally spaced bins, and then compute the true and estimated accuracies in each bin as follows: let B m be the ( x i , y i ) test samples whose confidence (i.e. estimated accuracy) falls into the interval I m =
(cid:0) ( m − 1) /M, m/M (cid:3) , for m = 2 , . . .
, M , and I 1 =
[0 , 1 M ] .
The true accuracy is acc ( ˆ f, B m ) and the textitestimated accuracy (i.e. average confidence) within B m is 1 / | B m | (cid:80) ( x i ,y i )
∈
B m ˆ p ( x i , ˆ f ) , which we write in short-hand as ˆ p ( ˆ f, B m ) .
The reliability diagram plots the difference between true accuracy and estimated accuracy for all M bins, and deviations from the line f ( x ) = x represent miscalibrations: areas where there is a significant difference between the estimated and true accuracy.
In Figure 1, the pink and purple portions of bars represent overconfidence and underconfidence, respectively, while the blue portions of bars represent how well-calibrated the model is.
For all experiments, we let M = 10 , as is standard (Guo et al., 2017; Rajendran & LeVine, 2019; Kull et al., 2019).
Following standard practice (Minderer et al., 2021), we also visualize a histogram of the number of points in each bin | B m | .
Mismatches between confidence and accuracy in bins with a relatively large amount of points are more grave than mismatches corresponding to bins with fewer points, since it means that the model was more miscalibrated on a larger number of points.
2.3. Q UANTIFYING M ISCALIBRATION V IA E XPECTED C ALIBRATION E RROR ( ECE )
We can quantify this miscalibration with the Expected Calibration Error ( ECE ) introduced in
Naeini et al.
ECE , aimed at summarizing the miscalibration visualized in reliability diagrams, is calculated as
[Equation]
In the leftmost columns of Figure 1, we show the calibration of CLIP, as it is regularly used, via reliability diagrams.
2. C ALIBRATING W ITH L ABELS V IA T EMPERATURE S CALING
Typically used to reduce miscalibration, Temperature Scaling (Guo et al., 2017) geometrically decreases the logit function L by a scalar T .
That is, ˆ f has a logit function that employs Temperature
Scaling as L calibrated c ( x i ; T )
= L c ( x i ) /T .
In the case of CLIP, the Temperature-Scaling-infused logit function is L calibrated c ( x i ; T ) = L CLIP c ( x i ) /T
[Table]
Temperature Scaling typically calibrates a frozen network post-training using a dataset
D calibration = { ( x i , y i ) } Ni =1 by minimizing the cross-entropy loss function L CE ( ˆ f, D calibration ) = − (cid:80)
[Equation]
In the rightmost two rows of Figure 1, we present reliability diagrams of CLIP once calibrated via
Temperature Scaling.
In Appendix Section A.1, we present reliability diagrams in this evaluation setting using additional supervised calibration methods Isotonic Regression introduced in Zadrozny & Elkan (2002), and Histogram Binning introduced in Zadrozny & Elkan (2001), as well as Unsupervised Temperature Scaling introduced in Mozafari et al.
These are solely for context, since the main benefits of using CLIP are to be able to perform inference on a task of interest both without having any training labels on that task (which are required by the supervised methods) and without a calibration dataset that is specific for each inference dataset (which are required by all of the previously mentioned methods).
O UR M ETHOD : Z ERO -S HOT -E NABLED T EMPERATURE S CALING
To address this gap of the inability to calibrate CLIP without a calibration dataset, we propose
Zero-Shot-Enabled Temperature Scaling.
For a given architecture and pre-training dataset of CLIP, we simply train a temperature on an auxiliary dataset via Temperature Scaling.
We then use this temperature on all downstream inferences of this model regardless of prompt or inference dataset.
For all experiments, we use ImageNet-1k (Huang & Li, 2021) as our auxiliary dataset with “a photo of {} ” as the prompt in the supervised training of the temperature ultimately used in our Zero-ShotEnabled Temperature Scaling.
Once trained, this model can be used for zero-shot inference since it does not require any re-training or tuning to be used on any given inference dataset with any given prompt.
We note that the training of Zero-Shot-Enabled Temperature Scaling does require a dataset on which to perform a training process.
However, this method enables zero-shot inference in an identical sense to CLIP: CLIP trains parameters on an auxiliary dataset and enables zero-shot inference on any arbitrary unseen distribution without any training on a dataset with significant distribution overlap to the inference distribution of interest. We note that CLIP allows the training of the parameters on a given architecture and a given pre-training dataset to be independent of the training of the parameters of different architectures and pre-training datasets.
Therefore, we allow a different temperature for each architecture/pre-training dataset pair, matching the CLIP paradigm.
Given this single parameter
T associated with the CLIP architecture and pre-training dataset, a user can utilize our method by simply diving the CLIP logits by T , without any training, tuning, or calibration processes necessary.
In the middle two columns of Figure 1, we present reliability diagrams of a CLIP-based model once updated via Zero-Shot-Enabled Temperature Scaling.
R ESULTS 4. C OMPARISON TO V ANILLA CLIP AND CLIP + T EMPERATURE S CALING
In Table 1, we show the Expected Calibration Error results of our method compared to CLIP and
CLIP calibrated via supervised Temperature Scaling on a variety of prompts, architectures, inference datasets, and pre-training datasets.
For detailed results across prompts and datasets for a single pretraining dataset and architecture (selected arbitrarily), please see Figure 3.
We note that our results are superior to CLIP without any calibration in all settings, but that our method results in models that are still significantly less calibrated than those calibrated via the supervised variant of Temperature
Scaling - thus, leaving room for future Zero-Shot-Enabled CLIP calibration methods that improve upon our method.
4. R OBUSTNESS TO C HANGES IN P ROMPT AND I NFERENCE D ATASET
In Figure 2, we show that, for a single architecture and pre-training dataset, the optimal temperature
T across prompts and inference datasets are approximately the same.
This is remarkable considering each of the inference and auxiliary datasets has a different number of classes from each other (since
CIFAR10 has 10 classes, CIFAR100 has 100 classes, SUN397 has 397 classes, and ImageNet-1k has 1000 classes), as well as different distributions.
We do note that this temperature T needs to be trained per architecute/pre-training dataset pair, as different pairs have (slightly) different optimal
T ’s, as can be visualized by comparing the approximate optimal T ’s in the left plot (around T = 1 . 55 ) and in the right plot (around T = 1 . 35 ) of Figure 2.
C ONCLUSION AND F UTURE W ORK
The miscalibration of supervised classification models has been extensively studied and improved via many calibration methods.
Yet, prior to this paper, that has not been the case for vision-language models (like CLIP) with a different evaluation setup than traditional deep learning models.
In this paper, we have shown that CLIP out-of-the-box is generally miscalibrated for a variety of experimental parameters.
Lastly, to address this miscalibration, we have also presented a calibration method for CLIP that modifies inference with a single parameter that is aligned with the CLIP zero-shotinference paradigm.
Future work will extend additional supervised calibration methods to CLIP’s zero-shot-inference setting and provide improvements to our method to close the gap in calibration between our Zero-Shot-Enabled Temperature Scaling and the supervised variant of Temperature
Scaling. A
C ALIBRATION R ESULTS OF S UPERVISED M ETHODS
In Figure 4 we present reliability diagrams for CLIP using the following supervised calibration methods: Isotonic Regression (Zadrozny & Elkan, 2002) and Histogram Binning (Zadrozny & Elkan, 2001), as well as Unsupervised Temperature Scaling (Mozafari et al., 2019).
All three of these methods perform well and significantly reduce miscalibration.
However, as mentioned in Section 2.4, the use of these methods is inconsistent with how CLIP is often used, and is therefore impractical for wide-scale adoption.
A. P ROMPTS AND D ATASETS U SED IN F IGURE
Our ECE results in Figure 1 are averaged over the following datasets and prompts:
SUN397 (Yu et al., 2015) with the following prompts: (a) “a photo of {} ” (b) “a photo of the {} ” 2.
CIFAR10 and CIFAR100 (Krizhevsky, 2009) with the following prompts: (a) “a photo of a {} ” (b) “a blurry photo of a {} ” (c) “a black and white photo of a {} ” (d) “a low contrast photo of a {} ” (e) “a high contrast photo of a {} ” (f) “a bad photo of a {} ” (g) “a good photo of a {} ” (h) “a photo of a small {} ” (i) “a photo of a big {} ” (j) “a photo of the {} ” (k) “a blurry photo of the {} ” (l) “a black and white photo of the {} ” (m) “a low contrast photo of the {} ” (n) “a high contrast photo of the {} ” (o) “a bad photo of the {} ” (p) “a good photo of the {} ” (q) “a photo of the small {} ” (r) “a photo of the big {} ”
A. A RCHITECTURES AND P RETRAINING D
All models in Figure 1 are from OpenCLIP (Ilharco et al., 2021).
All ViT models used are pretrained on either LAION-400M (Schuhmann et al., 2021) or LAION-2B (Schuhmann et al., 2022).
The
ResNet models are pretrained on YFCC15M, a subset of YFCC100M
(Thomee et al., 2016), or the
Conceptual Captions Dataset (Changpinyo et al., 2021)