Spaces:
Runtime error
Runtime error
File size: 4,593 Bytes
412c852 |
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 |
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Optional
import torch.nn as nn
from mmcv.cnn import ConvModule
from mmengine.model import BaseModule
from torch import Tensor
from mmseg.registry import MODELS
from mmseg.utils import OptConfigType
class BasicBlock(BaseModule):
"""Basic block from `ResNet <https://arxiv.org/abs/1512.03385>`_.
Args:
in_channels (int): Input channels.
channels (int): Output channels.
stride (int): Stride of the first block. Default: 1.
downsample (nn.Module, optional): Downsample operation on identity.
Default: None.
norm_cfg (dict, optional): Config dict for normalization layer.
Default: dict(type='BN').
act_cfg (dict, optional): Config dict for activation layer in
ConvModule. Default: dict(type='ReLU', inplace=True).
act_cfg_out (dict, optional): Config dict for activation layer at the
last of the block. Default: None.
init_cfg (dict, optional): Initialization config dict. Default: None.
"""
expansion = 1
def __init__(self,
in_channels: int,
channels: int,
stride: int = 1,
downsample: nn.Module = None,
norm_cfg: OptConfigType = dict(type='BN'),
act_cfg: OptConfigType = dict(type='ReLU', inplace=True),
act_cfg_out: OptConfigType = dict(type='ReLU', inplace=True),
init_cfg: OptConfigType = None):
super().__init__(init_cfg)
self.conv1 = ConvModule(
in_channels,
channels,
kernel_size=3,
stride=stride,
padding=1,
norm_cfg=norm_cfg,
act_cfg=act_cfg)
self.conv2 = ConvModule(
channels,
channels,
kernel_size=3,
padding=1,
norm_cfg=norm_cfg,
act_cfg=None)
self.downsample = downsample
if act_cfg_out:
self.act = MODELS.build(act_cfg_out)
def forward(self, x: Tensor) -> Tensor:
residual = x
out = self.conv1(x)
out = self.conv2(out)
if self.downsample:
residual = self.downsample(x)
out += residual
if hasattr(self, 'act'):
out = self.act(out)
return out
class Bottleneck(BaseModule):
"""Bottleneck block from `ResNet <https://arxiv.org/abs/1512.03385>`_.
Args:
in_channels (int): Input channels.
channels (int): Output channels.
stride (int): Stride of the first block. Default: 1.
downsample (nn.Module, optional): Downsample operation on identity.
Default: None.
norm_cfg (dict, optional): Config dict for normalization layer.
Default: dict(type='BN').
act_cfg (dict, optional): Config dict for activation layer in
ConvModule. Default: dict(type='ReLU', inplace=True).
act_cfg_out (dict, optional): Config dict for activation layer at
the last of the block. Default: None.
init_cfg (dict, optional): Initialization config dict. Default: None.
"""
expansion = 2
def __init__(self,
in_channels: int,
channels: int,
stride: int = 1,
downsample: Optional[nn.Module] = None,
norm_cfg: OptConfigType = dict(type='BN'),
act_cfg: OptConfigType = dict(type='ReLU', inplace=True),
act_cfg_out: OptConfigType = None,
init_cfg: OptConfigType = None):
super().__init__(init_cfg)
self.conv1 = ConvModule(
in_channels, channels, 1, norm_cfg=norm_cfg, act_cfg=act_cfg)
self.conv2 = ConvModule(
channels,
channels,
3,
stride,
1,
norm_cfg=norm_cfg,
act_cfg=act_cfg)
self.conv3 = ConvModule(
channels,
channels * self.expansion,
1,
norm_cfg=norm_cfg,
act_cfg=None)
if act_cfg_out:
self.act = MODELS.build(act_cfg_out)
self.downsample = downsample
def forward(self, x: Tensor) -> Tensor:
residual = x
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
if self.downsample:
residual = self.downsample(x)
out += residual
if hasattr(self, 'act'):
out = self.act(out)
return out
|