Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about Varifocal loss #8

Open
HAOCHENYE opened this issue Dec 30, 2020 · 7 comments
Open

Question about Varifocal loss #8

HAOCHENYE opened this issue Dec 30, 2020 · 7 comments

Comments

@HAOCHENYE
Copy link

In the paper, the negtive weight of BCE loss is alpha*p^gamma. However, in varifocal_loss.py, the loss is implemented by:

focal_weight = target * (target > 0.0).float() +
alpha * (pred_sigmoid - target).abs().pow(gamma) *
(target <= 0.0).float()

The negtive weight is alpha(p-q)^gamma*, why?

@hyz-xmaster
Copy link
Owner

This is the initial version of implementation of VFL and I forgot to refine it.
alpha * (pred_sigmoid - target).abs().pow(gamma) * (target <= 0.0).float() actually equals to alpha * pred_sigmoid.pow(gamma) * (target == 0.0).float(), because there is a multiplier (target <= 0.0).float() in that formula and the target is always >= 0.

@HAOCHENYE
Copy link
Author

This is the initial version of implementation of VFL and I forgot to refine it.
alpha * (pred_sigmoid - target).abs().pow(gamma) * (target <= 0.0).float() actually equals to alpha * pred_sigmoid.pow(gamma) * (target == 0.0).float(), because there is a multiplier (target <= 0.0).float() in that formula and the target is always >= 0.

You means alpha * pred_sigmoid.abs().pow(gamma) * (target <= 0.0).float() equals alpha * pred_sigmoid.pow(gamma) * (target == 0.0).float() or alpha * (pred_sigmoid - target).abs().pow(gamma) * (target <= 0.0).float() equals to alpha * pred_sigmoid.pow(gamma) * (target == 0.0).float()? I'd understand the situation if it is the former one.

According to paper, the negtive weight should be alpha * pred_sigmoid.abs().pow(gamma) * (target <= 0.0).float().Is the formular of paper current version?

@hyz-xmaster
Copy link
Owner

Hi, target is the IoU so it is always >= 0, which implies target <= 0 <=> target == 0.
In this way,
alpha * (pred_sigmoid - target).abs().pow(gamma) * (target <= 0.0).float() <=>
alpha * (pred_sigmoid - target).abs().pow(gamma) * (target == 0.0).float() <=>
alpha * pred_sigmoid.abs().pow(gamma) * (target == 0.0).float().

@HAOCHENYE
Copy link
Author

Ohhh! Thanks, I understand it now.

@feiyuhuahuo
Copy link

Screenshot from 2021-01-05 10-31-48
图片
Hi @hyz-xmaster ,

  1. I did not find the q in the red circle according to the code.
  2. I can't understand the item above the green line. Since log(1-p) is used to predict negative samples, why it appears in the q>0 case? And anyway, I did not find the related implementation from the code. I just understand the code by the following way:
    Screenshot from 2021-01-05 10-41-02
    Looking forward to your reply, thanks.

@hyz-xmaster
Copy link
Owner

Hi @feiyuhuahuo,

  1. target in the code represents q in that formula.
  2. qlog(p)+(1-q)log(1-p) is the binary cross entropy loss, which is calculated by F.binary_cross_entropy_with_logits. When q = 0, qlog(p)+(1-q)log(1-p) reduces to log(1-p). When q > 0, it keeps unchanged.

@yxx-byte
Copy link

This is the initial version of implementation of VFL and I forgot to refine it.
alpha * (pred_sigmoid - target).abs().pow(gamma) * (target <= 0.0).float() actually equals to alpha * pred_sigmoid.pow(gamma) * (target == 0.0).float(), because there is a multiplier (target <= 0.0).float() in that formula and the target is always >= 0.

You means alpha * pred_sigmoid.abs().pow(gamma) * (target <= 0.0).float() equals alpha * pred_sigmoid.pow(gamma) * (target == 0.0).float() or alpha * (pred_sigmoid - target).abs().pow(gamma) * (target <= 0.0).float() equals to alpha * pred_sigmoid.pow(gamma) * (target == 0.0).float()? I'd understand the situation if it is the former one.

According to paper, the negtive weight should be alpha * pred_sigmoid.abs().pow(gamma) * (target <= 0.0).float().Is the formular of paper current version?

Hello, did you add your loss to yolov5? Judge which place needs to be adjusted?
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants