Shortcuts

mmagic.models.editors.gca.resgca_enc

Module Contents

Classes

BasicBlock

Basic residual block.

ResNetEnc

ResNet encoder for image matting.

ResShortcutEnc

ResNet backbone for image matting with shortcut connection.

ResGCAEncoder

ResNet backbone with shortcut connection and gca module.

class mmagic.models.editors.gca.resgca_enc.BasicBlock(in_channels, out_channels, kernel_size=3, stride=1, interpolation=None, conv_cfg=None, norm_cfg=dict(type='BN'), act_cfg=dict(type='ReLU'), with_spectral_norm=False)[源代码]

Bases: torch.nn.Module

Basic residual block.

参数
  • in_channels (int) – Input channels of the block.

  • out_channels (int) – Output channels of the block.

  • kernel_size (int) – Kernel size of the convolution layers. Default: 3.

  • stride (int) – Stride of the first conv of the block. Default: 1.

  • interpolation (nn.Module, optional) – Interpolation module for skip connection. Default: None.

  • conv_cfg (dict) – dictionary to construct convolution layer. If it is None, 2d convolution will be applied. Default: None.

  • norm_cfg (dict) – Config dict for normalization layer. “BN” by default.

  • act_cfg (dict) – Config dict for activation layer, “ReLU” by default.

  • with_spectral_norm (bool) – Whether use spectral norm after conv. Default: False.

expansion = 1[源代码]
build_conv1(in_channels, out_channels, kernel_size, stride, conv_cfg, norm_cfg, act_cfg, with_spectral_norm)[源代码]

Build conv1 of the block.

参数
  • in_channels (int) – The input channels of the ConvModule.

  • out_channels (int) – The output channels of the ConvModule.

  • kernel_size (int) – The kernel size of the ConvModule.

  • stride (int) – The stride of the ConvModule. If stride is set to 2, then conv_cfg will be overwritten as dict(type='Deconv') and kernel_size will be overwritten as 4.

  • conv_cfg (dict) – The conv config of the ConvModule.

  • norm_cfg (dict) – The norm config of the ConvModule.

  • act_cfg (dict) – The activation config of the ConvModule.

  • with_spectral_norm (bool) – Whether use spectral norm.

返回

The built ConvModule.

返回类型

nn.Module

build_conv2(in_channels, out_channels, kernel_size, conv_cfg, norm_cfg, with_spectral_norm)[源代码]

Build conv2 of the block.

参数
  • in_channels (int) – The input channels of the ConvModule.

  • out_channels (int) – The output channels of the ConvModule.

  • kernel_size (int) – The kernel size of the ConvModule.

  • stride (int) – The stride of the ConvModule. If stride is set to 2, then conv_cfg will be overwritten as dict(type='Deconv') and kernel_size will be overwritten as 4.

  • conv_cfg (dict) – The conv config of the ConvModule.

  • norm_cfg (dict) – The norm config of the ConvModule.

  • act_cfg (dict) – The activation config of the ConvModule.

  • with_spectral_norm (bool) – Whether use spectral norm.

返回

The built ConvModule.

返回类型

nn.Module

forward(x)[源代码]

Forward function.

参数

inputs (torch.Tensor) – Input tensor.

返回

Output tensor.

返回类型

Tensor

class mmagic.models.editors.gca.resgca_enc.ResNetEnc(block, layers, in_channels, conv_cfg=None, norm_cfg=dict(type='BN'), act_cfg=dict(type='ReLU'), with_spectral_norm=False, late_downsample=False, init_cfg: Optional[dict] = None)[源代码]

Bases: mmengine.model.BaseModule

ResNet encoder for image matting.

This class is adopted from https://github.com/Yaoyi-Li/GCA-Matting. Implement and pre-train on ImageNet with the tricks from https://arxiv.org/abs/1812.01187 without the mix-up part.

参数
  • block (str) – Type of residual block. Currently only BasicBlock is implemented.

  • layers (list[int]) – Number of layers in each block.

  • in_channels (int) – Number of input channels.

  • conv_cfg (dict) – dictionary to construct convolution layer. If it is None, 2d convolution will be applied. Default: None.

  • norm_cfg (dict) – Config dict for normalization layer. “BN” by default.

  • act_cfg (dict) – Config dict for activation layer, “ReLU” by default.

  • with_spectral_norm (bool) – Whether use spectral norm after conv. Default: False.

  • late_downsample (bool) – Whether to adopt late downsample strategy, Default: False.

  • init_cfg (dict, optional) – Initialization config dict. Default: None.

