How to implement weighted Cross Entropy Loss for lane marking segmentation

Hi to everyone.
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.

Hi Fikrat!

If your problem is one a binary segmentation, presumably
black = background, color = foreground, then you should use
BCEWithLogitsLoss and the pos_weight argument that you
pass to its constructor.

If it’s multi-class segmentation, e.g., black = background, some
colors or patterns = lane 1, some other colors or patterns = lane 2,
etc., then you should use CrossEntropyLoss and the weight
argument passed to its constructor.


K. Frank

Good day KFrank,
Primarily, I have to thanks to you for your kind request and valuable time, shared with me.
Then, I have little doubt regarding 1 content as the following:
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.

Thansk in advance

Hello Fikrat!

Without knowing more about your actual problem, it’s hard to
give reliable advice.

I believe that you have a segmentation problem, in that you
classify individual pixels within an image to be, for example,
foreground vs. background.

This is distinct from a classification problem, where you classify
individual images to be, say, an image of a cat vs. a dog.

Some thoughts:

I speculate that you are using RandomScaleCrop /
RandomResizedCrop to augment your data. (RandomScaleCrop
looks like it’s a third-party class that isn’t part of pytorch proper.)

My intuition is that rescaling / resizing in order to augment your
data could make sense for image classification, but would make
less sense for image segmentation (where boundaries between
different kinds of pixels that define the segments could get blurred
by the rescaling). That’s just my guess, though.

(As an aside, it might be better to ask new questions like this
by starting a new topic, rather than adding them to an existing
thread. You can always link to the old thread if there is useful
context you don’t want to repeat.)


K. Frank

Thansk for your kind support.

Yes, I do apologize for inconvience