Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/utils/activations.py
Views: 475
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license1"""2Activation functions3"""45import torch6import torch.nn as nn7import torch.nn.functional as F8910# SiLU https://arxiv.org/pdf/1606.08415.pdf ----------------------------------------------------------------------------11class SiLU(nn.Module): # export-friendly version of nn.SiLU()12@staticmethod13def forward(x):14return x * torch.sigmoid(x)151617class Hardswish(nn.Module): # export-friendly version of nn.Hardswish()18@staticmethod19def forward(x):20# return x * F.hardsigmoid(x) # for TorchScript and CoreML21return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for TorchScript, CoreML and ONNX222324# Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------25class Mish(nn.Module):26@staticmethod27def forward(x):28return x * F.softplus(x).tanh()293031class MemoryEfficientMish(nn.Module):32class F(torch.autograd.Function):33@staticmethod34def forward(ctx, x):35ctx.save_for_backward(x)36return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))3738@staticmethod39def backward(ctx, grad_output):40x = ctx.saved_tensors[0]41sx = torch.sigmoid(x)42fx = F.softplus(x).tanh()43return grad_output * (fx + x * sx * (1 - fx * fx))4445def forward(self, x):46return self.F.apply(x)474849# FReLU https://arxiv.org/abs/2007.11824 -------------------------------------------------------------------------------50class FReLU(nn.Module):51def __init__(self, c1, k=3): # ch_in, kernel52super().__init__()53self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False)54self.bn = nn.BatchNorm2d(c1)5556def forward(self, x):57return torch.max(x, self.bn(self.conv(x)))585960# ACON https://arxiv.org/pdf/2009.04759.pdf ----------------------------------------------------------------------------61class AconC(nn.Module):62r""" ACON activation (activate or not).63AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter64according to "Activate or Not: Learning Customized Activation" <https://arxiv.org/pdf/2009.04759.pdf>.65"""6667def __init__(self, c1):68super().__init__()69self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))70self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))71self.beta = nn.Parameter(torch.ones(1, c1, 1, 1))7273def forward(self, x):74dpx = (self.p1 - self.p2) * x75return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x767778class MetaAconC(nn.Module):79r""" ACON activation (activate or not).80MetaAconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is generated by a small network81according to "Activate or Not: Learning Customized Activation" <https://arxiv.org/pdf/2009.04759.pdf>.82"""8384def __init__(self, c1, k=1, s=1, r=16): # ch_in, kernel, stride, r85super().__init__()86c2 = max(r, c1 // r)87self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))88self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))89self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True)90self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True)91# self.bn1 = nn.BatchNorm2d(c2)92# self.bn2 = nn.BatchNorm2d(c1)9394def forward(self, x):95y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True)96# batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/289197# beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable98beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed99dpx = (self.p1 - self.p2) * x100return dpx * torch.sigmoid(beta * dpx) + self.p2 * x101102103