常用的神经网络实现

VGG16

from torch import nn
class VGG(nn.Module):
    """ 一共6个版本,最常用VGG16
        VGG采用五组卷积,三个全连接,最后用Softmax分类
        VGG显著特点:每次经过池化层maxpool后特征图尺寸减小一倍,,通道数增加一倍(最后一个池化层除外)"""
    def __init__(self, num_classes=1000):
        super(VGG, self).__init__()
        layers = []  # 保存所有卷积层
        in_dim = 3
        out_dim = 64
        """ 循环构造卷积层,一共有13个卷积层 """
        for i in range(13):
            layers+= [nn.Conv2d(in_dim,out_dim, 3, 1, 1), nn.ReLU(inplace=True)]
            in_dim= out_dim
            if i in [1,3,6,9,12]:  # 增加池化层
                layers+= [nn.MaxPool2d(2,2)]
                if i!= 9:  #第10个卷积后保持和前面通道数一致
                    out_dim*= 2
        self.features= nn.Sequential(*layers)
        # VGGNet的3个全连接层,中间有ReLU和Dropout层
        self.classifier= nn.Sequential(
            nn.Linear(512*7*7, 4096),
            nn.ReLU(True),
            nn.Dropout(),

            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, num_classes)
        )

    def forward(self, x):
        """ Input:224*224 RGB """
        x= self.features(x)
        # 这里降特征图维度从[1,512,7,7]变到[1,512*7*7]
        x= x.view(x.size(0), -1)
        x= self.classifier(x)
        return x

DenseNet

import torch
from torch import nn
import torch.nn.functional as F
""" 实现一个Bottlenect,初始化需要输入的通道数与GrowthRate"""
class Bottleneck(nn.Module):
    """ Bottlenect"""
    def __init__(self, nChannels, growthRate):
        super(Bottleneck, self).__init__()
        #通常1x1卷积的通道数为GrowthRate的4倍
        interChannels= 4*growthRate
        self.bn1= nn.BatchNorm2d(nChannels)
        self.conv1= nn.Conv2d(nChannels,interChannels, kernel_size=1, bias=False)
        self.bn2= nn.BatchNorm2d(interChannels)
        self.conv2= nn.Conv2d(interChannels,growthRate, kernel_size=3, padding=1, bias=False)
    def forward(self, x):
        out= self.conv1(F.relu(self.bn1(x)))
        out= self.conv2(F.relu(self.bn2(out)))
        #将输入x同计算的结果out进行通道拼接
        out= torch.cat((x,out), 1)
        return out

class Denseblock(nn.Module):
    """ DenseNet"""
    def __init__(self, nChannels, growthRate, nDenseBlocks):
        super(Denseblock, self).__init__()
        layers= []
        #将每一个Bottleneck利用nn.Sequential整合,输入通道需要线性增长
        for i in range(int(nDenseBlocks)):
            layers.append(Bottleneck(nChannels, growthRate))
            nChannels+= growthRate
        self.denseblock= nn.Sequential(*layers)
    def forward(self, x):
        return self.denseblock(x)

inceptionV1

import torch
from torch import nn
import torch.nn.functional as F

""" 首先定义一个包含conv后relu的基础卷积类 """
class BasicConv2d(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, padding=0):
        super(BasicConv2d, self).__init__()
        self.conv= nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding)
    def forward(self, x):
        x= self.conv(x)
        return F.relu(x, inplace=True)

