Activation Function & IoU Losses

本文探讨了深度学习中的两种激活函数——Swish和Mish,并介绍了如何在PyTorch中实现它们。同时,文章详细讨论了IoU损失函数,包括IoU loss、GIoU Loss、DIoU Loss和CIOU Loss,以及如何在损失计算中灵活选择和应用这些损失函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Activations

1. Swish

# Swish activation
class Swish(nn.Module):
    def __init__(self):
        super(Swish, self).__init__()
 
    def forward(self, x):
        x = x * F.sigmoid(x)
        return x

2. Mish

# Mish activation
class Mish(nn.Module):
    def __init__(self):
        super().__init__()
        
    def forward(self,x):
        x = x * (torch.tanh(F.softplus(x)))
        return x

(以上两个cls全部放在model.py内)

3. 接口设置:

主要方法就是在create_modules的时候再传入一个参数 act_opt,默认是leaky,就是用pytorch中自带的leaky relu去做,如果指定为mish或者是swish就用自己写的方法用作激活函数。

def create_modules(module_defs, act_opt="leaky"):

再加上一些判断的条件:

            if module_def["activation"] == "leaky":
                if act_opt == "swish":
                    modules.add_module(f"swish_{module_i}", Swish)
                elif act_opt == "mish": 
                    modules.add_module(f"mish_{module_i}", Mish)
                else:
                    modules.add_module(f"leaky_{module_i}", nn.LeakyReLU(0.1))

(前面有很多空格是因为这部分是从代码段中间截下来的)本来是想改module_def中的activation的,但是这样一来好像会涉及cfg文件的修改,每次换激活函数还得重写生成cfg的bash,感觉反而不是很实用。

IoU Loss

(这一部分的代码都写在losses.py文件内了,用的时候直接

from losses import *

就好)

1. IoU loss

# IoU Loss
def iou(bboxes1, bboxes2):
    rows = bboxes1.shape[0]
    cols = bboxes2.shape[0]
    ious = torch.zeros((rows, cols))
    if rows * cols == 0:
        return ious
    exchange = False
    if bboxes1.shape[0] > bboxes2.shape[0]:
        bboxes1, bboxes2 = bboxes2, bboxes1
        ious = torch.zeros((cols, rows))
        exchange = True
    area1 = (bboxes1[:, 2] - bboxes1[:, 0]) * (
        bboxes1[:, 3] - bboxes1[:, 1])
    area2 = (bboxes2[:, 2] - bboxes2[:, 0]) * (
        bboxes2[:, 3] - bboxes2[:, 1])

    inter_max_xy = torch.min(bboxes1[:, 2:],bboxes2[:, 2:])
    inter_min_xy = torch.max(bboxes1[:, :2],bboxes2[:, :2])

    inter = torch.clamp((inter_max_xy - inter_min_xy), min=0)
    inter_area = inter[:, 0] * inter[:, 1]
    union = area1+area2-inter_area
    ious = inter_area / union
    ious = torch.clamp(ious,min=0,max = 1.0)
    if exchange:
        ious = ious.T
    return torch.sum(1-ious)

2. GIoU Loss

# GIoU Loss
def 
### Focal EIOU Loss Implementation and Explanation in Object Detection In the context of object detection, loss functions play a crucial role in training models to accurately predict bounding boxes around objects. The focal EIOU (Extended Intersection over Union) loss function is an advanced variant that addresses some limitations found in traditional IoU-based losses. The standard IoU measures how well two bounding boxes overlap but does not account for other factors like scale or aspect ratio differences between predicted and ground truth boxes. To improve upon this, researchers introduced variants such as CIoU (Complete IoU), DIoU (Distance IoU), and finally EIoU which considers three aspects: - **Overlap**: Measures intersection area relative to union. - **Scale Difference**: Accounts for size discrepancy by penalizing predictions with different scales more heavily. - **Aspect Ratio Distortion**: Penalizes distortions along both axes independently rather than just considering overall shape similarity[^1]. Focal loss modifies cross entropy so it focuses on hard examples during training while reducing contribution from easy negatives. Combining these ideas leads us to define focal-EIoU as follows: \[ \text{FL}(p_t)=\alpha(1-p_t)^{\gamma}\log(p_t)\] Where \( p_t \) represents prediction confidence score after applying sigmoid activation. For regression part we use modified version of EIoU where weights are adjusted according to difficulty level indicated through classification branch output. Here's Python code implementing focal EIoU loss: ```python import torch from torch import nn class FocalEIoULoss(nn.Module): def __init__(self, alpha=0.25, gamma=2.0, eps=1e-7): super(FocalEIoULoss, self).__init__() self.alpha = alpha self.gamma = gamma self.eps = eps def forward(self, pred_boxes, target_boxes, labels): """ Args: pred_boxes: Predicted box coordinates [batch_size, num_anchors, 4]. target_boxes: Ground-truth box coordinates [batch_size, num_anchors, 4]. labels: Classification scores indicating positive/negative samples [batch_size, num_anchors]. Returns: Total loss value combining classification and localization components. """ # Compute pairwise distances between centers c_x_p, c_y_p = pred_boxes[:, :, :2].split([1, 1], dim=-1) c_x_g, c_y_g = target_boxes[:, :, :2].split([1, 1], dim=-1) dist_center = ((c_x_p - c_x_g)**2 + (c_y_p - c_y_g)**2).sqrt() w_pred, h_pred = pred_boxes[:, :, 2:].split([1, 1], dim=-1) w_gt, h_gt = target_boxes[:, :, 2:].split([1, 1], dim=-1) iou_loss_term = _compute_eiou(pred_boxes, target_boxes) cls_focal_loss = -(labels * (1-labels)**self.gamma * torch.log(torch.sigmoid(labels)+self.eps)).mean()[^2] total_loss = cls_focal_loss + iou_loss_term.mean() return total_loss def _compute_eiou(box_a, box_b): """Helper function computing Extended IOU.""" # Calculate center points distance squared rho_squared = ... # Width/height difference terms v_w = ... v_h = ... # Regular IoU component ious = ... eious = ious - (rho_squared / ...) - (...*(v_w+v_h)/(...)) return eious ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值