Calibration of Neural Networks using Splines

1 INTRODUCTION . Despite the success of modern neural networks they are shown to be poorly calibrated ( Guo et al . ( 2017 ) ) , which has led to a growing interest in the calibration of neural networks over the past few years ( Kull et al . ( 2019 ) ; Kumar et al . ( 2019 ; 2018 ) ; Müller et al . ( 2019 ) ) . Considering classification problems , a classifier is said to be calibrated if the probability values it associates with the class labels match the true probabilities of correct class assignments . For instance , if an image classifier outputs 0.2 probability for the “ horse ” label for 100 test images , then out of those 100 images approximately 20 images should be classified as horse . It is important to ensure calibration when using classifiers for safety-critical applications such as medical image analysis and autonomous driving where the downstream decision making depends on the predicted probabilities . One of the important aspects of machine learning research is the measure used to evaluate the performance of a model and in the context of calibration , this amounts to measuring the difference between two empirical probability distributions . To this end , the popular metric , Expected Calibration Error ( ECE ) ( Naeini et al . ( 2015 ) ) , approximates the classwise probability distributions using histograms and takes an expected difference . This histogram approximation has a weakness that the resulting calibration error depends on the binning scheme ( number of bins and bin divisions ) . Even though the drawbacks of ECE have been pointed out and some improvements have been proposed ( Kumar et al . ( 2019 ) ; Nixon et al . ( 2019 ) ) , the histogram approximation has not been eliminated.1 In this paper , we first introduce a simple , binning-free calibration measure inspired by the classical Kolmogorov-Smirnov ( KS ) statistical test ( Kolmogorov ( 1933 ) ; Smirnov ( 1939 ) ) , which also provides an effective visualization of the degree of miscalibration similar to the reliability diagram ( NiculescuMizil & Caruana ( 2005 ) ) . To this end , the main idea of the KS-test is to compare the respective classwise cumulative ( empirical ) distributions . Furthermore , by approximating the empirical cumulative distribution using a differentiable function via splines ( McKinley & Levine ( 1998 ) ) , we 1We consider metrics that measure classwise ( top-r ) calibration error ( Kull et al . ( 2019 ) ) . Refer to section 2 for details . obtain an analytical recalibration function2 which maps the given network outputs to the actual class assignment probabilities . Such a direct mapping was previously unavailable and the problem has been approached indirectly via learning , for example , by optimizing the ( modified ) cross-entropy loss ( Guo et al . ( 2017 ) ; Mukhoti et al . ( 2020 ) ; Müller et al . ( 2019 ) ) . Similar to the existing methods ( Guo et al . ( 2017 ) ; Kull et al . ( 2019 ) ) the spline-fitting is performed using a held-out calibration set and the obtained recalibration function is evaluated on an unseen test set . We evaluated our method against existing calibration approaches on various image classification datasets and our spline-based recalibration approach consistently outperforms existing methods on KS error , ECE as well as other commonly used calibration measures . Our approach to calibration does not update the model parameters , which allows it to be applied on any trained network and it retains the original classification accuracy in all the tested cases . 2 NOTATION AND PRELIMINARIES . We abstract the network as a function fθ : D → [ 0 , 1 ] K , where D ⊂ IRd , and write fθ ( x ) = z . Here , x may be an image , or other input datum , and z is a vector , sometimes known as the vector of logits . In this paper , the parameters θ will not be considered , and we write simply f to represent the network function . We often refer to this function as a classifier , and in theory this could be of some other type than a neural network . In a classification problem , K is the number of classes to be distinguished , and we call the value zk ( the k-th component of vector z ) the score for the class k. If the final layer of a network is a softmax layer , then the values zk satisfy ∑K k=1 zk = 1 , and zk ≥ 0 . Hence , the zk are pseudoprobabilities , though they do not necessarily have anything to do with real probabilities of correct class assignments . Typically , the value y∗ = argmaxk zk is taken as the ( top-1 ) prediction of the network , and the corresponding score , maxk zk is called the confidence of the prediction . However , the term confidence does not have any mathematical meaning in this context and we deprecate its use . We assume we are given a set of training data ( xi , yi ) ni=1 , where xi ∈ D is an input data element , which for simplicity we call an image , and yi ∈ K = { 1 , . . . , K } is the so-called ground-truth label . Our method also uses two other sets of data , called calibration data and test data . It would be desirable if the numbers zk output by a network represented true probabilities . For this to make sense , we posit the existence of joint random variables ( X , Y ) , where X takes values in a domain D ⊂ IRd , and Y takes values in K. Further , let Z = f ( X ) , another random variable , and Zk = fk ( X ) be its k-th component . Note that in this formulation X and Y are joint random variables , and the probability P ( Y | X ) is not assumed to be 1 for single class , and 0 for the others . A network is said to be calibrated if for every class k , P ( Y = k | Z = z ) = zk . ( 1 ) This can be written briefly as P ( k | f ( x ) ) = fk ( x ) = zk . Thus , if the network takes input x and outputs z = f ( x ) , then zk represents the probability ( given f ( x ) ) that image x belongs to class k. The probability P ( k | z ) is difficult to evaluate , even empirically , and most metrics ( such as ECE ) use or measure a different notion called classwise calibration ( Kull et al . ( 2019 ) ; Zadrozny & Elkan ( 2002 ) ) , defined as , P ( Y = k | Zk = zk ) = zk . ( 2 ) This paper uses this definition ( 2 ) of calibration in the proposed KS metric . Calibration and accuracy of a network are different concepts . For instance , one may consider a classifier that simply outputs the class probabilities for the data , ignoring the input x . Thus , if fk ( x ) = zk = P ( Y = k ) , this classifier f is calibrated but the accuracy is no better than the random predictor . Therefore , in calibration of a classifier , it is important that this is not done while sacrificing classification ( for instance top-1 ) accuracy . 2Open-source implementation available at https : //github.com/kartikgupta-at-anu/ spline-calibration The top-r prediction . The classifier f being calibrated means that fk ( x ) is calibrated for each class k , not only for the top class . This means that scores zk for all classes k give a meaningful estimate of the probability of the sample belonging to class k. This is particularly important in medical diagnosis where one may wish to have a reliable estimate of the probability of certain unlikely diagnoses . Frequently , however , one is most interested in the probability of the top scoring class , the top-1 prediction , or in general the top-r prediction . Suppose a classifier f is given with values in [ 0 , 1 ] K and let y be the ground truth label . Let us use f ( −r ) to denote the r-th top score ( so f ( −1 ) would denote the top score ; the notation follows python semantics in which A [ −1 ] represents the last element in array A ) . Similarly we define max ( −r ) for the r-th largest value . Let f ( −r ) : D → [ 0 , 1 ] be defined as f ( −r ) ( x ) = max ( −r ) k fk ( x ) , and y ( −r ) = { 1 if y = argmax ( −r ) k fk ( x ) 0 otherwise . ( 3 ) In words , y ( −r ) is 1 if the r-th top predicted class is the correct ( ground-truth ) choice . The network is calibrated for the top-r predictor if for all scores σ , P ( y ( −r ) = 1 | f ( −r ) ( x ) = σ ) = σ . ( 4 ) In words , the conditional probability that the top-r-th choice of the network is the correct choice , is equal to the r-th top score . Similarly , one may consider probabilities that a datum belongs to one of the top-r scoring classes . The classifier is calibrated for being within-the-top-r classes if P ( ∑r s=1 y ( −s ) = 1 ∣∣ ∑r s=1 f ( −s ) ( x ) = σ ) = σ . ( 5 ) Here , the sum on the left is 1 if the ground-truth label is among the top r choices , 0 otherwise , and the sum on the right is the sum of the top r scores . 3 KOLMOGOROV-SMIRNOV CALIBRATION ERROR . We now consider a way to measure if a classifier is classwise calibrated , including top-r and withintop-r calibration . This test is closely related to the Kolmogorov-Smirnov test ( Kolmogorov ( 1933 ) ; Smirnov ( 1939 ) ) for the equality of two probability distributions . This may be applied when the probability distributions are represented by samples . We start with the definition of classwise calibration : P ( Y = k | fk ( X ) = zk ) = zk . ( 6 ) P ( Y = k , fk ( X ) = zk ) = zk P ( fk ( X ) = zk ) , Bayes ’ rule . This may be written more simply but with a less precise notation as P ( zk , k ) = zk P ( zk ) . Motivation of the KS test . One is motivated to test the equality ( or difference between ) two distributions , defined on the interval [ 0 , 1 ] . However , instead of having a functional form of these distributions , one has only samples from them . Given samples ( xi , yi ) , it is not straight-forward to estimate P ( zk ) or P ( zk | k ) , since a given value zk is likely to occur only once , or not at all , since the sample set is finite . One possibility is to use histograms of these distributions . However , this requires selection of the bin size and the division between bins , and the result depends on these parameters . For this reason , we believe this is an inadequate solution . The approach suggested by the Kolmogorov-Smirnov test is to compare the cumulative distributions . Thus , with k given , one tests the equality∫ σ 0 P ( zk , k ) dzk = ∫ σ 0 zk P ( zk ) dzk . ( 7 ) Writing φ1 ( σ ) and φ2 ( σ ) to be the two sides of this equation , the KS-distance between these two distributions is defined as KS = maxσ |φ1 ( σ ) − φ2 ( σ ) | . The fact that simply the maximum is used here may suggest a lack of robustness , but this is a maximum difference between two integrals , so it reflects an accumulated difference between the two distributions . To provide more insights into the KS-distance , let us a consider a case where zk consistently over or under-estimates P ( k | zk ) ( which is usually the case , at least for top-1 classification ( Guo et al . ( 2017 ) ) ) , then P ( k | zk ) −zk has constant sign for all values of zk . It follows that P ( zk , k ) −zkP ( zk ) has constant sign and so the maximum value in the KS-distance is achieved when σ = 1 . In this case , KS = ∫ 1 0 ∣∣P ( zk , k ) − zkP ( zk ) ∣∣ dzk = ∫ 1 0 ∣∣P ( k | zk ) − zk∣∣P ( zk ) dzk , ( 8 ) which is the expected difference between zk and P ( k | zk ) . This can be equivalently referred to as the expected calibration error for the class k. Sampled distributions . Given samples ( xi , yi ) Ni=1 , and a fixed k , one can estimate these cumulative distributions by ∫ σ 0 P ( zk , k ) dzk ≈ 1 N N∑ i=1 1 ( fk ( xi ) ≤ σ ) × 1 ( yi = k ) , ( 9 ) where 1 : B → { 0 , 1 } is the function that returns 1 if the Boolean expression is true and otherwise 0 . Thus , the sum is simply a count of the number of samples for which yi = k and fk ( xi ) ≤ σ , and so the integral represents the proportion of the data satisfying this condition . Similarly , ∫ σ 0 zk P ( zk ) dzk ≈ 1 N N∑ i=1 1 ( fk ( xi ) ≤ σ ) fk ( xi ) . ( 10 ) These sums can be computed quickly by sorting the data according to the values fk ( xi ) , then defining two sequences as follows . h̃0 = h0 = 0 , hi = hi−1 + 1 ( yi = k ) /N , h̃i = h̃i−1 + fk ( xi ) /N . ( 11 ) The two sequences should be the same , and the metric KS ( fk ) = max i |hi − h̃i| , ( 12 ) gives a numerical estimate of the similarity , and hence a measure of the degree of calibration of fk . This is essentially a version of the Kolmogorov-Smirnov test for equality of two distributions . Remark . All this discussion holds also when k < 0 , for top-r and within-top-r predictions as discussed in section 2 . In ( 11 ) , for instance , f−1 ( xi ) means the top score , f−1 ( xi ) = maxk ( fk ( xi ) ) , or more generally , f−r ( xi ) means the r-th top score . Similarly , the expression yi = −r means that yi is the class that has the r-th top score . Note when calibrating the top-1 score , our method is applied after identifying the top-1 score , hence , it does not alter the classification accuracy .

Summary: The paper presents a post-hoc calibration method for deep neural net classification. The method proposes to first reduces the well-known ECE score to a special case of the Kolmogorov-Smirnov (KS) test, and this way solves the dependency of ECE on the limiting binning assumption. The method proposes next to recalibrate the classification probabilities by fitting a cubic spline to the KS test score.