|
|
|
|
|
import numpy as np |
|
import torch |
|
import torch.nn as nn |
|
|
|
from models.common import Conv, DWConv |
|
from utils.google_utils import attempt_download |
|
|
|
|
|
class CrossConv(nn.Module): |
|
|
|
def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False): |
|
|
|
super(CrossConv, self).__init__() |
|
c_ = int(c2 * e) |
|
self.cv1 = Conv(c1, c_, (1, k), (1, s)) |
|
self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g) |
|
self.add = shortcut and c1 == c2 |
|
|
|
def forward(self, x): |
|
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) |
|
|
|
|
|
class Sum(nn.Module): |
|
|
|
def __init__(self, n, weight=False): |
|
super(Sum, self).__init__() |
|
self.weight = weight |
|
self.iter = range(n - 1) |
|
if weight: |
|
self.w = nn.Parameter(-torch.arange(1., n) / 2, requires_grad=True) |
|
|
|
def forward(self, x): |
|
y = x[0] |
|
if self.weight: |
|
w = torch.sigmoid(self.w) * 2 |
|
for i in self.iter: |
|
y = y + x[i + 1] * w[i] |
|
else: |
|
for i in self.iter: |
|
y = y + x[i + 1] |
|
return y |
|
|
|
|
|
class GhostConv(nn.Module): |
|
|
|
def __init__(self, c1, c2, k=1, s=1, g=1, act=True): |
|
super(GhostConv, self).__init__() |
|
c_ = c2 // 2 |
|
self.cv1 = Conv(c1, c_, k, s, None, g, act) |
|
self.cv2 = Conv(c_, c_, 5, 1, None, c_, act) |
|
|
|
def forward(self, x): |
|
y = self.cv1(x) |
|
return torch.cat([y, self.cv2(y)], 1) |
|
|
|
|
|
class GhostBottleneck(nn.Module): |
|
|
|
def __init__(self, c1, c2, k=3, s=1): |
|
super(GhostBottleneck, self).__init__() |
|
c_ = c2 // 2 |
|
self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), |
|
DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), |
|
GhostConv(c_, c2, 1, 1, act=False)) |
|
self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False), |
|
Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity() |
|
|
|
def forward(self, x): |
|
return self.conv(x) + self.shortcut(x) |
|
|
|
|
|
class MixConv2d(nn.Module): |
|
|
|
def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): |
|
super(MixConv2d, self).__init__() |
|
groups = len(k) |
|
if equal_ch: |
|
i = torch.linspace(0, groups - 1E-6, c2).floor() |
|
c_ = [(i == g).sum() for g in range(groups)] |
|
else: |
|
b = [c2] + [0] * groups |
|
a = np.eye(groups + 1, groups, k=-1) |
|
a -= np.roll(a, 1, axis=1) |
|
a *= np.array(k) ** 2 |
|
a[0] = 1 |
|
c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() |
|
|
|
self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)]) |
|
self.bn = nn.BatchNorm2d(c2) |
|
self.act = nn.LeakyReLU(0.1, inplace=True) |
|
|
|
def forward(self, x): |
|
return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1))) |
|
|
|
|
|
class Ensemble(nn.ModuleList): |
|
|
|
def __init__(self): |
|
super(Ensemble, self).__init__() |
|
|
|
def forward(self, x, augment=False): |
|
y = [] |
|
for module in self: |
|
y.append(module(x, augment)[0]) |
|
|
|
|
|
y = torch.cat(y, 1) |
|
return y, None |
|
|
|
|
|
def attempt_load(weights, map_location=None): |
|
|
|
model = Ensemble() |
|
for w in weights if isinstance(weights, list) else [weights]: |
|
attempt_download(w) |
|
model.append(torch.load(w, map_location=map_location)['model'].float().fuse().eval()) |
|
|
|
|
|
for m in model.modules(): |
|
if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]: |
|
m.inplace = True |
|
elif type(m) is Conv: |
|
m._non_persistent_buffers_set = set() |
|
|
|
if len(model) == 1: |
|
return model[-1] |
|
else: |
|
print('Ensemble created with %s\n' % weights) |
|
for k in ['names', 'stride']: |
|
setattr(model, k, getattr(model[-1], k)) |
|
return model |
|
|