当前位置:   article > 正文

pytorch mmcv工程之卷积层定义_mmcv convmodule

mmcv convmodule

MMCV卷积层定义

Introduction

MMCV is a foundational python library for computer vision research and supports many
research projects as below:

It provides the following functionalities.

  • Universal IO APIs
  • Image/Video processing
  • Image and annotation visualization
  • Useful utilities (progress bar, timer, …)
  • PyTorch runner with hooking mechanism
  • Various CNN architectures
  • High-quality implementation of common CUDA ops
    Note: MMCV requires Python 3.6+.

卷积块:
一个卷积块包含卷积、正则和激活层。
A conv block that bundles conv/norm/activation layers.

  • pytorch中定义的ResNet卷积层:
  (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)
  • 1
  • 2
  • 3

mmcv中mmcv/mmcv/cnn/bricks/conv_module.py

  1. 借助三个函数build_conv_layerbuild_norm_layerbuild_activation_layer()定义卷积块以简化卷积神经网络中卷积层的使用。
  2. 模块中额外添加的功能:
    1)自动设置卷积层的偏差
    2)支持频谱正则化
    3)支持更多的填充模式。 在PyTorch 1.5之前,仅限nn.Conv2d
    支持零填充和圆形填充,这里添加了“reflect”填充模式。
    zero padding; circular padding
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

参数:
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/194251
推荐阅读
相关标签
  

闽ICP备14008679号