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