activations.py 3.6 KB

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