|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +from typing import Optional, Tuple, Union |
| 3 | + |
| 4 | +from mmcv.cnn import build_activation_layer, build_conv_layer, build_norm_layer |
| 5 | +from mmengine.model import BaseModule |
| 6 | +from mmengine.registry import MODELS |
| 7 | +from torch import nn |
| 8 | + |
| 9 | +from mmdet3d.utils import ConfigType, OptConfigType, OptMultiConfig |
| 10 | + |
| 11 | +try: |
| 12 | + from MinkowskiEngine import (MinkowskiBatchNorm, MinkowskiConvolution, |
| 13 | + MinkowskiConvolutionTranspose, MinkowskiReLU, |
| 14 | + MinkowskiSyncBatchNorm, SparseTensor) |
| 15 | + from MinkowskiEngine.modules.resnet_block import BasicBlock, Bottleneck |
| 16 | +except ImportError: |
| 17 | + SparseTensor = None |
| 18 | + from mmcv.cnn.resnet import BasicBlock, Bottleneck |
| 19 | + IS_MINKOWSKI_ENGINE_AVAILABLE = False |
| 20 | +else: |
| 21 | + MODELS._register_module(MinkowskiConvolution, 'MinkowskiConvNd') |
| 22 | + MODELS._register_module(MinkowskiConvolutionTranspose, |
| 23 | + 'MinkowskiConvNdTranspose') |
| 24 | + MODELS._register_module(MinkowskiBatchNorm, 'MinkowskiBN') |
| 25 | + MODELS._register_module(MinkowskiSyncBatchNorm, 'MinkowskiSyncBN') |
| 26 | + MODELS._register_module(MinkowskiReLU, 'MinkowskiReLU') |
| 27 | + IS_MINKOWSKI_ENGINE_AVAILABLE = True |
| 28 | + |
| 29 | + |
| 30 | +class MinkowskiConvModule(BaseModule): |
| 31 | + """A minkowski engine conv block that bundles conv/norm/activation layers. |
| 32 | +
|
| 33 | + Args: |
| 34 | + in_channels (int): In channels of block. |
| 35 | + out_channels (int): Out channels of block. |
| 36 | + kernel_size (int or Tuple[int]): Kernel_size of block. |
| 37 | + stride (int or Tuple[int]): Stride of the first block. Defaults to 1. |
| 38 | + dilation (int): Dilation of block. Defaults to 1. |
| 39 | + bias (bool): Whether to use bias in conv. Defaults to False. |
| 40 | + conv_cfg (:obj:`ConfigDict` or dict, optional): Config of conv layer. |
| 41 | + Defaults to None. |
| 42 | + norm_cfg (:obj:`ConfigDict` or dict): The config of normalization. |
| 43 | + Defaults to dict(type='MinkowskiBN'). |
| 44 | + act_cfg (:obj:`ConfigDict` or dict): The config of activation. |
| 45 | + Defaults to dict(type='MinkowskiReLU', inplace=True). |
| 46 | + init_cfg (:obj:`ConfigDict` or dict, optional): Initialization config. |
| 47 | + Defaults to None. |
| 48 | + """ |
| 49 | + |
| 50 | + def __init__(self, |
| 51 | + in_channels: int, |
| 52 | + out_channels: int, |
| 53 | + kernel_size: Union[int, Tuple[int, int, int]], |
| 54 | + stride: Union[int, Tuple[int, int, int]] = 1, |
| 55 | + dilation: int = 1, |
| 56 | + bias: bool = False, |
| 57 | + conv_cfg: OptConfigType = None, |
| 58 | + norm_cfg: ConfigType = dict(type='MinkowskiBN'), |
| 59 | + act_cfg: ConfigType = dict( |
| 60 | + type='MinkowskiReLU', inplace=True), |
| 61 | + init_cfg: OptMultiConfig = None, |
| 62 | + **kwargs) -> None: |
| 63 | + super().__init__(init_cfg) |
| 64 | + layers = [] |
| 65 | + if conv_cfg is None: |
| 66 | + conv_cfg = dict(type='MinkowskiConvNd') |
| 67 | + conv = build_conv_layer( |
| 68 | + conv_cfg, |
| 69 | + in_channels, |
| 70 | + out_channels, |
| 71 | + kernel_size, |
| 72 | + stride, |
| 73 | + dilation, |
| 74 | + bias, |
| 75 | + dimension=3) |
| 76 | + layers.append(conv) |
| 77 | + |
| 78 | + if norm_cfg is not None: |
| 79 | + _, norm = build_norm_layer(norm_cfg, out_channels) |
| 80 | + layers.append(norm) |
| 81 | + if act_cfg is not None: |
| 82 | + activation = build_activation_layer(act_cfg) |
| 83 | + layers.append(activation) |
| 84 | + self.net = nn.Sequential(*layers) |
| 85 | + |
| 86 | + def forward(self, x: SparseTensor) -> SparseTensor: |
| 87 | + out = self.net(x) |
| 88 | + return out |
| 89 | + |
| 90 | + |
| 91 | +class MinkowskiBasicBlock(BasicBlock, BaseModule): |
| 92 | + """A wrapper of minkowski engine basic block. It inherits from mmengine's |
| 93 | + `BaseModule` and allows additional keyword arguments. |
| 94 | +
|
| 95 | + Args: |
| 96 | + inplanes (int): In channels of block. |
| 97 | + planes (int): Out channels of block. |
| 98 | + stride (int or Tuple[int]): Stride of the first conv. Defaults to 1. |
| 99 | + dilation (int): Dilation of block. Defaults to 1. |
| 100 | + downsample (nn.Module, optional): Residual branch conv module if |
| 101 | + necessary. Defaults to None. |
| 102 | + bn_momentum (float): Momentum of batch norm layer. Defaults to 0.1. |
| 103 | + dimension (int): Dimension of minkowski convolution. Defaults to 3. |
| 104 | + init_cfg (:obj:`ConfigDict` or dict, optional): Initialization config. |
| 105 | + Defaults to None. |
| 106 | + """ |
| 107 | + |
| 108 | + def __init__(self, |
| 109 | + inplanes: int, |
| 110 | + planes: int, |
| 111 | + stride: int = 1, |
| 112 | + dilation: int = 1, |
| 113 | + downsample: Optional[nn.Module] = None, |
| 114 | + bn_momentum: float = 0.1, |
| 115 | + dimension: int = 3, |
| 116 | + init_cfg: OptConfigType = None, |
| 117 | + **kwargs): |
| 118 | + BaseModule.__init__(self, init_cfg) |
| 119 | + BasicBlock.__init__( |
| 120 | + self, |
| 121 | + inplanes, |
| 122 | + planes, |
| 123 | + stride=stride, |
| 124 | + dilation=dilation, |
| 125 | + downsample=downsample, |
| 126 | + bn_momentum=bn_momentum, |
| 127 | + dimension=dimension) |
| 128 | + |
| 129 | + |
| 130 | +class MinkowskiBottleneck(Bottleneck, BaseModule): |
| 131 | + """A wrapper of minkowski engine bottleneck block. It inherits from |
| 132 | + mmengine's `BaseModule` and allows additional keyword arguments. |
| 133 | +
|
| 134 | + Args: |
| 135 | + inplanes (int): In channels of block. |
| 136 | + planes (int): Out channels of block. |
| 137 | + stride (int or Tuple[int]): Stride of the second conv. Defaults to 1. |
| 138 | + dilation (int): Dilation of block. Defaults to 1. |
| 139 | + downsample (nn.Module, optional): Residual branch conv module if |
| 140 | + necessary. Defaults to None. |
| 141 | + bn_momentum (float): Momentum of batch norm layer. Defaults to 0.1. |
| 142 | + dimension (int): Dimension of minkowski convolution. Defaults to 3. |
| 143 | + init_cfg (:obj:`ConfigDict` or dict, optional): Initialization config. |
| 144 | + Defaults to None. |
| 145 | + """ |
| 146 | + |
| 147 | + def __init__(self, |
| 148 | + inplanes: int, |
| 149 | + planes: int, |
| 150 | + stride: int = 1, |
| 151 | + dilation: int = 1, |
| 152 | + downsample: Optional[nn.Module] = None, |
| 153 | + bn_momentum: float = 0.1, |
| 154 | + dimension: int = 3, |
| 155 | + init_cfg: OptConfigType = None, |
| 156 | + **kwargs): |
| 157 | + BaseModule.__init__(self, init_cfg) |
| 158 | + Bottleneck.__init__( |
| 159 | + self, |
| 160 | + inplanes, |
| 161 | + planes, |
| 162 | + stride=stride, |
| 163 | + dilation=dilation, |
| 164 | + downsample=downsample, |
| 165 | + bn_momentum=bn_momentum, |
| 166 | + dimension=dimension) |
0 commit comments