|
from models.common import * |
|
|
|
|
|
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, g, act) |
|
self.cv2 = Conv(c_, c_, 5, 1, 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, s): |
|
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) |
|
|