Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

activations.py 3.7 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...