You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 satır
4.2KB

  1. # This file contains modules common to various models
  2. from utils.utils import *
  3. def autopad(k, p=None): # kernel, padding
  4. # Pad to 'same'
  5. if p is None:
  6. p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
  7. return p
  8. def DWConv(c1, c2, k=1, s=1, act=True):
  9. # Depthwise convolution
  10. return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
  11. class Conv(nn.Module):
  12. # Standard convolution
  13. def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
  14. super(Conv, self).__init__()
  15. self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
  16. self.bn = nn.BatchNorm2d(c2)
  17. self.act = nn.LeakyReLU(0.1, inplace=True) if act else nn.Identity()
  18. def forward(self, x):
  19. return self.act(self.bn(self.conv(x)))
  20. def fuseforward(self, x):
  21. return self.act(self.conv(x))
  22. class Bottleneck(nn.Module):
  23. # Standard bottleneck
  24. def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
  25. super(Bottleneck, self).__init__()
  26. c_ = int(c2 * e) # hidden channels
  27. self.cv1 = Conv(c1, c_, 1, 1)
  28. self.cv2 = Conv(c_, c2, 3, 1, g=g)
  29. self.add = shortcut and c1 == c2
  30. def forward(self, x):
  31. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  32. class BottleneckCSP(nn.Module):
  33. # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
  34. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  35. super(BottleneckCSP, self).__init__()
  36. c_ = int(c2 * e) # hidden channels
  37. self.cv1 = Conv(c1, c_, 1, 1)
  38. self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
  39. self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
  40. self.cv4 = Conv(2 * c_, c2, 1, 1)
  41. self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
  42. self.act = nn.LeakyReLU(0.1, inplace=True)
  43. self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
  44. def forward(self, x):
  45. y1 = self.cv3(self.m(self.cv1(x)))
  46. y2 = self.cv2(x)
  47. return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
  48. class SPP(nn.Module):
  49. # Spatial pyramid pooling layer used in YOLOv3-SPP
  50. def __init__(self, c1, c2, k=(5, 9, 13)):
  51. super(SPP, self).__init__()
  52. c_ = c1 // 2 # hidden channels
  53. self.cv1 = Conv(c1, c_, 1, 1)
  54. self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
  55. self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
  56. def forward(self, x):
  57. x = self.cv1(x)
  58. return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
  59. class Focus(nn.Module):
  60. # Focus wh information into c-space
  61. def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
  62. super(Focus, self).__init__()
  63. self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
  64. def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
  65. return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
  66. class Concat(nn.Module):
  67. # Concatenate a list of tensors along dimension
  68. def __init__(self, dimension=1):
  69. super(Concat, self).__init__()
  70. self.d = dimension
  71. def forward(self, x):
  72. return torch.cat(x, self.d)
  73. class Flatten(nn.Module):
  74. # Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions
  75. @staticmethod
  76. def forward(x):
  77. return x.view(x.size(0), -1)
  78. class Classify(nn.Module):
  79. # Classification head, i.e. x(b,c1,20,20) to x(b,c2)
  80. def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups
  81. super(Classify, self).__init__()
  82. self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1)
  83. self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) # to x(b,c2,1,1)
  84. self.flat = Flatten()
  85. def forward(self, x):
  86. z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list
  87. return self.flat(self.conv(z)) # flatten to x(b,c2)