Introduction
MMCV is a foundational python library for computer vision research and supports many
research projects as below:
It provides the following functionalities.
卷积块:
一个卷积块包含卷积、正则和激活层。
A conv block that bundles conv/norm/activation layers.
(conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
mmcv中mmcv/mmcv/cnn/bricks/conv_module.py
build_conv_layer
,build_norm_layer
,build_activation_layer()
定义卷积块以简化卷积神经网络中卷积层的使用。ConvModule(nn.Module):
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1,
groups=1,
bias='auto',
conv_cfg=None,
norm_cfg=None,
act_cfg=dict(type='ReLU'),
inplace=True,
with_spectral_norm=False,
padding_mode='zeros',
order=('conv', 'norm', 'act')):
参数:
1,in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1,
同nn.Conv2d
2, bias (bool | str):
True\False\Auto
如果是Auto
,偏差由norm_cfg
决定
norm_cfg is None
,bias=True
;否则 bias=False
3, norm_cfg (dict)
:正则化层的配置字典Default: None
.
4,act_cfg (dict)
: Config dict for activation layer.
Default: dict(type='ReLU').
5,inplace (bool)
: Whether to use inplace mode for activation.
Default: True
.
6,with_spectral_norm (bool)
: Whether use spectral norm in conv module.
Default: False
.
7,padding_mode (str)
: 如果目前的pytorch中的 Conv2d
不支持该 padding_mode
,使用自定义的padding layer
。目前,该卷积模块支持官方的 [‘zeros’, ‘circular’] 和自己实现的[‘reflect’]。 Default: 'zeros'
.
8,order (tuple[str])
: conv/norm/activation layers的顺序. “conv”, “norm” and “act”.常见的有 ("conv", "norm", "act") and ("act", "conv", "norm")
. Default: ('conv', 'norm', 'act')
.
def forward(self, x, activate=True, norm=True):
for layer in self.order:
if layer == 'conv':
if self.with_explicit_padding:
x = self.padding_layer(x)
x = self.conv(x)
elif layer == 'norm' and norm and self.with_norm:
x = self.norm(x)
elif layer == 'act' and activate and self.with_activation:
x = self.activate(x)
return x