Activation Function & IoU Losses

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

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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值