折再返怎么 做網(wǎng)站2023年7月最新新聞?wù)?/h1>
一、本文介紹
本文給大家?guī)淼氖欠诸悡p失?SlideLoss、VFLoss、FocalLoss損失函數(shù),我們之前看那的那些IoU都是邊界框回歸損失,和本文的修改內(nèi)容并不沖突,所以大家可以知道損失函數(shù)分為兩種一種是分類損失另一種是邊界框回歸損失,上一篇文章里面我們總結(jié)了過去百分之九十的邊界框回歸損失的使用方法,本文我們就來介紹幾種市面上流行的和最新的分類損失函數(shù),同時在開始講解之前推薦一下我的專欄,本專欄的內(nèi)容支持(分類、檢測、分割、追蹤、關(guān)鍵點檢測),專欄目前為限時折扣,歡迎大家訂閱本專欄,本專欄每周更新3-5篇最新機制,更有包含我所有改進(jìn)的文件和交流群提供給大家,本文支持的損失函數(shù)共有如下圖片所示
歡迎大家訂閱我的專欄一起學(xué)習(xí)YOLO!?
?專欄回顧:YOLOv10改進(jìn)系列專欄——本專欄持續(xù)復(fù)習(xí)各種頂會內(nèi)容——科研必備?
目錄
一、本文介紹
二、原理介紹
三、核心代碼
三、使用方式?
3.1 修改一
3.2 修改二
3.3 使用方法?
四 、本文總
二、原理介紹
其中絕大多數(shù)損失在前面我們都講過了本文主要講一下SlidLoss的原理,SlideLoss的損失首先是由YOLO-FaceV2提出來的。
??
官方論文地址:?官方論文地址點擊即可跳轉(zhuǎn)
官方代碼地址:?官方代碼地址點擊即可跳轉(zhuǎn)
??
從摘要上我們可以看出SLideLoss的出現(xiàn)是通過權(quán)重函數(shù)來解決簡單和困難樣本之間的不平衡問題題,什么是簡單樣本和困難樣本?
樣本不平衡問題是一個常見的問題,尤其是在分類和目標(biāo)檢測任務(wù)中。它通常指的是訓(xùn)練數(shù)據(jù)集中不同類別的樣本數(shù)量差異很大。對于人臉檢測這樣的任務(wù)來說,簡單樣本和困難樣本之間的不平衡問題可以具體描述如下:
簡單樣本:
- 容易被模型正確識別的樣本。
- 通常出現(xiàn)在數(shù)據(jù)集中的數(shù)量較多。
- 特征明顯,分類或檢測邊界清晰。
- 在訓(xùn)練中,這些樣本會給出較低的損失值,因為模型可以輕易地正確預(yù)測它們。
困難樣本:
- 模型難以正確識別的樣本。
- 在數(shù)據(jù)集中相對較少,但對模型性能的提升至關(guān)重要。
- 可能由于多種原因變得難以識別,如遮擋、變形、模糊、光照變化、小尺寸或者與背景的低對比度。
- 在訓(xùn)練中,這些樣本會產(chǎn)生較高的損失值,因為模型很難對它們給出準(zhǔn)確的預(yù)測。
解決樣本不平衡的問題是提高模型泛化能力的關(guān)鍵。如果模型大部分只見過簡單樣本,它可能在實際應(yīng)用中遇到困難樣本時性能下降。因此采用各種策略來解決這個問題,例如重采樣(對困難樣本進(jìn)行過采樣或?qū)唵螛颖具M(jìn)行欠采樣)、修改損失函數(shù)(給困難樣本更高的權(quán)重),或者是設(shè)計新的模型結(jié)構(gòu)來專門關(guān)注困難樣本。在YOLO-FaceV2中,作者通過Slide Loss這樣的權(quán)重函數(shù)來讓模型在訓(xùn)練過程中更關(guān)注那些困難樣本(這也是本文的修改內(nèi)容)。
三、核心代碼
使用方式看章節(jié)四
import mathclass SlideLoss(nn.Module):def __init__(self, loss_fcn):super(SlideLoss, self).__init__()self.loss_fcn = loss_fcnself.reduction = loss_fcn.reductionself.loss_fcn.reduction = 'none' # required to apply SL to each elementdef forward(self, pred, true, auto_iou=0.5):loss = self.loss_fcn(pred, true)if auto_iou < 0.2:auto_iou = 0.2b1 = true <= auto_iou - 0.1a1 = 1.0b2 = (true > (auto_iou - 0.1)) & (true < auto_iou)a2 = math.exp(1.0 - auto_iou)b3 = true >= auto_ioua3 = torch.exp(-(true - 1.0))modulating_weight = a1 * b1 + a2 * b2 + a3 * b3loss *= modulating_weightif self.reduction == 'mean':return loss.mean()elif self.reduction == 'sum':return loss.sum()else: # 'none'return lossclass Focal_Loss(nn.Module):# Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)def __init__(self, loss_fcn, gamma=1.5, alpha=0.25):super().__init__()self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss()self.gamma = gammaself.alpha = alphaself.reduction = loss_fcn.reductionself.loss_fcn.reduction = 'none' # required to apply FL to each elementdef forward(self, pred, true):loss = self.loss_fcn(pred, true)# p_t = torch.exp(-loss)# loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability# TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.pypred_prob = torch.sigmoid(pred) # prob from logitsp_t = true * pred_prob + (1 - true) * (1 - pred_prob)alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha)modulating_factor = (1.0 - p_t) ** self.gammaloss *= alpha_factor * modulating_factorif self.reduction == 'mean':return loss.mean()elif self.reduction == 'sum':return loss.sum()else: # 'none'return lossdef reduce_loss(loss, reduction):"""Reduce loss as specified.Args:loss (Tensor): Elementwise loss tensor.reduction (str): Options are "none", "mean" and "sum".Return:Tensor: Reduced loss tensor."""reduction_enum = F._Reduction.get_enum(reduction)# none: 0, elementwise_mean:1, sum: 2if reduction_enum == 0:return losselif reduction_enum == 1:return loss.mean()elif reduction_enum == 2:return loss.sum()def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None):"""Apply element-wise weight and reduce loss.Args:loss (Tensor): Element-wise loss.weight (Tensor): Element-wise weights.reduction (str): Same as built-in losses of PyTorch.avg_factor (float): Avarage factor when computing the mean of losses.Returns:Tensor: Processed loss values."""# if weight is specified, apply element-wise weightif weight is not None:loss = loss * weight# if avg_factor is not specified, just reduce the lossif avg_factor is None:loss = reduce_loss(loss, reduction)else:# if reduction is mean, then average the loss by avg_factorif reduction == 'mean':loss = loss.sum() / avg_factor# if reduction is 'none', then do nothing, otherwise raise an errorelif reduction != 'none':raise ValueError('avg_factor can not be used with reduction="sum"')return lossdef varifocal_loss(pred,target,weight=None,alpha=0.75,gamma=2.0,iou_weighted=True,reduction='mean',avg_factor=None):"""`Varifocal Loss <https://arxiv.org/abs/2008.13367>`_Args:pred (torch.Tensor): The prediction with shape (N, C), C is thenumber of classestarget (torch.Tensor): The learning target of the iou-awareclassification score with shape (N, C), C is the number of classes.weight (torch.Tensor, optional): The weight of loss for eachprediction. Defaults to None.alpha (float, optional): A balance factor for the negative part ofVarifocal Loss, which is different from the alpha of Focal Loss.Defaults to 0.75.gamma (float, optional): The gamma for calculating the modulatingfactor. Defaults to 2.0.iou_weighted (bool, optional): Whether to weight the loss of thepositive example with the iou target. Defaults to True.reduction (str, optional): The method used to reduce the loss intoa scalar. Defaults to 'mean'. Options are "none", "mean" and"sum".avg_factor (int, optional): Average factor that is used to averagethe loss. Defaults to None."""# pred and target should be of the same sizeassert pred.size() == target.size()pred_sigmoid = pred.sigmoid()target = target.type_as(pred)if iou_weighted:focal_weight = target * (target > 0.0).float() + \alpha * (pred_sigmoid - target).abs().pow(gamma) * \(target <= 0.0).float()else:focal_weight = (target > 0.0).float() + \alpha * (pred_sigmoid - target).abs().pow(gamma) * \(target <= 0.0).float()loss = F.binary_cross_entropy_with_logits(pred, target, reduction='none') * focal_weightloss = weight_reduce_loss(loss, weight, reduction, avg_factor)return lossclass Vari_focalLoss(nn.Module):def __init__(self,use_sigmoid=True,alpha=0.75,gamma=2.0,iou_weighted=True,reduction='sum',loss_weight=1.0):"""`Varifocal Loss <https://arxiv.org/abs/2008.13367>`_Args:use_sigmoid (bool, optional): Whether the prediction isused for sigmoid or softmax. Defaults to True.alpha (float, optional): A balance factor for the negative part ofVarifocal Loss, which is different from the alpha of FocalLoss. Defaults to 0.75.gamma (float, optional): The gamma for calculating the modulatingfactor. Defaults to 2.0.iou_weighted (bool, optional): Whether to weight the loss of thepositive examples with the iou target. Defaults to True.reduction (str, optional): The method used to reduce the loss intoa scalar. Defaults to 'mean'. Options are "none", "mean" and"sum".loss_weight (float, optional): Weight of loss. Defaults to 1.0."""super(Vari_focalLoss, self).__init__()assert use_sigmoid is True, \'Only sigmoid varifocal loss supported now.'assert alpha >= 0.0self.use_sigmoid = use_sigmoidself.alpha = alphaself.gamma = gammaself.iou_weighted = iou_weightedself.reduction = reductionself.loss_weight = loss_weightdef forward(self,pred,target,weight=None,avg_factor=None,reduction_override=None):"""Forward function.Args:pred (torch.Tensor): The prediction.target (torch.Tensor): The learning target of the prediction.weight (torch.Tensor, optional): The weight of loss for eachprediction. Defaults to None.avg_factor (int, optional): Average factor that is used to averagethe loss. Defaults to None.reduction_override (str, optional): The reduction method used tooverride the original reduction method of the loss.Options are "none", "mean" and "sum".Returns:torch.Tensor: The calculated loss"""assert reduction_override in (None, 'none', 'mean', 'sum')reduction = (reduction_override if reduction_override else self.reduction)if self.use_sigmoid:loss_cls = self.loss_weight * varifocal_loss(pred,target,weight,alpha=self.alpha,gamma=self.gamma,iou_weighted=self.iou_weighted,reduction=reduction,avg_factor=avg_factor)else:raise NotImplementedErrorreturn loss_cls
三、使用方式?
3.1 修改一
我們找到如下的文件'ultralytics/utils/loss.py'然后將上面的核心代碼粘貼到文件的開頭位置(注意是其他模塊的導(dǎo)入之后!)粘貼后的樣子如下圖所示!
??
3.2 修改二
第二步我門中到函數(shù)class v8DetectionLoss:(沒看錯V10繼承的v8損失函數(shù)我們修改v8就相當(dāng)于修改了v10)!我們下下面的代碼全部替換class v8DetectionLoss:的內(nèi)容!
class v8DetectionLoss:"""Criterion class for computing training losses."""def __init__(self, model): # model must be de-paralleled"""Initializes v8DetectionLoss with the model, defining model-related properties and BCE loss function."""device = next(model.parameters()).device # get model deviceh = model.args # hyperparametersm = model.model[-1] # Detect() moduleself.bce = nn.BCEWithLogitsLoss(reduction="none")"下面的代碼注釋掉就是正常的損失函數(shù),如果不注釋使用的就是使用對應(yīng)的損失失函數(shù)"# self.bce = Focal_Loss(nn.BCEWithLogitsLoss(reduction='none')) # Focal# self.bce = Vari_focalLoss() # VFLoss# self.bce = SlideLoss(nn.BCEWithLogitsLoss(reduction='none')) # SlideLoss# self.bce = QualityfocalLoss() # 目前僅支持者目標(biāo)檢測需要注意 分割 Pose 等用不了!self.hyp = hself.stride = m.stride # model stridesself.nc = m.nc # number of classesself.no = m.nc + m.reg_max * 4self.reg_max = m.reg_maxself.device = deviceself.use_dfl = m.reg_max > 1self.assigner = TaskAlignedAssigner(topk=10, num_classes=self.nc, alpha=0.5, beta=6.0)self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=self.use_dfl).to(device)self.proj = torch.arange(m.reg_max, dtype=torch.float, device=device)def preprocess(self, targets, batch_size, scale_tensor):"""Preprocesses the target counts and matches with the input batch size to output a tensor."""if targets.shape[0] == 0:out = torch.zeros(batch_size, 0, 5, device=self.device)else:i = targets[:, 0] # image index_, counts = i.unique(return_counts=True)counts = counts.to(dtype=torch.int32)out = torch.zeros(batch_size, counts.max(), 5, device=self.device)for j in range(batch_size):matches = i == jn = matches.sum()if n:out[j, :n] = targets[matches, 1:]out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor))return outdef bbox_decode(self, anchor_points, pred_dist):"""Decode predicted object bounding box coordinates from anchor points and distribution."""if self.use_dfl:b, a, c = pred_dist.shape # batch, anchors, channelspred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype))# pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype))# pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2)return dist2bbox(pred_dist, anchor_points, xywh=False)def __call__(self, preds, batch):"""Calculate the sum of the loss for box, cls and dfl multiplied by batch size."""loss = torch.zeros(3, device=self.device) # box, cls, dflfeats = preds[1] if isinstance(preds, tuple) else predspred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split((self.reg_max * 4, self.nc), 1)pred_scores = pred_scores.permute(0, 2, 1).contiguous()pred_distri = pred_distri.permute(0, 2, 1).contiguous()dtype = pred_scores.dtypebatch_size = pred_scores.shape[0]imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w)anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5)# Targetstargets = torch.cat((batch["batch_idx"].view(-1, 1), batch["cls"].view(-1, 1), batch["bboxes"]), 1)targets = self.preprocess(targets.to(self.device), batch_size, scale_tensor=imgsz[[1, 0, 1, 0]])gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxymask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0)# pboxespred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4)target_labels, target_bboxes, target_scores, fg_mask, _ = self.assigner(pred_scores.detach().sigmoid(), (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype),anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt)target_scores_sum = max(target_scores.sum(), 1)# Cls loss# loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL wayif isinstance(self.bce, (nn.BCEWithLogitsLoss, Vari_focalLoss, Focal_Loss)):loss[1] = self.bce(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE VFLoss Focalelif isinstance(self.bce, SlideLoss):if fg_mask.sum():auto_iou = bbox_iou(pred_bboxes[fg_mask], target_bboxes[fg_mask], xywh=False, CIoU=True).mean()else:auto_iou = 0.1loss[1] = self.bce(pred_scores, target_scores.to(dtype), auto_iou).sum() / target_scores_sum # SlideLosselif isinstance(self.bce, QualityfocalLoss):if fg_mask.sum():pos_ious = bbox_iou(pred_bboxes, target_bboxes / stride_tensor, xywh=False).clamp(min=1e-6).detach()# 10.0x Faster than torch.one_hottargets_onehot = torch.zeros((target_labels.shape[0], target_labels.shape[1], self.nc),dtype=torch.int64,device=target_labels.device) # (b, h*w, 80)targets_onehot.scatter_(2, target_labels.unsqueeze(-1), 1)cls_iou_targets = pos_ious * targets_onehotfg_scores_mask = fg_mask[:, :, None].repeat(1, 1, self.nc) # (b, h*w, 80)targets_onehot_pos = torch.where(fg_scores_mask > 0, targets_onehot, 0)cls_iou_targets = torch.where(fg_scores_mask > 0, cls_iou_targets, 0)else:cls_iou_targets = torch.zeros((target_labels.shape[0], target_labels.shape[1], self.nc),dtype=torch.int64,device=target_labels.device) # (b, h*w, 80)targets_onehot_pos = torch.zeros((target_labels.shape[0], target_labels.shape[1], self.nc),dtype=torch.int64,device=target_labels.device) # (b, h*w, 80)loss[1] = self.bce(pred_scores, cls_iou_targets.to(dtype), targets_onehot_pos.to(torch.bool)).sum() / max(fg_mask.sum(), 1)else:loss[1] = self.bce(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # 確保有損失可用# Bbox lossif fg_mask.sum():target_bboxes /= stride_tensorloss[0], loss[2] = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes, target_scores,target_scores_sum, fg_mask,((imgsz[0] ** 2 + imgsz[1] ** 2) / torch.square(stride_tensor)).repeat(1,batch_size).transpose(1, 0))loss[0] *= self.hyp.box # box gainloss[1] *= self.hyp.cls # cls gainloss[2] *= self.hyp.dfl # dfl gainreturn loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl)
3.3 使用方法?
將上面的代碼復(fù)制粘貼之后,我門找到下圖所在的位置,使用方法就是那個取消注釋就是使用的就是那個!?
??
四 、本文總結(jié)
到此本文的正式分享內(nèi)容就結(jié)束了,在這里給大家推薦我的YOLOv10改進(jìn)有效漲點專欄,本專欄目前為新開的平均質(zhì)量分98分,后期我會根據(jù)各種最新的前沿頂會進(jìn)行論文復(fù)現(xiàn),也會對一些老的改進(jìn)機制進(jìn)行補充,如果大家覺得本文幫助到你了,訂閱本專欄,關(guān)注后續(xù)更多的更新~
專欄回顧:YOLOv10改進(jìn)系列專欄——本專欄持續(xù)復(fù)習(xí)各種頂會內(nèi)容——科研必備?
??