# Semi-Supervised Learning in Hasty

Many projects nowadays use large-scale datasets with thousands and millions of images. Training models on them requires many labeled examples that are expensive to annotate and acquire.

In Hasty, we want to help you reduce annotation effort and time while increasing your model’s performance. Automated labeling, Active Learning, and AI assistants serve this goal.

**Semi-Supervised learning** (SSL) is another feature that improves your model performance by using unlabeled data training sets more effectively. We have built it based on the [Unbiased Teacher for Semi-Supervised Object Detection](https://openreview.net/pdf?id=MJIve1zgR_) paper.

As the [research](https://ycliu93.github.io/projects/unbiasedteacher.html) shows, with only **1%** labeled data from MS-COCO, the proposed method improves mAP **6.8** times in comparison to the state-of-the-art method, STAC (Sohn et al., 2020b). Moreover, Unbiased Teacher consistently improves mAP **10** times using only 0.5, 1, 2, and 5% of labeled data compared to the supervised approach.

You should give SSL a try if:

* You work with large datasets and would like to process them faster;
* You have a small percentage of your dataset labeled (from 1% to 10%);
* You work on Object Detection or Instance Segmentation task (other models are not supported).

### **How Semi-Supervised Learning works** <a href="#strong-how-semi-supervised-learning-works-strong" id="strong-how-semi-supervised-learning-works-strong"></a>

#### **Supervised, Unsupervised, and Semi-Supervised approaches** <a href="#strong-supervised-unsupervised-and-semi-supervised-approaches-strong" id="strong-supervised-unsupervised-and-semi-supervised-approaches-strong"></a>

To understand the mechanism behind SSL in Hasty, let’s first observe other well-known ML algorithms.

In classical **Supervised Learning**, the model trains on the given labeled data. The shortcoming of this approach is that in real life, it is hard to find or build clean, precisely labeled datasets.

**Unsupervised Learning** is used when we do not know what exact outcome to expect. In this case, we allow the model to explore the underlying data structures on its own and then analyze the results. In Object Detection or Instance Segmentation tasks, however, we usually know in advance what classes we expect to distinguish. Thus, Unsupervised Learning might not be perfect here, as well.

Therefore, **Semi-Supervised Learning** comes into play. Essentially, it uses a combination of labeled, poorly labeled, or unlabeled data to train the model. This is more efficient than labeling everything and allows us to require the desired output from the model.\


#### **Pseudo-labeling & Teacher-Student paradigm** <a href="#strong-pseudo-labeling-amp-teacher-student-paradigm-strong" id="strong-pseudo-labeling-amp-teacher-student-paradigm-strong"></a>

One common way to speed up the model’s learning is to train it on both labeled and unlabeled images simultaneously.

The workflow is the following:

1. We create two separate models – a **Teacher** and a **Student**.
2. The Teacher is trained on labeled images as in a regular supervised approach using Cross-entropy loss.
3. Once the Teacher is trained, it creates **pseudo-labels** for the unlabeled images. The mechanism is simple: the Teacher predicts classes for unlabeled pictures –> the class the model is the most confident about is taken as a pseudo-label for the given image. Pseudo-labels are assigned only if the model is sure about them enough.
4. The Student trains on combined labeled and unlabeled images (with pseudo-labels). Various augmentations and noise can be introduced to the images to train the model better.
5. When the Student is trained, it becomes the new Teacher. The process is repeated again until a certain performance criterion is reached.

<figure><img src="https://wiki.cloudfactory.com/media/pages/docs/userdocs/semi-supervised-learning/semi-supervised-learning-in-hasty/ea9fda9bc7-1684131892/pseudo-labels.webp" alt=""><figcaption><p>Example of pseudo-labeling<br><a href="https://amitness.com/2020/07/semi-supervised-learning/">Source</a></p></figcaption></figure>

### **Bias problem in pseudo-labeling** <a href="#strong-bias-problem-in-pseudo-labeling-strong" id="strong-bias-problem-in-pseudo-labeling-strong"></a>

Object Detection and Instance Segmentation tasks have their own peculiarities which might affect the efficiency of pseudo-labeling. Here are the most common problems that appear:

* **Foreground classes imbalance:** certain class(-es) might be dominant throughout the dataset, while others might be underrepresente&#x64;**.**

<figure><img src="https://wiki.cloudfactory.com/media/pages/docs/userdocs/semi-supervised-learning/semi-supervised-learning-in-hasty/a77ce8ee45-1684131892/class-imbalance-1.webp" alt=""><figcaption><p>Class imbalance on the example of the <a href="https://cocodataset.org/">COCO </a>dataset</p></figcaption></figure>

* **Foreground-background imbalance:** the background area might be much bigger than the foreground, so most bounding boxes might get labeled as background.

<figure><img src="https://wiki.cloudfactory.com/media/pages/docs/userdocs/semi-supervised-learning/semi-supervised-learning-in-hasty/4905cb9ea8-1684131892/forback-imbalance-1.webp" alt=""><figcaption><p>An example of foreground-background imbalance. <a href="https://towardsdatascience.com/review-retinanet-focal-loss-object-detection-38fba6afabe4">Source</a></p></figcaption></figure>

These imbalances introduce bias to the model’s predictions. The problem with pseudo-labeling is that the model might consistently lean towards the most dominant and confident classes, ignoring less represented ones. Thus, wrong pseudo-labels are created and fetched to the model, decreasing its performance.

### **Solution: Unbiased Teacher model** <a href="#strong-solution-unbiased-teacher-model-strong" id="strong-solution-unbiased-teacher-model-strong"></a>

To address the problem of biased pseudo-labels, the following techniques were introduced:

* **Multi-class Focal loss** instead of the standard **Cross-entropy loss.**
  * When we try to minimize Cross-entropy loss, we punish the model when it makes _wrong_ predictions. However, the model might still get the most predictions correctly simply due to the prevalence and high confidence of some classes. As a result, the loss metric does not drop much since wrong predictions for minor classes do not affect the loss metric significantly.
  * With a multi-class Focal loss, we punish the algorithm more for wrong _low-confidence_ predictions. Thus, we prompt the model to pay more “attention” to less-represented classes and harder samples, increasing its performance. As a result, the model focuses on hard samples instead of the easier examples that are likely from dominant classes.\

* **Exponential Moving Average (EMA) training** was added.
  * EMA is a metric that measures the average value while placing greater weight on the most _recent_ data. This approach is opposed to the Simple Moving Average (SMA), where equal weight is applied to all data points. In our case, EMA helps to update the Teacher model gradually.
  * Since the Student model trains in iterations, the slowly progressing Teacher can be viewed as the ensemble of Student models in different time steps (iterations). This regularization leads to a more robust Teacher model.

### **The workflow behind the Hasty SSL feature** <a href="#strong-the-workflow-behind-the-hasty-ssl-feature-strong" id="strong-the-workflow-behind-the-hasty-ssl-feature-strong"></a>

The framework we adopted is the **Unbiased Teacher** approach, where Student and gradually progressing Teacher help each other to progress.

The process can be divided into 2 stages:

1. **BurnIn stage**: at this stage, we simply train the object detector using the available supervised data to get started with predictions. Usually, 100 iterations are enough at this stage.\

2. **Teacher-Student Mutual Learning stage:** we copy the initialized object detector into Teacher and Student models.\
   Two steps are repeated within this stage:\
   \- **Student Learning:** the Teacher generates pseudo-labels to train the Student. To increase the model’s robustness, the Teacher makes predictions on weakly augmented images, and the Student is trained on strongly augmented images;\
   \- **Teacher Refinement:** the Student gradually updates the Teacher using Exponential Moving Average (EMA). We can manually specify when we want to update the Teacher - in 10, 100, or 1000 iterations.\
   The updated Teacher generates new pseudo-labels for the next batch of images. Thus, the pseudo-labels on which the Student is trained are also refined, leading to the improvement of the Teacher, in turn.

<figure><img src="https://wiki.cloudfactory.com/media/pages/docs/userdocs/semi-supervised-learning/semi-supervised-learning-in-hasty/da30720a40-1684131892/snimok-ekrana-1191.webp" alt=""><figcaption><p>How the model works. <a href="https://ycliu93.github.io/projects/unbiasedteacher.html">Source</a></p></figcaption></figure>

When the Teacher reaches the desired performance threshold, we take it as our final model.

### **When Semi-Supervised Learning is efficient** <a href="#strong-when-semi-supervised-learning-is-efficient-strong" id="strong-when-semi-supervised-learning-is-efficient-strong"></a>

It’s hard to give rigid estimates since every dataset and every use case is unique. However, there are certain recommendations you might want to follow to squeeze the most out of the feature:

* **SSL shows the most efficiency on large datasets.** The more images you have, the more improvement in model performance you can expect to see.
* **The most optimal use case is when around 1% of your dataset is labeled.** The more images you have labeled already, the less will be the relative improvement brought by SSL. For instance, if your dataset is 50% labeled, your model will likely train quite well already, so the effect of SSL will not be that critical.
* **However, if your dataset is complex, it would be better to label more than 1%** to get any benefit from SSL. For example, if you work with complex medical data requiring high-level human expertise, the SSL algorithm might need more labeled input to train well on it and produce good results.
* **The number of classes matters.** The more classes you have, the more images you need for SSL to be effective. For example, if you need to detect 1000 classes and have a dataset of only 5k images, it might be quite tough to train a robust SSL model based on that.\
  On the contrary, if you have only 2-4 classes, you might require fewer images for SSL to be helpful.

### **How to access Semi-Supervised Learning** <a href="#strong-how-to-access-semi-supervised-learning-strong" id="strong-how-to-access-semi-supervised-learning-strong"></a>

You can access the Semi-Supervised learning feature within Model Playground.

To do so, create a split and toggle on the **“Semi-Supervised Learning”** option. Thus, you will be able to include unlabeled data in your split.

<figure><img src="https://wiki.cloudfactory.com/media/pages/docs/userdocs/semi-supervised-learning/semi-supervised-learning-in-hasty/309fe1a3c0-1684131892/snimok-ekrana-1185.webp" alt=""><figcaption><p>Toggle on the SSL feature</p></figcaption></figure>

When creating new experiments, enable the SSL option as well.

<figure><img src="https://wiki.cloudfactory.com/media/pages/docs/userdocs/semi-supervised-learning/semi-supervised-learning-in-hasty/1e6ccc1ae7-1684131892/snimok-ekrana-1187.webp" alt=""><figcaption></figcaption></figure>

You will be able to play with the following training parameters:

* **Burn up step** – the number of iterations required to finish the BurnIn stage;
* **Unsupervised loss weight** – a coefficient by which we will multiply the loss calculated on pseudo-labels;
* **Bounding Box Threshold** – only pseudo-labels with confidences exceeding this threshold will be used;
* **Exponential Mean Average** – the number of recent iterations the average of which you want to take;
* **Teacher Update iteration** – the number of iterations of the Student model after which the Teacher is updated.

<figure><img src="https://wiki.cloudfactory.com/media/pages/docs/userdocs/semi-supervised-learning/semi-supervised-learning-in-hasty/ab477d4142-1684131892/snimok-ekrana-1189-1.webp" alt=""><figcaption></figcaption></figure>

Please remember that SSL is available only for Object Detection and Instance Segmentation tasks.

### **Summary** <a href="#strong-summary-strong" id="strong-summary-strong"></a>

Semi-Supervised learning helps to increase your model performance for OD/IS tasks by leveraging raw data in the dataset. It shows the best efficiency when **1%** (ideally) with up to 5-10% of the images are labeled.

When used in such cases, the SSL feature can help automate your labeling up to **6-10** times.

Thanks for reading, and happy training!
