VarifocalNet
VarifocalNet copied to clipboard
VFocalLoss in yolov5
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
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.
- Try different values of
gamma
andalpha
. - 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.
Did you solve it?
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
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