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

本文介绍了3DResNet系列网络,由KenshoHara等人提出。文章详细阐述了网络结构,包括3x3x3卷积、Bottleneck层和ResNet的实现,其中重点解析了Bottleneck类的定义和ResNet类的构建过程,适用于3D图像处理和视频分析。
摘要由CSDN通过智能技术生成

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

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


import math
import torch
import torch.nn as nn
import torch.nn.functional 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(nn.Module):
    expansion = 4

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

        self.stride     = 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 * self.expansion, kernel_size=1, bias=False)
        self.bn3        = nn.BatchNorm3d(planes * self.expansion)
        self.relu       = nn.ReLU(inplace=True)
        self.downsample = downsample

    def forward(self, x):
        residual = x

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

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

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

        if self.downsample is not None:
            residual = self.downsample(x)

        out = out + residual
        out = self.relu(out)
        return out

#------------------------------#
# 此为对于ResNet的定义
# 这里需要注意,如果更换了数据集,那么
# 我们就要更换相应的num_classes值!!!
#------------------------------#
class ResNet(nn.Module):
    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)
        self.relu      = nn.ReLU(inplace=True)
        self.maxpool   = 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)
        #-------------------------------------#
        # math.ceil()方法的含义为向上取整
        # 之后又加了一个int限制,以充分确保该数为整数
        #-------------------------------------#
        last_duration  = int(math.ceil(sample_duration / 16))
        last_size      = int(math.ceil(sample_size / 32))

        self.avgpool   = nn.AvgPool3d((last_duration, last_size, last_size), stride=1)
        self.fc        = nn.Linear(512 * block.expansion, num_classes)

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

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

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

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

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

        self.in_planes = planes * block.expansion

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

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

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

        x = self.avgpool(x)

        x = x.view(x.size(0), -1)
        x = self.fc(x)

        return x
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XuecWu3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值