Segmentation Dice Loss for Background Class Images (no TP, FP, FN) Possible?

username: ai_padawan
cooked: <p>I’ve been training U-Net models with a regular dataset, and one augmented with images that only has background classes.</p>
<p>For those situation with background class image, by definition there can be no TP. The ideal situation is the model predicts TP = 0, FP = 0, TN = 0, and FN = every pixel. If this ideal situation is achieved, the dice loss goes to zero.</p>
<p>BUT, if there’s even a single FP or FN, the dice loss goes to the max value. So there is no ability for the model to learn how to classify every pixel as background pixels. So when I train with images with non-background pixels and images with background pixels only… the loss gets dominated by this effect. Is there a workaround for this?</p>

username: ptrblck
cooked: <p>Wouldn’t an all background input predicted as such yield TN = 1?<br>
If the model predicts another class and the dice loss shoots up (you might need to limit it in case it goes to Inf), the model will get a high signal to predict background only.</p>
<p>Do you mean that the very high dice loss biases the model to only predict all background images?<br>
If so, you could try to clip the loss or just remove these images.</p>

username: Danesh-A
cooked: <p>Is there a way to assign weights to dice loss? For example I want foreground predictions to have a higher impact since it is rarer in a dataset. Im having issues where the model simply predicts background for every image after some training time.</p>

username: ptrblck
cooked: <p>If your model only predicts the background class, the intersection part of the dice loss calculation would be zero and thus your dice loss should be high.<br>
I don’t know if there is a clear way to add weighting to it, but you could try to combine the dice loss with e.g. <code>nn.CrossEntropyLoss</code> (and add weights there).</p>

username: tom
cooked: <aside class="quote no-group" data-username="Danesh-A" data-post="3" data-topic="71359" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/danesh-a/48/34449_2.png" class="avatar"> Danesh-A:</div>
<blockquote>
<p>Is there a way to assign weights to dice loss? For example I want foreground predictions to have a higher impact since it is rarer in a dataset. Im having issues where the model simply predicts background for every image after some training time.</p>
</blockquote>
</aside>
<ol>
<li>You can give higher weight to classes by pretending the pixels where these are the ground truth are there multiple times (i.e. TP and FN are multiplied and also the total number of pixels is adjusted).</li>
<li>So if you have very few “interesting” pixels in a large number of uninteresting ones, you essentially have a heavily imbalanced problem. In my experience, balancing the training data might work even better than applying weights to the losses. When training the U-Net in our book in chapter 13, we take care that we have enough slices with nodules fed into the U-Net. A very brief discussion is in section 13.5.5 (Designing our training and validation data).</li>
</ol>
<p>Best regards</p>
<p>Thomas</p>

username: zzzf
cooked: <p>Suppose you have C classes as well as background, you can model the background as a separate class, then you will have C+1 classes, and TP = every pixel for images that contain only background.</p>