KD-FixMatch: Knowledge Distillation Siamese Neural Networks
Semi-supervised learning (SSL) has become a crucial approach in deep learning
as a way to address the challenge of limited labeled data. The success of deep
neural networks heavily relies on the availability of large-scale high-quality
labeled data. However, the process of data labeling is time-consuming and
unscalable, leading to shortages in labeled data. SSL aims to tackle this
problem by leveraging additional unlabeled data in the training process. One of
the popular SSL algorithms, FixMatch, trains identical weight-sharing teacher
and student networks simultaneously using a siamese neural network (SNN).
However, it is prone to performance degradation when the pseudo labels are
heavily noisy in the early training stage. We present KD-FixMatch, a novel SSL
algorithm that addresses the limitations of FixMatch by incorporating knowledge
distillation. The algorithm utilizes a combination of sequential and
simultaneous training of SNNs to enhance performance and reduce performance
degradation. Firstly, an outer SNN is trained using labeled and unlabeled data.
After that, the network of the well-trained outer SNN generates pseudo labels
for the unlabeled data, from which a subset of unlabeled data with trusted
pseudo labels is then carefully created through high-confidence sampling and
deep embedding clustering. Finally, an inner SNN is trained with the labeled
data, the unlabeled data, and the subset of unlabeled data with trusted pseudo
labels. Experiments on four public data sets demonstrate that KD-FixMatch
outperforms FixMatch in all cases. Our results indicate that KD-FixMatch has a
better training starting point that leads to improved model performance
compared to FixMatch.
Chien-Chih Wang, Shaoyuan Xu, Jinmiao Fu, Yang Liu, Bryan Wang
# KD-FixMatch: Knowledge Distillation Siamese Neural Networks

###### Abstract

Semi-supervised learning (SSL) has become a crucial approach in deep learning as a way to address the challenge of limited labeled data. The success of deep neural networks heavily relies on the availability of large-scale high-quality labeled data. However, the process of data labeling is time-consuming and unscalable, leading to shortages in labeled data. SSL aims to tackle this problem by leveraging additional unlabeled data in the training process. One of the popular SSL algorithms, FixMatch[1], trains identical weight-sharing teacher and student networks simultaneously using a siamese neural network (SNN). However, it is prone to performance degradation when the pseudo labels are heavily noisy in the early training stage. We present KD-FixMatch, a novel SSL algorithm that addresses the limitations of FixMatch by incorporating knowledge distillation. The algorithm utilizes a combination of sequential and simultaneous training of SNNs to enhance performance and reduce performance degradation. Firstly, an outer SNN is trained using labeled and unlabeled data. After that, the network of the well-trained outer SNN generates pseudo labels for the unlabeled data, from which a subset of unlabeled data with trusted pseudo labels is then carefully created through high-confidence sampling and deep embedding clustering. Finally, an inner SNN is trained with the labeled data, the unlabeled data, and the subset of unlabeled data with trusted pseudo labels. Experiments on four public data sets demonstrate that KD-FixMatch outperforms FixMatch in all cases. Our results indicate that KD-FixMatch has a better training starting point that leads to improved model performance compared to FixMatch.

Chien-Chih Wang, Shaoyuan Xu, Jinmiao Fu, Yang Liu, Bryan Wang {ccwang, shaoyux, jinmiaof, yliuu, brywan}@amazon.com semi-supervised learning, knowledge distillation, siamese neural networks, high-confidence sampling, deep embedding clustering

## 1 Introduction

Large-scale high-quality labeled data is the key to the tremendous success of supervised deep learning models in many fields. However, collecting labeled data remains a significant challenge, as the process of data labeling is time-consuming and resource-intensive. This is particularly evident in tasks such as detecting product image defects in e-commerce, where high-quality labeled data is essential for accurate predictions and customer satisfaction. The numbers of defective and non-defective images are sometimes extremely unbalanced and thus it is necessary to label a large number of randomly selected images in order to collect sufficient high-quality labeled images for supervised learning.

