Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

157 rindas
5.8KB

  1. # This file contains modules common to various models
  2. import torch.nn.functional as F
  3. from utils.utils import *
  4. def DWConv(c1, c2, k=1, s=1, act=True): # depthwise convolution
  5. return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
  6. class Conv(nn.Module): # standard convolution
  7. def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
  8. super(Conv, self).__init__()
  9. self.conv = nn.Conv2d(c1, c2, k, s, k // 2, groups=g, bias=False)
  10. self.bn = nn.BatchNorm2d(c2)
  11. self.act = nn.LeakyReLU(0.1, inplace=True) if act else nn.Identity()
  12. def forward(self, x):
  13. return self.act(self.bn(self.conv(x)))
  14. class Bottleneck(nn.Module):
  15. def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
  16. super(Bottleneck, self).__init__()
  17. c_ = int(c2 * e) # hidden channels
  18. self.cv1 = Conv(c1, c_, 1, 1)
  19. self.cv2 = Conv(c_, c2, 3, 1, g=g)
  20. self.add = shortcut and c1 == c2
  21. def forward(self, x):
  22. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  23. class BottleneckLight(nn.Module):
  24. def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
  25. super(BottleneckLight, self).__init__()
  26. c_ = int(c2 * e) # hidden channels
  27. self.cv1 = Conv(c1, c_, 1, 1)
  28. self.cv2 = nn.Conv2d(c_, c2, 3, 1, 3 // 2, groups=g, bias=False)
  29. self.bn = nn.BatchNorm2d(c2)
  30. self.act = nn.LeakyReLU(0.1, inplace=True)
  31. self.add = shortcut and c1 == c2
  32. def forward(self, x):
  33. return self.act(self.bn(x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))))
  34. class BottleneckCSP(nn.Module):
  35. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  36. super(BottleneckCSP, self).__init__()
  37. c_ = int(c2 * e) # hidden channels
  38. self.cv1 = Conv(c1, c_, 1, 1)
  39. self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
  40. self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
  41. self.cv4 = Conv(c2, c2, 1, 1)
  42. self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
  43. self.act = nn.LeakyReLU(0.1, inplace=True)
  44. self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
  45. def forward(self, x):
  46. y1 = self.cv3(self.m(self.cv1(x)))
  47. y2 = self.cv2(x)
  48. return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
  49. class Narrow(nn.Module):
  50. def __init__(self, c1, c2, shortcut=True, g=1): # ch_in, ch_out, shortcut, groups
  51. super(Narrow, self).__init__()
  52. c_ = c2 // 2 # hidden channels
  53. self.cv1 = Conv(c1, c_, 1, 1)
  54. self.cv2 = Conv(c_, c2, 3, 1, g=g)
  55. self.add = shortcut and c1 == c2
  56. def forward(self, x):
  57. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  58. class Origami(nn.Module): # 5-side layering
  59. def forward(self, x):
  60. y = F.pad(x, [1, 1, 1, 1])
  61. return torch.cat([x, y[..., :-2, 1:-1], y[..., 1:-1, :-2], y[..., 2:, 1:-1], y[..., 1:-1, 2:]], 1)
  62. class ConvPlus(nn.Module): # standard convolution
  63. def __init__(self, c1, c2, k=3, s=1, g=1, bias=True): # ch_in, ch_out, kernel, stride, groups
  64. super(ConvPlus, self).__init__()
  65. self.cv1 = nn.Conv2d(c1, c2, (k, 1), s, (k // 2, 0), groups=g, bias=bias)
  66. self.cv2 = nn.Conv2d(c1, c2, (1, k), s, (0, k // 2), groups=g, bias=bias)
  67. def forward(self, x):
  68. return self.cv1(x) + self.cv2(x)
  69. class SPP(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP
  70. def __init__(self, c1, c2, k=(5, 9, 13)):
  71. super(SPP, self).__init__()
  72. c_ = c1 // 2 # hidden channels
  73. self.cv1 = Conv(c1, c_, 1, 1)
  74. self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
  75. self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
  76. def forward(self, x):
  77. x = self.cv1(x)
  78. return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
  79. class Flatten(nn.Module):
  80. # Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions
  81. def forward(self, x):
  82. return x.view(x.size(0), -1)
  83. class Focus(nn.Module):
  84. # Focus wh information into c-space
  85. def __init__(self, c1, c2, k=1):
  86. super(Focus, self).__init__()
  87. self.conv = Conv(c1 * 4, c2, k, 1)
  88. def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
  89. return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
  90. class Concat(nn.Module):
  91. # Concatenate a list of tensors along dimension
  92. def __init__(self, dimension=1):
  93. super(Concat, self).__init__()
  94. self.d = dimension
  95. def forward(self, x):
  96. return torch.cat(x, self.d)
  97. class MixConv2d(nn.Module):
  98. # Mixed Depthwise Conv https://arxiv.org/abs/1907.09595
  99. def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True):
  100. super(MixConv2d, self).__init__()
  101. groups = len(k)
  102. if equal_ch: # equal c_ per group
  103. i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices
  104. c_ = [(i == g).sum() for g in range(groups)] # intermediate channels
  105. else: # equal weight.numel() per group
  106. b = [c2] + [0] * groups
  107. a = np.eye(groups + 1, groups, k=-1)
  108. a -= np.roll(a, 1, axis=1)
  109. a *= np.array(k) ** 2
  110. a[0] = 1
  111. c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
  112. self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)])
  113. self.bn = nn.BatchNorm2d(c2)
  114. self.act = nn.LeakyReLU(0.1, inplace=True)
  115. def forward(self, x):
  116. return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))