pytorch mmcv工程之卷积层定义

MMCV卷积层定义

Introduction

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

  • MMDetection: Detection toolbox and benchmark
  • MMDetection3D: General 3D object detection toolbox and benchmark
  • MMSegmentation: Semantic segmentation toolbox and benchmark
  • MMEditing: Image and video editing toolbox
  • MMPose: Pose estimation toolbox and benchmark
  • MMAction2: Action understanding toolbox and benchmark
  • MMClassification: Image classification toolbox and benchmark

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)

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,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

你可能感兴趣的:(Pytorch源码学习)