【CVPR2018 3D ResNet】3D ResNet网络结构详解

时间:2025-04-28 13:58:12

3D ResNet系列网络由*家工业科学技术研究院的Kensho Hara等人提出。接下来,我将对3D ResNet系列网络做出详细的网络结构解释,欢迎大家补充与提问。

我的github链接主页为XuecWu (Conna) · GitHub


import math
import torch
import  as nn
import  as F
from functools import partial


def conv3x3x3(in_planes, out_planes, stride=1):
    # 3x3x3 convolution with padding
    return nn.Conv3d(in_channels=in_planes, out_channels=out_planes, kernel_size=3, stride=stride, padding=1,bias=False)

#------------------------------------#
# 此为对于ResNet-101中的Bottleneck的定义
#------------------------------------#
class Bottleneck():
    expansion = 4

    def __init__(self, in_planes, planes, stride=1, downsample=None):
        super(Bottleneck, self).__init__()

             = stride
        self.conv1      = nn.Conv3d(in_planes, planes, kernel_size=1, bias=False)
        self.bn1        = nn.BatchNorm3d(planes)
        self.conv2      = nn.Conv3d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2        = nn.BatchNorm3d(planes)
        self.conv3      = nn.Conv3d(planes, planes * , kernel_size=1, bias=False)
        self.bn3        = nn.BatchNorm3d(planes * )
               = (inplace=True)
         = downsample

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = (out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = (out)

        out = self.conv3(out)
        out = self.bn3(out)

        if  is not None:
            residual = (x)

        out = out + residual
        out = (out)
        return out

#------------------------------#
# 此为对于ResNet的定义
# 这里需要注意,如果更换了数据集,那么
# 我们就要更换相应的num_classes值!!!
#------------------------------#
class ResNet():
    def __init__(self, block, layers, sample_size, sample_duration, shortcut_type='B', num_classes=8):
        super(ResNet, self).__init__()

        self.in_planes = 64
        self.conv1     = nn.Conv3d(3, 64, kernel_size=7, stride=(1, 2, 2), padding=(3, 3, 3), bias=False)
        self.bn1       = nn.BatchNorm3d(64)
              = (inplace=True)
           = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=2, padding=1)
        self.layer1    = self._make_layer(block, 64, layers[0], shortcut_type)
        self.layer2    = self._make_layer(block, 128, layers[1], shortcut_type, stride=2)
        self.layer3    = self._make_layer(block, 256, layers[2], shortcut_type, stride=2)
        self.layer4    = self._make_layer(block, 512, layers[3], shortcut_type, stride=2)
        #-------------------------------------#
        # ()方法的含义为向上取整
        # 之后又加了一个int限制,以充分确保该数为整数
        #-------------------------------------#
        last_duration  = int((sample_duration / 16))
        last_size      = int((sample_size / 32))

           = nn.AvgPool3d((last_duration, last_size, last_size), stride=1)
                = (512 * , num_classes)

        for m in ():
            if isinstance(m, nn.Conv3d):
                .kaiming_normal_(, mode='fan_out') #对于3D卷积所采用的权重初始化方法

            elif isinstance(m, nn.BatchNorm3d):
                .fill_(1)
                .zero_()

    def _make_layer(self, block, planes, blocks, shortcut_type, stride=1):
        downsample = None

        if stride != 1 or self.in_planes != planes * :
            if shortcut_type == 'A':
                assert True, 'Not implemented!!!'
            else:
                downsample = (
                    nn.Conv3d(self.in_planes, planes * , kernel_size=1, stride=stride, bias=False),
                    nn.BatchNorm3d(planes * ),)

        layers = []
        (block(self.in_planes, planes, stride, downsample))

        self.in_planes = planes * 

        for i in range(1, blocks):
            (block(self.in_planes, planes))
        return (*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = (x)
        x = (x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = (x)

        x = ((0), -1)
        x = (x)

        return x