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

VFocalLoss in yolov5 #18

Open
tangsipeng opened this issue Aug 1, 2021 · 3 comments
Open

VFocalLoss in yolov5 #18

tangsipeng opened this issue Aug 1, 2021 · 3 comments

Comments

@tangsipeng
Copy link

tangsipeng commented Aug 1, 2021

hi,i test VFocalLoss in yolov5,but not getting improvment.
did you have done some test about yolov5 ? or any suggestion ?
thank your ~

class VFocalLoss(nn.Module):

def __init__(self, loss_fcn, gamma=2.0, alpha=0.75): #runs/train/exp28

    super(VFocalLoss, self).__init__()
    # 传递 nn.BCEWithLogitsLoss() 损失函数  must be nn.BCEWithLogitsLoss()

    self.loss_fcn = loss_fcn  #

    self.gamma = gamma

    self.alpha = alpha

    self.reduction = loss_fcn.reduction

    self.loss_fcn.reduction = 'mean'  # required to apply VFL to each element

def forward(self, pred, true):

    loss = self.loss_fcn(pred, true)

    pred_prob = torch.sigmoid(pred)  # prob from logits

    focal_weight = true * (true > 0.0).float() + self.alpha * (pred_prob - true).abs().pow(self.gamma) * (true <= 0.0).float()
    loss *= focal_weight

    if self.reduction == 'mean':
        return loss.mean()
    elif self.reduction == 'sum':
        return loss.sum()
    else:
        return loss
@hyz-xmaster
Copy link
Owner

Hi, I haven't yet played YOLOv5 with Varifocal Loss. I think you may try the methods below to see if you can achieve some improvements.

  1. Try different values of gamma and alpha.
  2. Predict the IACS (as the final detection score) instead of the classification score with Varifocal Loss. Please refer to our paper for what is the IACS.

@RichardcLee
Copy link

Did you solve it?

@lascreia
Copy link

lascreia commented Apr 29, 2022

Hello I tried to implement this in YOLOv5 but why do I get an error saying "AttributeError: 'Model' object has no attribute 'reduction'" Help me pleaseee thank youu

This is the full error I get
image

the FocalLoss there is actually a VFocalLoss, I just named it focal loss.

hi,i test VFocalLoss in yolov5,but not getting improvment. did you have done some test about yolov5 ? or any suggestion ? thank your ~

class VFocalLoss(nn.Module):

def __init__(self, loss_fcn, gamma=2.0, alpha=0.75): #runs/train/exp28

    super(VFocalLoss, self).__init__()
    # 传递 nn.BCEWithLogitsLoss() 损失函数  must be nn.BCEWithLogitsLoss()

    self.loss_fcn = loss_fcn  #

    self.gamma = gamma

    self.alpha = alpha

    self.reduction = loss_fcn.reduction

    self.loss_fcn.reduction = 'mean'  # required to apply VFL to each element

def forward(self, pred, true):

    loss = self.loss_fcn(pred, true)

    pred_prob = torch.sigmoid(pred)  # prob from logits

    focal_weight = true * (true > 0.0).float() + self.alpha * (pred_prob - true).abs().pow(self.gamma) * (true <= 0.0).float()
    loss *= focal_weight

    if self.reduction == 'mean':
        return loss.mean()
    elif self.reduction == 'sum':
        return loss.sum()
    else:
        return loss

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