|
|
@ -4,7 +4,7 @@ from torch import nn as nn
|
|
|
|
|
|
|
|
|
|
|
|
from timm.models.registry import register_model
|
|
|
|
from timm.models.registry import register_model
|
|
|
|
from timm.models.helpers import load_pretrained
|
|
|
|
from timm.models.helpers import load_pretrained
|
|
|
|
from timm.models.conv2d_layers import SelectiveKernelConv
|
|
|
|
from timm.models.conv2d_layers import SelectiveKernelConv, ConvBnAct
|
|
|
|
from timm.models.resnet import ResNet, SEModule
|
|
|
|
from timm.models.resnet import ResNet, SEModule
|
|
|
|
from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
|
|
|
|
from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
|
|
|
|
|
|
|
|
|
|
|
@ -29,61 +29,53 @@ default_cfgs = {
|
|
|
|
class SelectiveKernelBasic(nn.Module):
|
|
|
|
class SelectiveKernelBasic(nn.Module):
|
|
|
|
expansion = 1
|
|
|
|
expansion = 1
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, inplanes, planes, stride=1, downsample=None,
|
|
|
|
def __init__(self, inplanes, planes, stride=1, downsample=None, cardinality=1, base_width=64,
|
|
|
|
cardinality=1, base_width=64, use_se=False, sk_kwargs=None,
|
|
|
|
use_se=False, sk_kwargs=None, reduce_first=1, dilation=1, first_dilation=None,
|
|
|
|
reduce_first=1, dilation=1, first_dilation=None, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d):
|
|
|
|
drop_block=None, drop_path=None, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d):
|
|
|
|
super(SelectiveKernelBasic, self).__init__()
|
|
|
|
super(SelectiveKernelBasic, self).__init__()
|
|
|
|
|
|
|
|
|
|
|
|
sk_kwargs = sk_kwargs or {}
|
|
|
|
sk_kwargs = sk_kwargs or {}
|
|
|
|
|
|
|
|
conv_kwargs = dict(drop_block=drop_block, act_layer=act_layer, norm_layer=norm_layer)
|
|
|
|
assert cardinality == 1, 'BasicBlock only supports cardinality of 1'
|
|
|
|
assert cardinality == 1, 'BasicBlock only supports cardinality of 1'
|
|
|
|
assert base_width == 64, 'BasicBlock doest not support changing base width'
|
|
|
|
assert base_width == 64, 'BasicBlock doest not support changing base width'
|
|
|
|
first_planes = planes // reduce_first
|
|
|
|
first_planes = planes // reduce_first
|
|
|
|
outplanes = planes * self.expansion
|
|
|
|
out_planes = planes * self.expansion
|
|
|
|
first_dilation = first_dilation or dilation
|
|
|
|
first_dilation = first_dilation or dilation
|
|
|
|
|
|
|
|
|
|
|
|
_selective_first = True # FIXME temporary, for experiments
|
|
|
|
_selective_first = True # FIXME temporary, for experiments
|
|
|
|
if _selective_first:
|
|
|
|
if _selective_first:
|
|
|
|
self.conv1 = SelectiveKernelConv(
|
|
|
|
self.conv1 = SelectiveKernelConv(
|
|
|
|
inplanes, first_planes, stride=stride, dilation=first_dilation, **sk_kwargs)
|
|
|
|
inplanes, first_planes, stride=stride, dilation=first_dilation, **conv_kwargs, **sk_kwargs)
|
|
|
|
else:
|
|
|
|
conv_kwargs['act_layer'] = None
|
|
|
|
self.conv1 = nn.Conv2d(
|
|
|
|
self.conv2 = ConvBnAct(
|
|
|
|
inplanes, first_planes, kernel_size=3, stride=stride, padding=first_dilation,
|
|
|
|
first_planes, out_planes, kernel_size=3, dilation=dilation, **conv_kwargs)
|
|
|
|
dilation=first_dilation, bias=False)
|
|
|
|
|
|
|
|
self.bn1 = norm_layer(first_planes)
|
|
|
|
|
|
|
|
self.act1 = act_layer(inplace=True)
|
|
|
|
|
|
|
|
if _selective_first:
|
|
|
|
|
|
|
|
self.conv2 = nn.Conv2d(
|
|
|
|
|
|
|
|
first_planes, outplanes, kernel_size=3, padding=dilation,
|
|
|
|
|
|
|
|
dilation=dilation, bias=False)
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.conv1 = ConvBnAct(
|
|
|
|
|
|
|
|
inplanes, first_planes, kernel_size=3, stride=stride, dilation=first_dilation, **conv_kwargs)
|
|
|
|
|
|
|
|
conv_kwargs['act_layer'] = None
|
|
|
|
self.conv2 = SelectiveKernelConv(
|
|
|
|
self.conv2 = SelectiveKernelConv(
|
|
|
|
first_planes, outplanes, dilation=dilation, **sk_kwargs)
|
|
|
|
first_planes, out_planes, dilation=dilation, **conv_kwargs, **sk_kwargs)
|
|
|
|
self.bn2 = norm_layer(outplanes)
|
|
|
|
self.se = SEModule(out_planes, planes // 4) if use_se else None
|
|
|
|
self.se = SEModule(outplanes, planes // 4) if use_se else None
|
|
|
|
self.act = act_layer(inplace=True)
|
|
|
|
self.act2 = act_layer(inplace=True)
|
|
|
|
|
|
|
|
self.downsample = downsample
|
|
|
|
self.downsample = downsample
|
|
|
|
self.stride = stride
|
|
|
|
self.stride = stride
|
|
|
|
self.dilation = dilation
|
|
|
|
self.dilation = dilation
|
|
|
|
|
|
|
|
self.drop_block = drop_block
|
|
|
|
|
|
|
|
self.drop_path = drop_path
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
def forward(self, x):
|
|
|
|
residual = x
|
|
|
|
residual = x
|
|
|
|
|
|
|
|
x = self.conv1(x)
|
|
|
|
out = self.conv1(x)
|
|
|
|
x = self.conv2(x)
|
|
|
|
out = self.bn1(out)
|
|
|
|
|
|
|
|
out = self.act1(out)
|
|
|
|
|
|
|
|
out = self.conv2(out)
|
|
|
|
|
|
|
|
out = self.bn2(out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.se is not None:
|
|
|
|
if self.se is not None:
|
|
|
|
out = self.se(out)
|
|
|
|
x = self.se(x)
|
|
|
|
|
|
|
|
if self.drop_path is not None:
|
|
|
|
|
|
|
|
x = self.drop_path(x)
|
|
|
|
if self.downsample is not None:
|
|
|
|
if self.downsample is not None:
|
|
|
|
residual = self.downsample(x)
|
|
|
|
residual = self.downsample(residual)
|
|
|
|
|
|
|
|
x += residual
|
|
|
|
out += residual
|
|
|
|
x = self.act(x)
|
|
|
|
out = self.act2(out)
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SelectiveKernelBottleneck(nn.Module):
|
|
|
|
class SelectiveKernelBottleneck(nn.Module):
|
|
|
@ -91,54 +83,46 @@ class SelectiveKernelBottleneck(nn.Module):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, inplanes, planes, stride=1, downsample=None,
|
|
|
|
def __init__(self, inplanes, planes, stride=1, downsample=None,
|
|
|
|
cardinality=1, base_width=64, use_se=False, sk_kwargs=None,
|
|
|
|
cardinality=1, base_width=64, use_se=False, sk_kwargs=None,
|
|
|
|
reduce_first=1, dilation=1, first_dilation=None, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d):
|
|
|
|
reduce_first=1, dilation=1, first_dilation=None,
|
|
|
|
|
|
|
|
drop_block=None, drop_path=None,
|
|
|
|
|
|
|
|
act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d):
|
|
|
|
super(SelectiveKernelBottleneck, self).__init__()
|
|
|
|
super(SelectiveKernelBottleneck, self).__init__()
|
|
|
|
|
|
|
|
|
|
|
|
sk_kwargs = sk_kwargs or {}
|
|
|
|
sk_kwargs = sk_kwargs or {}
|
|
|
|
|
|
|
|
conv_kwargs = dict(drop_block=drop_block, act_layer=act_layer, norm_layer=norm_layer)
|
|
|
|
width = int(math.floor(planes * (base_width / 64)) * cardinality)
|
|
|
|
width = int(math.floor(planes * (base_width / 64)) * cardinality)
|
|
|
|
first_planes = width // reduce_first
|
|
|
|
first_planes = width // reduce_first
|
|
|
|
outplanes = planes * self.expansion
|
|
|
|
out_planes = planes * self.expansion
|
|
|
|
first_dilation = first_dilation or dilation
|
|
|
|
first_dilation = first_dilation or dilation
|
|
|
|
|
|
|
|
|
|
|
|
self.conv1 = nn.Conv2d(inplanes, first_planes, kernel_size=1, bias=False)
|
|
|
|
self.conv1 = ConvBnAct(inplanes, first_planes, kernel_size=1, **conv_kwargs)
|
|
|
|
self.bn1 = norm_layer(first_planes)
|
|
|
|
|
|
|
|
self.act1 = act_layer(inplace=True)
|
|
|
|
|
|
|
|
self.conv2 = SelectiveKernelConv(
|
|
|
|
self.conv2 = SelectiveKernelConv(
|
|
|
|
first_planes, width, stride=stride, dilation=first_dilation, groups=cardinality, **sk_kwargs)
|
|
|
|
first_planes, width, stride=stride, dilation=first_dilation, groups=cardinality,
|
|
|
|
self.bn2 = norm_layer(width)
|
|
|
|
**conv_kwargs, **sk_kwargs)
|
|
|
|
self.act2 = act_layer(inplace=True)
|
|
|
|
conv_kwargs['act_layer'] = None
|
|
|
|
self.conv3 = nn.Conv2d(width, outplanes, kernel_size=1, bias=False)
|
|
|
|
self.conv3 = ConvBnAct(width, out_planes, kernel_size=1, **conv_kwargs)
|
|
|
|
self.bn3 = norm_layer(outplanes)
|
|
|
|
self.se = SEModule(out_planes, planes // 4) if use_se else None
|
|
|
|
self.se = SEModule(outplanes, planes // 4) if use_se else None
|
|
|
|
self.act = act_layer(inplace=True)
|
|
|
|
self.act3 = act_layer(inplace=True)
|
|
|
|
|
|
|
|
self.downsample = downsample
|
|
|
|
self.downsample = downsample
|
|
|
|
self.stride = stride
|
|
|
|
self.stride = stride
|
|
|
|
self.dilation = dilation
|
|
|
|
self.dilation = dilation
|
|
|
|
|
|
|
|
self.drop_block = drop_block
|
|
|
|
|
|
|
|
self.drop_path = drop_path
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
def forward(self, x):
|
|
|
|
residual = x
|
|
|
|
residual = x
|
|
|
|
|
|
|
|
x = self.conv1(x)
|
|
|
|
out = self.conv1(x)
|
|
|
|
x = self.conv2(x)
|
|
|
|
out = self.bn1(out)
|
|
|
|
x = self.conv3(x)
|
|
|
|
out = self.act1(out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out = self.conv2(out)
|
|
|
|
|
|
|
|
out = self.bn2(out)
|
|
|
|
|
|
|
|
out = self.act2(out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out = self.conv3(out)
|
|
|
|
|
|
|
|
out = self.bn3(out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.se is not None:
|
|
|
|
if self.se is not None:
|
|
|
|
out = self.se(out)
|
|
|
|
x = self.se(x)
|
|
|
|
|
|
|
|
if self.drop_path is not None:
|
|
|
|
|
|
|
|
x = self.drop_path(x)
|
|
|
|
if self.downsample is not None:
|
|
|
|
if self.downsample is not None:
|
|
|
|
residual = self.downsample(x)
|
|
|
|
residual = self.downsample(residual)
|
|
|
|
|
|
|
|
x += residual
|
|
|
|
out += residual
|
|
|
|
x = self.act(x)
|
|
|
|
out = self.act3(out)
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@register_model
|
|
|
|
@register_model
|
|
|
|