# How to generate labels for self-supervised training?

I've been reading a lot lately about self-supervised learning and I didn't understand very well how to generate the desired label for a given image.

Let's say that I have an image classification task, and I have very little labeled data.

How can I generate the target label from the other data in the dataset?

• Hi, I am also working on SSL task or more learning how to do it. Is it please possible, if you show me the way you did it please? :) Nov 20 '20 at 0:45
• @TimcoVanco See my updated answer below. It should clarify your doubts, at this point, I believe.
– nbro
Nov 20 '20 at 3:42
• You should accept my answer below (by clicking on the checkmark), if it answers your question. It should answer your question, at this point. If not, please, let me know what is still unclear.
– nbro
Nov 20 '20 at 12:50

How can I generate the target label from the other data in the dataset?

If you are asking how you can create the learning signal in SSL, when given an unlabelled dataset, for learning representations of these unlabelled data, then there is no general answer. The answer depends on the type of data that you have (which can be e.g. textual or visual), and which features do you think you want to learn or can be learned from your unlabelled data. This paper and other answers to this question provide some examples of how that can be done (depending on the type of data). Below, I also provide an example.

Let me try to explain this more in detail.

Let's assume that you have both

1. an unlabelled dataset $$U = \{ u_i \}_{i=1}^m$$ and

2. a labelled dataset $$D = \{(x_i, y_i) \}_{i=1}^n$$

where we may have $$m \gg n$$ (although this is not a strict requirement), i.e. you may have a lot more unlabelled data than labelled data (this can easily be the case, given that, in general, manual data annotation is expensive/laborious). Let's say that your ultimate task is to perform object recognition (or classification). Let's call this task the downstream task. So, you may think that $$x_i$$ and $$u_i$$ are images and $$y_i$$ are labels, like "cat" or "dog" (let's say that you want to differentiate between cats and dogs).

You want to solve this downstream task by supervised learning with $$D$$. However, given that your labeled dataset is not big enough, you may think that training a neural network from scratch (i.e. by randomly initializing its weights) with $$D$$ may not lead to good performance. So, you think that it could be useful to start training from a pre-trained model that already contains useful representations of data similar to your labeled data, i.e. to perform transfer learning. To pre-train such a model, you could use SSL.

So, to solve your downstream task with SSL, there are 2 different steps

1. Self-supervised learning (SSL): learn representations of your images $$u_i \in U$$ by training a neural network $$M$$ with $$U$$ to solve a so-called pretext (or auxiliary task); there are many pre-text tasks: you can find many examples here, here and here (see example below too);

2. Supervised learning (SL) by transfer learning: fine-tune $$M$$ with $$D$$ (the labeled dataset), in a supervised way; this task is known as downstream task (as stated above)

In this process, there are 2 different labels.

• In step 1, you have the labels that are generated automatically. But how are these labels generated? As I said, there are many ways. Let me describe one way (among many others!). Let's say that your unlabelled dataset $$U$$ contains high-resolution images (i.e. $$u_i \in U$$ are high-resolution images), then you could define your pre-text task as follows. You lower the resolution of your high-resolution images to create other images. Let $$v_i$$ be the low-resolution image created from the high-resolution image $$u_i \in U$$, then the training pair to your neural network $$M$$ is $$(v_i, u_i) \in U'$$, where $$u_i$$ is the label (which is the original high-resolution image) and $$U'$$ the labeled dataset automatically generated (i.e. with the algorithm I've just explained).

So, these labels $$u_i$$ (high-resolution images) are semantically different than $$y_i$$ ("cat" or "dog") in the pairs $$(x_i, y_i) \in D$$. They are different because, here, we want to learn representations and not to perform object recognition/classification: the idea is that, by solving this pre-text task, your final trained neural network, should have learned features of the images in the unlabelled data (i.e. representation learning). These learned features can then be used to bootstrap training in the downstream task.

• In step 2, you use the labeled dataset $$D$$, which has been typically annotated (or labeled) by a human. As stated above, this dataset contains pairs $$(x_i, y_i)$$, where $$y_i$$ is, for example, the label "cat" or "dog".

In this step, the pre-trained model $$M$$, with the SSL technique, can be fine-tuned with $$D$$ in a supervised fashion. Given that we start with a pre-trained model $$M$$, we are effectively performing transfer learning.

Note that SSL can also refer to something (slightly) different than what has been explained in this answer. See my other answer for more details. Moreover, note that you can perform representation learning with SSL without necessarily solving a downstream task later, which may also not be an SL task (in the example above, I've described a downstream task that is an SL task only for simplicity).

If this answer is still unclear, maybe you should have a look at existing implementations of SSL techniques (such as this) for more inspiration.

• Thanks for the answer. I used semi supervised for autoencoders, but i don't have a clue how to use semi supervised for image classification or document classification :) Aug 4 '20 at 11:58