Recently, [1] came up with a widely used SSL algorithm called FixMatch, which trains an SNN with limited labeled and extra unlabeled data, and achieved significant improvement comparing to traditional supervised learning methods. However, despite the superiority and success of FixMatch, it has a noticeable disadvantage. It trains the teacher and the student in an SNN simultaneously and since the pseudo labels generated by the teacher in the early stage may not be correct, using them directly may introduce large amount of label noise. Therefore, we propose a modified FixMatch called KD-FixMatch, which trains an outer and an inner SNN sequentially. Our proposed method improves upon FixMatch because an outer SNN will be first trained with labeled and unlabeled data and thus the percentage of label noise in the pseudo labels generated by the network of the well-trained outer SNN would be lower than that of the teacher in FixMatch, especially in the early stage. Our main contributions are summarized as follows:

1. In KD-FixMatch, the outer SNN is first well-trained and its network serves as a teacher for the inner SNN. Thus, we have a better training starting point than direcly applying FixMatch algorithm to the inner SNN.
2. Unlike self-training [2, 3] or Noisy Student [4], where its neural network is re-trained multiple times when its pseudo labels are required to be updated, we only need to train each of the outer and inner SNN once.
3. KD-FixMatch outperforms FixMatch in our experiments although the time complexity of KD-FixMatch is more than two times that of FixMatch.

## 2 Relation to Prior Work

SSL has gained significant attention in recent years as a method to overcome the challenge of limited labeled data. By incorporating large-scale unlabeled data, SSL has shown to enhance the performance of deep neural networks, as demonstrated in various studies [1, 4, 5, 6, 7].
### Teacher-Student Sequential Training

Knowledge distillation [8] is a well-known model compression technique that transfers knowledge from a pre-trained, larger teacher model to a smaller student model. By using the teacher to generate pseudo labels for the unlabeled data, the student trained on both labeled data and pseudo-labeled data performs better than when trained solely on the limited labeled data [8].

Noisy Student [4] takes inspiration from knowledge distillation and uses a student model that is equal to or larger than the teacher model. This allows for the inclusion of noise-inducing techniques such as dropout, stochastic depth, and data augmentation in the student's training, resulting in better generalization compared to the teacher [4]. In Noisy Student, once the student is found to be better than the teacher, the pseudo labels are updated by the current student, and a new student is re-initialized. This procedure is then repeated several times. However, this approach requires waiting for the teacher to be well-trained before generating highly confident pseudo labels for the student's training.

### Teacher-Student Simultaneous Training

Besides the aforementioned sequential methods, the simultaneous training method is also an alternative option [1, 9, 10]. We take FixMatch[1] as an example. The core neural network architecture for FixMatch is SNN which consists of two identical weight-sharing classifier networks, a teacher and a student. See Figure 1.

Assume that \(f_{\mathbf{\theta}}\) is a standard neural network with softmax output layer, \(\mathbf{\theta}\in\Re^{n}\) is a long vector, \(\ell\) is the number of training examples, \(K\) is the number of the classes, and \(n\) is the total number of the parameters of \(f_{\mathbf{\theta}}\). We define the optimization problem for FixMatch to be

\[\min_{\mathbf{\theta}}\ f(\mathbf{\theta}),\text{where }f(\mathbf{\theta})=\frac{1}{2C} \mathbf{\theta}^{T}\mathbf{\theta}+\frac{1}{\ell}\sum_{i=1}^{\ell}\xi(\mathbf{\theta}; \mathbf{x}^{i},\mathbf{y}^{i}) \tag{1}\]

\(C>0\) is the parameter to avoid overfitting by regularization, \(\mathbf{x}^{i}\) is the \(i\)th input image, and \(\mathbf{y}^{i}\) is the label vector of \(\mathbf{x}^{i}\). In addition, if \(\mathbf{x}^{i}\) belongs to the \(s\)th class, the label vector, \(\mathbf{y}^{i}\), is \(\underbrace{[0,\dots,0}_{s-1},1,0,\dots,0]^{T}\in\Re^{K}\). However, training examples for SSL consists of both a set of the labeled data, \(S_{l}\), and a set of the unlabeled data, \(S_{u}\). Label vectors for the unlabeled data are not available. Therefore, FixMatch uses teacher inside SNN to generate pseudo labels as label vectors for \(\mathbf{x}^{i},i\in S_{u}\) and then the loss function, \(\xi\), in (1) can be defined as follows:

