无人机视角的行人小目标检测
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

activations.py 3.7KB

il y a 10 mois
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.0, 6.0) / 6.0 # 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