Question about fine tuning a fcn_resnet101 model with 2 classes

alex_d: <p>I want to fine tune the pre-trained fcn resnet segmentation model with my own data set which only contains two classes.</p>
<p>I am following this <a href="https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html" rel="nofollow noopener">tutorial</a>, but I want to understand how the code knows which 2 classes I am referring to? So lets say my custom data set contains images with only two classes, background and car for example, when I set num_classes = 2, how does the code know that I’m specifically referring to background and car, instead of any of the other 20 classes it was originally trained on like cat, dog, horse, bicycle, etc?</p>
<p>Can it just tell by the specific color in the masked images? Or does it have to be specified somewhere in the code when fine tuning?</p>
<p>I am new to pytorch and machine learning so I would like to gain a better understanding of this so I can finetune the fcn resnet model correctly.</p>

kabilan: <p>Basically resnet is trained in imagenet dataset. while doing transfer learning you’re freezing the last couple of layers and using your dataset to train on that model.</p>

alex_d: <p>Thanks so much for your response. So basically you are saying that there is no need to specify which two classes you are referring to right?</p>

kabilan: <p>yeah. If you have any doubt in the future message me.</p>

ptrblck: <p>As can be seen in the tutorial <code>num_classes</code> is used to initialize a new linear layer, which acts as the classification layer.<br>
The output activations will provide <code>num_classes</code> outputs, which represent the logit for each class, i.e. <code>output[0]</code> would be the logit for class0.</p>
<p>If you define your <code>Dataset</code>, you would have to make sure that each sample is mapped to a valid class index.</p>
<p>E.g. in your use case with <code>num_classes = 2</code>, your target should contain the values 0 and 1.<br>
The mapping between these class indices and the meaning is made by you.<br>
I.e. if you only care about the background and car class, you would have to make sure the you are only using samples containing these two classes.<br>
The mapping is arbitrary. So you could define <code>background = 0</code> and <code>car = 1</code> or vice versa.</p>

alex_d: <p>Thanks so much for your detailed response!</p>
<p>I have a few questions to gain more clarity and understanding from your response:</p>
<ol>
<li>" If you define your  <code>Dataset</code> , you would have to make sure that each sample is mapped to a valid class index."</li>
</ol>
<p>When you are talking about mapping, is this done simply by using the masked image? Like in my masked images, all the background pixels are black, and the ones that belong to the car class are blue. Is using that enough to map the classes correctly? Or do you need to do something else besides that to map it?  You mentioned output[0], but I didn’t see anything in the tutorial where you index an output and map it to a class.</p>
<ol start="2">
<li>“if you only care about the background and car class, you would have to make sure the you are only using samples containing these two classes.”</li>
</ol>
<p>In my image data set, I only have two labels/classes ‘background’ or ‘car’. Basically everything that is not a car is marked as the background. In some of those unlabeled images, I have objects in the background like a ‘chair’ or ‘bird’ that are classes in the original pre-trained model, but in my masked images I mark them as background because I don’t care about those classes. Would doing this (masking objects that belonged to a different class in the original pre-trained model as background) confuse the model and cause a loss in accuracy?</p>
<p>Basically I want to clarify if what you mean by I should “only have samples containing those two classes”  means that the original unlabeled images should not have objects that belong to other classes other than ‘background’ and ‘car’. Or if it’s ok to have images like these, as long as the masked/labeled images only have ‘background’ and ‘car’ class?</p>

ptrblck: <aside class="quote no-group" data-username="alex_d" data-post="6" data-topic="66330">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/f0a364/48.png" class="avatar"> alex_d:</div>
<blockquote>
<p>Like in my masked images, all the background pixels are black, and the ones that belong to the car class are blue. Is using that enough to map the classes correctly?</p>
</blockquote>
</aside>
<p>Most likely not. Base on your description, it seems you are dealing with color images as masks.<br>
For a usual segmentation use case, the mask should be a <code>LongTensor</code> with the shape <code>[batch_size, height, width]</code> containing class indices.</p>
<p>Have a look at <a href="https://discuss.pytorch.org/t/training-semantic-segmentation/49275/4">this post</a> to see, how a mapping between the color values and your class indices can be created.</p>