Undoing the Damage of Label Shift for Cross-domain Semantic Segmentation

Existing works typically treat cross-domain semantic segmentation (CDSS) as a data distribution mismatch problem and focus on aligning the marginal distribution or conditional distribution. However, the label shift issue is unfortunately overlooked, which actually commonly exists in the CDSS task, and often causes a classifier bias in the learnt model. In this paper, we give an in-depth analysis and show that the damage of label shift can be overcome by aligning the data conditional distribution and correcting the posterior probability. To this end, we propose a novel approach to undo the damage of the label shift problem in CDSS. In implementation, we adopt class-level feature alignment for conditional distribution alignment, as well as two simple yet effective methods to rectify the classifier bias from source to target by remolding the classifier predictions. We conduct extensive experiments on the benchmark datasets of urban scenes, including GTA5 to Cityscapes and SYNTHIA to Cityscapes, where our proposed approach outperforms previous methods by a large margin. For instance, our model equipped with a self-training strategy reaches 59.3% mIoU on GTA5 to Cityscapes, pushing to a new state-of-the-art. The code will be available at https://github.com/manmanjun/Undoing UDA.


Introduction
Semantic segmentation aims to classify every pixel in a given image. As a fundamental visual perception problem, it is the basic module of many visual applications, such as autonomous driving systems. Remarkable progress in semantic segmentation has been made in recent years, driven by large-scale annotated datasets [5,13,53]. However, the massive and high-quality annotation, especially for semantic segmentation, can be costly and labor-intensive. Thus it is not always realistic to collect a sufficient number of well-annotated images for a new environment. Fortunately, * The corresponding author we can obtain synthetic images with accurate pixel-level annotations rendered from the computer by a physical engine [36,38]. However, the model trained with a synthetic domain often suffers from performance degradation since synthetic images (source domain) and testing images (target domain) are drawn from different distributions. This phenomenon commonly exists in cross-domain semantic segmentation (CDSS) tasks, and many unsupervised domain adaptation models have been proposed to address this issue by transferring the knowledge from a label-rich source domain to the unlabeled target domain.
Most existing CDSS methods [11,18,29,34,42,44] seek to learn domain-invariant representations via adversarial training to align marginal distributions (p(x)) or conditional distributions (p(x|y)). However, they ignore the label shift problem, which commonly exists in CDSS tasks, since the label distribution is often different across domains. As shown in Fig. 1, taking the GTA5 [36] to Cityscapes [13] as an example, the frequency of "truck" and "wall" in the source domain is higher than that in the target domain, and the frequency of "bicycle" is much lower than that in the target domain.
Therefore, in this work, we propose a novel approach to undo the damage of label shift in CDSS. First, we give an in-depth analysis and show that the classifier bias is the critical factor leading to the poor generalization ability of the learnt semantic segmentation model on the target domain when a label shift problem exists. It is inevitable that the classifier will be biased towards the source domain because the supervision signal comes only from it. At the same time, we show that the damage of label shift can be overcome by aligning the data conditional distribution and correcting the posterior probability. Then we adopt class-level feature alignment for conditional distribution alignment and propose two simple yet effective methods to rectify the classifier bias from source to target by remolding the classifier predictions. In particular, after aligning the conditional distribution, we adjust the predictions of the classifier either in the training stage or the inference stage using the source and target label distribution.  There is an obvious label shift problem between the two datasets. For example, the frequency of "rider" in GTA5 is much less than that in Cityscapes while that of "wall" is opposite (Best viewed in color).
To achieve this, we design a new method to estimate the target label distribution using model predictions and the source pixel distribution prior. In this way, we can effectively address label shift, thus improving the model generalization ability on the target domain. We conduct extensive experiments on the benchmark datasets of urban scenes, including GTA5 to Cityscapes and SYNTHIA to Cityscapes. By simply classifier rectification after aligning conditional distribution, our approach is remarkably effective. For example, for the GTA5 to Cityscapes task, it can reach 49.0% mIoU, which surpasses all the adversarial based approaches. And our model can achieve 59.3% mIoU after employing self-training. This validates the effectiveness of our proposed method.
In a nutshell, our contributions can be summarized as follows: • We propose to address the label shift issue for CDSS tasks in a more realistic scenario (i.e., the conditional distributions are different across domains) and reveal that the classifier bias is the critical factor leading to poor generalization on the target domain.
• We propose two simple yet effective methods to rectify the classifier bias from source to target by remolding the classifier predictions after explicitly aligning the conditional distribution.
• We conduct extensive experiments on the benchmark datasets of urban scenes. The experimental results show that our approach outperforms other existing works, reaching a new state-of-the-art, validating our in-depth analysis on label shift.