\[\xi(\mathbf{\theta};\mathbf{x}^{i},\mathbf{y}^{i})=\begin{cases}\xi_{l}\left(\mathbf{y}^{i},f _{\mathbf{\theta}}(\mathbf{x}^{i}_{w})\right)&\text{if }\ i\in S_{l},\\ \lambda_{u}\xi_{u}(\hat{\mathbf{y}}^{i},f_{\mathbf{\theta}}(\mathbf{x}^{i}_{s}))&\text{if }\ i\in S_{u}, \end{cases} \tag{2}\]

where \(\hat{\mathbf{y}}^{i}\) is the pseudo label vector of \(\mathbf{x}^{i},i\in S_{u}\), \(\lambda_{u}\) is a pre-defined parameter, \(\mathbf{x}^{i}_{w}\) is the weakly augmented image of \(\mathbf{x}^{i}\), \(\mathbf{x}^{i}_{s}\) is the strongly augmented image of \(\mathbf{x}^{i}\), \(\xi_{l}\) is a loss function for the labeled data, and \(\xi_{u}\) is a loss function for the unlabeled data.

In [1], \(\xi_{l}\) in (2) is the standard cross-entropy (CE) function to measure the difference between \(f_{\mathbf{\theta}}(\mathbf{x}^{i}_{w})\) and its label vector \(\mathbf{y}^{i}\). For \(\xi_{u}\) in (2), when the input of \(f_{\mathbf{\theta}}\) is \(\mathbf{x}^{i}_{w}\), \(f_{\mathbf{\theta}}\) is considered as the teacher and \(f_{\mathbf{\theta}}(\mathbf{x}^{i}_{w})\) is \(\hat{\mathbf{y}}^{i}\) of \(\mathbf{x}^{i}\). Conversely, \(f_{\mathbf{\theta}}\) is considered as the student when the input of \(f_{\mathbf{\theta}}\) is \(\mathbf{x}^{i}_{s}\). However, in order to reduce label noise, we only choose the highly confident pseudo labels whose maximum value are greater than or equal to a pre-defined threshold \(\tau>0\) and thus \(\xi_{u}\) in (2) can be defined as follows:

\[\mathbb{1}(\max(\hat{\mathbf{y}}^{i})\geq\tau)\xi_{u}(\text{OHL}(\hat{\mathbf{y}}^{i}),f_{\mathbf{\theta}}(\mathbf{x}^{i}_{s})), \tag{3}\]

where \(\mathbb{1}\) is an indicator function and OHL is a function mapping a softmax pseudo label vector to its one-hot label vector. After the loss function, \(\xi\), in (1) is prepared, back-propagation is applied to update the weights in \(f_{\mathbf{\theta}}\) and thus both teacher and student are updated simultaneously by minimizing (1).

For the method where teacher and student are trained simultaneously, there is no need to wait for the teacher's training procedure to complete before the student's can begin. However, in the early stage, heavy label noise or insufficient correct pseudo labels generated by the immature teacher may affect the generalization ability of the student model.

## 3 Proposed Algorithm

We propose an algorithm called KD-FixMatch, which contains an outer SNN, \(f^{\text{outer}}\), and an inner SNN, \(f^{\text{inner}}\). The goal is not only to use teacher-student sequential training to fix label noise problem in the early stage, but also to incorporate teacher-student simultaneous training such as FixMatch.

### Trusted Pseudo Label Selection

After the outer SNN is well-trained, it generates pseudo labels for the unlabeled data. Due to the imperfection of the outer SNN, we need to choose a subset of trusted pseudo labels to reduce label noise. Otherwise, it may affect inner SNN's model performance.

Similar to (3), we first choose a pre-defined threshold, \(\tau^{\text{select}}\), and choose the pseudo labels whose maximum value

Figure 1: An example of SNN for FixMatch.
is greater than or equal to \(\tau^{\text{select}}\). Then, we extract the latent representations1 of the unlabeled data which has passed the \(\tau^{\text{select}}\) selection. We do a deep embedding clustering [11, 12] on the representations and choose the unlabeled data whose clustering results are consistent with their predicted class.2 Finally, the indices of the chosen unlabeled data form \(T_{u}\).

Footnote 1: The representations are derived from the layer before the last layer.

