選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

175 行
6.6KB

  1. # This file contains modules common to various models
  2. import torch.nn.functional as F
  3. from utils.utils import *
  4. def DWConv(c1, c2, k=1, s=1, act=True): # depthwise convolution
  5. return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
  6. class Conv(nn.Module): # standard convolution
  7. def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
  8. super(Conv, self).__init__()
  9. self.conv = nn.Conv2d(c1, c2, k, s, k // 2, groups=g, bias=False)
  10. self.bn = nn.BatchNorm2d(c2)
  11. self.act = nn.LeakyReLU(0.1, inplace=True) if act else nn.Identity()
  12. def forward(self, x):
  13. return self.act(self.bn(self.conv(x)))
  14. class Bottleneck(nn.Module):
  15. def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
  16. super(Bottleneck, self).__init__()
  17. c_ = int(c2 * e) # hidden channels
  18. self.cv1 = Conv(c1, c_, 1, 1)
  19. self.cv2 = Conv(c_, c2, 3, 1, g=g)
  20. self.add = shortcut and c1 == c2
  21. def forward(self, x):
  22. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  23. class BottleneckLight(nn.Module):
  24. def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
  25. super(BottleneckLight, self).__init__()
  26. c_ = int(c2 * e) # hidden channels
  27. self.cv1 = Conv(c1, c_, 1, 1)
  28. self.cv2 = nn.Conv2d(c_, c2, 3, 1, 3 // 2, groups=g, bias=False)
  29. self.bn = nn.BatchNorm2d(c2)
  30. self.act = nn.LeakyReLU(0.1, inplace=True)
  31. self.add = shortcut and c1 == c2
  32. def forward(self, x):
  33. return self.act(self.bn(x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))))
  34. class BottleneckCSP(nn.Module):
  35. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  36. super(BottleneckCSP, self).__init__()
  37. c_ = int(c2 * e) # hidden channels
  38. self.cv1 = Conv(c1, c_, 1, 1)
  39. self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
  40. self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
  41. self.cv4 = Conv(c2, c2, 1, 1)
  42. self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
  43. self.act = nn.LeakyReLU(0.1, inplace=True)
  44. self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
  45. def forward(self, x):
  46. y1 = self.cv3(self.m(self.cv1(x)))
  47. y2 = self.cv2(x)
  48. return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
  49. class BottleneckCSPF(nn.Module):
  50. def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  51. super(BottleneckCSPF, self).__init__()
  52. c_ = int(c2 * e) # hidden channels
  53. self.cv1 = Conv(c1, c_, 1, 1)
  54. self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
  55. self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
  56. self.cv4 = Conv(c2, c2, 1, 1)
  57. self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
  58. self.act = nn.LeakyReLU(0.1, inplace=True)
  59. self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
  60. def forward(self, x):
  61. y1 = self.cv3(self.m(self.cv1(x)))
  62. y2 = self.cv2(x)
  63. return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
  64. class Narrow(nn.Module):
  65. def __init__(self, c1, c2, shortcut=True, g=1): # ch_in, ch_out, shortcut, groups
  66. super(Narrow, self).__init__()
  67. c_ = c2 // 2 # hidden channels
  68. self.cv1 = Conv(c1, c_, 1, 1)
  69. self.cv2 = Conv(c_, c2, 3, 1, g=g)
  70. self.add = shortcut and c1 == c2
  71. def forward(self, x):
  72. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  73. class Origami(nn.Module): # 5-side layering
  74. def forward(self, x):
  75. y = F.pad(x, [1, 1, 1, 1])
  76. return torch.cat([x, y[..., :-2, 1:-1], y[..., 1:-1, :-2], y[..., 2:, 1:-1], y[..., 1:-1, 2:]], 1)
  77. class ConvPlus(nn.Module): # standard convolution
  78. def __init__(self, c1, c2, k=3, s=1, g=1, bias=True): # ch_in, ch_out, kernel, stride, groups
  79. super(ConvPlus, self).__init__()
  80. self.cv1 = nn.Conv2d(c1, c2, (k, 1), s, (k // 2, 0), groups=g, bias=bias)
  81. self.cv2 = nn.Conv2d(c1, c2, (1, k), s, (0, k // 2), groups=g, bias=bias)
  82. def forward(self, x):
  83. return self.cv1(x) + self.cv2(x)
  84. class SPP(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP
  85. def __init__(self, c1, c2, k=(5, 9, 13)):
  86. super(SPP, self).__init__()
  87. c_ = c1 // 2 # hidden channels
  88. self.cv1 = Conv(c1, c_, 1, 1)
  89. self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
  90. self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
  91. def forward(self, x):
  92. x = self.cv1(x)
  93. return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
  94. class Flatten(nn.Module):
  95. # Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions
  96. def forward(self, x):
  97. return x.view(x.size(0), -1)
  98. class Focus(nn.Module):
  99. # Focus wh information into c-space
  100. def __init__(self, c1, c2, k=1):
  101. super(Focus, self).__init__()
  102. self.conv = Conv(c1 * 4, c2, k, 1)
  103. def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
  104. return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
  105. class Concat(nn.Module):
  106. # Concatenate a list of tensors along dimension
  107. def __init__(self, dimension=1):
  108. super(Concat, self).__init__()
  109. self.d = dimension
  110. def forward(self, x):
  111. return torch.cat(x, self.d)
  112. class MixConv2d(nn.Module):
  113. # Mixed Depthwise Conv https://arxiv.org/abs/1907.09595
  114. def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True):
  115. super(MixConv2d, self).__init__()
  116. groups = len(k)
  117. if equal_ch: # equal c_ per group
  118. i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices
  119. c_ = [(i == g).sum() for g in range(groups)] # intermediate channels
  120. else: # equal weight.numel() per group
  121. b = [c2] + [0] * groups
  122. a = np.eye(groups + 1, groups, k=-1)
  123. a -= np.roll(a, 1, axis=1)
  124. a *= np.array(k) ** 2
  125. a[0] = 1
  126. c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
  127. self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)])
  128. self.bn = nn.BatchNorm2d(c2)
  129. self.act = nn.LeakyReLU(0.1, inplace=True)
  130. def forward(self, x):
  131. return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))