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.

101 rinda
3.5KB

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