Footnote 2: The predicted class is the index of the maximum value of \(\tilde{\mathbf{y}}^{i}\).

### Merging Conflict Pseudo Labels

From Section 3.1, \(T_{u}\) is determined. However, our inner SNN also generates pseudo labels for the unlabeled data during its training process. Following (3), we choose the pseudo labels whose maximum value is greater than or equal to a pre-defined threshold, \(\tau^{\text{inner}}>0\), to be inner SNN's trusted pseudo labels. Therefore, conflicts may arise when both the outer and inner SNN provide their own trusted pseudo labels for the same image. Heuristic 1 is proposed to determine the pseudo label vector, \(\hat{\mathbf{y}}^{i}\), in (3) for the unlabeled data which has conflict pusedo labels. The main idea is that the inner SNN intuitively has better generalization ability than the outer SNN since it has more reliable pseudo labels in the early stage.

### Robust Loss Functions

Label noise is one of the major problems for SSL since teacher model is not perfect. In other words, there exist noisy labels among the pseudo labels generated by the teacher. [7] mentioned that robust loss functions may be helpful in case of noisy pseudo labels. The widely used loss functions in robust learning is symmetric loss functions [13, 14] which has been proven to be robust to noisy labels. However, there are some constraints for symmetric loss functions.

Assume that \(f\) is a standard neural network with softmax output layer. From [14], a loss function \(\xi\) is called symmetric if it satisfies \(\sum_{k=1}^{K}\xi(f(\mathbf{x}),\mathbf{e}_{k})=M,\;\forall\mathbf{x}\in X,\;\forall f\), where \(K\) is the number of classes, \(X\) is the feature space, \(f\) is a function mapping an input \(\mathbf{x}\) to a softmax output \(\mathbf{y}\), \(M\) is a constant value, and \(\mathbf{e}_{k}=[\underbrace{0,\ldots,0}_{k-1},1,0,\ldots,0]^{T}\in\Re^{K}\).

Mean absolute error (MAE) [15], symmetric cross-entropy (SCE) [16], and normalized cross-entropy (NCE) [17] are examples of robust loss functions.

### Knowledge Distillation Siamese Neural Networks

We initialize an inner SNN and then train it with not only the labeled and unlabeled data but also the unlabeled data with trusted pseudo labels, \((\mathbf{x}^{i},\hat{\mathbf{y}}^{i}),i\in T_{u}\). For the inner SNN, the optimization problem and loss functions are the same as (1), (2), and (3) except that we substitute \(f\), \(C\), \(\xi_{l}\), \(\lambda_{u}\), \(\xi_{u}\), and \(\tau\) with \(f^{\text{inner}}\), \(C^{\text{inner}}\), \(\xi_{l}^{\text{inner}}\), \(\lambda_{u}^{\text{inner}}\), \(\xi_{u}^{\text{inner}}\), and \(\tau^{\text{inner}}\), respectively. A summary of our proposed algorithm is in Algorithm 1.

```
Given a labeled data, \(S_{l}\), and an unlabeled data, \(S_{u}\);
1: Initialize an outer SNN, \(f^{\text{outer}}\), and train it with \(S_{l}\) and \(S_{u}\) by using back-propagation to solve (1);
2: Generate pseudo labels for \(S_{u}\) using the well-trained \(f^{\text{outer}}\);
3: Select a trusted index subset \(T_{u}\) from \(S_{u}\);
4: Choose CE or a robust loss function to be \(\xi_{u}^{\text{inner}}\);
5: Initialize an inner SNN, \(f^{\text{inner}}\), and train it with not only the labeled and unlabeled data but also the unlabeled data with trusted pseudo labels, \((\mathbf{x}^{i},\hat{\mathbf{y}}^{i}),i\in T_{u}\);
6: Return the well-trained \(f^{\text{inner}}\);
```

**ALGORITHM 1**KD-FixMatch

## 4 Experiments

