Question about class_weights in loss functions
See original GitHub issueHello,
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:
- Created 3 years ago
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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.
The number of indexes/classes is just to tell a helper function which channel to pull from both the ground-truth and the prediction:
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’.