vandit15 / class-balanced-loss-pytorch Goto Github PK
View Code? Open in Web Editor NEWPytorch implementation of the paper "Class-Balanced Loss Based on Effective Number of Samples"
License: MIT License
Pytorch implementation of the paper "Class-Balanced Loss Based on Effective Number of Samples"
License: MIT License
It seems that you forgot to include weights for sigmoid loss
Instead calculating a weight for each batch, applying to class using pos_weight argument in torch.nn.BCELoss(pos_weights=weights)
Simply, https://github.com/vandit15/Class-balanced-loss-pytorch/blob/master/class_balanced_loss.py#L71-L82
Are those line of codes same with
effective_num = 1.0 - np.power(beta, samples_per_cls)
weights = (1.0 - beta) / np.array(effective_num)
weights = weights / np.sum(weights) * no_of_classes
loss = torch.nn.BCELoss(reduction='mean', pos_weight=weights)
this code?
does someone has implement focal_loss with softmax correctly?could you recommend its link for me?thank you very much
shouldn't it be F.cross_entropy instead?
I should compute samples_per_cls of whole dataset or each batch? If there is 0 in samples_per_cls of each batch, the loss will be nan.
Hi, thanks for your code sharing!
I am now trying to understand how do you implement and I have something want to discuss.
In your main function you have provide us with a toy example, I noticed that you have set samples_per_cls equals [2,3,1,2,2] which indicates label 0 has 2 samples , am i right?
But you have random labels,so should I count samples of each class every times ?
I would be appreciated if you can answer my question as quick as possible. Have a good day!
Hey can this be applied to multi label problems?
Thanks
should be alpha: A float tensor of size [num_classes]
Thanks for sharing your code.
I have 4D tensor..
I just understand your code. Thanks!
Thanks for sharing code.I have modified the code. I want to calculate the multivariate classification. The code is as follows. I can always report errors. I wonder if you have tried it?
pred = logits.log_softmax(dim=1)
cb_loss = F.cross_etropy(input=pred, target=labels, weights=weights)
Thanks.
@vandit15 thanks for open-sourcing the code , is it possible to use Cb loss in the object detection or segmentation architecture ?? did you experiment it with any std architecture as yolo retina, deeplab ?? since i am planning on using to our custom object detection architecture so
Hi, i'm interested in your work! Now i have problem, why your implement code of focal loss use "modulator = torch.exp(-gamma * labels * logits - gamma * torch.log(1 + torch.exp(-1.0 * logits)))" ?
thank you for sharing Pytorch implementation,
I was wondering if we need to specify "reduction="none"" for the "binary_cross_entropy_with_logits" function here since we are going to do weighting and reduction on our own later?
Hi, I wonder if this implementation is correct?
When I run the code with softmax, I get weights like this:
tensor([[0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
[0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
[0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
[0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
[1.7646, 1.7646, 1.7646, 1.7646, 1.7646],
[1.7646, 1.7646, 1.7646, 1.7646, 1.7646],
[0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
[0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
[0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
[0.8824, 0.8824, 0.8824, 0.8824, 0.8824]])
where each sample enjoin the same weights.
This is weird, right? According to the original paper, we should have different weight for each class (column), right?
Hi, Inside the CBloss you are using binary cross-entropy, so why not using cross-entropy ? Could you explain? Thank you
original:
cb_loss = F.binary_cross_entropy_with_logits(input = logits,target = labels_one_hot, weights = weights)
update:
cb_loss = F.binary_cross_entropy_with_logits(input = logits,target = labels_one_hot, weight = weights)
Thanks for the resource, it worked great!!
Hi @vandit15,
Thanks for sharing your code.
In line 73, weights = weights / np.sum(weights) * no_of_classes
, why is no_of_classes included here to normalise the weights? Any help would be thankful.
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.