{'fraction_non_alphanumeric': 0.03290034605546918, 'fraction_numerical': 0.011635488239129344, 'mean_word_length': 4.929229854296759, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Test-time Adaptation with Calibration of Medical Image Classification Nets for Label Distribution Shift

Class distribution plays an important role in learning deep classifiers. When the proportion of each class in the test set differs from the training set, the performance of classification nets usually degrades. Such a label distribution shift problem is common in medical diagnosis since the prevalence of disease vary over location and time. In this paper, we propose the first method to tackle label shift for medical image classification, which effectively adapt the model learned from a single training label distribution to arbitrary unknown test label distribution. Our approach innovates distribution calibration to learn multiple representative classifiers, which are capable of handling different one-dominating-class distributions. When given a test image, the diverse classifiers are dynamically aggregated via the consistency-driven test-time adaptation, to deal with the unknown test label distribution. We validate our method on two important medical image classification tasks including liver fibrosis staging and COVID-19 severity prediction. Our experiments clearly show the decreased model performance under label shift. With our method, model performance significantly improves on all the test datasets with different label shifts for both medical image diagnosis tasks.


Introduction
Intelligent medical image diagnosis has witnessed great success on accurate predictions for various tasks such as disease staging [14,23], lesion diagnosis [9,15], and severity prediction [11,25]. However, real-world use of classification models is challenged by the inevitable shift in class distributions on test data at deployment [1,27,32,33]. Usually, the proportion of samples belonging to each class is associated with patient demographics and region-related prevalence of disease, which differs from one hospital to another. This issue is called label distribution shift, which means that the label distribution can change across training and test datasets. As label distribution plays a vital role in classification tasks [7,16], such shift can make the learned classifier become suboptimal on unseen datasets, thus suffering from performance degradation in testing.
Label distribution shifts are very common in medical diagnosis as the disease distributions vary across location and time. For example, the prevalence of liver diseases significantly differs among regions due to the difference in vaccination coverage [31]. Such label shifts often degrade the performance of a learned classifier on test data, leading to erroneous predictions as observed in prior works [3,4,6]. For example, Davis et al. find that the prediction accuracy of their machine learning models decreases due to the declining incidence of acute kidney injury over time [6]. Since the proportion of normal and disease cases differs between the screening and diagnostic scenarios, an accurate model for screening purpose could perform poorly for diagnosis purpose, even for the same disease [3]. Park et al. [20] show in three disease classification models that dataset shifts including the label shift can lead to unreasonable predictions. Despite being observed in many real applications, the problem of label distribution shift has not yet been tackled for medical image diagnosis, severely hindering the large-scale deployment of deep models in clinical practice.
To generalize model under label shift, if the label distribution of test data can be known, such as the uniform distribution assumption made in [24,30], the label shift can be alleviated by re-sampling training data or adjusting the prediction probability in the softmax loss [22,24] accordingly. In practical scenarios, however, it is unlikely to anticipate the label distribution of test data, which is usually unknown and arbitrary, and may even continuously change. In this regard, we aim to mitigate label shift in a highly practical yet challenging setting, where the test label distribution is unknown and the trained model itself must accommodate label shift by utilizing the test data only. To tackle this problem, we consider two key ingredients. Firstly, since the test label distribution can be arbitrary, it is important to enlarge the capacity of models for an extensive label distribution space. The difficulty lies in how to establish such a representative space during model learning from the training set with a fixed label distribution. Secondly, motivated by the recent test-time learning works [28,29], although the knowledge of test dataset is unknown during model training, it can be explored from the test data at inference time.
In this paper, to our best knowledge, we present the first work to effectively tackle the label distribution shift in medical image classification. Our method learns representative classifiers with distribution calibration, by extending the concept of balanced softmax loss [24,34] to simulate multiple distributions that one class dominates other classes. Compared with [34], our method can be more flexible and be more targeted for ordinal classification, as our one-dominatingclass distributions can represent more diverse label distributions and we use ordinal encoding instead of one-hot encoding to train the model. Then, at model deployment to new test data, we dynamically combine the representative classifiers by adapting their outputs to the label distribution of test data. The test-time  adaptation is driven by a consistency regularization loss to adjust the weights of different classifier. We evaluate our method on two important medical applications of liver fibrosis staging and COVID-19 severity prediction. With our proposed method, the label shift can be largely mitigated with consistent performance improvement.

Problem Formulation of Label Distribution Shift
For disease diagnosis, consider a classification task that aims to train a model to predict the disease class y correctly given an input image x. Letp(x, y) and p(x, y) denote the training and test set distributions respectively. In practice, a deployed model often suffers from label distribution shift, which means the label distribution of training setp(y) is different from that of test setp(y), i.e., p(y) =p(y), but the conditional distributions are consistent, i.e.,p(x|y) =p(x|y). This phenomenon is especially common in medical image classification, where the disease label y is often the causal variable and the image data x can be regarded as the manifestations of a disease [26,32]. According to the Bayesian inferencê p(y|x) =p (x|y)p(y) , the model predictionp(y|x) is strongly coupled with the label distributionp(y), thus the shift inp(y) can cause erroneous prediction ofp(y|x). Regarding this problem, our goal is to adapt a classifier that is learned from the training set to perform well on any unseen test set with label distribution shift.

Learning Diverse Classifiers via Distribution Calibration
Since the test label distribution can be arbitrary, to generalize models under label shift, we consider it is important to enlarge the capacity of classifiers to a broad range of label distributions. However, during training, the model is only presented to a fixed training label distribution thus has limited capacity. Inspired by balanced softmax [24] which calibrates skewed label distribution to be uniform by adding a compensating term to the softmax loss, we propose to learn diverse classifiers via dedicated distribution calibration. As shown in Fig. 1, our insight is to simulate representative one-dominating-class distributions so that the proper combination of learned classifiers can handle arbitrary test label distribution.
Before introducing how to achieve distribution calibration, we first clarify the ordinal encoding in our classification task. To encourage classification network to learn the commonness of all classes and the distinctions between different classes, we use ordinal encoding [18] instead of one-hot encoding for the ordinal classes in our liver fibrosis staging and COVID-19 severity prediction tasks. This ordinal encoding performs multiple binary classifications with sigmoid function and combines the multiple binary outputs by taking the highest class that is predicted as 1 as the final prediction. Furthermore, for distribution calibration in our ordinal regression, we extend the balanced softmax to the sigmoid function and derive the corresponding compensating term. Let p(y i = 1|x) be the desired conditional probability for the expected label distribution, andp(y i = 1|x) be the desired conditional probability of the training set, and assume p(y i = 1|x) is expressed by the standard sigmoid function of the network output φ i in i-th ordinal vector: p(y i = 1|x) = e φ i 1+e φ i , then thep(y i = 1|x) with the same output φ i can be expressed as: where r i and r i are the positive label proportion in the i-th ordinal vector for the expected label distribution p and factual label distribution respectivelyp, and the term log is the compensating term. The proof of Eq.(1) is provided in the supplementary material.
In this way, the calibrated loss function is: Where K denotes the total number of classes. This calibrated loss function enables the model learned on the training label distribution to generate the prediction for the expected label distribution. Moreover, we aim to properly construct different r i to simulate K onedominating-class distributions for K classifiers. Assume the proportion of dominating class j is λ times other classes, then the value of r i can be calculated as: where 1 i≥j is the indicator function. The derivation of Eq.(3) can be found in the supplementary material. Notably, our distribution-calibrated networks use independent parameters only at the last stages and fully-connected layer of networks, while share the parameters at other layers (see the shared network χ θ and the independent networks ψ θ in Fig. 1). This is motivated by the observation that decoupling the representation learning and classification gives more generalizable representations [10]. In this way, we obtain diverse classifiers to handle different label distributions, but adding only minimal computational cost.

Test-time Adaptation for Dynamic Classifier Aggregation
After obtaining diverse distribution-calibrated classifiers during training phase, then at test time, the key is how to aggregate these classifiers to handle the unknown test label distribution with the given inference samples. To build the connection between the obtained classifiers and the test data, we aggregate the outputs of all classifiers with learnable weights, which are dynamically adapted using information implicitly provided by the test data. It's worth to mention that a set of test data, which can reflect the label distribution in the test center, should be accessible simultaneously during this phase.
Specifically, the aggregated output is defined asp agg = K k=1 w kpk , where K k=1 w k = 1 andp k is the output of k-th classifiers with the form of ordinal vector. As different combination of {w 1 , w 2 , ..., w K } can enable the model to deal with different test label distributions, the aim of our test-time adaptation is to find the optimal combination for a given test set. Our assumption is that if the aggregated model has adapted to a particular test label distribution, for the test images generated from such a label distribution, the model should give similar predictions to perturbed versions of the same image. Based on this assumption, we design a consistency regularization mechanism to drive the test-time learning. Given an input x, we generate two augmented views g(x) = v 1 and g (x) = v 2 using the data augmentation approaches, including rotating, flipping, and shifting the images, and adding Gaussian noise to the images. The two views are then forwarded to the trained model f (·) respectively, yielding the ordinal encoded output f (v 1 ) =p agg = w 1 ·p 1 + w 2 ·p 2 + · · · + w K ·p K and f (v 2 ) = p agg = w 1 ·p 1 + w 2 ·p 2 + · · · + w K ·p K . The consistency regularization for the outputs of the two views is imposed with a cosine similarity loss: The loss L test drives the updates of the weights set {w 1 , w 2 , ..., w K } with the implicit knowledge of label distribution on the test set, while the other network parameters of f (·) are frozen. This implicit knowledge is reflected by the consistency that measures whether the aggregated model has adapted to the test label distribution successfully. Each weight of {w 1 , w 2 , ..., w K } is initialized to 1 K and we use softmax function to maintain the sum of them equals to one after each iteration. As a result, the test results can be obtained after the test-time adaptation given the optimized weights set.

Dataset and Experimental Setup
Datasets. We have validated our proposed method on two tasks: 1) liver fibrosis staging with an in-house abdominal CT dataset, and 2) COVID-19 severity prediction with a public chest CT dataset (iCTCF [17]). The liver CT dataset consists of three centers with different label distributions, including 823 cases from our center, 99 cases from external center A and 50 cases from external center B. The ground truths of the liver fibrosis staging come from the pathology results of liver biopsy. The liver fibrosis disease is divided into 5 stages, including no fibrosis (F0), portal fibrosis without septa (F1), portal fibrosis with few septa (F2), numerous septa without cirrhosis (F3) and cirrhosis (F4). Segmentation of the liver is pre-computed with an out-of-the-box tool in a related clinical study, so we adopt it in our paper as the region of interest for classification. The slice thickness of the CT images is 5 mm and the in-plane resolution is 512 × 512. For the COVID-19 dataset, it contains 969 cases from HUST-Union Hospital for training and 370 cases from HUST-Liyuan Hospital for test. The severity of COVID-19 is divided to 6 levels: control (S0), suspected (S1), mild (S2), regular (S3), severe (S4) and critically (S5). The preprocessing and automatic lung segmentation process are the same as a recent work [2] on this dataset. Experimental setting. For liver fibrosis staging, we take 630 cases from our center as the training set, 193 cases from our center as evaluation set and the data from two external centers as two different test sets. For COVID-19 severity prediction, we use the data from HUST-Union Hospital for training and data from HUST-Liyuan Hospital for test. Label distribution statistics of different centers for both datasets are provided in supplementary.
Evaluation metrics. For both tasks, the diagnosis performance is evaluated with accuracy, area under the receiver operating characteristic curve (AUC) and Obuchowski index (OI) [19], as reported in related works [2,5,21]. Considering the AUC is defined for binary classification while ours are multi-class classification tasks, we combine the classes and convert the multi-class classification to several binary classifications. Specifically, we calculate the AUC of F0 vs F1-4, F0-1 vs F2-4, F0-2 vs F3-4 and F0-3 vs F4 for the liver fibrosis staging, and the AUC of S0 vs S1-5, S0-1 vs S2-5, S0-2 vs S3-5, S0-3 vs S4-5 and S0-4 vs S5 for COVID-19 severity prediction. We report the average of all the AUC values as overall performance. The Obuchowski index (OI) is a metric which is proved to have no bias when label distributions are different between training and test sets [12].
Implementation details. Considering model efficiency while still capturing 3D information in CT scans, we use ResNet-50 to get a vector of spatial features and then forward the features of adjacent slices to a LSTM module and a fully connected layer for classification. We train the models using Adam with an initial learning rate of 1e − 5, a weight decay of 1e − 4 and batch size of 4. Our models are implemented using a workstation with four NVIDIA TITAN Xp GPUs.

