Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

119 lines
4.2KB

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