question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about class_weights in loss functions

See original GitHub issue

Hello,

I’m a bit confused by class_weights parameter in loss functions. For instance, in JaccardLoss description it says: class_weights: Array (``np.array``) of class weights (``len(weights) = num_classes``) So, it’s a numpy array. What dimension should it be? I assume it should be (image width x image height x number of classes). But how can I assign specific weights to specific classes? I beleive the process should be different gor binary segmentation and for multiclass segmentation problems.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
EtagiBIcommented, Apr 1, 2020

Thanks for your reply!

I was confused by a PyCharm warning. It claims that list is an unexpected data type for class_weights parameter of loss functions.

As for difference between binary segmentation and multiclass segmentation, I thought that one-hot encoding isn’t widely used for binary problems since it’s possible to determine both classes using one mask with a single channel.

1reaction
JordanMakesMapscommented, Apr 1, 2020

It doesn’t need to be an numpy array, and it’s definitely not a 2D array. It can just be a python list where each index contains the weight for the class whose channel is the same when one-hot-encoded.

class_weights = [.5, .1, .95] 

The number of indexes/classes is just to tell a helper function which channel to pull from both the ground-truth and the prediction:

# base/functional.py, line 88
gt, pr = gather_channels(gt, pr, indexes=class_indexes, **kwargs)

The determination of what the class weight values should be, is the question to ask. Usually people will use sklearn.utils.class_weight.compute_class_weight, but you could implement your own version by calculating the numbers of samples per class and dividing the total number of samples over each one. See here for discussion. How you calculate the number of samples in a multi-channel image? I’m not sure how others do it, but I count the number of pixels for each class in the whole dataset, along with the total number of pixels in the dataset and compute the weights from that.

In this case, it’s not different for binary and multi-class classification because if you think about it, binary classification in semantic segmentation is still two classes. If you’re looking at pictures of cats, the two classes are ‘cat’, and ‘not cat’.

...

# score calculation
intersection = backend.sum(gt * pr, axis=axes)
union = backend.sum(gt + pr, axis=axes) - intersection

score = (intersection + smooth) / (union + smooth)
score = average(score, per_image, class_weights, **kwargs) 

return score

...

def average(x, per_image=False, class_weights=None, **kwargs):
    backend = kwargs['backend']
    if per_image:
        x = backend.mean(x, axis=0)
    if class_weights is not None:
        x = x * class_weights
    return backend.mean(x)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I select the class weights for the loss function in the ...
I have a machine learning task where I would like to weight losses based on the frequency of the categorical values appearing in...
Read more >
Class weights for categorical loss | by Borun Chowdhury Ph.D.
Class weights are often used for classification problems when datasets are imbalanced. ... The categorical cross entropy loss function for one data point...
Read more >
Handling Class Imbalance by Introducing Sample Weighting ...
The idea is to weigh the loss computed for different samples differently based on whether they belong to the majority or the minority...
Read more >
How to use class weights in loss function for imbalanced dataset
do you think working with a weighted loss function is the right approach if I want to manually imbalance classes? Example: I have...
Read more >
Handling Imbalanced Classes with Weighted Loss in PyTorch
This is so simple. What I did was calculating a manual re-scaling weight for each class and pass it to “weight” parameter in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found