Semantic Segmentation
Semantic segmentation is a pixel-level prediction that can be used in a large number of visual applications, such as autonomous driving and security. Long [27] proposed the Fully Convolutional Network (FCN), after which researchers started to focus on how to design the backbones for semantic segmentation, and proposed many excellent solutions such as UNet [37], SegNet [1], HRnet [45], and SegFormer [46]. Since the classifier plays an important role in improving the segmentation accuracy in semantic segmentation, many works delve into the mechanism of classifiers, including DeepLab [8], PSPnet [50] and OCRnet [48]. However, although these approaches achieve impressive performance for semantic segmentation, they still require a large-scale well-annotated dataset, which is not easy to collect in real-world applications.

Unsupervised Domain Adaptation
Unsupervised Domain Adaptation (UDA) methods aim to transfer the knowledge from a label-rich domain to an unlabelled domain and have been explored in many computer vision tasks, e.g., classification [28,39], object detection [10,15], and semantic segmentation [23,42]. Most UDA methods are based on discrepancies between the source domain and target domain (e.g., H-divergence [2] or A-distance [3]). Among them, adversarial training is the most widely adopted strategy [12,20,28,39]. This methodology usually focuses on aligning the marginal distribution between the source domain and target domain under the assumption of covariate shift. Recently, researchers have found that this alignment strategy may lead to the phenomenon of misalignment [7,24]. Therefore, some methods attempt to address the conditional shift between source and target domain by adversarial training [30,40].

Cross-domain Semantic Segmentation
Exploiting synthetic data to help the real-world data has been a popular research topic in unsupervised domain adaptation. To address the cross-domain semantic segmentation problem, [23,42,43] use the domain adversarial learning with the structure information to align the marginal distribution. However, to eliminate the phenomenon of misalignment, many methods [18,29,34,44] have been proposed to conduct the conditional distribution alignment to learn class-level domain-invariant representation.
However, the label shift problem between the source and target domain is still a long-standing but under-explored problem. Some approaches [6,19] try to address this problem, but they typically assume that the conditional distribution is the same across domains, which is not realistic in practice. In a different approach than other studies, we propose to address the conditional shift and label shift simultaneously. Currently, the most related work is CLS [25], which proposes an algorithm to align the marginal distribution by adjusting the label distribution as an alternative to conditional distribution alignment. Different from it, we propose to conduct a two-stage learning paradigm by aligning conditional distribution and then considering label shift, which gives results in a better performance. Detailed analysis can be found in Section 3.5.

Methodology
In cross-domain semantic segmentation (CDSS), we are given a labeled source domain s is an image and y i s is the corresponding pixel-level annotation, as well as an unlabeled target t is a target image without annotation. Although the target domain is unlabeled, we generally assume the source and target domains share the same label space. For simplicity, we ignore the image size H and W in following loss function. The task of CDSS is to learn a semantic segmentation model G which performs well on the target domain.
Generally, a segmentation network can be represented as G = C • F , where F is the feature extractor and C is the classifier. Existing methods [11,23,42] mainly treated CDSS as a data distribution mismatch problem, and focused on training F to align source and target domains by aligning either the marginal distribution [11,42] or the conditional distribution [18,29,34,44].
However, data distribution mismatch is not the only problem of CDSS. There is also often a label distribution discrepancy between different domains. For example, the frequency of cars and buildings in the country is much lower than that in the urban scene, and the frequency of plants is higher in the country than it is in the city. More examples can be observed from Fig. 1. Such a label distribution discrepancy will cause a classifier discrepancy between the source and target domains, and even though their data distributions are well aligned. In other words, we need to pay attention to the learnt classifier C of the CDSS models to reduce such classifier bias caused by label distribution discrepancy.
To this end, we propose a novel approach to simultaneously handle the data distribution discrepancy and the label distribution discrepancy shown in Fig. 2, in which we exploit the conditional distribution alignment strategy and design two label distribution adjustment strategies for this purpose. In the following, we first present a rigorous analysis on the classifier bias and label distribution discrepancy in Section 3.1, then follow with the conditional distribu-tion alignment strategy in Section 3.2 and label distribution adjustment strategies in Section 3.3. Additional details and discussions are presented in Section 3.4 and 3.5.

