VarifocalNet icon indicating copy to clipboard operation
VarifocalNet copied to clipboard

VFocalLoss in yolov5

Open tangsipeng opened this issue 3 years ago • 3 comments

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

tangsipeng avatar Aug 01 '21 03:08 tangsipeng

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.

hyz-xmaster avatar Aug 01 '21 08:08 hyz-xmaster

Did you solve it?

RichardcLee avatar Mar 02 '22 02:03 RichardcLee

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

lascreia avatar Apr 29 '22 11:04 lascreia