You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.1 KiB
89 lines
3.1 KiB
5 years ago
|
""" Conv2d + BN + Act
|
||
|
|
||
4 years ago
|
Hacked together by / Copyright 2020 Ross Wightman
|
||
5 years ago
|
"""
|
||
3 years ago
|
import functools
|
||
5 years ago
|
from torch import nn as nn
|
||
|
|
||
5 years ago
|
from .create_conv2d import create_conv2d
|
||
3 years ago
|
from .create_norm_act import get_norm_act_layer
|
||
5 years ago
|
|
||
|
|
||
3 years ago
|
class ConvNormAct(nn.Module):
|
||
|
def __init__(
|
||
|
self, in_channels, out_channels, kernel_size=1, stride=1, padding='', dilation=1, groups=1,
|
||
|
bias=False, apply_act=True, norm_layer=nn.BatchNorm2d, act_layer=nn.ReLU, drop_layer=None):
|
||
|
super(ConvNormAct, self).__init__()
|
||
|
self.conv = create_conv2d(
|
||
|
in_channels, out_channels, kernel_size, stride=stride,
|
||
|
padding=padding, dilation=dilation, groups=groups, bias=bias)
|
||
|
|
||
|
# NOTE for backwards compatibility with models that use separate norm and act layer definitions
|
||
|
norm_act_layer = get_norm_act_layer(norm_layer, act_layer)
|
||
|
# NOTE for backwards (weight) compatibility, norm layer name remains `.bn`
|
||
|
norm_kwargs = dict(drop_layer=drop_layer) if drop_layer is not None else {}
|
||
|
self.bn = norm_act_layer(out_channels, apply_act=apply_act, **norm_kwargs)
|
||
|
|
||
|
@property
|
||
|
def in_channels(self):
|
||
|
return self.conv.in_channels
|
||
|
|
||
|
@property
|
||
|
def out_channels(self):
|
||
|
return self.conv.out_channels
|
||
|
|
||
|
def forward(self, x):
|
||
|
x = self.conv(x)
|
||
|
x = self.bn(x)
|
||
|
return x
|
||
|
|
||
|
|
||
|
ConvBnAct = ConvNormAct
|
||
|
|
||
|
|
||
3 years ago
|
def create_aa(aa_layer, channels, stride=2, enable=True):
|
||
|
if not aa_layer or not enable:
|
||
|
return nn.Identity()
|
||
|
if isinstance(aa_layer, functools.partial):
|
||
|
if issubclass(aa_layer.func, nn.AvgPool2d):
|
||
|
return aa_layer()
|
||
|
else:
|
||
|
return aa_layer(channels)
|
||
|
elif issubclass(aa_layer, nn.AvgPool2d):
|
||
|
return aa_layer(stride)
|
||
|
else:
|
||
|
return aa_layer(channels=channels, stride=stride)
|
||
|
|
||
|
|
||
3 years ago
|
class ConvNormActAa(nn.Module):
|
||
|
def __init__(
|
||
|
self, in_channels, out_channels, kernel_size=1, stride=1, padding='', dilation=1, groups=1,
|
||
|
bias=False, apply_act=True, norm_layer=nn.BatchNorm2d, act_layer=nn.ReLU, aa_layer=None, drop_layer=None):
|
||
|
super(ConvNormActAa, self).__init__()
|
||
3 years ago
|
use_aa = aa_layer is not None and stride == 2
|
||
4 years ago
|
|
||
5 years ago
|
self.conv = create_conv2d(
|
||
|
in_channels, out_channels, kernel_size, stride=1 if use_aa else stride,
|
||
4 years ago
|
padding=padding, dilation=dilation, groups=groups, bias=bias)
|
||
5 years ago
|
|
||
|
# NOTE for backwards compatibility with models that use separate norm and act layer definitions
|
||
3 years ago
|
norm_act_layer = get_norm_act_layer(norm_layer, act_layer)
|
||
|
# NOTE for backwards (weight) compatibility, norm layer name remains `.bn`
|
||
|
norm_kwargs = dict(drop_layer=drop_layer) if drop_layer is not None else {}
|
||
|
self.bn = norm_act_layer(out_channels, apply_act=apply_act, **norm_kwargs)
|
||
3 years ago
|
self.aa = create_aa(aa_layer, out_channels, stride=stride, enable=use_aa)
|
||
5 years ago
|
|
||
4 years ago
|
@property
|
||
|
def in_channels(self):
|
||
|
return self.conv.in_channels
|
||
|
|
||
|
@property
|
||
|
def out_channels(self):
|
||
|
return self.conv.out_channels
|
||
|
|
||
5 years ago
|
def forward(self, x):
|
||
|
x = self.conv(x)
|
||
|
x = self.bn(x)
|
||
3 years ago
|
x = self.aa(x)
|
||
5 years ago
|
return x
|