How to implement weighted Cross Entropy Loss for lane marking segmentation

username: Fikrat
cooked: <p>Hi to everyone.<br>
I have 5000 ground truth and RGB images, then I have to note that I have many black pixels on ground truh image, compared to colorful pixels, as a result, cross entropy loss is not optimized while training. Therefore, my target is to implement Weighted Cross Entropy Loss, aiming at providing more weights to colourful pixels, rather than black pixels.</p>

username: KFrank
cooked: <p>Hi Fikrat!</p>
<aside class="quote no-group" data-username="Fikrat" data-post="1" data-topic="74021" 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/fikrat/48/21597_2.png" class="avatar"> Fikrat:</div>
<blockquote>
<p>Therefore, my target is to implement Weighted Cross Entropy Loss, aiming at providing more weights to colourful pixels, rather than black pixels.</p>
</blockquote>
</aside>
<p>If your problem is one a <em>binary</em> segmentation, presumably<br>
black = background, color = foreground, then you should use<br>
<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.BCEWithLogitsLoss" rel="noopener nofollow ugc">BCEWithLogitsLoss</a> and the <code>pos_weight</code> argument that you<br>
pass to its constructor.</p>
<p>If it’s <em>multi-class</em> segmentation, e.g., black = background, some<br>
colors or patterns = lane 1, some other colors or patterns = lane 2,<br>
etc., then you should use <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.CrossEntropyLoss" rel="noopener nofollow ugc">CrossEntropyLoss</a> and the <code>weight</code><br>
argument passed to its constructor.</p>
<p>Best.</p>
<p>K. Frank</p>

username: Fikrat
cooked: <p>Good day KFrank,<br>
Primarily, I have to thanks to you for your kind request and valuable time, shared with me.<br>
Then, I have little doubt regarding 1 content as the following:<br>
I have RandomScaleCrop function in train set, such that it scales RGB images which leads  images to be descructive, in other words, plenty of reasonable pixels are destroied. In this respect, according to my experience, there is not Random Scale Crop Fucntion to be explained, but instead, I found RandomResizedCrop function. As a consequence, I can not figure out about ongoing the fact that I shoud eliminated RandomScaleCrop in order to remove such an occurence, or I have to leave it as now. I please you to guide me in this domain.</p>
<p>Thansk in advance</p>

username: KFrank
cooked: <p>Hello Fikrat!</p>
<aside class="quote no-group quote-modified" data-username="Fikrat" data-post="3" data-topic="74021" 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/fikrat/48/21597_2.png" class="avatar"> Fikrat:</div>
<blockquote>
<p>I have RandomScaleCrop function in train set, such that it scales RGB images which leads  images to be descructive, in other words, plenty of reasonable pixels are destroied.<br>
…<br>
I shoud eliminated RandomScaleCrop in order to remove such an occurence, or I have to leave it as now.</p>
</blockquote>
</aside>
<p>Without knowing more about your actual problem, it’s hard to<br>
give reliable advice.</p>
<p>I believe that you have a <em>segmentation</em> problem, in that you<br>
classify individual pixels within an  image to be, for example,<br>
foreground vs. background.</p>
<p>This is distinct from a <em>classification</em> problem, where you classify<br>
individual images to be, say, an image of a cat vs. a dog.</p>
<p>Some thoughts:</p>
<p>I speculate that you are using <code>RandomScaleCrop</code> /<br>
<code>RandomResizedCrop</code> to <em>augment</em> your data.  (<code>RandomScaleCrop</code><br>
looks like it’s a third-party class that isn’t part of pytorch proper.)</p>
<p>My intuition is that rescaling / resizing in order to augment your<br>
data could make sense for image classification, but would make<br>
less sense for image segmentation (where boundaries between<br>
different kinds of pixels that define the segments could get blurred<br>
by the rescaling).  That’s just my guess, though.</p>
<p>(As an aside, it might be better to ask new questions like this<br>
by starting a new topic, rather than adding them to an existing<br>
thread.  You can always link to the old thread if there is useful<br>
context you don’t want to repeat.)</p>
<p>Best.</p>
<p>K. Frank</p>

username: Fikrat
cooked: <p>Thansk for your kind support.</p>
<p>Yes, I do apologize for inconvience</p>