""" InceptionV1类,初始化时需要提供各个子模块的通道数大小 """
class InceptionV1(nn.Module):
    def __init__(self, in_dim, hid_1_1, hid_2_1, hid_2_3, hid_3_1, out_3_5, out_4_1):
        super(InceptionV1, self).__init__()
        """ 定义四个子模块 """
        self.branch1x1= BasicConv2d(in_dim, hid_1_1, 1)  #1×1卷积降维
        self.branch3x3= nn.Sequential(
            BasicConv2d(in_dim, hid_2_1, 1),
            BasicConv2d(hid_2_1, hid_2_3, 3, padding=1)
        )
        self.branch5x5= nn.Sequential(
            BasicConv2d(in_dim, hid_3_1, 1),
            BasicConv2d(hid_3_1, out_3_5, 5, padding=2)
        )
        self.branch_pool= nn.Sequential(
            nn.MaxPool2d(3, stride=1, padding=1),
            BasicConv2d(in_dim, out_4_1, 1)
        )
    def forward(self, x):
        b1= self.branch1x1(x)
        b2= self.branch3x3(x)
        b3= self.branch5x5(x)
        b4= self.branch_pool(x)
        #将这四个子模块沿着通道方向拼接
        output= torch.cat((b1, b2, b3, b4), dim=1)
        return output

Resnet Bottlenect

由ResNet提出的子模块,通过引入一个shortcut分支,将需要拟合的映射变为残差映射F(x):H(x)-x

ResNet假设:相较于直接优化潜在映射H(x),优化残差映射F(x)更为容易,残差模块称为Bottlenect

ResNet50主要部分在于中间的4个大的卷积组,而这4个卷积组分别包含了3,4,5这3个Bottlenect模块,最后经过一个全局平均池化使得特征图大小变为1x1,然后进行1000维的全连接,最后softmax输出分类得分

F(x)+x是逐通道进行相加,根据通道数是否相同,存在两种Bottlenect结构,通道数不同时,例如每个卷积组的第一个Bottlenect,需要利用1x1卷积对x进行DownSample操作,将通道数变为相同,再相加,对于通道数相同情况,则直接相加

from torch import nn
class Bottlenect(nn.Module):
    def __init__(self, in_dim, out_dim, stride=1):
        super(Bottlenect, self).__init__()
        self.bottlenect= nn.Sequential(
            nn.Conv2d(in_dim, in_dim, 1, bias=False),
            nn.BatchNorm2d(in_dim),
            nn.ReLU(inplace= True),

            nn.Conv2d(in_dim, in_dim, 3, stride, 1, bias=False),
            nn.BatchNorm2d(in_dim),
            nn.ReLU(inplace= True),

            nn.Conv2d(in_dim, out_dim, 1, bias= False),
            nn.BatchNorm2d(out_dim)
        )
        self.relu= nn.ReLU(inplace= True)
        """ DownSample部分是由一个包含BN层的1x1卷积组成 """
        self.downsample= nn.Sequential(
            nn.Conv2d(in_dim, out_dim, 1, 1),
            nn.BatchNorm2d(out_dim)
        )

    def forward(self, x):
        identify= x
        out= self.bottlenect(x)
        identify= self.downsample(x)
        #将identify恒等映射与网络堆叠层输出相加,并经过ReLU后输出
        out+= identify
        out= self.relu(out)
        out= self.relu(out)
        return out

对上述网络进行测试

"""Test"""
import torch

"""----------test VGG16----------"""
from vgg16 import VGG
vgg= VGG(21).cuda()
input= torch.randn(1, 3, 224,224).cuda()
score= vgg(input)
print(score.shape)
# 单独调用卷积模块,输出最后一张特征图
features= vgg.features(input)
print(features.shape)
print(vgg.classifier)

"""----------test InceptionV1----------"""
from inceptionV1 import InceptionV1
net_inceptionv1= InceptionV1(3, 64, 32, 64, 64, 96, 32).cuda()
print(net_inceptionv1)
input= torch.randn(1, 3, 256,256).cuda()
print("input.shape:",input.shape, "\noutput.shape:",net_inceptionv1(input).shape)  #32+96+64+64

"""----------test ResNet50.Bottlenect----------"""
from resnet_bottlenect import Bottlenect
bottlenect_1_1= Bottlenect(64, 256).cuda()
print(bottlenect_1_1)
input= torch.randn(1, 64, 56, 56).cuda()
outpupt= bottlenect_1_1(input)
print("input.shape:",input.shape, "output.shape:",outpupt.shape)
posted @ 2024-11-02 18:37  sgqmax  阅读(31)  评论(0)    收藏  举报