experimental.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
  2. """
  3. Experimental modules
  4. """
  5. import math
  6. import numpy as np
  7. import torch
  8. import torch.nn as nn
  9. from utils.downloads import attempt_download
  10. class Sum(nn.Module):
  11. # Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070
  12. def __init__(self, n, weight=False): # n: number of inputs
  13. super().__init__()
  14. self.weight = weight # apply weights boolean
  15. self.iter = range(n - 1) # iter object
  16. if weight:
  17. self.w = nn.Parameter(-torch.arange(1.0, n) / 2, requires_grad=True) # layer weights
  18. def forward(self, x):
  19. y = x[0] # no weight
  20. if self.weight:
  21. w = torch.sigmoid(self.w) * 2
  22. for i in self.iter:
  23. y = y + x[i + 1] * w[i]
  24. else:
  25. for i in self.iter:
  26. y = y + x[i + 1]
  27. return y
  28. class MixConv2d(nn.Module):
  29. # Mixed Depth-wise Conv https://arxiv.org/abs/1907.09595
  30. def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): # ch_in, ch_out, kernel, stride, ch_strategy
  31. super().__init__()
  32. n = len(k) # number of convolutions
  33. if equal_ch: # equal c_ per group
  34. i = torch.linspace(0, n - 1E-6, c2).floor() # c2 indices
  35. c_ = [(i == g).sum() for g in range(n)] # intermediate channels
  36. else: # equal weight.numel() per group
  37. b = [c2] + [0] * n
  38. a = np.eye(n + 1, n, k=-1)
  39. a -= np.roll(a, 1, axis=1)
  40. a *= np.array(k) ** 2
  41. a[0] = 1
  42. c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
  43. self.m = nn.ModuleList([
  44. nn.Conv2d(c1, int(c_), k, s, k // 2, groups=math.gcd(c1, int(c_)), bias=False) for k, c_ in zip(k, c_)])
  45. self.bn = nn.BatchNorm2d(c2)
  46. self.act = nn.SiLU()
  47. def forward(self, x):
  48. return self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))
  49. class Ensemble(nn.ModuleList):
  50. # Ensemble of models
  51. def __init__(self):
  52. super().__init__()
  53. def forward(self, x, augment=False, profile=False, visualize=False):
  54. y = [module(x, augment, profile, visualize)[0] for module in self]
  55. # y = torch.stack(y).max(0)[0] # max ensemble
  56. # y = torch.stack(y).mean(0) # mean ensemble
  57. y = torch.cat(y, 1) # nms ensemble
  58. return y, None # inference, train output
  59. def attempt_load(weights, device=None, inplace=True, fuse=True):
  60. # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
  61. from models.yolo import Detect, Model
  62. model = Ensemble()
  63. for w in weights if isinstance(weights, list) else [weights]:
  64. ckpt = torch.load(attempt_download(w), map_location='cpu') # load
  65. ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model
  66. # Model compatibility updates
  67. if not hasattr(ckpt, 'stride'):
  68. ckpt.stride = torch.tensor([32.])
  69. if hasattr(ckpt, 'names') and isinstance(ckpt.names, (list, tuple)):
  70. ckpt.names = dict(enumerate(ckpt.names)) # convert to dict
  71. model.append(ckpt.fuse().eval() if fuse and hasattr(ckpt, 'fuse') else ckpt.eval()) # model in eval mode
  72. # Module compatibility updates
  73. for m in model.modules():
  74. t = type(m)
  75. if t in (nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model):
  76. m.inplace = inplace # torch 1.7.0 compatibility
  77. if t is Detect and not isinstance(m.anchor_grid, list):
  78. delattr(m, 'anchor_grid')
  79. setattr(m, 'anchor_grid', [torch.zeros(1)] * m.nl)
  80. elif t is nn.Upsample and not hasattr(m, 'recompute_scale_factor'):
  81. m.recompute_scale_factor = None # torch 1.11.0 compatibility
  82. # Return model
  83. if len(model) == 1:
  84. return model[-1]
  85. # Return detection ensemble
  86. print(f'Ensemble created with {weights}\n')
  87. for k in 'names', 'nc', 'yaml':
  88. setattr(model, k, getattr(model[0], k))
  89. model.stride = model[torch.argmax(torch.tensor([m.stride.max() for m in model])).int()].stride # max stride
  90. assert all(model[0].nc == m.nc for m in model), f'Models have different class counts: {[m.nc for m in model]}'
  91. return model