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)[source]

Bases: torch.nn.Module

Basic residual block.

Parameters
  • 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[source]
build_conv1(in_channels, out_channels, kernel_size, stride, conv_cfg, norm_cfg, act_cfg, with_spectral_norm)[source]

Build conv1 of the block.

Parameters
  • 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.

Returns

The built ConvModule.

Return type

nn.Module

build_conv2(in_channels, out_channels, kernel_size, conv_cfg, norm_cfg, with_spectral_norm)[source]

Build conv2 of the block.

Parameters
  • 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.

Returns

The built ConvModule.

Return type

nn.Module

forward(x)[source]

Forward function.

Parameters

inputs (torch.Tensor) – Input tensor.

Returns

Output tensor.

Return type

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)[source]

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.

Parameters
  • 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()[source]

Init weights for the module.

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

Forward function.

Parameters

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

Returns

Output tensor.

Return type

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)[source]

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.

Parameters
  • 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)[source]
forward(x)[source]

Forward function.

Parameters

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

Returns

Contains the output tensor and shortcut feature.

Return type

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)[source]

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.

Parameters
  • 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)[source]

Forward function.

Parameters

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

Returns

Contains the output tensor, shortcut feature and intermediate feature.

Return type

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.