No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

98 líneas
3.4KB

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