The main objective in this section is to compare our proposed algorithm with other methods. For a fair comparison, we implement all of the methods using Tensorflow _2.3.0_. EfficientNet-B0[18] pre-trained on ImageNet3 is chosen for all of the experiments and SNN-EB0 is an SNN which consists of two identical weight-sharing EfficientNet-B0. We compare the following five methods: 4 (a) Baseline: EfficientNet-B0 is trained with only the labeled data by solving (1) with CE loss function; (b) FixMatch: SNN-EB0 is trained with the labeled and unlabeled data by solving (1) with (2) and (3) and both \(\xi_{l}\) and \(\xi_{u}\) are CE functions; (c) KD-FixMatch-CE: an outer and an inner SNN-EB0 are trained using Algorithm 1 with the labeled and unlabeled data and both \(\xi_{l}\) and \(\xi_{u}\) for outer and inner SNN-EB0 are CE functions; (d) KD-FixMatch-SCE-1.0-0.01: the same as KD-FixMatch-CE except that \(\xi_{u}\) for inner SNN-EB0 is an SCE function with \(\alpha=1.0\) and \(\beta=0.01\); and (e) KD-FixMatch-SCE-1.0-0.1: the same as
KD-FixMatch-CE except that \(\xi_{u}\) for inner SNN-EB0 is an SCE function with \(\alpha=1.0\) and \(\beta=0.1\). For the labeled training data in all of our experiments, we follow the sampling strategy mentioned in [1] to choose an equal number of images from each class in order to avoid model bias.

For all the experiments, Adam [19] optimizer is used and the exponential decay rates for \(1\)st and \(2\)nd moment estimates are \(0.9\) and \(0.999\), respectively. Batch size is set to \(128\).5 The initial learning rate is \(7\mathrm{e}{-5}\) and the learning rate scheduler mentioned in Section \(5.2\) of [18] is applied. We set \(1/C\) in (1) to be \(5\mathrm{e}{-4}\) except that \(1\mathrm{e}{-3}\) for CIFAR100. For weak augmentation, we follow the method in Section 2.3 of [1]. For strong augmentation, we follow RandAugment implementation in [20]. Following [1], we set \(\tau\) in (3) to be \(0.95\) and \(\lambda_{u}\) be \(1\). Also, we set \(\tau^{\mathrm{select}}\) and \(\tau^{\mathrm{inner}}\) to be \(0.80\) and \(0.95\), respectively. For inference, we follow [1] to use the maintained exponential moving average of the trained parameters. The decay is set to be \(0.9999\) and num_updates be \(10\). 6

Footnote 5: Except for Baseline, 64 labeled and 64 unlabeled images are in a batch.

Footnote 6: See [https://github.com/petewarden/tensorflow_makefile/blob/master/tensorflow/python/training/moving_averages.py](https://github.com/petewarden/tensorflow_makefile/blob/master/tensorflow/python/training/moving_averages.py).

### Experiments on Four Public Data Sets

In this subsection, we conduct our experiments on four public data sets: SVHN[21], CIFAR10/100[22], and FOOD101[23]. In order to mimic the practical use, we randomly split the training data from their official websites into original training data (\(80\%\)) and original validation data (\(20\%\)) for our experiments. The unlabeled data from the official websites is not used. Instead, we follow [1] to ignore the labels in the original training data and deem it as unlabeled data. All of these four data sets are publicly available and the summary is in Table 1.

From Table 2, we observe that: (a) FixMatch is better than Baseline for all the cases. This is as expected because, comparing to Baseline, FixMatch leverages an additional unlabeled data; (b) KD-FixMatch is better than FixMatch for all the cases. The reason is that KD-FixMatch has a better starting point than FixMatch; (c) Comparing to FixMatch, the more labeled data we use, the less improvements we have for KD-FixMatch. The possible reason is that when FixMatch has a large enough initial labeled data it has a good starting point to reach the result competitive to KD-FixMatch; and (d) KD-FixMatch-SCE has slightly better model performance than KD-FixMatch-CE in most of the cases even without grid search for the two parameters, \((\alpha,\beta)\). The possible reason is that the robust loss function, SCE, is applied.

## 5 Conclusion

In this paper, we have proposed an SSL algorithm, KD-FixMatch, which is an improved version of FixMatch that utilizes knowledge distillation. Based on our experiments, KD-FixMatch outperforms FixMatch and Baseline on four public data sets. Interestingly, despite the absence of the parameter selection for SCE (robust loss function), the performance of KD-FixMatch-SCE is slightly better than KD-FixMatch-CE in most of the cases.
