jkouubb / retinanet-based-on-pytorch-1.1.0

35 stars 10 forks source link

About Focal Loss issues: #1

Open GeroCI opened 5 years ago

GeroCI commented 5 years ago

hello , i read your code ,i have an issue in loss.py, line 88-90: cl = -(judge_matrix * torch.log(possibility_matrix) + (1.0 - judge_matrix) * torch.log(1.0 - possibility_matrix)) cl = possibility_matrix * cl cl = torch.where(torch.ne(judge_matrix, -1.0), cl, torch.zeros(cl.shape).cuda()) classification_loss.append(cl.sum() / torch.clamp(positive_indices.sum().float(), min=1.0)), i think cl = -(judge_matrix torch.log(classifications)+(1.0-judge_matrix)torch.log(1-classifications)),not possibility, because possibility has been appliyed from 1-classification,this will lead to increase values of positive classes, can u give an answer?

jkouubb commented 5 years ago

the Folcal Loss is Classification Loss(p) = -alpha * (1 - p) gamma log(p) in my code, possibility_matrix represents alpha (1 - p) gamma, if you replace it with classifications,which represents p, it does not match the fomula so I think it should be possility matrix. Maybe I have not understood Focal Loss well?

BenedictYoung commented 3 years ago

hello , i read your code ,i have an issue in loss.py, line 88-90: cl = -(judge_matrix * torch.log(possibility_matrix) + (1.0 - judge_matrix) * torch.log(1.0 - possibility_matrix)) cl = possibility_matrix * cl cl = torch.where(torch.ne(judge_matrix, -1.0), cl, torch.zeros(cl.shape).cuda()) classification_loss.append(cl.sum() / torch.clamp(positive_indices.sum().float(), min=1.0)), i think cl = -(judge_matrix torch.log(classifications)+(1.0-judge_matrix)torch.log(1-classifications)),not possibility, because possibility has been appliyed from 1-classification,this will lead to increase values of positive classes, can u give an answer?

I also think classification loss shoud be cl = -(judge_matrix torch.log(classifications)+(1.0-judge_matrix)torch.log(1-classifications)), because Folcal Loss should be "bce alpha_t ((1 - p_t) ** gamma)". You may check it with the pytorch official implementation "torchvision.ops.sigmoid_focal_loss"

jkouubb commented 1 year ago

hello , i read your code ,i have an issue in loss.py, line 88-90: cl = -(judge_matrix * torch.log(possibility_matrix) + (1.0 - judge_matrix) * torch.log(1.0 - possibility_matrix)) cl = possibility_matrix * cl cl = torch.where(torch.ne(judge_matrix, -1.0), cl, torch.zeros(cl.shape).cuda()) classification_loss.append(cl.sum() / torch.clamp(positive_indices.sum().float(), min=1.0)), i think cl = -(judge_matrix torch.log(classifications)+(1.0-judge_matrix)torch.log(1-classifications)),not possibility, because possibility has been appliyed from 1-classification,this will lead to increase values of positive classes, can u give an answer?

I also think classification loss shoud be cl = -(judge_matrix torch.log(classifications)+(1.0-judge_matrix)torch.log(1-classifications)), because Folcal Loss should be "bce alpha_t ((1 - p_t) ** gamma)". You may check it with the pytorch official implementation "torchvision.ops.sigmoid_focal_loss"

Oh, you are right. It is a mistake, thanks for pointing out. I will correct it when I have free time. Feel free to submit a PR if you like :)