Motivation
Given a segmentation model G = C • F , the classifier C is expected to output the posterior probability in the ideal case, i.e., G(x) = C(F (x)) = p(Y |F (x)) where x is an image, and Y is a random variable representing the semantic label. From Bayes' theorem, we have: where p(F (x)) is a constant, and x is a sampled image. We can observe that the classifier is affected by the conditional distribution P (F (x)|Y ) and the label distribution P (Y ).
Correspondingly, for a segmentation model G s = C s • F s trained on the source data, the output of C s meets the following relationship: Similarly, for the target domain, an ideal segmentation In CDSS, our goal is to adapt a model trained with source supervision to the target domain. Without loss of generality, we assume the feature extractor is identical for two domains, and unify them as F = F s = F t . Then, we further assume that the conditional data distribution P (X|Y ) is well aligned, i.e., we have Using Eq. (2) and Eq. (3), the relation of C s and C t can be derived as: We can observe from Eq. (4) that the label distribution discrepancy between the source and target domains would cause a classifier bias issue. It also points out a way to rectify the source classifier to approach the ideal target classifier. We detail the solution in the following subsections.

Conditional Distribution Alignment
As shown above, a presumption for rectifying the classifier bias using Eq. (4) is the data conditional distribution is well aligned. Next, we discuss how to perform conditional distribution alignment in CDSS.
Adversarial training is a common strategy to align two distributions in CDSS, where a domain discriminator D is applied to learn domain-invariant features. The discriminator D adapts the features and tries to identify the domain label for the inputted images while the feature extractor F attempts to extract domain-invariant features. This is achieved by a Gradient Reversal Layer (GRL) between the feature extractor F and the discriminator D or by employing an alternative optimization in a min-max manner.
For conditional distributional alignment, a few solutions [18,29,34,44] were also proposed. Generally, they are implemented by integrating the label information into the domain discriminator D. For example, FADA [44] proposed to extend the domain discriminator to output both the domain label and the class label. Specifically, they use a discriminator with 2K-dimensional output, representing the class conditional domain probability. Formally, the optimization process of the adversarial learning process can be written as: min where L seg is the cross-entropy loss for source domain, L D is designed to train the discriminator D, L adv is used to make F extract the conditional domain invariant feature. They are defined as follows: where a ik s and a jk t are the k-th class knowledge for the different domains and d is the domain label, 0 represents the source domain and 1 represents the target domain. Please refer to [44] for more details. In this way, the feature extractor will obtain conditional domain-invariant features after training the model using the conditional distribution alignment strategy. We simply employ FADA for conditional data distribution alignment and focus on validating the importance of classifier bias correction.

Classifier Rectification
After aligning the conditional distribution P (X|Y ), we are able to use Eq. (4) to rectify the source classifier to be the target one, i.e., through adjusting the predictions of C s by the ratio P t (Y )/P s (Y ). We propose two strategies, classifier refinement and inference adjustment, for this purpose. Classifier Refinement (CR) We consider how to train a target classifier using the source supervision in CDSS. In particular, supposing we have a classifier C t , according to Eq. (4), we have, This implies that when training the segmentation model with source supervision, if we remold the predictions of the classifier using the above formulation, we are able to obtain a target classifier that can be directly used for predicting target domain samples.
Specifically, after performing the conditional distribution alignment, we first fix the backbone such that the conditional distribution of features remains unchanged. Then we continually train the classifier using labeled source sample by minimizing the semantic segmentation loss as follows: Inference Adjustment (IA): An alternative idea is that we directly adjust the output of the existing network G cda in the inference stage on the target domain. Since the conditional probability distribution is now aligned, the hypothesis of label shift is satisfied. So according to the Eq. (4), we adjust the output p i t of the existing network G cda in the inference stage of the target domain data x i to obtain the final prediction y IA . Formally, we adjust the predictions of the classifier as follows: In CDSS, both CR and IA are from the perspective of the classifier to correct the problem caused by domain shift. CR makes the classifier biased towards the target domain at the training stage, while IA aims to directly change the output of the model for the target domain at the inference stage. Both strategies are essentially the same in theory, and one can choose either way in combination with the data distribution alignment.
Moreover, it is popular in existing state-of-the-art CDSS methods [32,49,52] to use the domain alignment model as a warm-up model for generating pseudo-labels and then perform self-training. As our proposed strategies are able to improve the performance of adversarial based CDSS models, the quality of pseudo-labels in these self-training methods will also be improved, thus further boosting the current state-of-the-art CDSS methods.

Label Distribution Estimation
Recall that when correcting the classifier using the two strategies in Section 3.3, the prior label distribution P s (Y ) and P t (Y ) are required. We discuss how to estimate them below.
For the source domain, as the labels are available, we directly obtain the label distribution estimation P s (Ŷ ) by counting the image-level class labels from the ground truth label throughout the whole dataset. Specifically, we denote the count of the image-level label of i-th source image as I i s (k). We can obtain I i s (k) as follows: where n s means that we ignore the labels that only exist very few pixels in an image and 1[·] is an indicator function that equals 1 when [·] is true, otherwise 0. The image-level source label distribution can be estimated for the whole dataset as follows: For the target domain without ground truth, we use the conditional alignment model G cda to estimate the imagelevel category probabilities p i (Y ) by the smooth max pooling strategy [31,34,35]. Then, we compare it with the pixel ratio p pix (Y ) of each category in the source domain dataset.
For the class k, if p i (k) is greater than p pix (k), it is deemed that the category appears in the image, otherwise I i t (k) is 0. After dataset normalization, we can obtain the label distribution estimation P t (Ŷ ) of the target domain. The details are as follows:

Discussion
In a recent study, CLS [25] was proposed to address the label shift issue in the unsupervised domain adaptation task. While they also validate their approach for CDSS in experiments, the performance is less satisfactory compared to that of ours (see details in Section 4). We analyze the potential reasons as follows.
In CLS, they showed that conditional distribution alignment can be achieved by merging the class classifier and the domain classifier together and re-weight the class classifier loss with the label distribution ratio. After that, a posterior alignment is performed to correct the trained class classifier.
We clarify the difference between CLS and ours to gain a better understanding of the label shift problem for CDSS. In particular, given a source image x i s with label k, the modified classifier loss can be written as: For convenience, we rewrite the loss of classifier refinement in Eq. (12) as follows: It can be observed that the difference is that CLS applies the label distribution ratio as the loss weight while we directly adjust the posterior probability output from the classifier. As stated in [4,33], while re-weigting the loss with the label distribution ratio also helps to guide the classifier to approach the target classifier, yet not as effectively as directly using it to correct the classifier output like our classifier refinement loss.
Another important difference is that CLS utilizes the label distribution ratio in both the training and inference phases. As discussed in Section 3.3, the label distribution ratio is able to recover the target classifier in either phase if it is correctly applied. Using it in both phases is not desirable from the probabilistic perspective. We conjecture that due to the loss weighting in the training phase is less effective, they might hope to compensate for it by applying the label distribution ratio again at the inference phase.
On the contrary, in our approach, we clearly disentangle the conditional distribution alignment and label shift correction issues. By applying the label distribution ratio in either the classifier refinement or the inference adjustment, we achieve satisfactory improvement on segmentation performance.

Datasets
We conduct experiments on two common CDSS benchmarks: GTA5 to Cityscapes and SYNTHIA to Cityscapes. They are both synthetic-to-real scenarios. GTA5 and SYN-THIA are synthetic datasets used as source domains and Cityscapes is a real dataset as the target domain.
• Cityscapes [13] is a real-world urban scene semantic segmentation benchmark dataset. Following [23,42,43], we use the 2,975 images from its training set without the annotation as the unlabeled target samples and evaluate our method on its validation set which includes 500 images.
• GTA5 [36] is a synthetic dataset automatically rendered from a computer game named Grand Theft Auto V (GTA5). We use all of its 24,966 images as the source domain, which consists of 19 classes in common with Cityscapes.
• SYNTHIA [38] is also a popular synthetic semantic segmentation benchmark dataset. We use its subset SYNTHIA-RAND-CITYSCAPES as the source domain, which contains 9, 400 images and 16 classes in common with Cityscapes.

