Loading... ## 前言 在CV领域, 已经证明了预训练模型对于模型加速收敛或者取得更好的效果(一般分类预期有2-3点左右提升)方面有积极作用, 但是在某些领域缺无法很好的利用起来, 比如医学图像中普遍需要处理3D的完整影像或者其中的PATCH, 无法很好很直接的利用广泛的2D公开数据集, 比如IMAGENET/COCO等2D开源标注数据集. 下面介绍一种直观的应用方式来对resnet网络进行改造,使得可以正常进行预训练. --- ## 实现与代码 *核心思路* 以IMAGENet-1k中分类数据集为例, 常规预处理后的图片尺寸为(3,224,224), 送入2D网络的Batch形态为`[B, C, W, H] => [B, 3, 224,224]`, 为了适配3D网络, 需要将输入改造为`[B,C,D, W, H]=>[B, 1, 3, 224,224]`, 即将原来的3通道作为D(深度维度), 通道`C=1` *代码实现* * 基于[MMdetection](https://github.com/open-mmlab/mmdetection), 可以方便实现改造后的ResNet3D网络在IMAGENET数据集上的预训练 * 需要在本地的MMdetection工程目录下的`mmdet/models/backbones/resnet3d.py`路径下新建以及增加其它相关配置 ```python from functools import partial import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.checkpoint as cp from mmcv.cnn import (ConvModule, build_conv_layer, build_norm_layer, constant_init, kaiming_init) from mmcv.runner import BaseModule from mmcv.utils.parrots_wrapper import _BatchNorm from ..builder import BACKBONES from .base_backbone import BaseBackbone def downsample_basic_block(x, planes, stride, no_cuda=False): out = F.avg_pool3d(x, kernel_size=1, stride=stride) zero_pads = torch.Tensor(out.size(0), planes - out.size(1), out.size(2), out.size(3), out.size(4)).zero_() if not no_cuda: if isinstance(out.data, torch.cuda.FloatTensor): zero_pads = zero_pads.cuda() #out = Variable(torch.cat([out.data, zero_pads], dim=1)) out = torch.cat([out.data, torch.zeros_like(out.data)], dim=1) return out class BasicBlock(BaseModule): """BasicBlock for ResNet. Args: in_channels (int): Input channels of this block. out_channels (int): Output channels of this block. expansion (int): The ratio of ``out_channels/mid_channels`` where ``mid_channels`` is the output channels of conv1. This is a reserved argument in BasicBlock and should always be 1. Default: 1. stride (int): stride of the block. Default: 1 dilation (int): dilation of convolution. Default: 1 downsample (BaseModule): downsample operation on identity branch. Default: None. style (str): `pytorch` or `caffe`. It is unused and reserved for unified API with Bottleneck. with_cp (bool): Use checkpoint or not. Using checkpoint will save some memory while slowing down the training speed. conv_cfg (dict): dictionary to construct and config conv layer. Default: None norm_cfg (dict): dictionary to construct and config norm layer. Default: dict(type='BN') """ def __init__(self, in_channels, out_channels, expansion=1, stride=1, dilation=1, downsample=None, style='pytorch', with_cp=False, conv_cfg=None, norm_cfg=dict(type='BN'), init_cfg=None): super(BasicBlock, self).__init__(init_cfg=init_cfg) self.in_channels = in_channels self.out_channels = out_channels self.expansion = expansion assert self.expansion == 1 assert out_channels % expansion == 0 self.mid_channels = out_channels // expansion self.stride = stride self.dilation = dilation self.style = style self.with_cp = with_cp self.conv_cfg = conv_cfg self.norm_cfg = norm_cfg self.norm1_name, norm1 = build_norm_layer(norm_cfg, self.mid_channels, postfix=1) self.norm2_name, norm2 = build_norm_layer(norm_cfg, out_channels, postfix=2) self.conv1 = build_conv_layer( conv_cfg, in_channels, self.mid_channels, 3, stride=stride, padding=dilation, dilation=dilation, bias=False) self.add_module(self.norm1_name, norm1) self.conv2 = build_conv_layer(conv_cfg, self.mid_channels, out_channels, 3, padding=1, bias=False) self.add_module(self.norm2_name, norm2) self.relu = nn.ReLU(inplace=True) self.downsample = downsample @property def norm1(self): return getattr(self, self.norm1_name) @property def norm2(self): return getattr(self, self.norm2_name) def forward(self, x): def _inner_forward(x): identity = x # [1, 64, 2, 112, 112] out = self.conv1(x) # [1, 64, 2, 112, 112] out = self.norm1(out) out = self.relu(out) out = self.conv2(out) # [1, 64, 2, 112, 112] out = self.norm2(out) if self.downsample is not None: identity = self.downsample(x) out += identity return out if self.with_cp and x.requires_grad: out = cp.checkpoint(_inner_forward, x) else: out = _inner_forward(x) out = self.relu(out) return out class Bottleneck(BaseModule): """Bottleneck block for ResNet. Args: in_channels (int): Input channels of this block. out_channels (int): Output channels of this block. expansion (int): The ratio of ``out_channels/mid_channels`` where ``mid_channels`` is the input/output channels of conv2. Default: 4. stride (int): stride of the block. Default: 1 dilation (int): dilation of convolution. Default: 1 downsample (BaseModule): downsample operation on identity branch. Default: None. style (str): ``"pytorch"`` or ``"caffe"``. If set to "pytorch", the stride-two layer is the 3x3 conv layer, otherwise the stride-two layer is the first 1x1 conv layer. Default: "pytorch". with_cp (bool): Use checkpoint or not. Using checkpoint will save some memory while slowing down the training speed. conv_cfg (dict): dictionary to construct and config conv layer. Default: None norm_cfg (dict): dictionary to construct and config norm layer. Default: dict(type='BN') """ def __init__(self, in_channels, out_channels, expansion=4, stride=1, dilation=1, downsample=None, style='pytorch', with_cp=False, conv_cfg=None, norm_cfg=dict(type='BN'), init_cfg=None): super(Bottleneck, self).__init__(init_cfg=init_cfg) assert style in ['pytorch', 'caffe'] self.in_channels = in_channels self.out_channels = out_channels self.expansion = expansion assert out_channels % expansion == 0 self.mid_channels = out_channels // expansion self.stride = stride self.dilation = dilation self.style = style self.with_cp = with_cp self.conv_cfg = conv_cfg self.norm_cfg = norm_cfg if self.style == 'pytorch': self.conv1_stride = 1 self.conv2_stride = stride else: self.conv1_stride = stride self.conv2_stride = 1 self.norm1_name, norm1 = build_norm_layer(norm_cfg, self.mid_channels, postfix=1) self.norm2_name, norm2 = build_norm_layer(norm_cfg, self.mid_channels, postfix=2) self.norm3_name, norm3 = build_norm_layer(norm_cfg, out_channels, postfix=3) self.conv1 = build_conv_layer( conv_cfg, in_channels, self.mid_channels, kernel_size=1, stride=self.conv1_stride, bias=False) self.add_module(self.norm1_name, norm1) self.conv2 = build_conv_layer( conv_cfg, self.mid_channels, self.mid_channels, kernel_size=3, stride=self.conv2_stride, padding=dilation, dilation=dilation, bias=False) self.add_module(self.norm2_name, norm2) self.conv3 = build_conv_layer(conv_cfg, self.mid_channels, out_channels, kernel_size=1, bias=False) self.add_module(self.norm3_name, norm3) self.relu = nn.ReLU(inplace=True) self.downsample = downsample @property def norm1(self): return getattr(self, self.norm1_name) @property def norm2(self): return getattr(self, self.norm2_name) @property def norm3(self): return getattr(self, self.norm3_name) def forward(self, x): def _inner_forward(x): identity = x out = self.conv1(x) out = self.norm1(out) out = self.relu(out) out = self.conv2(out) out = self.norm2(out) out = self.relu(out) out = self.conv3(out) out = self.norm3(out) if self.downsample is not None: identity = self.downsample(x) out += identity return out if self.with_cp and x.requires_grad: out = cp.checkpoint(_inner_forward, x) else: out = _inner_forward(x) out = self.relu(out) return out def get_expansion(block, expansion=None): """Get the expansion of a residual block. The block expansion will be obtained by the following order: 1. If ``expansion`` is given, just return it. 2. If ``block`` has the attribute ``expansion``, then return ``block.expansion``. 3. Return the default value according the the block type: 1 for ``BasicBlock`` and 4 for ``Bottleneck``. Args: block (class): The block class. expansion (int | None): The given expansion ratio. Returns: int: The expansion of the block. """ if isinstance(expansion, int): assert expansion > 0 elif expansion is None: if hasattr(block, 'expansion'): expansion = block.expansion elif issubclass(block, BasicBlock): expansion = 1 elif issubclass(block, Bottleneck): expansion = 4 else: raise TypeError(f'expansion is not specified for {block.__name__}') else: raise TypeError('expansion must be an integer or None') return expansion class ResLayer(nn.Sequential): """ResLayer to build ResNet style backbone. Args: block (BaseModule): Residual block used to build ResLayer. num_blocks (int): Number of blocks. in_channels (int): Input channels of this block. out_channels (int): Output channels of this block. expansion (int, optional): The expansion for BasicBlock/Bottleneck. If not specified, it will firstly be obtained via ``block.expansion``. If the block has no attribute "expansion", the following default values will be used: 1 for BasicBlock and 4 for Bottleneck. Default: None. stride (int): stride of the first block. Default: 1. avg_down (bool): Use AvgPool instead of stride conv when downsampling in the bottleneck. Default: False conv_cfg (dict): dictionary to construct and config conv layer. Default: None norm_cfg (dict): dictionary to construct and config norm layer. Default: dict(type='BN') """ def __init__(self, block, num_blocks, in_channels, out_channels, expansion=None, shortcut_type='B', stride=1, avg_down=False, conv_cfg=None, norm_cfg=dict(type='BN'), **kwargs): self.block = block self.expansion = get_expansion(block, expansion) downsample = None if isinstance(stride, int): stride_flag = stride != 1 elif isinstance(stride, tuple): stride_flag = stride[0] != 1 if stride_flag or in_channels != out_channels: downsample = [] conv_stride = stride if shortcut_type == 'A': downsample = partial(downsample_basic_block, planes=in_channels * expansion, stride=stride) else: if avg_down and stride != 1: conv_stride = 1 downsample.append( nn.AvgPool3d(kernel_size=stride, stride=stride, ceil_mode=True, count_include_pad=False)) downsample.extend([ build_conv_layer( conv_cfg, in_channels, out_channels, kernel_size=1, stride=conv_stride, bias=False), build_norm_layer(norm_cfg, out_channels)[1] ]) downsample = nn.Sequential(*downsample) layers = [] layers.append( block( in_channels=in_channels, out_channels=out_channels, expansion=self.expansion, stride=stride, downsample=downsample, conv_cfg=conv_cfg, norm_cfg=norm_cfg, **kwargs)) in_channels = out_channels for i in range(1, num_blocks): layers.append( block( in_channels=in_channels, out_channels=out_channels, expansion=self.expansion, stride=1, conv_cfg=conv_cfg, norm_cfg=norm_cfg, **kwargs)) super(ResLayer, self).__init__(*layers) # @BACKBONES.register_module() class ResNet3D(BaseBackbone): """ResNet backbone. Please refer to the `paper <https://arxiv.org/abs/1512.03385>`_ for details. Args: depth (int): Network depth, from {18, 34, 50, 101, 152}. in_channels (int): Number of input image channels. Default: 3. stem_channels (int): Output channels of the stem layer. Default: 64. base_channels (int): Middle channels of the first stage. Default: 64. num_stages (int): Stages of the network. Default: 4. depth_stride (bool): Stride in depth axis. Normally set to True for detection, and False for classification task. stem_stride (bool): Whether to use stride in stem layers(conv1 & maxpool). Set to False to reduce resolution loss when input shape is small. strides (Sequence[int]): Strides of the first block of each stage. Default: ``(1, 2, 2, 2)``. dilations (Sequence[int]): Dilation of each stage. Default: ``(1, 1, 1, 1)``. out_indices (Sequence[int]): Output from which stages. If only one stage is specified, a single tensor (feature map) is returned, otherwise multiple stages are specified, a tuple of tensors will be returned. Default: ``(3, )``. style (str): `pytorch` or `caffe`. If set to "pytorch", the stride-two layer is the 3x3 conv layer, otherwise the stride-two layer is the first 1x1 conv layer. deep_stem (bool): Replace 7x7 conv in input stem with 3 3x3 conv. Default: False. avg_down (bool): Use AvgPool instead of stride conv when downsampling in the bottleneck. Default: False. frozen_stages (int): Stages to be frozen (stop grad and set eval mode). -1 means not freezing any parameters. Default: -1. conv_cfg (dict | None): The config dict for conv layers. Default: None. norm_cfg (dict): The config dict for norm layers. norm_eval (bool): Whether to set norm layers to eval mode, namely, freeze running stats (mean and var). Note: Effect on Batch Norm and its variants only. Default: False. with_cp (bool): Use checkpoint or not. Using checkpoint will save some memory while slowing down the training speed. Default: False. zero_init_residual (bool): Whether to use zero init for last norm layer in resblocks to let them behave as identity. Default: True. Example: >>> from mmcls.models import ResNet >>> import torch >>> self = ResNet(depth=18) >>> self.eval() >>> inputs = torch.rand(1, 3, 32, 32) >>> level_outputs = self.forward(inputs) >>> for level_out in level_outputs: ... print(tuple(level_out.shape)) (1, 64, 8, 8) (1, 128, 4, 4) (1, 256, 2, 2) (1, 512, 1, 1) """ arch_settings = { 18: (BasicBlock, (2, 2, 2, 2)), 34: (BasicBlock, (3, 4, 6, 3)), 50: (Bottleneck, (3, 4, 6, 3)), 101: (Bottleneck, (3, 4, 23, 3)), 152: (Bottleneck, (3, 8, 36, 3)) } def __init__( self, depth, in_channels=3, stem_channels=64, base_channels=64, expansion=None, num_stages=4, depth_stride=False, stem_depth_stride=True, # newly added to control stem depth stem_stride=True, stem_pool=False, strides=(1, 2, 2, 2), dilations=(1, 1, 1, 1), out_indices=(3, ), shortcut_type='B', style='pytorch', deep_stem=False, avg_down=False, frozen_stages=-1, conv_cfg=None, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=False, with_cp=False, zero_init_residual=True, init_cfg=None): super(ResNet3D, self).__init__(init_cfg) if depth not in self.arch_settings: raise KeyError(f'invalid depth {depth} for resnet') self.depth = depth self.stem_channels = stem_channels self.base_channels = base_channels self.num_stages = num_stages assert num_stages >= 1 and num_stages <= 4 self.depth_stride = depth_stride self.stem_depth_stride = stem_depth_stride self.stem_stride = stem_stride self.stem_pool = stem_pool self.strides = strides self.dilations = dilations assert len(strides) == len(dilations) == num_stages self.out_indices = out_indices self.shortcut_type = shortcut_type assert max(out_indices) < num_stages self.style = style self.deep_stem = deep_stem self.avg_down = avg_down self.frozen_stages = frozen_stages self.conv_cfg = conv_cfg self.norm_cfg = norm_cfg self.with_cp = with_cp self.norm_eval = norm_eval self.zero_init_residual = zero_init_residual self.block, stage_blocks = self.arch_settings[depth] self.stage_blocks = stage_blocks[:num_stages] self.expansion = get_expansion(self.block, expansion) conv1_stride = 2 if self.stem_stride else 1 #if not self.depth_stride: # conv1_stride = (1, ) + (conv1_stride, conv1_stride) # No stride in depth axis if not self.stem_depth_stride: conv1_stride = (1, ) + (conv1_stride, conv1_stride) # No stride in depth axis self._make_stem_layer(in_channels, stem_channels, conv1_stride) self.res_layers = [] _in_channels = stem_channels _out_channels = base_channels * self.expansion for i, num_blocks in enumerate(self.stage_blocks): if self.depth_stride: # Use stride in depth axis stride = strides[i] else: stride = (1, ) + (strides[i], strides[i]) # No stride in depth axis dilation = dilations[i] res_layer = self.make_res_layer( block=self.block, num_blocks=num_blocks, in_channels=_in_channels, out_channels=_out_channels, expansion=self.expansion, stride=stride, dilation=dilation, style=self.style, avg_down=self.avg_down, shortcut_type=self.shortcut_type, with_cp=with_cp, conv_cfg=conv_cfg, norm_cfg=norm_cfg) _in_channels = _out_channels _out_channels *= 2 layer_name = f'layer{i + 1}' self.add_module(layer_name, res_layer) self.res_layers.append(layer_name) self._freeze_stages() self.feat_dim = res_layer[-1].out_channels print(self) def make_res_layer(self, **kwargs): return ResLayer(**kwargs) @property def norm1(self): return getattr(self, self.norm1_name) def _make_stem_layer(self, in_channels, stem_channels, conv1_stride): if self.deep_stem: self.stem = nn.Sequential( ConvModule( in_channels, stem_channels // 2, kernel_size=3, stride=conv1_stride, padding=1, conv_cfg=self.conv_cfg, norm_cfg=self.norm_cfg, inplace=True), ConvModule( stem_channels // 2, stem_channels // 2, kernel_size=3, stride=1, padding=1, conv_cfg=self.conv_cfg, norm_cfg=self.norm_cfg, inplace=True), ConvModule( stem_channels // 2, stem_channels, kernel_size=3, stride=1, padding=1, conv_cfg=self.conv_cfg, norm_cfg=self.norm_cfg, inplace=True)) else: self.conv1 = build_conv_layer( self.conv_cfg, in_channels, stem_channels, kernel_size=7, #stride=2, stride=conv1_stride, padding=3, bias=False) self.norm1_name, norm1 = build_norm_layer(self.norm_cfg, stem_channels, postfix=1) self.add_module(self.norm1_name, norm1) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool3d(kernel_size=3, stride=2, padding=1) #self.maxpool = nn.MaxPool3d(kernel_size=3, stride=conv1_stride, padding=1) def _freeze_stages(self): if self.frozen_stages >= 0: if self.deep_stem: self.stem.eval() for param in self.stem.parameters(): param.requires_grad = False else: self.norm1.eval() for m in [self.conv1, self.norm1]: for param in m.parameters(): param.requires_grad = False for i in range(1, self.frozen_stages + 1): m = getattr(self, f'layer{i}') m.eval() for param in m.parameters(): param.requires_grad = False def init_weights(self): super(ResNet3D, self).init_weights() if self.init_cfg is not None: pretrained = self.init_cfg.get('checkpoint', None) else: pretrained = None if pretrained is not None: pretrained = pretrained[0] if pretrained is None: for m in self.modules(): if isinstance(m, nn.Conv2d): kaiming_init(m) elif isinstance(m, (_BatchNorm, nn.GroupNorm)): constant_init(m, 1) if self.zero_init_residual: for m in self.modules(): if isinstance(m, Bottleneck): constant_init(m.norm3, 0) elif isinstance(m, BasicBlock): constant_init(m.norm2, 0) def forward(self, x): if len(x.shape) == 4: x = x.unsqueeze(1) if self.deep_stem: x = self.stem(x) else: x = self.conv1(x) # [1,1,3,224,224] => [1,64,2,112,112] x = self.norm1(x) x = self.relu(x) if self.stem_pool: x = self.maxpool(x) print('output of stem layer', x.shape) outs = [] for i, layer_name in enumerate(self.res_layers): res_layer = getattr(self, layer_name) x = res_layer(x) print('output of layer', i ,x.shape, layer_name) if i in self.out_indices: outs.append(x) if len(outs) == 1: return outs[0] else: return tuple(outs) def train(self, mode=True): super(ResNet3D, self).train(mode) self._freeze_stages() if mode and self.norm_eval: for m in self.modules(): # trick: eval have effect on BatchNorm only if isinstance(m, _BatchNorm): m.eval() class ResNetV1d(ResNet3D): """ResNetV1d variant described in `Bag of Tricks <https://arxiv.org/pdf/1812.01187.pdf>`_. Compared with default ResNet(ResNetV1b), ResNetV1d replaces the 7x7 conv in the input stem with three 3x3 convs. And in the downsampling block, a 2x2 avg_pool with stride 2 is added before conv, whose stride is changed to 1. """ def __init__(self, **kwargs): super(ResNetV1d, self).__init__(deep_stem=True, avg_down=True, **kwargs) if __name__ == '__main__': ''' input torch.Size([1, 1, 3, 224, 224]) after conv1 torch.Size([1, 64, 3, 56, 56]) after stage 0 torch.Size([1, 64, 3, 56, 56]) after stage 1 torch.Size([1, 128, 3, 28, 28]) after stage 2 torch.Size([1, 256, 3, 14, 14]) after stage 3 torch.Size([1, 512, 3, 7, 7]) torch.Size([1, 512, 3, 7, 7]) ''' data = torch.ones((1, 1, 3, 224, 224)) # data = torch.ones((1, 1, 32, 32, 32)) model = ResNet3D(depth=18, num_stages=4, out_indices=(3, ),\ in_channels=1, conv_cfg=dict(type='Conv3d'), norm_cfg=dict(type='GN', num_groups=32, requires_grad=True), style='pytorch') output = model(data) print(output.shape) # [1, 512, 2, 14, 14] ``` *网络输出* * 示例 ResNet18-3D ```bash ResNet3D( # 1. 经过步长为2x2x2的7x7x7卷积对DWH降维 # 同时将通道数从1升到64维 (conv1): Conv3d(1, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3), bias=False) (gn1): GroupNorm(32, 64, eps=1e-05, affine=True) (relu): ReLU(inplace=True) (maxpool): MaxPool3d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False) # 2.1 stage1中, 通道与DWH均为改变 (layer1): ResLayer( (0): BasicBlock( (conv1): Conv3d(64, 64, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn1): GroupNorm(32, 64, eps=1e-05, affine=True) (conv2): Conv3d(64, 64, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn2): GroupNorm(32, 64, eps=1e-05, affine=True) (relu): ReLU(inplace=True) ) (1): BasicBlock( (conv1): Conv3d(64, 64, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn1): GroupNorm(32, 64, eps=1e-05, affine=True) (conv2): Conv3d(64, 64, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn2): GroupNorm(32, 64, eps=1e-05, affine=True) (relu): ReLU(inplace=True) ) ) # 2.2 stage2中, 首先对通道升维, D保持不变, WH降维 # 之后部分保持不变 (layer2): ResLayer( (0): BasicBlock( (conv1): Conv3d(64, 128, kernel_size=(3, 3, 3), stride=(1, 2, 2), padding=(1, 1, 1), bias=False) (gn1): GroupNorm(32, 128, eps=1e-05, affine=True) (conv2): Conv3d(128, 128, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn2): GroupNorm(32, 128, eps=1e-05, affine=True) (relu): ReLU(inplace=True) (downsample): Sequential( (0): Conv3d(64, 128, kernel_size=(1, 1, 1), stride=(1, 2, 2), bias=False) (1): GroupNorm(32, 128, eps=1e-05, affine=True) ) ) (1): BasicBlock( (conv1): Conv3d(128, 128, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn1): GroupNorm(32, 128, eps=1e-05, affine=True) (conv2): Conv3d(128, 128, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn2): GroupNorm(32, 128, eps=1e-05, affine=True) (relu): ReLU(inplace=True) ) ) # 2.3 stage3中, 首先对通道升维, D保持不变, WH降维 # 之后部分保持不变 (layer3): ResLayer( (0): BasicBlock( (conv1): Conv3d(128, 256, kernel_size=(3, 3, 3), stride=(1, 2, 2), padding=(1, 1, 1), bias=False) (gn1): GroupNorm(32, 256, eps=1e-05, affine=True) (conv2): Conv3d(256, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn2): GroupNorm(32, 256, eps=1e-05, affine=True) (relu): ReLU(inplace=True) (downsample): Sequential( (0): Conv3d(128, 256, kernel_size=(1, 1, 1), stride=(1, 2, 2), bias=False) (1): GroupNorm(32, 256, eps=1e-05, affine=True) ) ) (1): BasicBlock( (conv1): Conv3d(256, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn1): GroupNorm(32, 256, eps=1e-05, affine=True) (conv2): Conv3d(256, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn2): GroupNorm(32, 256, eps=1e-05, affine=True) (relu): ReLU(inplace=True) ) ) # 2.4 stage4中, 首先对通道升维, D保持不变, WH降维 # 之后部分保持不变 (layer4): ResLayer( (0): BasicBlock( (conv1): Conv3d(256, 512, kernel_size=(3, 3, 3), stride=(1, 2, 2), padding=(1, 1, 1), bias=False) (gn1): GroupNorm(32, 512, eps=1e-05, affine=True) (conv2): Conv3d(512, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn2): GroupNorm(32, 512, eps=1e-05, affine=True) (relu): ReLU(inplace=True) (downsample): Sequential( (0): Conv3d(256, 512, kernel_size=(1, 1, 1), stride=(1, 2, 2), bias=False) (1): GroupNorm(32, 512, eps=1e-05, affine=True) ) ) (1): BasicBlock( (conv1): Conv3d(512, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn1): GroupNorm(32, 512, eps=1e-05, affine=True) (conv2): Conv3d(512, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False) (gn2): GroupNorm(32, 512, eps=1e-05, affine=True) (relu): ReLU(inplace=True) ) ) ) ``` *网络每一层主要输出尺寸* * 网络 ResNet18-3D * 输入尺寸: 第一组: 3,224,224; 第二组: 32, 32, 32 * 降采样合计: 在D做了1次, 在W,H做了4次 ```bash # 第一组输入: data = torch.ones((1, 1, 3, 224, 224)) # 模型输出: model(data) output of stem layer torch.Size([1, 64, 2, 112, 112]) output of layer 0 torch.Size([1, 64, 2, 112, 112]) layer1 output of layer 1 torch.Size([1, 128, 2, 56, 56]) layer2 output of layer 2 torch.Size([1, 256, 2, 28, 28]) layer3 output of layer 3 torch.Size([1, 512, 2, 14, 14]) layer4 # 默认使用的输出 # 第二组输入: data = torch.ones((1, 1, 32, 32, 32)) # 模型输出: model(data) output of stem layer torch.Size([1, 64, 16, 16, 16]) output of layer 0 torch.Size([1, 64, 16, 16, 16]) layer1 output of layer 1 torch.Size([1, 128, 16, 8, 8]) layer2 output of layer 2 torch.Size([1, 256, 16, 4, 4]) layer3 output of layer 3 torch.Size([1, 512, 16, 2, 2]) layer4 # 默认使用的输出 ``` *输出头* * 作为分类网络示例, 可以在此后再增加全局平均池化层[&线性层..., ]&Softmax等输出最后的结果 THE END 本文作者:将夜 本文链接:https://zoe.red/2024/500.html 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。 最后修改:2024 年 04 月 16 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