2020-02-10 06:46:28 +08:00
|
|
|
""" Conv2d + BN + Act
|
|
|
|
|
2020-07-28 04:44:56 +08:00
|
|
|
Hacked together by / Copyright 2020 Ross Wightman
|
2020-02-10 06:46:28 +08:00
|
|
|
"""
|
2024-05-28 13:06:22 +08:00
|
|
|
from typing import Any, Dict, Optional, Type
|
|
|
|
|
2020-02-10 06:46:28 +08:00
|
|
|
from torch import nn as nn
|
|
|
|
|
2024-05-28 13:06:22 +08:00
|
|
|
from .typing import LayerType, PadType
|
|
|
|
from .blur_pool import create_aa
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
2020-06-02 07:59:51 +08:00
|
|
|
from .create_conv2d import create_conv2d
|
2021-12-15 05:48:30 +08:00
|
|
|
from .create_norm_act import get_norm_act_layer
|
2020-02-10 06:46:28 +08:00
|
|
|
|
|
|
|
|
2021-12-15 05:48:30 +08:00
|
|
|
class ConvNormAct(nn.Module):
|
|
|
|
def __init__(
|
2023-04-27 06:52:13 +08:00
|
|
|
self,
|
2024-05-28 13:06:22 +08:00
|
|
|
in_channels: int,
|
|
|
|
out_channels: int,
|
|
|
|
kernel_size: int = 1,
|
|
|
|
stride: int = 1,
|
|
|
|
padding: PadType = '',
|
|
|
|
dilation: int = 1,
|
|
|
|
groups: int = 1,
|
|
|
|
bias: bool = False,
|
|
|
|
apply_act: bool = True,
|
|
|
|
norm_layer: LayerType = nn.BatchNorm2d,
|
|
|
|
act_layer: LayerType = nn.ReLU,
|
|
|
|
drop_layer: Optional[Type[nn.Module]] = None,
|
|
|
|
conv_kwargs: Optional[Dict[str, Any]] = None,
|
|
|
|
norm_kwargs: Optional[Dict[str, Any]] = None,
|
|
|
|
act_kwargs: Optional[Dict[str, Any]] = None,
|
2023-04-27 06:52:13 +08:00
|
|
|
):
|
2021-12-15 05:48:30 +08:00
|
|
|
super(ConvNormAct, self).__init__()
|
2024-05-28 13:06:22 +08:00
|
|
|
conv_kwargs = conv_kwargs or {}
|
2023-04-27 06:52:13 +08:00
|
|
|
norm_kwargs = norm_kwargs or {}
|
|
|
|
act_kwargs = act_kwargs or {}
|
|
|
|
|
2021-12-15 05:48:30 +08:00
|
|
|
self.conv = create_conv2d(
|
2024-05-28 13:06:22 +08:00
|
|
|
in_channels,
|
|
|
|
out_channels,
|
|
|
|
kernel_size,
|
|
|
|
stride=stride,
|
|
|
|
padding=padding,
|
|
|
|
dilation=dilation,
|
|
|
|
groups=groups,
|
|
|
|
bias=bias,
|
|
|
|
**conv_kwargs,
|
|
|
|
)
|
2021-12-15 05:48:30 +08:00
|
|
|
|
|
|
|
# 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`
|
2023-04-27 06:52:13 +08:00
|
|
|
if drop_layer:
|
|
|
|
norm_kwargs['drop_layer'] = drop_layer
|
|
|
|
self.bn = norm_act_layer(
|
|
|
|
out_channels,
|
|
|
|
apply_act=apply_act,
|
|
|
|
act_kwargs=act_kwargs,
|
|
|
|
**norm_kwargs,
|
|
|
|
)
|
2021-12-15 05:48:30 +08:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
|
|
|
|
class ConvNormActAa(nn.Module):
|
|
|
|
def __init__(
|
2023-04-27 06:52:13 +08:00
|
|
|
self,
|
2024-05-28 13:06:22 +08:00
|
|
|
in_channels: int,
|
|
|
|
out_channels: int,
|
|
|
|
kernel_size: int = 1,
|
|
|
|
stride: int = 1,
|
|
|
|
padding: PadType = '',
|
|
|
|
dilation: int = 1,
|
|
|
|
groups: int = 1,
|
|
|
|
bias: bool = False,
|
|
|
|
apply_act: bool = True,
|
|
|
|
norm_layer: LayerType = nn.BatchNorm2d,
|
|
|
|
act_layer: LayerType = nn.ReLU,
|
|
|
|
aa_layer: Optional[LayerType] = None,
|
|
|
|
drop_layer: Optional[Type[nn.Module]] = None,
|
|
|
|
conv_kwargs: Optional[Dict[str, Any]] = None,
|
|
|
|
norm_kwargs: Optional[Dict[str, Any]] = None,
|
|
|
|
act_kwargs: Optional[Dict[str, Any]] = None,
|
2023-04-27 06:52:13 +08:00
|
|
|
):
|
2021-12-15 05:48:30 +08:00
|
|
|
super(ConvNormActAa, self).__init__()
|
2022-07-02 06:14:01 +08:00
|
|
|
use_aa = aa_layer is not None and stride == 2
|
2024-05-28 13:06:22 +08:00
|
|
|
conv_kwargs = conv_kwargs or {}
|
2023-04-27 06:52:13 +08:00
|
|
|
norm_kwargs = norm_kwargs or {}
|
|
|
|
act_kwargs = act_kwargs or {}
|
2020-07-24 01:28:57 +08:00
|
|
|
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
2020-06-02 07:59:51 +08:00
|
|
|
self.conv = create_conv2d(
|
2024-05-28 13:06:22 +08:00
|
|
|
in_channels, out_channels, kernel_size,
|
|
|
|
stride=1 if use_aa else stride,
|
|
|
|
padding=padding,
|
|
|
|
dilation=dilation,
|
|
|
|
groups=groups,
|
|
|
|
bias=bias,
|
|
|
|
**conv_kwargs,
|
|
|
|
)
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
2020-06-02 07:59:51 +08:00
|
|
|
|
|
|
|
# NOTE for backwards compatibility with models that use separate norm and act layer definitions
|
2021-12-15 05:48:30 +08:00
|
|
|
norm_act_layer = get_norm_act_layer(norm_layer, act_layer)
|
|
|
|
# NOTE for backwards (weight) compatibility, norm layer name remains `.bn`
|
2023-04-27 06:52:13 +08:00
|
|
|
if drop_layer:
|
|
|
|
norm_kwargs['drop_layer'] = drop_layer
|
2024-05-28 13:06:22 +08:00
|
|
|
self.bn = norm_act_layer(
|
|
|
|
out_channels,
|
|
|
|
apply_act=apply_act,
|
|
|
|
act_kwargs=act_kwargs,
|
|
|
|
**norm_kwargs,
|
|
|
|
)
|
2022-07-02 06:14:01 +08:00
|
|
|
self.aa = create_aa(aa_layer, out_channels, stride=stride, enable=use_aa)
|
2020-02-10 06:46:28 +08:00
|
|
|
|
2020-07-24 01:28:57 +08:00
|
|
|
@property
|
|
|
|
def in_channels(self):
|
|
|
|
return self.conv.in_channels
|
|
|
|
|
|
|
|
@property
|
|
|
|
def out_channels(self):
|
|
|
|
return self.conv.out_channels
|
|
|
|
|
2020-02-10 06:46:28 +08:00
|
|
|
def forward(self, x):
|
|
|
|
x = self.conv(x)
|
|
|
|
x = self.bn(x)
|
2021-12-15 05:48:30 +08:00
|
|
|
x = self.aa(x)
|
2020-02-10 06:46:28 +08:00
|
|
|
return x
|