Spaces:
Sleeping
Sleeping
File size: 10,145 Bytes
9bf4bd7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Dict, List, Optional, Tuple, Union
import torch
from mmcv.cnn import ConvModule, build_plugin_layer
from mmengine.model import BaseModule, Sequential
import mmocr.utils as utils
from mmocr.models.textrecog.layers import BasicBlock
from mmocr.registry import MODELS
@MODELS.register_module()
class ResNet(BaseModule):
"""
Args:
in_channels (int): Number of channels of input image tensor.
stem_channels (list[int]): List of channels in each stem layer. E.g.,
[64, 128] stands for 64 and 128 channels in the first and second
stem layers.
block_cfgs (dict): Configs of block
arch_layers (list[int]): List of Block number for each stage.
arch_channels (list[int]): List of channels for each stage.
strides (Sequence[int] or Sequence[tuple]): Strides of the first block
of each stage.
out_indices (Sequence[int], optional): Indices of output stages. If not
specified, only the last stage will be returned.
plugins (dict, optional): Configs of stage plugins
init_cfg (dict or list[dict], optional): Initialization config dict.
"""
def __init__(self,
in_channels: int,
stem_channels: List[int],
block_cfgs: dict,
arch_layers: List[int],
arch_channels: List[int],
strides: Union[List[int], List[Tuple]],
out_indices: Optional[List[int]] = None,
plugins: Optional[Dict] = None,
init_cfg: Optional[Union[Dict, List[Dict]]] = [
dict(type='Xavier', layer='Conv2d'),
dict(type='Constant', val=1, layer='BatchNorm2d'),
]):
super().__init__(init_cfg=init_cfg)
assert isinstance(in_channels, int)
assert isinstance(stem_channels, int) or utils.is_type_list(
stem_channels, int)
assert utils.is_type_list(arch_layers, int)
assert utils.is_type_list(arch_channels, int)
assert utils.is_type_list(strides, tuple) or utils.is_type_list(
strides, int)
assert len(arch_layers) == len(arch_channels) == len(strides)
assert out_indices is None or isinstance(out_indices, (list, tuple))
self.out_indices = out_indices
self._make_stem_layer(in_channels, stem_channels)
self.num_stages = len(arch_layers)
self.use_plugins = False
self.arch_channels = arch_channels
self.res_layers = []
if plugins is not None:
self.plugin_ahead_names = []
self.plugin_after_names = []
self.use_plugins = True
for i, num_blocks in enumerate(arch_layers):
stride = strides[i]
channel = arch_channels[i]
if self.use_plugins:
self._make_stage_plugins(plugins, stage_idx=i)
res_layer = self._make_layer(
block_cfgs=block_cfgs,
inplanes=self.inplanes,
planes=channel,
blocks=num_blocks,
stride=stride,
)
self.inplanes = channel
layer_name = f'layer{i + 1}'
self.add_module(layer_name, res_layer)
self.res_layers.append(layer_name)
def _make_layer(self, block_cfgs: Dict, inplanes: int, planes: int,
blocks: int, stride: int) -> Sequential:
"""Build resnet layer.
Args:
block_cfgs (dict): Configs of blocks.
inplanes (int): Number of input channels.
planes (int): Number of output channels.
blocks (int): Number of blocks.
stride (int): Stride of the first block.
Returns:
Sequential: A sequence of blocks.
"""
layers = []
downsample = None
block_cfgs_ = block_cfgs.copy()
if isinstance(stride, int):
stride = (stride, stride)
if stride[0] != 1 or stride[1] != 1 or inplanes != planes:
downsample = ConvModule(
inplanes,
planes,
1,
stride,
norm_cfg=dict(type='BN'),
act_cfg=None)
if block_cfgs_['type'] == 'BasicBlock':
block = BasicBlock
block_cfgs_.pop('type')
else:
raise ValueError('{} not implement yet'.format(block['type']))
layers.append(
block(
inplanes,
planes,
stride=stride,
downsample=downsample,
**block_cfgs_))
inplanes = planes
for _ in range(1, blocks):
layers.append(block(inplanes, planes, **block_cfgs_))
return Sequential(*layers)
def _make_stem_layer(self, in_channels: int,
stem_channels: Union[int, List[int]]) -> None:
"""Make stem layers.
Args:
in_channels (int): Number of input channels.
stem_channels (list[int] or int): List of channels in each stem
layer. If int, only one stem layer will be created.
"""
if isinstance(stem_channels, int):
stem_channels = [stem_channels]
stem_layers = []
for _, channels in enumerate(stem_channels):
stem_layer = ConvModule(
in_channels,
channels,
kernel_size=3,
stride=1,
padding=1,
bias=False,
norm_cfg=dict(type='BN'),
act_cfg=dict(type='ReLU'))
in_channels = channels
stem_layers.append(stem_layer)
self.stem_layers = Sequential(*stem_layers)
self.inplanes = stem_channels[-1]
def _make_stage_plugins(self, plugins: List[Dict], stage_idx: int) -> None:
"""Make plugins for ResNet ``stage_idx``th stage.
Currently we support inserting ``nn.Maxpooling``,
``mmcv.cnn.Convmodule``into the backbone. Originally designed
for ResNet31-like architectures.
Examples:
>>> plugins=[
... dict(cfg=dict(type="Maxpooling", arg=(2,2)),
... stages=(True, True, False, False),
... position='before_stage'),
... dict(cfg=dict(type="Maxpooling", arg=(2,1)),
... stages=(False, False, True, Flase),
... position='before_stage'),
... dict(cfg=dict(
... type='ConvModule',
... kernel_size=3,
... stride=1,
... padding=1,
... norm_cfg=dict(type='BN'),
... act_cfg=dict(type='ReLU')),
... stages=(True, True, True, True),
... position='after_stage')]
Suppose ``stage_idx=1``, the structure of stage would be:
.. code-block:: none
Maxpooling -> A set of Basicblocks -> ConvModule
Args:
plugins (list[dict]): List of plugin configs to build.
stage_idx (int): Index of stage to build
"""
in_channels = self.arch_channels[stage_idx]
self.plugin_ahead_names.append([])
self.plugin_after_names.append([])
for plugin in plugins:
plugin = plugin.copy()
stages = plugin.pop('stages', None)
position = plugin.pop('position', None)
assert stages is None or len(stages) == self.num_stages
if stages[stage_idx]:
if position == 'before_stage':
name, layer = build_plugin_layer(
plugin['cfg'],
f'_before_stage_{stage_idx+1}',
in_channels=in_channels,
out_channels=in_channels)
self.plugin_ahead_names[stage_idx].append(name)
self.add_module(name, layer)
elif position == 'after_stage':
name, layer = build_plugin_layer(
plugin['cfg'],
f'_after_stage_{stage_idx+1}',
in_channels=in_channels,
out_channels=in_channels)
self.plugin_after_names[stage_idx].append(name)
self.add_module(name, layer)
else:
raise ValueError('uncorrect plugin position')
def forward_plugin(self, x: torch.Tensor,
plugin_name: List[str]) -> torch.Tensor:
"""Forward tensor through plugin.
Args:
x (torch.Tensor): Input tensor.
plugin_name (list[str]): Name of plugins.
Returns:
torch.Tensor: Output tensor.
"""
out = x
for name in plugin_name:
out = getattr(self, name)(out)
return out
def forward(self,
x: torch.Tensor) -> Union[torch.Tensor, List[torch.Tensor]]:
"""
Args: x (Tensor): Image tensor of shape :math:`(N, 3, H, W)`.
Returns:
Tensor or list[Tensor]: Feature tensor. It can be a list of
feature outputs at specific layers if ``out_indices`` is specified.
"""
x = self.stem_layers(x)
outs = []
for i, layer_name in enumerate(self.res_layers):
res_layer = getattr(self, layer_name)
if not self.use_plugins:
x = res_layer(x)
if self.out_indices and i in self.out_indices:
outs.append(x)
else:
x = self.forward_plugin(x, self.plugin_ahead_names[i])
x = res_layer(x)
x = self.forward_plugin(x, self.plugin_after_names[i])
if self.out_indices and i in self.out_indices:
outs.append(x)
return tuple(outs) if self.out_indices else x
|