YOLOv5系列(十三) 解析激活函数部分activations(详尽) 您所在的位置:网站首页 运用卷积函数conv时需要注意什么问题 YOLOv5系列(十三) 解析激活函数部分activations(详尽)

YOLOv5系列(十三) 解析激活函数部分activations(详尽)

2024-07-01 20:45| 来源: 网络整理| 查看: 265

前言

源码: YOLOv5源码. 注释版全部项目文件已上传至GitHub: yolov5-5.x-annotations.

这个文件是yolov5做的一些关于激活函数改进的实验,且都是近年来比较火的也是效果比较好的一些激活函数。大家可以尽情的尝试放在自己的数据集或者模型上试试效果。

使用起来也很方便,直接修改common.py在的Conv函数即可:

class Conv(nn.Module): # Standard convolution def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups """ Standard convolution conv+BN+act :params c1: 输入的channel值 :params c2: 输出的channel值 :params k: 卷积的kernel_size :params s: 卷积的stride :params p: 卷积的padding 一般是None 可以通过autopad自行计算需要pad的padding数 :params g: 卷积的groups数 =1就是普通的卷积 >1就是深度可分离卷积 :params act: 激活函数类型 True就是SiLU()/Swish False就是不使用激活函数 类型是nn.Module就使用传进来的激活函数类型 """ super(Conv, self).__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) self.bn = nn.BatchNorm2d(c2) # self.act = nn.Identity() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = nn.Tanh() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = nn.Sigmoid() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = nn.ReLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = nn.LeakyReLU(0.1) if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = nn.Hardswish() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = Mish() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = FReLU(c2) if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = AconC(c2) if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = MetaAconC(c2) if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = DyReLUA(c2, conv_type='2d') if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) # self.act = DyReLUB(c2, conv_type='2d') if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) def forward(self, x): return self.act(self.bn(self.conv(x))) def fuseforward(self, x): """ 前向融合conv+bn计算 减少推理时间 """ return self.act(self.conv(x))

导包部分:

# Activation functions import torch import torch.nn as nn import torch.nn.functional as F 0、ReLU、Leaky ReLU、PReLU、RReLU

在这里插入图片描述

ReLU优点:

不会发生梯度消失问题。Sigmoid函数在 x>0 会发生梯度消失问题,造成信息损失,从而无法完成深度网络的训练。而 ReLU 函数当 x>0 为线性结构,有固定的梯度,不会消失。ReLU激活函数在 x BxCxL result = torch.max(output, dim=-1)[0].permute(1, 2, 0) elif self.conv_type == '2d': # BxCxHxW -> HxWxBxCx1 x_perm = x.permute(2, 3, 0, 1).unsqueeze(-1) output = x_perm * relu_coefs[:, :, :self.k] + relu_coefs[:, :, self.k:] # HxWxBxCx2 -> BxCxHxW result = torch.max(output, dim=-1)[0].permute(2, 3, 0, 1) return result

注意:我们一般不太使用DyReLUC,因为参数实在太大了。

总结

这个文件主要是一些今年来比较火或者比较新颖的一些激活函数。我比较看好的激活函数是 DyReLU和meta-AconC这两个激活函数,感兴趣的可以试试用在自己的项目里。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有