Yolov5 改进之损失函数 SlideLoss & 注意力机制 MultiSEAM

用于学习记录

文章目录

  • 前言
  • 一、SlideLoss
    • 1.1 utils/loss.py
    • 1.2 data/hyps/hyp.scratch-low.yaml
  • 二、注意力机制 MultiSEAM
    • 2.1 models/common.py
    • 2.2 models/yolo.py
    • 2.3 models/MultiSEAM.yaml
  • 三、训练结果图
  • 总结


前言


一、SlideLoss

YOLO-FaceV2: A Scale and Occlusion Aware Face Detector
Yolov5 改进之损失函数 SlideLoss & 注意力机制 MultiSEAM_第1张图片

1.1 utils/loss.py

Yolov5 改进之损失函数 SlideLoss & 注意力机制 MultiSEAM_第2张图片
插入以下代码:

import math
class SlideLoss(nn.Module):
    def __init__(self, loss_fcn):
        super(SlideLoss, self).__init__()
        self.loss_fcn = loss_fcn
        self.reduction = loss_fcn.reduction
        self.loss_fcn.reduction = 'none'  # required to apply SL to each element
 
    def forward(self, pred, true, auto_iou=0.5):
        loss = self.loss_fcn(pred, true)
        if auto_iou < 0.2:
            auto_iou = 0.2
        b1 = true <= auto_iou - 0.1
        a1 = 1.0
        b2 = (true > (auto_iou - 0.1)) & (true < auto_iou)
        a2 = math.exp(1.0 - auto_iou)
        b3 = true >= auto_iou
        a3 = torch.exp(-(true - 1.0))
        modulating_weight = a1 * b1 + a2 * b2 + a3 * b3
        loss *= modulating_weight
        if self.reduction == 'mean':
            return loss.mean()
        elif self.reduction == 'sum':
            return loss.sum()
        else:  # 'none'
            return loss

修改 class ComputeLoss
Yolov5 改进之损失函数 SlideLoss & 注意力机制 MultiSEAM_第3张图片

        # Focal loss
        g = h['fl_gamma']  # focal loss gamma
        if g > 0:
            BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g)    
   		
   		# 添加以下代码:
        Slide_Loss = h['SlideLoss']
        if Slide_Loss > 0:
            BCEcls, BCEobj = SlideLoss(BCEcls), SlideLoss(BCEobj)

1.2 data/hyps/hyp.scratch-low.yaml

添加以下内容:
Yolov5 改进之损失函数 SlideLoss & 注意力机制 MultiSEAM_第4张图片

SlideLoss: 0.5

二、注意力机制 MultiSEAM

2.1 models/common.py

class Residual(nn.Module):
    def __init__(self, fn):
        super(Residual, self).__init__()
        self.fn = fn
 
    def forwar

你可能感兴趣的:(YOLO,python)