Implementation Details
In our experiments, following the methods of previous studies [18, 21, 29, 32, 41-44, 47, 49, 51, 52, 54, 55], we take the ResNet-101 [22] model pretrained on Ima-geNet [14] as the backbone. For a fair comparison, we conduct experiments with the original Atrous Spatial Pyramid Pooling (ASPP) in Deeplab-v2 [9] and the modified ASPP [51,52] as the segmentation classifier. When performing the conditional adversarial learning, we choose FADA [44] as our baseline. For the original ASPP, we use checkpoints provided by FADA. For methods that use the modified ASPP, we reproduce experiments for FADA as a new baseline. In our experiments, we use the Stochastic Gradient Descent (SGD) optimizer with the momentum is set to 0.9 and the weight decay is set to 10 −4 . We employ the polynomial decay with power of 0.9 with the initial learning rate of 2.5 × 10 −4 . At the self-training stage, we follow the corresponding training strategy of the pseudo label methods [32,49]. We use Intersection over Union (IoU) as the evaluation metric and report per-class IoU and mean IoU over all classes. We conduct all the experiments on Tesla V100 GPUs with a PyTorch implementation.

Analysis
Comparisons with state-of-the-art methods We compare our proposed methods with previous state-of-the-art CDSS approaches on the GTA5 to Cityscapes and SYN-THIA to Cityscapes in Table 1 and Table 2, respectively. The CDSS methods can be divided into three categories: 1) domain alignment methods including AdaptSegNet [42], AdvEnt [43], CLAN [29], and FADA [44], 2) self-training methods including CRST [55], R-MRNet [52], IAST [32], and ProDA [49], 3) the data argument methods including FDA [47], DACS [41]. All the models are trained using ResNet-101 as the backbone and original ASPP as the classifier except that ProDA [49] and R-MRNet [52], which use the modified ASPP as the classifier. Thus, we reproduce FADA [44] based on the modified ASPP when compared with ProDA and R-MRNet. It is noted that our work primarily addresses the label shift after the conditional distribution alignment. Thus, it is clear that our method obtains comparable or greater results compared with domain alignment methods [29,[42][43][44]. Moreover, equipped with different pseudo label strategies, our method can improve their result and even achieve new state-of-the-art results.
Specifically, in the scenario of GTA5 to Cityscapes, the proposed inference adjustment (IA) and classifier refinement (CR) achieve 48.8% mIoU and 49.0% mIoU, respectively. They outperform all the previous domain alignment methods by a notable margin. Our work can improve the performance for those categories with large different densities between two domains, e.g., "truck" and "bike". Combined with the self-training methods, e.g. SD [44], IAST [32] and ProDA [49], we achieve 52.7%, 55.5% and 59.3% mIoU, reaching new state-of-the-art results, respectively. Since the CR and IA are essentially same, we only use CR in combination with self-training methods. CLS [25] reports the result based on IAST, and the experimental results clearly show that our work can offer a mIoU gain by 2.5% with the same pseudo label method (i.e. IAST). The qualitative examples of the segmentation results of our methods are presented in Fig. 3, and we can observe that our method can predict a more accurate segmentation map. The similar results for SYNTHIA to Cityscapes can also be observed in Table 2. For this task, we report the mIoU on 16 classes and 13 classes (excluding "wall", Table 1. Results (in %) of adapting GTA5 to Cityscapes. All the results are obtained from the ResNet-101-based models. Except ProDA [49] and R-MRNet [52] using the modified ASPP as the classifier, other methods use the original ASPP.  "fence", "pole"). Combined with the self-training methods, we can achieve 56.7% and 64.5% mIoU over 16 and 13 categories respectively, which shows that our work can outperform the existing methods on both datasets by a notable margin. This again validates the effectiveness of our method that undoes the damage of label shift for CDSS.
Combining with self-training Since the self-training methods [32,44,49] generally use domain alignment meth-ods as their warm-up model to produce pseudo labels, our model can be easily plugged into them. We only show the result of classifier refinement in this part for conciseness. As shown in Table 3, we first combine the self distillation (SD) [44] with our methods, and we can increase our baseline 49.2% mIoU to 52.7% mIoU. By combining our method with advanced self-training methods [ Figure 3. Qualitative segmentation results on GTA5 to Cityscapes. We present (a) Target Image, (b) Ground Truth, (c) Source Only, (d) Baseline [44], (e) Ours. Classifier agnostic The baseline method FADA [44] builds upon the original ASPP, and we reproduce the modified ASPP version for comparisons. As shown in Table 4, the proposed method can improve the baseline by 1.1% ∼ 2.1% mIoU in two different classifiers. Considering various semantic segmentation classifiers, we believe our methods can be applied to any other type of classifier.

Conclusion
In this paper, we tackle the label shift problem for the CDSS task. We show in depth that label shift often causes the classifier bias problem in a learnt model, which, however, can be effectively avoided by aligning the data con-