CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hukaixuan19970627

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: hukaixuan19970627/yolov5_obb
Path: blob/master/utils/activations.py
Views: 475
1
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2
"""
3
Activation functions
4
"""
5
6
import torch
7
import torch.nn as nn
8
import torch.nn.functional as F
9
10
11
# SiLU https://arxiv.org/pdf/1606.08415.pdf ----------------------------------------------------------------------------
12
class SiLU(nn.Module): # export-friendly version of nn.SiLU()
13
@staticmethod
14
def forward(x):
15
return x * torch.sigmoid(x)
16
17
18
class Hardswish(nn.Module): # export-friendly version of nn.Hardswish()
19
@staticmethod
20
def forward(x):
21
# return x * F.hardsigmoid(x) # for TorchScript and CoreML
22
return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for TorchScript, CoreML and ONNX
23
24
25
# Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------
26
class Mish(nn.Module):
27
@staticmethod
28
def forward(x):
29
return x * F.softplus(x).tanh()
30
31
32
class MemoryEfficientMish(nn.Module):
33
class F(torch.autograd.Function):
34
@staticmethod
35
def forward(ctx, x):
36
ctx.save_for_backward(x)
37
return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))
38
39
@staticmethod
40
def backward(ctx, grad_output):
41
x = ctx.saved_tensors[0]
42
sx = torch.sigmoid(x)
43
fx = F.softplus(x).tanh()
44
return grad_output * (fx + x * sx * (1 - fx * fx))
45
46
def forward(self, x):
47
return self.F.apply(x)
48
49
50
# FReLU https://arxiv.org/abs/2007.11824 -------------------------------------------------------------------------------
51
class FReLU(nn.Module):
52
def __init__(self, c1, k=3): # ch_in, kernel
53
super().__init__()
54
self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False)
55
self.bn = nn.BatchNorm2d(c1)
56
57
def forward(self, x):
58
return torch.max(x, self.bn(self.conv(x)))
59
60
61
# ACON https://arxiv.org/pdf/2009.04759.pdf ----------------------------------------------------------------------------
62
class AconC(nn.Module):
63
r""" ACON activation (activate or not).
64
AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter
65
according to "Activate or Not: Learning Customized Activation" <https://arxiv.org/pdf/2009.04759.pdf>.
66
"""
67
68
def __init__(self, c1):
69
super().__init__()
70
self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
71
self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
72
self.beta = nn.Parameter(torch.ones(1, c1, 1, 1))
73
74
def forward(self, x):
75
dpx = (self.p1 - self.p2) * x
76
return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x
77
78
79
class MetaAconC(nn.Module):
80
r""" ACON activation (activate or not).
81
MetaAconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is generated by a small network
82
according to "Activate or Not: Learning Customized Activation" <https://arxiv.org/pdf/2009.04759.pdf>.
83
"""
84
85
def __init__(self, c1, k=1, s=1, r=16): # ch_in, kernel, stride, r
86
super().__init__()
87
c2 = max(r, c1 // r)
88
self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
89
self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
90
self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True)
91
self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True)
92
# self.bn1 = nn.BatchNorm2d(c2)
93
# self.bn2 = nn.BatchNorm2d(c1)
94
95
def forward(self, x):
96
y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True)
97
# batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/2891
98
# beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable
99
beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed
100
dpx = (self.p1 - self.p2) * x
101
return dpx * torch.sigmoid(beta * dpx) + self.p2 * x
102
103