ethanrom/helm
0
1# YOLOv5 ๐ by Ultralytics, GPL-3.0 license2"""3Experimental modules4"""5import math6 7import numpy as np8import torch9import torch.nn as nn10 11from utils.downloads import attempt_download12 13 14class Sum(nn.Module):15 # Weighted sum of 2 or more layers https://arxiv.org/abs/1911.0907016 def __init__(self, n, weight=False): # n: number of inputs17 super().__init__()18 self.weight = weight # apply weights boolean19 self.iter = range(n - 1) # iter object20 if weight:21 self.w = nn.Parameter(-torch.arange(1.0, n) / 2, requires_grad=True) # layer weights22 23 def forward(self, x):24 y = x[0] # no weight25 if self.weight:26 w = torch.sigmoid(self.w) * 227 for i in self.iter:28 y = y + x[i + 1] * w[i]29 else:30 for i in self.iter:31 y = y + x[i + 1]32 return y33 34 35class MixConv2d(nn.Module):36 # Mixed Depth-wise Conv https://arxiv.org/abs/1907.0959537 def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): # ch_in, ch_out, kernel, stride, ch_strategy38 super().__init__()39 n = len(k) # number of convolutions40 if equal_ch: # equal c_ per group41 i = torch.linspace(0, n - 1E-6, c2).floor() # c2 indices42 c_ = [(i == g).sum() for g in range(n)] # intermediate channels43 else: # equal weight.numel() per group44 b = [c2] + [0] * n45 a = np.eye(n + 1, n, k=-1)46 a -= np.roll(a, 1, axis=1)47 a *= np.array(k) ** 248 a[0] = 149 c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b50 51 self.m = nn.ModuleList([52 nn.Conv2d(c1, int(c_), k, s, k // 2, groups=math.gcd(c1, int(c_)), bias=False) for k, c_ in zip(k, c_)])53 self.bn = nn.BatchNorm2d(c2)54 self.act = nn.SiLU()55 56 def forward(self, x):57 return self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))58 59 60class Ensemble(nn.ModuleList):61 # Ensemble of models62 def __init__(self):63 super().__init__()64 65 def forward(self, x, augment=False, profile=False, visualize=False):66 y = [module(x, augment, profile, visualize)[0] for module in self]67 # y = torch.stack(y).max(0)[0] # max ensemble68 # y = torch.stack(y).mean(0) # mean ensemble69 y = torch.cat(y, 1) # nms ensemble70 return y, None # inference, train output71 72 73def attempt_load(weights, device=None, inplace=True, fuse=True):74 # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a75 from models.yolo import Detect, Model76 77 model = Ensemble()78 for w in weights if isinstance(weights, list) else [weights]:79 ckpt = torch.load(attempt_download(w), map_location='cpu') # load80 ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model81 82 # Model compatibility updates83 if not hasattr(ckpt, 'stride'):84 ckpt.stride = torch.tensor([32.])85 if hasattr(ckpt, 'names') and isinstance(ckpt.names, (list, tuple)):86 ckpt.names = dict(enumerate(ckpt.names)) # convert to dict87 88 model.append(ckpt.fuse().eval() if fuse and hasattr(ckpt, 'fuse') else ckpt.eval()) # model in eval mode89 90 # Module compatibility updates91 for m in model.modules():92 t = type(m)93 if t in (nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model):94 m.inplace = inplace # torch 1.7.0 compatibility95 if t is Detect and not isinstance(m.anchor_grid, list):96 delattr(m, 'anchor_grid')97 setattr(m, 'anchor_grid', [torch.zeros(1)] * m.nl)98 elif t is nn.Upsample and not hasattr(m, 'recompute_scale_factor'):99 m.recompute_scale_factor = None # torch 1.11.0 compatibility100 101 # Return model102 if len(model) == 1:103 return model[-1]104 105 # Return detection ensemble106 print(f'Ensemble created with {weights}\n')107 for k in 'names', 'nc', 'yaml':108 setattr(model, k, getattr(model[0], k))109 model.stride = model[torch.argmax(torch.tensor([m.stride.max() for m in model])).int()].stride # max stride110 assert all(model[0].nc == m.nc for m in model), f'Models have different class counts: {[m.nc for m in model]}'111 return model112 