init_weights()[源代码]

Init weights for the module.

_make_layer(block, planes, num_blocks, stride, conv_cfg, norm_cfg, act_cfg, with_spectral_norm)[源代码]
forward(x)[源代码]

Forward function.

参数

x (Tensor) – Input tensor with shape (N, C, H, W).

返回

Output tensor.

返回类型

Tensor

class mmagic.models.editors.gca.resgca_enc.ResShortcutEnc(block, layers, in_channels, conv_cfg=None, norm_cfg=dict(type='BN'), act_cfg=dict(type='ReLU'), with_spectral_norm=False, late_downsample=False, order=('conv', 'act', 'norm'), init_cfg: Optional[dict] = None)[源代码]

Bases: ResNetEnc

ResNet backbone for image matting with shortcut connection.

image ---------------- shortcut[0] --- feat1
  |
conv1-conv2 ---------- shortcut[1] --- feat2
       |
      conv3-layer1 --- shortcut[2] --- feat3
              |
             layer2 -- shortcut[4] --- feat4
               |
              layer3 - shortcut[5] --- feat5
                |
               layer4 ---------------- out

Baseline model of Natural Image Matting via Guided Contextual Attention https://arxiv.org/pdf/2001.04069.pdf.

参数
  • block (str) – Type of residual block. Currently only BasicBlock is implemented.

  • layers (list[int]) – Number of layers in each block.

  • in_channels (int) – Number of input channels.

  • conv_cfg (dict) – Dictionary to construct convolution layer. If it is None, 2d convolution will be applied. Default: None.

  • norm_cfg (dict) – Config dict for normalization layer. “BN” by default.

  • act_cfg (dict) – Config dict for activation layer, “ReLU” by default.

  • with_spectral_norm (bool) – Whether use spectral norm after conv. Default: False.

  • late_downsample (bool) – Whether to adopt late downsample strategy. Default: False.

  • order (tuple[str]) – Order of conv, norm and act layer in shortcut convolution module. Default: (‘conv’, ‘act’, ‘norm’).

  • init_cfg (dict, optional) – Initialization config dict. Default: None.

_make_shortcut(in_channels, out_channels, conv_cfg, norm_cfg, act_cfg, order, with_spectral_norm)[源代码]
forward(x)[源代码]

Forward function.

参数

x (Tensor) – Input tensor with shape (N, C, H, W).

返回

Contains the output tensor and shortcut feature.

返回类型

dict

class mmagic.models.editors.gca.resgca_enc.ResGCAEncoder(block, layers, in_channels, conv_cfg=None, norm_cfg=dict(type='BN'), act_cfg=dict(type='ReLU'), with_spectral_norm=False, late_downsample=False, order=('conv', 'act', 'norm'), init_cfg: Optional[dict] = None)[源代码]

Bases: ResShortcutEnc

ResNet backbone with shortcut connection and gca module.

image ---------------- shortcut[0] -------------- feat1
 |
conv1-conv2 ---------- shortcut[1] -------------- feat2
       |
     conv3-layer1 ---- shortcut[2] -------------- feat3
             |
             | image - guidance_conv ------------ img_feat
             |             |
            layer2 --- gca_module - shortcut[4] - feat4
                            |
                          layer3 -- shortcut[5] - feat5
                             |
                           layer4 --------------- out
  • gca module also requires unknown tensor generated by trimap which is ignored in the above graph.

Implementation of Natural Image Matting via Guided Contextual Attention https://arxiv.org/pdf/2001.04069.pdf.

参数
  • block (str) – Type of residual block. Currently only BasicBlock is implemented.

  • layers (list[int]) – Number of layers in each block.

  • in_channels (int) – Number of input channels.

  • conv_cfg (dict) – Dictionary to construct convolution layer. If it is None, 2d convolution will be applied. Default: None.

  • norm_cfg (dict) – Config dict for normalization layer. “BN” by default.

  • act_cfg (dict) – Config dict for activation layer, “ReLU” by default.

  • late_downsample (bool) – Whether to adopt late downsample strategy. Default: False.

  • order (tuple[str]) – Order of conv, norm and act layer in shortcut convolution module. Default: (‘conv’, ‘act’, ‘norm’).

  • init_cfg (dict, optional) – Initialization config dict. Default: None.

forward(x)[源代码]

Forward function.

参数

x (Tensor) – Input tensor with shape (N, C, H, W).

返回

Contains the output tensor, shortcut feature and intermediate feature.

返回类型

dict

Read the Docs v: latest
Versions
latest
stable
0.x
Downloads
pdf
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.