|
|
|
|
|
import numpy as np |
|
import torch |
|
from torch import nn |
|
|
|
from facelib.detection.yolov5face.models.common import Conv |
|
|
|
|
|
class CrossConv(nn.Module): |
|
|
|
def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False): |
|
|
|
super().__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 MixConv2d(nn.Module): |
|
|
|
def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): |
|
super().__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))) |
|
|