SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Muhammad Akhtar Munir $^{1,2}$ , Salman Khan $^{1,3}$ , Muhammad Haris Khan $^{1}$ , Mohsen Ali $^{2}$ , Fahad Shahbaz Khan $^{1,4}$
<sup>1</sup>Mohamed bin Zayed University of AI, <sup>2</sup>Information Technology University,
$^{3}$ Australian National University,  $^{4}$ Linköping University
akhtar.munir@mbzuai.ac.ae
Albeit revealing impressive predictive performance for several computer vision tasks, deep neural networks (DNNs) are prone to making overconfident predictions. This limits the adoption and wider utilization of DNNs in many safety-critical applications. There have been recent efforts toward calibrating DNNs, however, almost all of them focus on the classification task. Surprisingly, very little attention has been devoted to calibrating modern DNN-based object detectors, especially detection transformers, which have recently demonstrated promising detection performance and are influential in many decision-making systems. In this work, we address the problem by proposing a mechanism for calibrated detection transformers (Cal-DETR), particularly for Deformable-DETR, UP-DETR and DINO. We pursue the train-time calibration route and make the following contributions. First, we propose a simple yet effective approach for quantifying uncertainty in transformer-based object detectors. Second, we develop an uncertainty-guided logit modulation mechanism that leverages the uncertainty to modulate the class logits. Third, we develop a logit mixing approach that acts as a regularizer with detection-specific losses and is also complementary to the uncertainty-guided logit modulation technique to further improve the calibration performance. Lastly, we conduct extensive experiments across three in-domain and four out-domain scenarios. Results corroborate the effectiveness of Cal-DETR against the competing train-time methods in calibrating both in-domain and out-domain detections while maintaining or even improving the detection performance. Our codebase and pre-trained models can be accessed at https://github.com/akhtarvision/cal-detr.
Deep neural networks (DNNs) have enabled notable advancements in computer vision, encompassing tasks such as classification [29, 6, 44, 11], object detection [2, 53, 5, 38, 43, 51], and semantic segmentation [47, 42, 3]. Due to their high predictive performance, DNNs are increasingly adopted in many real-world applications. However, it has been shown that DNNs often make highly confident predictions even when they are incorrect [10]. This misalignment between the predictive confidence and the actual likelihood of correctness undermines trust in DNNs and can lead to irreversible harm in safety-critical applications, such as medical diagnosis [7, 41], autonomous driving [9], and legal decision-making [49].
Among others, an important factor behind the overconfident predictions of DNNs is the training with zero-entropy target vectors, which do not encompass uncertainty information [12]. An early class of methods aimed at reducing the model's miscalibration proposes various post-hoc techniques [10, 15, 16, 37, 21]. Typically, a single temperature scaling parameter is used to re-scale the logits of a trained model which is learned using a held-out validation set. Despite being effective and simple, they are architecture-dependent and require a held-out set which is unavailable in many real-world scenarios [28]. Recently, we have seen an emergence of train-time calibration methods. They are
typically based on an auxiliary loss that acts as a regularizer for predictions during the training process [12, 28, 25, 19, 33]. For instance, [28] imposes a margin constraint on the logit distances in the label smoothing technique, and [12] develops an auxiliary loss that calibrates the confidence of both the maximum class and the non-maximum classes. These train-time approaches reveal improved calibration performance than various existing post-hoc methods.
We note that majority of the DNN calibration methods target the classification task [10, 15, 16, 37, 21, 12, 28, 25, 19]. Strikingly, much little attention has been directed towards studying the calibration of object detectors [33]. Object detection is a fundamental task in computer vision whose application to safety-critical tasks is growing rapidly. Furthermore, most of the existing methods focus on calibrating in-domain predictions. However, a deployed model could be exposed to constantly changing distributions which could be radically different from its training distribution. Therefore, in this paper, we aim to investigate the calibration of object detection methods both for in-domain and out-domain predictions. Particularly, we consider the recent transformer-based object detection methods [5, 53, 51] as they are the current state-of-the-art with a simpler detection pipeline. Moreover, they are gaining increasing attention from both the scientific community and industry practitioners.
In this paper, inspired by the train-time calibration paradigm, we propose an uncertainty-guided logit modulation and a logit mixing approach to improve the calibration of detection transformers (dubbed as Cal-DETR). In order to modulate the logits, we propose a new and simple approach to quantify uncertainty in transformer-based object detectors. Unlike existing approaches [8, 23], it does not require any architectural modifications and imposes minimal computational overhead. The estimated uncertainty is then leveraged to modulate the class logits. Next, we develop a mixing approach in the logit space which acts as a regularizer with detection-specific losses to further improve the calibration performance. Both uncertainty-guided logit modulation and logit mixing as a regularizer are complementary to each other. We perform extensive experiments with three recent transformer-based detectors (Deformable-DETR (D-DETR) [53], UP-DETR [5] and DINO [51]) on three in-domains and four out-domain scenarios, including the large-scale object detection MS-COCO dataset [27]. Our results show the efficacy of Cal-DETR against the established train-time calibration methods in improving both in-domain and out-domain calibration while maintaining or even improving detection performance.
Vision transformer-based object detection: Vision transformers are enabling new state-of-the-art performances for image classification [6, 46], semantic segmentation [47, 42], and object detection [2, 53, 5, 51]. The work of DETR [2] introduced the first-ever transformer-based object detection pipeline to the vision community by eliminating post-processing steps like non-maximum suppression with object matching to particular ground truth using bipartite matching. However, DETR is very slow in convergence and performs poorly on small objects, and to address these issues, Deformable-DETR [53] was proposed that is based on a deformable attention module as opposed to vanilla self-attention in DETR [2]. The work of [5] further enhanced the performance of this object detection pipeline by pretraining the encoder-decoder parameters. Recently, DINO [51] improved detection performance with contrastive denoising training, look forward twice approach and integrated effective initialization of anchors.
Model calibration: Model calibration requires the perfect alignment of model's predictive confidence with the actual likelihood of its correctness. One of the earliest techniques for model calibration is known as post-hoc temperature scaling (TS) [10], which is a variant of Platt scaling [37]. The idea is to re-scale the logits with a single temperature parameter (T) which is learned using a held-out validation set. However, a limitation of TS is that the selection of temperature parameter T value is reliant on the network architecture and data [28]. And, TS has been shown to be effective for calibrating in-domain predictions. To extend TS to domain-drift scenarios, [45] proposed to perturb the held-out validation set before the post-hoc processing step. Dirichlet calibration (DC) [20] was extended from beta calibration [21] to a multi-class setting. It uses an extra neural network layer that log-transforms the miscalibrated probabilities, and the parameters for this layer are learned using the validation set. Despite being effective and computationally inexpensive, post-hoc calibration methods demand the availability of a held-out validation set, which is a difficult requirement to be met for many real-world use cases.
Figure 1: Main architecture: An image is passed through a feature extractor CNN and unmodified transformer's encoder structure, whereas the decoder is modified to get the model well calibrated. Logits are modulated based on uncertainty quantification that exploits the inbuilt design of the decoder without extra computational cost (Sec. 3.3.1). This module is followed by a logit mixing mechanism which is comprised of a regularizer  $(\mathcal{L}_R)$  to obtain calibrated predictions (Sec. 3.3.2). <category:confidence> are depicted on the right side of figure. The baseline detector (D-DETR [53]) outputs wrong dog prediction with a relatively high confidence, however, our Cal-DETR provides accurate cat prediction with a high confidence. Green boxes are accurate predictions whereas Red (dashed) box is inaccurate prediction. Blue bounding boxes represent ground truths for corresponding detections.  $\mathcal{L}_{cls}$  and  $\mathcal{L}_{loc}$  are classification and localization losses respectively [5, 53, 51].
Train-time calibration methods tend to engage model parameters during training by proposing an auxiliary loss function that is optimized along with the task-specific loss function [12, 28]. Some methods aim at maximizing the entropy of the predictive distribution either explicitly or implicitly. For instance, [35] formulated an auxiliary term, that is based on negative of entropy, which is minimized during training to alleviate the impact of overconfident predictions. Along similar lines, [32] investigated that the label smoothing (LS) technique is also capable of reducing miscalibration, and [31] demonstrated that the focal loss (FL) can improve the model calibration. These methods are effective, however, their logit-distance constraints can often lead to non-informative solutions. To circumvent this, [28] proposed a margin-based generalization of logit-distance constraints to achieve a better balance between discriminative and calibration performance. [12] proposed to calibrate the confidence of both predicted and non-predicted classes by formulating an auxiliary loss term known as the multi-class difference of confidence and accuracy. All aforementioned calibration techniques target classification task, recently [33] developed a train-time calibration loss for object detection which imposes constraints on multi-class predictions and the difference between the predicted confidence and the IoU score.
Uncertainty estimation and model calibration: Owing to the computational intractability of Bayesian inference, different approximate inference methods have been proposed, such as variational inference [1, 30], and stochastic expectation propagation [14]. The work of [34] evaluated predictive uncertainty of models under distributional shift and investigates its impact on the accuracy-uncertainty trade-off. [19] leveraged the predictive uncertainty of model and proposed to align this with accuracy, and [18] induced soft bins and makes calibration error estimates differentiable. It is also possible to quantify uncertainty by ensemble learning that takes into account empirical variance of model predictions. It allows better discriminative performance and improved predictive uncertainty estimates leading to well-calibrated models. Ensembles can emerge with difference in weights initializations and random shuffling of training examples [23] and Monte Carlo (MC) dropout [8]. However, ensemble-based uncertainty estimation techniques could be computationally expensive, especially with complex models and large-scale datasets. In this work, we propose a simple and computationally efficient technique to quantify uncertainty in a transformer-based object detection pipeline.
This section begins by discussing the preliminaries, including the problem settings and the background knowledge relevant to calibrating object detectors. We then detail our proposed approach, Cal-DETR, encompassing uncertainty-guided logit modulation and logit mixing for calibrating the recent vision transformer-based (ViT) object detection methods (Fig. 1).
Assume a joint distribution  $S(\mathcal{X},\mathcal{Y})$ , where  $\mathcal{X}$  is an input space and  $\mathcal{Y}$  is the corresponding label space. Let  $\mathcal{D} = \{(\mathbf{x}_i,\hat{\mathbf{y}}_i)_{i = 1}^N\}$  be the dataset sampled from this joint distribution consisting of pairs of input image  $\mathbf{x}_i\in \mathcal{X}$  and corresponding label  $\hat{\mathbf{y}}_i\in \mathcal{V}$ . The input image belongs to  $\mathbf{x}_i\in \mathbb{R}^{H\times W\times Z}$ , where  $H$  is the height,  $W$  is the width and  $Z$  is the number of channels. For each input image  $\mathbf{x}_i$ , we have the corresponding ground truth label,  $\hat{\mathbf{y}}_i\in \mathbb{R}^5$ , and it consists of class label and the bounding box coordinates  $(\hat{c},\hat{x},\hat{y},\hat{w},\hat{h})$ . Here  $\hat{c}$  is the class label in  $\{1,2,\dots,C\}$  and  $\hat{x},\hat{y},\hat{w},\hat{h}$  are the bounding box  $\hat{\mathbf{b}}\in \mathbb{R}^4$  coordinates. Next, we define the calibration of a model in classification and object detection tasks.
A model is said to be calibrated when its accuracy perfectly aligns with the predictive confidence. An uncalibrated model is prone to making over-confident and under-confident predictions. To mitigate these problems, for an image, a model should be able to predict the class confidence that matches with the actual likelihood of its correctness. Let  $\mathcal{H}_{cls}$  be a classification model which predicts a class label  $\bar{c}$  with the confidence score  $\bar{z}$ . Now, the calibration for the classification task can be defined as:  $\mathbb{P}(\bar{c} = \hat{c}|\bar{z} = z) = z$ , s.t.,  $z \in [0,1]$  [22]. This expression shows that, to achieve the perfect calibration in classification, the predicted confidence and accuracy must match.
Let  $\mathcal{H}_{\text{det}}$  be the detection model, which predicts a bounding box  $\bar{\mathbf{b}}$  and the corresponding class label  $\bar{c}$  with the confidence score  $\bar{z}$ . The calibration for a detection task is defined as  $\mathbb{P}(F = 1|\bar{z} = z) = z$ ,  $\forall z \in [0,1][22]^1$ . Where  $F = 1$  denotes the true prediction of the object detection model which occurs when the predicted and the ground truth classes are the same, and the Intersection over Union (IoU) of both the predicted and ground truth boxes are above a pre-defined threshold  $k$  i.e.  $\mathbb{1}[IoU(\bar{\mathbf{b}},\hat{\mathbf{b}})\geq k]\mathbb{1}[\bar{c} = \hat{c} ]$ .  $F = 0$  denotes the false prediction and together with  $F = 1$ , the precision is determined. In object detection, to achieve perfect calibration, the precision of a model must match the predicted confidence. In the next subsection, we revisit the formulae for measuring calibration.
3.2 Measuring Calibration
Classification: For classification problems, miscalibration is measured by computing Expected Calibration Error (ECE) [10]. It computes the expected divergence of accuracy from all possible confidence levels as:  $\mathbb{E}_{\bar{z}}[|\mathbb{P}(\bar{c} = \hat{c} |\bar{z} = z) - z|]$ . In practice, the ECE metric is approximated by (1) first dividing the space of confidence scores into B equal-sized bins, (2) then computing the (absolute) difference between the average accuracy and the average confidence of samples falling into a bin, and finally (3) summing all the differences after weighting each difference by the relative frequency of samples in a bin.
$$
\mathrm {E C E} = \sum_ {b = 1} ^ {B} \frac {| S (b) |}{| D |} | \text {a c c u r a c y} (b) - \text {c o n f i d e n c e} (b) |, \tag {1}
$$
where, accuracy  $(b)$  and confidence  $(b)$  represent the average accuracy and the average confidence of samples in the  $b^{th}$  bin, respectively.  $S(b)$  is a set of samples in the  $b^{th}$  bin and  $|D|$  is the total number of samples.
Object Detection: Similar to ECE, the Detection Expected Calibration Error (D-ECE) is measured as the expected divergence of precision from confidence, at all confidence levels [22]. It can be expressed as  $\mathbb{E}_{\bar{s}}[|\mathbb{P}(F = 1|\bar{z} = z) - z|]$ . As in Eq.(1), confidence is a continuous variable, therefore we can approximate D-ECE:
$$
\mathrm {D} - \mathrm {E C E} = \sum_ {b = 1} ^ {B} \frac {| S (b) |}{| D |} | \text {p r e c i s i o n} (b) - \text {c o n f i d e n c e} (b) |, \tag {2}
$$
where  $S(b)$  contains a set of object instances in  $b^{th}$  bin. Note that, the D-ECE considers only the precision vs. confidence, and not the mean Average Precision (mAP). The mAP requires the computation of recall for which detections are not present, and so there are no associated confidences.
This calibration definition of object detectors can be extended after including box information.
3.3 Cal-DETR: Uncertainty-Guided Logit Modulation & Mixing
Modern object detection methods occupy a central place in many safety-critical applications, so it is of paramount importance to study their calibration. Recently, vision transformer-based (ViT) object detectors (e.g., Deformable-DETR (D-DETR) [53], UP-DETR [5] and DINO [51]) are gaining importance for both industry practitioners and researchers owing to their state-of-the-art performance and relatively simple detection pipeline. Therefore, we aim to study the calibration of ViT-based object detectors. Towards improving calibration in transformer-based object detectors, we first describe our uncertainty-guided logit modulation approach (Sec. 3.3.1) and then propose a new logit mixing regularization scheme (Sec.3.3.2).
3.3.1 Uncertainty-Guided Logit Modulation
We first briefly review the DETR pipeline [2], which is a popular ViT-based object detection pipeline. Recent transformer-based detectors [5, 53, 51] largely borrow the architecture of DETR pipeline [2]. Then, we describe our new uncertainty quantification approach and how it is leveraged to modulate the logits from the decoder to eventually calibrate confidences.
Revisiting DETR pipeline: DETR [2] was proposed as the first ViT-based object detection pipeline with the motivation of eliminating the need for hand-designed components in existing object detection pipelines. It consists of a transformer encoder-decoder architecture with a set-based Hungarian loss which ensures unique predictions against each ground truth bounding box. Assuming the existence of feature maps  $f \in \mathbb{R}^{H \times W \times C}$  which are generated from ResNet [11] backbone, DETR relies on the standard transformer encoder-decoder architecture to project these feature maps as the features of a set of object queries. A feed-forward neural network (FFN) in a detection head regresses the bounding box coordinates and the classification confidence scores.
Quantifying uncertainty in DETR pipeline: Some recent studies have shown that the overall (output) predictive uncertainty of the model is related to its calibration [23, 19]. So estimating a model's uncertainty could be helpful in aiding the confidence predictions to align with the precision of a detection model. To this end, we propose a new uncertainty quantification approach for DETR pipeline after leveraging the innate core component of transformer architecture i.e. the decoder. It is a simple technique that requires no changes to the existing architecture and yields minimal computational cost, unlike Monte Carlo dropout [8] and Deep Ensembles [23]. Specifically, let  $\mathrm{O_D} \in \mathbb{R}^{L \times M \times Q \times C}$  and  $\mathrm{O_D}^L \in \mathbb{R}^{M \times Q \times C}$  be the outputs of all decoder layers and final decoder layer of the detection transformer, respectively. Where  $L$  is the number of decoder layers,  $M$  is the mini-batch,  $Q$  is the number of queries, and  $C$  represents the number of classes. To quantify the uncertainty for every class logit  $u_c$ , we compute the variance along the  $L$  dimension which consists of multiple outputs of decoder layers. In other words, the variation in the output of decoder layers reflects the uncertainty for a class logit  $u_c$ . Overall, after computing variance along the  $L^{th}$  dimension in  $\mathrm{O_D}$ , we obtain a tensor  $\mathbf{u} \in \mathbb{R}^{M \times Q \times C}$ , which reflects the uncertainty for every class logit across query and minibatch dimensions.
Logit modulation: We convert the uncertainty  $\mathbf{u}$  into certainty using:  $1 - \tanh (\mathbf{u})$ , where  $\tanh$  is used to scale the uncertainty values to [0, 1] from [0, inf], and use this certainty to modulate the logits output from the final layer as:
$$
\tilde {\mathrm {O}} _ {\mathrm {D}} ^ {L} = \mathrm {O} _ {\mathrm {D}} ^ {L} \otimes (1 - \tanh  (\mathbf {u})), \tag {3}
$$
where  $\tilde{\mathrm{O}}_{\mathrm{D}}^{L}$  is the uncertainty-modulated decoder output and then this becomes the input to the loss function.  $1 - \tanh (\mathbf{u})$  indicates that if an output in logit space shows higher uncertainty, it will be down-scaled accordingly. In other words, a higher uncertainty implicitly implies a likely poor calibration of the output logit value (i.e. the pre-confidence score).
3.3.2 Logit Mixing for Confidence Calibration
In addition to uncertainty-guided logit modulation, we develop a new logit mixing approach which acts as a regularizer with the detection-specific losses for further improving the confidence calibration. We propose logit mixing as the mixing in the logit space of an object detector which is inspired by the input space mixup for classification [50]. In a DETR pipeline, this logit space is of dimensions  $Q \times C$ , where  $Q$  is the number of object queries and  $C$  is the number of classes. We only consider positive queries for performing logit mixing as they are responsible for the detection of objects. In
object detection, the instance-level object information is important as we consider the confidence scores associated with the positive class assigned to the bounding boxes.
In our logit mixing scheme, we first build a prototypical representation by computing the mean across all positive queries, which is then used to achieve the mixing for any given positive query. For example, given three positive queries, let  $Q_{p} = \{Q_{1}, Q_{2}, Q_{3}\} \subset Q$  (total number of queries) belonging to the object classes and let  $\tilde{Q}$  be a prototypical representation of all the positive queries, we formulate the expression as:
$$
\hat {Q} _ {i} = \alpha Q _ {i} + (1 - \alpha) \tilde {Q}. \quad \forall Q _ {i} \in Q _ {p} \tag {4}
$$
Now,  $\{\dot{Q}_1,\dot{Q}_2,\dot{Q}_3\}$  are the mixed versions of  $\{Q_1,Q_2,Q_3\}$  in the logit space.  $\alpha$  is the mixing weight for logits. For the selection of  $\alpha$ , more details are in Sec. 4.2.