SlowGuess/NeurIPS_2023_OCR huggingface dataset:
where  $\lambda_1, \lambda_2$  and  $\lambda_3$  are hyper-parameters to balance each loss. During the warm-up process, the model is trained only with labeled data, i.e.,  $\lambda_1 = \lambda_2 = \lambda_3 = 0$ . The whole process is summarized in Algorithm 1 (the framework figure can be found in Appendix A of the supplementary material), where the domain index is omitted for brevity.
Algorithm 1 Class-Wise Adaptive Exploration and Exploitation (CWAEE)
Input: Labeled dataset  $\mathcal{D}^l$ , unlabeled dataset  $\mathcal{D}^u$ , validation dataset  $\mathcal{D}^v$ , training epoch  $T$ , number of known classes  $|\mathcal{C}^l|$ .  
Output: Feature extractor  $g_{\theta}$ , linear classifiers  $h_{\omega}^{1:|\mathcal{C}^l|}$ .  
1: Initialize  $g_{\theta}, h_{\omega}^{1:|\mathcal{C}^l|}$ , score queues  $q^{1:|\mathcal{C}^l|}$ , temperatures  $\tau^{1:|\mathcal{C}^l|}$ , thresholds  $\delta_{knw}^{1:|\mathcal{C}^l|}, \delta_{unk}^{1:|\mathcal{C}^l|}$ ;  
2: for  $t = 1$  to  $T$  do  
3: for  $i = 1$  to max_iteration do  
4: Draw a batch of labeled samples  $B^l$  and unlabeled samples  $B^u$  from  $\mathcal{D}^l$  and  $\mathcal{D}^u$ ;  
5: Calculate loss  $\mathcal{L}^l$  on  $B^l$  with Eq.2;  
6: Predict on  $B^u$  with  $\tau^{1:|\mathcal{C}^l|}$  to get scaled confidence scores  $\tilde{P}^u$ ;  
7: Split  $B^u$  into known classes  $B_{knw}^u$ , unknown classes  $B_{unk}^u$  and null  $B_{null}^u$  with  $\tilde{P}^u$  and  $\delta_{knw}^{1:|\mathcal{C}^l|}, \delta_{unk}^{1:|\mathcal{C}^l|}$ ;  
8: Calculate loss  $\mathcal{L}_{knw}^u$  and  $\mathcal{L}_{unk}^u$  on  $B_{knw}^u$  and  $B_{unk}^u$  with Eqs.3 and 4 respectively;  
9: Conduct data augmentation within  $B^u$  to get  $\tilde{B}^u$  with Eqs.5-7;  
10: Calculate loss  $\mathcal{L}_{con}^u$  on  $B^u$  and  $\tilde{B}^u$  with Eq.8 and get total loss  $\mathcal{L}$  with Eq.9;  
11: Backward loss  $\mathcal{L}$  and update  $g_{\theta}, h_{\omega}^{1:|\mathcal{C}^l|}$  with SGD;  
12: Update  $q^{1:|\mathcal{C}^l|}$  with  $\tilde{P}^u$ ;  
13: Update  $\tau^{1:|\mathcal{C}^l|}, \delta_{knw}^{1:|\mathcal{C}^l|}$  and  $\delta_{unk}^{1:|\mathcal{C}^l|}$  with Procedure 1.  
14: end for  
15: end for
We use PACS [45], Office-Home [46] and miniDomainNet [47] datasets in the experiments. PACS [45] consists of four domains corresponding to four different image styles, including Photo (P), Art painting (A), Cartoon (C) and Sketch (S), and the four domains have the same label set of 7 classes, and contain 9,991 images in total; OfficeHome [46] consists of images from four different domains: Artistic (A), Clipart (C), Product (P) and Real-World (R), and has a large domain gap and around 15,500 images of 65 classes; miniDomainNet [47] is a subset of DomainNet [48] and has four domains including 18,703 images of Clipart (C), 31,202 images of Painting (P), 65,609 images of Real (R) and 24,492 images of Sketch (S), and it has 126 classes and maintains the complexity of the original DomainNet.
We adopt the common leave-one-domain-out protocol [12, 16]: three domains are used as the source domains and the remaining one as the target domain. Similar to [23, 38] we split the classes into known and unknown classes, specifically we split the original label set into 3:2:2, 25:20:20 and 42:42:42 (known classes, seen unknown classes and unseen unknown classes) in PACS [45], OfficeHome [46] and miniDomainNet [47] respectively in alphabetical order of the class name. On each source domain, 10 labeled samples of each known class are randomly sampled to construct the labeled data, and the remaining samples of known classes and seen unknown classes construct the unlabeled data. All samples on the target domain are used for evaluation.
4.2 Compared Methods
We compare our method with various DG methods, OOD detection methods and SSL methods. DeepAll naively puts labeled data from all source domains together, and trains the model with Empirical Risk Minimization (ERM); DAML [23] conducts meta-learning over augmented domains to learn open-domain generalizable representations for OpenDG problem; UDG [32] adopts two classification heads to dynamically separate ID and OOD samples in the unlabeled data and optimizes the model with different learning objectives; FixMatch [37] generates highly confident pseudo-labels on weakly-augmented unlabeled samples, and trains the model to predict the pseudo-labels for the strongly-augmented version of the same samples; OpenMatch[38] extends FixMatch with OVANet [39] and open-set soft-consistency regularization to detect outliers in unlabeled data for OSSL;
Table 1: Leave-one-domain-out results of known classes accuracy (left of the slash) and unknown classes AUROC (right of the slash) on PACS, OfficeHome and miniDomainNet.
Target Domain	PACS
Art	Cartoon	Photo	Sketch	Average
DeepAll	62.96 / 60.06	53.41 / 58.15	79.17 / 71.26	48.60 / 50.16	61.03 / 59.91
UDG [32]	42.98 / 49.83	46.92 / 48.52	58.75 / 57.28	38.82 / 45.21	46.87 / 50.21
DAML [23]	42.07 / 50.27	57.74 / 54.80	42.87 / 54.00	45.29 / 47.20	46.99 / 51.57
FixMatch [37]	81.32 / 68.67	61.85 / 56.34	85.63 / 64.87	76.39 / 48.01	76.30 / 59.47
OpenMatch [38]	83.28 / 68.97	75.39 / 66.60	91.45 / 68.37	58.05 / 47.42	77.04 / 62.84
StyleMatch [16]	82.66 / 63.35	71.95 / 56.86	90.81 / 67.40	77.34 / 43.33	80.69 / 57.73
CWAEE	87.08 / 81.21	76.65 / 72.88	93.19 / 80.30	79.87 / 82.46	84.20 / 79.21
OfficeHome
Target Domain	Art	Clipart	Product	Real-World	Average
DeepAll	61.95 / 69.97	50.80 / 60.96	75.23 / 71.38	84.55 / 76.63	68.13 / 69.73
UDG [32]	52.25 / 60.71	41.97 / 55.58	63.64 / 64.74	72.24 / 65.90	57.52 / 61.73
DAML [23]	45.73 / 62.96	43.98 / 55.46	58.50 / 67.09	64.46 / 67.75	53.17 / 63.31
FixMatch [37]	65.25 / 67.60	59.32 / 62.18	73.31 / 67.72	82.35 / 73.16	70.06 / 67.67
OpenMatch [38]	64.95 / 69.27	55.82 / 61.60	75.20 / 72.93	81.76 / 75.71	69.43 / 69.90
StyleMatch [16]	67.83 / 67.40	63.02 / 60.15	75.46 / 69.16	84.79 / 74.44	72.77 / 67.79
CWAEE	70.55 / 75.85	64.00 / 66.57	76.22 / 76.56	86.60 / 81.82	74.34 / 75.20
miniDomainNet
Target Domain	Clipart	Painting	Real	Sketch	Average
DeepAll	52.58 / 66.31	52.13 / 62.96	66.10 / 73.17	44.15 / 64.90	53.74 / 66.83
UDG [32]	56.30 / 68.49	49.51 / 61.47	61.70 / 70.21	36.99 / 57.25	51.12 / 64.36
DAML [23]	56.16 / 67.16	50.32 / 65.62	57.23 / 69.14	46.52 / 65.15	52.55 / 66.77
FixMatch [37]	59.71 / 62.83	59.71 / 62.37	65.63 / 63.58	64.78 / 64.90	62.01 / 63.42
OpenMatch [38]	64.53 / 72.70	61.55 / 69.80	70.61 / 74.87	61.40 / 71.30	64.52 / 72.17
StyleMatch [16]	62.42 / 63.63	61.23 / 62.21	66.02 / 62.58	65.44 / 63.46	63.77 / 62.97
CWAEE	66.68 / 73.38	65.65 / 73.07	69.86 / 75.98	66.36 / 74.96	67.14 / 74.35
StyleMatch [16] extends FixMatch with stochastic classifier and multi-view consistency to improve the quality of pseudo-labels and generalization of model respectively for SSDG.
4.3 Setup
Most hyper-parameters follow the setting in [16] for a fair comparison. The ImageNet-pretrained ResNet18 [2] is used as the CNN backbone, and the linear classifiers is implemented with stochastic classifiers. The initial learning rate of SGD optimizer is set to 0.003 for the pretrained backbone and 0.01 for the randomly initialized stochastic classifier, both decaying following the cosine annealing rule. The running epochs are 40, 20 and 20 for PACS, OfficeHome and miniDomainNet respectively. For each mini-batch, we
Table 2: Leave-one-domain-out average AUROC of seen (left of the slash) and unseen unknown classes (right of the slash) on PACS, OfficeHome and miniDomainNet.
Dataset	PACS	OfficeHome	miniDomainNet
DeepAll	58.28 / 60.89	68.48 / 71.04	69.21 / 67.89
UDG	50.73 / 49.08	63.35 / 60.07	64.26 / 64.33
DAML	50.45 / 52.94	62.21 / 64.46	67.20 / 66.36
FixMatch	53.84 / 67.87	64.05 / 71.39	58.56 / 67.45
OpenMatch	55.61 / 73.73	68.12 / 71.72	72.86 / 71.55
StyleMatch	49.77 / 68.91	63.46 / 72.27	56.57 / 68.31
CWAEE	84.09 / 74.53	74.57 / 75.87	76.81 / 72.31
randomly sample 16 labeled samples and 16 unlabeled samples from each source domain. We set  $\lambda_{1} = 1.0$  on PACS,  $\lambda_{1} = 0.4$  on OfficeHome and  $\lambda_{1} = 0.1$  on miniDomainNet. We set  $\lambda_{2} = 0.4$  and  $\lambda_{3} = 1.0$  for all three datasets. We evaluate the accuracy on known classes and AUROC on unknown classes of the methods with 3 different random seeds, and report the average results. More implementation details can be found in Appendix B of the supplementary material.
The results are summarized in Table 1 (the results with standard deviations can be found in Tables 6 and 7 in Appendix C of the supplementary material). From the results, it can be found that our method consistently outperforms the compared methods on all datasets. Although the UDG and DAML methods are developed for unknown class detection on the unseen target domain, their performance is much worse than ours. FixMatch has poor classification performance when evaluated on an unseen target domain, due to not considering the domain shifts problem. The performance of our method is much better than that of OpenMatch in most cases since it depends on the fixed thresholds to detect unknown classes in unlabeled data. StyleMatch has a worse performance than ours since it treats all unlabeled samples as one of the known classes. In order to evaluate the detection performance of methods on seen and unseen unknown classes respectively, we also report the AUROC of seen and unseen unknown classes on the target domain in Table 2 (the results with standard deviations can be found in Table 8 in Appendix C of the supplementary material). It can be found that our method has better detection performance on both seen and unseen unknown classes.
In order to provide additional insight into what makes our method successful, we conduct ablation experiments on OfficeHome.
4.5.1 Effectiveness of Used Modules
Our method contains several modules, including supervised loss (Eq.2), unsupervised loss on known classes (Eq.3), unsupervised loss on unknown classes (Eq.4), class-wise adaptive thresholds (Procedure 1) and consistency regularization loss (Eq.8). The results with standard deviations are presented in Table 3. With the supervised loss, our
Table 3: Ablation study on the used modules.
Ablation	Accuracy / AUROC
Supervised loss	68.13±0.88 / 69.73±1.01
+ Unsupervised loss on known classes with fixed thresholds	72.30±0.18 / 64.47±0.60
+ Unsupervised loss on unknown classes with fixed thresholds	72.38±0.16 / 65.16±0.43
+ Class-wise adaptive thresholds	73.70±0.39 / 74.44±0.92
+ Consistency regularization loss	74.34±0.35 / 75.20±0.74
method trains the model only with labeled samples. After detecting known and unknown classes with the fixed thresholds (i.e.,  $\delta_{knw}^{1:|\mathcal{C}^l|} = \delta_{unk}^{1:|\mathcal{C}^l|} = 0.5$ ) and trained with the unsupervised loss on known and unknown classes, the performance can be improved with these detected samples. By replacing the fixed thresholds with class-wise adaptive thresholds, the performance can be further boosted. With all the used modules, we can get the best performance.
To better show the effectiveness of the class-wise adaptive thresholds, we compare the quality of pseudo-labels when trained with fixed thresholds and the class-wise adaptive thresholds. The results are depicted in Figure 2. It can be found that the accuracy of pseudo-labels assigned by our method is higher than that of compared method on known classes and unknown classes in all 4 domains, which means that our method has better performance on known classes classification and unknown classes detection.
(a) Known classes
(b) Unknown classes
4.5.2 Sensitivity to Number of Seen Unknown Classes
In order to study the sensitivity of our method to the number of seen unknown classes, we compare our method with existing SSL methods under different numbers of seen unknown classes  $(|\mathcal{C}^u\backslash \mathcal{C}^l |$  varies from 0 to 30), and the results are depicted in Figure 3. It can be found that our
Figure 2: The accuracy of pseudo-labels on unlabeled samples.
(a) Accuracy
Figure 3: The results with various numbers of seen unknown classes.
(b) AUROC
method outperforms the compared methods. With the increase in the number of seen unknown classes, the AUROC on unknown classes obtained by our method increases while the accuracy on known classes is maintained.
4.5.3 Sensitivity to Number of Labeled Samples
In order to study the sensitivity of our method to the number of labeled samples, we compare our method with existing methods under different numbers of labeled samples (5, 10 and 20 labels of each known class on each domain) and the results are summarized in Table 4 (the results with standard deviations can be found in Table 9 in Appendix C of the supplementary material). It can be found that our method outperforms compared methods under different numbers of labeled samples. The performance of
Table 4: Average accuracy / AUROC on OfficeHome with different numbers of labeled samples.
# Labels	5	10	20
DeepAll	62.87 / 67.77	68.13 / 69.73	72.17 / 70.83
UDG	47.02 / 55.68	57.52 / 61.73	65.31 / 67.66
DAML	52.67 / 64.15	53.17 / 63.31	50.95 / 63.07
FixMatch	66.90 / 64.29	70.06 / 67.67	71.92 / 70.21
OpenMatch	66.24 / 69.81	69.43 / 69.90	70.14 / 69.81
StyleMatch	70.16 / 63.57	72.77 / 67.79	75.36 / 70.45
CWAEE	71.24 / 73.23	74.34 / 75.20	76.09 / 73.96
our method under extremely few labels (i.e., 5 labels of each class on each domain) is promising, since it can exploit known and unknown classes in unlabeled data in a more reasonable way.
4.5.4 Histograms of Confidence Scores
The empirical probability density function of confidence scores of unlabeled data in different queues and different epochs are depicted in Figure 4. Here, we report class 0 and class 1 in different epochs as examples, i.e., Figure 4a vs 4b, Figure 4c vs 4d, which shows that the confidence scores have different distributions in the different training epochs. It can be found that our method CWAEE can find the adaptive thresholds for known and unknown classes.
(a) Class 0 in epoch 1
Figure 4: The empirical p.d.f. of the confidence scores of unlabeled data.
(c) Class 1 in epoch 1
(b) Class 0 in epoch 20
(d) Class 1 in epoch 20
In this paper, we focus on a more realistic semi-supervised domain generalization scenario, where known classes and seen unknown classes are mixed in unlabeled training data while known classes, seen unknown classes and unseen unknown classes are mixed in testing data. In order to utilize unlabeled training data, we detect known and unknown classes in them with the class-wise adaptive thresholds based on one-vs-rest classifiers. We adopt consistency regularization on augmented training samples based on Fourier Transformation to improve the generalization on the unseen target domain. The experimental results conducted on different real-world datasets show that our method outperforms the existing state-of-the-art methods.
Our work provides an effective method for semi-supervised domain generalization with known and unknown classes. We believe our work will be beneficial for domain generalization projects with few labeled data and many unlabeled data, and does not have negative societal impacts.
This work is supported by the National Science Foundation of China (62276125, 61921006), the Fundamental Research Funds for the Central Universities (022114380013), and the Collaborative Innovation Center of Novel Software Technology and Industrialization.
[46] Hemanth Venkateswara, Jose Eusebio, Shayok Chakraborty, and Sethuraman Panchathan. Deep Hashing Network for Unsupervised Domain Adaptation. In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR'17), pages 5385-5394, 2017.
[47] Kaiyang Zhou, Yongxin Yang, Yu Qiao, and Tao Xiang. Domain Adaptive Ensemble Learning. IEEE Transactions on Image Processing, 30:8008-8018, 2021.
[48] Xingchao Peng, Qinxun Bai, Xide Xia, Zijun Huang, Kate Saenko, and Bo Wang. Moment Matching for Multi-Source Domain Adaptation. In Proceedings of the IEEE International Conference on Computer Vision (ICCV'19), 2019.