Experimental Results
Observation of label distribution shift. Label distribution shift and data distribution shift are two types of dataset shift, as introduced in previous work [27]. We first clearly show in the multi-center liver CT datasets that under label distribution shift, the performance of classification model would degrade. Fig. 2 (a) and (b) present that the segmentation performance for region-of-interest liver extraction is consistent between the evaluation set and test sets, while the final classification performance of Obuchowski index largely decreases by 12.9% and 27.5% at test set A and B. It worth to mention that the label distribution of evaluation set is consistent with the training set while the test sets are not. In Fig. 2 (c), we progressively adjust the class distribution of training set to approach the label distribution of test set A, by random sampling a certain proportion of images belonging to each class. We can see that the classification performance increases when the class distribution of the training set becomes closer to the test set. These experiments clearly demonstrate it is indeed the label shift causes the performance drop of classification model in our datasets.
Comparison with state-of-the-art methods. We here compare our method with state-of-the-art approaches for label shift in natural images as strong competitors, including BALMS [24], which calibrates the training label distribution to be uniform, LADE [8], which disentangles the training label distribution from the model prediction, and TADE [34], which also proposes to train multiple networks with different expertise but their networks are less representative than ours. Note that BALMS, LADE, and TADE need to use our derived compensating term in Eq. 1 to be applied in our classification tasks with ordinal regression. We also compare our method with TENT [29], which is a general test-time adaptation approach for domain shift problem, and Focal Loss [13], which can alleviate class imbalance by increasing the focus on hard samples. Table 1 presents the comparison results on the test centers of both liver fibrosis staging and COVID-19 severity prediction. Our TTADC significantly improves the model performance over baseline on all test sets, with 4.6%, 3.6%, Ablation analysis. Comprehensive ablation studies have been conducted with the liver CT dataset to analyze the key ingredients regarding our TTADC. As shown in Fig. 3 (a), adding only the learning distribution-calibrated classifier (LDC) or test-time adaptation (TTA) over baseline is not able to improve over baseline. This is as expected since the two key components are strongly coupled, i.e., the diverse classifiers need to be properly aggregated at test time for the unknown label distribution. In Fig. 3 (b), we manually sample a few images from the training center to construct the test subsets with different one-dominating-class distributions. We can see that given the test subset with k-th class dominating other classes, the best performance comes from the k-th classifier, demonstrating that our proposed distribution calibration successfully generate classifiers that have expertise on different one-dominating-class distributions. Moreover, Fig. 3 (c) compares the model performance trained with different λ in Eq. 3.
The results show that the optimal choice of λ is 2.

Conclusion
We present, to our best knowledge, the first method to generalize deep classifiers to unknown test label distributions for medical image classification. Our methods innovates distribution calibration to learn multiple representative classifiers during training, which are then dynamically aggregated via test-time adaptation to deal with arbitrary label shift. Our method is general and experiments on two important medical diagnosis tasks demonstrate the effectiveness of our method.