Weighted loss function

How can i find class weights for pixel-wise loss for image segmentation?I am working with camvid dataset with 12 classes.

You could calculate your mask and multiply it with the unreduced loss.
I’ve created a small gist a while ago, which might be a good starter.
Let me know, if this works for you!

1 Like

that code is giving errors when i tried to run, can u explain the approach a bit , so that i can implement it.

I’ve checked the code again and it’s running on my machine.
Could you post the error message, so that I can have a look?

I think I’ve misunderstood your question. I thought you would like to weight pixel-wise, not class-wise.
For a class weighting you could use the weight argument in nn.NLLLoss or nn.CrossEntropyLoss.

In my example I create a weight mask to weight the edges of the targets more, but that’s apparently not your use case.
Let me know, if the class weight works for you!

the errors are
:‘module’ object has no attribute ‘long’
‘module’ object has no attribute ‘where’
These errors are mostly because of pytorch version
Yes i tried to find class weights for each class, by there frequency, but not much improvement.
So what’s a good approach to find class weights.

How does your confusion matrix look?
Is your model overfitting on the majority class?

I think it’s over-fitting for some classes, because the overall IOU is good but some classes which are around less than 1-2%(column-pole, sign-symbol,etc) in the dataset the results are not good, i used IOU as a metric.

Did the weighting change your stats?
If a certain class is underperforming you could weight it more and trade the other accuracies for this specific class accuracy.
I’m not sure there are other approaches besides manually balancing your class accuracies.

Yes,the mean IOU increased but the IOU of some classes was not that much good.