pytorch mmcv工程之卷积层定义

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)

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

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
PyTorch中可以使用`torch.nn.Conv2d`来实现膨胀卷积层。膨胀卷积层也被称为空洞卷积层(dilated convolutional layer),它在卷积核上引入了间隔,从而扩大了卷积核的感受野。 以下是一个使用膨胀卷积层的示例代码: ```python import torch import torch.nn as nn # 定义一个包含膨胀卷积层的模型 class DilatedConvNet(nn.Module): def __init__(self): super(DilatedConvNet, self).__init__() self.dilated_conv = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=2, dilation=2) self.relu = nn.ReLU() def forward(self, x): x = self.dilated_conv(x) x = self.relu(x) return x # 创建模型实例 model = DilatedConvNet() # 创建输入张量 inputs = torch.randn(1, 3, 32, 32) # 前向传播 outputs = model(inputs) # 输出张量的形状 print(outputs.shape) ``` 在上述示例中,我们首先定义了一个包含一个膨胀卷积层的模型`DilatedConvNet`。在模型的初始化方法中,我们使用`nn.Conv2d`来创建一个膨胀卷积层,其中`dilation=2`表示膨胀卷积的膨胀系数为2。然后,在模型的前向传播方法中,我们将输入张量`x`传递给膨胀卷积层,并通过ReLU激活函数进行非线性变换。最后,我们使用模型对输入张量进行前向传播,并输出得到的输出张量的形状。 注意:在实际使用膨胀卷积层时,可以根据具体需求调整卷积核的参数,例如输入通道数、输出通道数、kernel_size等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值