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.

153 lines
5.7KB

  1. # This file contains experimental modules
  2. import numpy as np
  3. import torch
  4. import torch.nn as nn
  5. from models.common import Conv, DWConv
  6. from utils.google_utils import attempt_download
  7. class CrossConv(nn.Module):
  8. # Cross Convolution Downsample
  9. def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False):
  10. # ch_in, ch_out, kernel, stride, groups, expansion, shortcut
  11. super(CrossConv, self).__init__()
  12. c_ = int(c2 * e) # hidden channels
  13. self.cv1 = Conv(c1, c_, (1, k), (1, s))
  14. self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g)
  15. self.add = shortcut and c1 == c2
  16. def forward(self, x):
  17. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  18. class C3(nn.Module):
  19. # Cross Convolution CSP
  20. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  21. super(C3, self).__init__()
  22. c_ = int(c2 * e) # hidden channels
  23. self.cv1 = Conv(c1, c_, 1, 1)
  24. self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
  25. self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
  26. self.cv4 = Conv(2 * c_, c2, 1, 1)
  27. self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
  28. self.act = nn.LeakyReLU(0.1, inplace=True)
  29. self.m = nn.Sequential(*[CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)])
  30. def forward(self, x):
  31. y1 = self.cv3(self.m(self.cv1(x)))
  32. y2 = self.cv2(x)
  33. return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
  34. class Sum(nn.Module):
  35. # Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070
  36. def __init__(self, n, weight=False): # n: number of inputs
  37. super(Sum, self).__init__()
  38. self.weight = weight # apply weights boolean
  39. self.iter = range(n - 1) # iter object
  40. if weight:
  41. self.w = nn.Parameter(-torch.arange(1., n) / 2, requires_grad=True) # layer weights
  42. def forward(self, x):
  43. y = x[0] # no weight
  44. if self.weight:
  45. w = torch.sigmoid(self.w) * 2
  46. for i in self.iter:
  47. y = y + x[i + 1] * w[i]
  48. else:
  49. for i in self.iter:
  50. y = y + x[i + 1]
  51. return y
  52. class GhostConv(nn.Module):
  53. # Ghost Convolution https://github.com/huawei-noah/ghostnet
  54. def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
  55. super(GhostConv, self).__init__()
  56. c_ = c2 // 2 # hidden channels
  57. self.cv1 = Conv(c1, c_, k, s, None, g, act)
  58. self.cv2 = Conv(c_, c_, 5, 1, None, c_, act)
  59. def forward(self, x):
  60. y = self.cv1(x)
  61. return torch.cat([y, self.cv2(y)], 1)
  62. class GhostBottleneck(nn.Module):
  63. # Ghost Bottleneck https://github.com/huawei-noah/ghostnet
  64. def __init__(self, c1, c2, k, s):
  65. super(GhostBottleneck, self).__init__()
  66. c_ = c2 // 2
  67. self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw
  68. DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw
  69. GhostConv(c_, c2, 1, 1, act=False)) # pw-linear
  70. self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False),
  71. Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity()
  72. def forward(self, x):
  73. return self.conv(x) + self.shortcut(x)
  74. class MixConv2d(nn.Module):
  75. # Mixed Depthwise Conv https://arxiv.org/abs/1907.09595
  76. def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True):
  77. super(MixConv2d, self).__init__()
  78. groups = len(k)
  79. if equal_ch: # equal c_ per group
  80. i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices
  81. c_ = [(i == g).sum() for g in range(groups)] # intermediate channels
  82. else: # equal weight.numel() per group
  83. b = [c2] + [0] * groups
  84. a = np.eye(groups + 1, groups, k=-1)
  85. a -= np.roll(a, 1, axis=1)
  86. a *= np.array(k) ** 2
  87. a[0] = 1
  88. c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
  89. self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)])
  90. self.bn = nn.BatchNorm2d(c2)
  91. self.act = nn.LeakyReLU(0.1, inplace=True)
  92. def forward(self, x):
  93. return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))
  94. class Ensemble(nn.ModuleList):
  95. # Ensemble of models
  96. def __init__(self):
  97. super(Ensemble, self).__init__()
  98. def forward(self, x, augment=False):
  99. y = []
  100. for module in self:
  101. y.append(module(x, augment)[0])
  102. # y = torch.stack(y).max(0)[0] # max ensemble
  103. # y = torch.cat(y, 1) # nms ensemble
  104. y = torch.stack(y).mean(0) # mean ensemble
  105. return y, None # inference, train output
  106. def attempt_load(weights, map_location=None):
  107. # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
  108. model = Ensemble()
  109. for w in weights if isinstance(weights, list) else [weights]:
  110. attempt_download(w)
  111. model.append(torch.load(w, map_location=map_location)['model'].float().fuse().eval()) # load FP32 model
  112. # Compatibility updates
  113. for m in model.modules():
  114. if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6]:
  115. m.inplace = True # pytorch 1.7.0 compatibility
  116. elif type(m) is Conv:
  117. m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility
  118. if len(model) == 1:
  119. return model[-1] # return model
  120. else:
  121. print('Ensemble created with %s\n' % weights)
  122. for k in ['names', 'stride']:
  123. setattr(model, k, getattr(model[-1], k))
  124. return model # return ensemble