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