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.

97 line
3.3KB

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