用pytorch实现google net

   GoogleNet(也称为Inception v1)是由Google在2014年提出的一个深度卷积神经网络架构。它在ImageNet Large Scale Visual Recognition Challenge (ILSVRC) 2014比赛中取得了优秀的成绩,并引起了广泛的关注。
   GoogleNet的设计目标是构建一个更深的网络架构,以提高准确性,并通过减少网络参数的数量来降低过拟合的风险。它采用了"Inception"模块,其中包含多个并行的卷积层和池化层,这使得网络能够同时捕捉不同尺度的特征。
Inception模块通过使用不同大小的卷积核和池化操作,可以在不同的感受野尺度上提取特征。这样的设计允许网络在不同层级上学习到更具判别力的特征,并且不会对图像进行显式的尺度改变或池化。此外,为了减少计算量和参数数量,Inception模块还引入了1x1卷积核,用于降低通道的维度。
     整个GoogleNet架构包含多个堆叠的Inception模块,并通过使用池化层和丢弃层来减小特征图的尺寸和防止过拟合。最后,全连接层用于输出最终的分类结果。
     GoogleNet的创新点在于其深度和复杂度,并且采用了多个Inception模块的并行组合,使得模型能够同时学习到不同尺度和层次的特征。这使得GoogleNet在图像分类等计算机视觉任务中表现出色,并为后续网络架构的发展提供了启示。

1Inception结构      

在GoogLeNet中,基本的卷积块被称为Inception块(Inception block)。这很可能得名于电影《盗梦空间》(Inception),因为电影中的一句话“我们需要走得更深”(“We need to go deeper”)。引入Inception结构(融入不同尺度的特征信息,即融合不同尺寸的感受野)

 2、使用1x1的卷积核进行降维以及映射处理

 3、整体网络结构

import torch
import torch.nn as nn


# 定义Inception模块
class InceptionModule(nn.Module):
    def __init__(self, in_channels, out1x1, reduce3x3, out3x3, reduce5x5, out5x5, out1x1pool):
        super(InceptionModule, self).__init__()

        self.branch1 = nn.Sequential(
            nn.Conv2d(in_channels, out1x1, kernel_size=1),
            nn.ReLU(inplace=True)
        )

        self.branch2 = nn.Sequential(
            nn.Conv2d(in_channels, reduce3x3, kernel_size=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(reduce3x3, out3x3, kernel_size=3, padding=1),
            nn.ReLU(inplace=True)
        )

        self.branch3 = nn.Sequential(
            nn.Conv2d(in_channels, reduce5x5, kernel_size=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(reduce5x5, out5x5, kernel_size=5, padding=2),
            nn.ReLU(inplace=True)
        )

        self.branch4 = nn.Sequential(
            nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
            nn.Conv2d(in_channels, out1x1pool, kernel_size=1),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        out1 = self.branch1(x)
        out2 = self.branch2(x)
        out3 = self.branch3(x)
        out4 = self.branch4(x)
        out = torch.cat([out1, out2, out3, out4], 1)
        return out


# 定义GoogLeNet模型
class GoogLeNet(nn.Module):
    def __init__(self, num_classes=10):
        super(GoogLeNet, self).__init__()

        self.conv1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)
        )

        self.conv2 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 192, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)
        )

        self.inception1 = InceptionModule(192, 64, 96, 128, 16, 32, 32)
        self.inception2 = InceptionModule(256, 128, 128, 192, 32, 96, 64)

        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)

        self.inception3 = nn.Sequential(
            InceptionModule(480, 192, 96, 208, 16, 48, 64),
            InceptionModule(512, 160, 112, 224, 24, 64, 64),
            InceptionModule(512, 128, 128, 256, 24, 64, 64),
            InceptionModule(512, 112, 144, 288, 32, 64, 64),
            InceptionModule(528, 256, 160, 320, 32, 128, 128),
            nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)
        )

        self.inception4 = nn.Sequential(
            InceptionModule(832, 256, 160, 320, 32, 128, 128),
            InceptionModule(832, 384, 192, 384, 48, 128, 128),
            nn.AdaptiveAvgPool2d((1, 1))
        )

        self.dropout = nn.Dropout(0.4)
        self.fc = nn.Linear(1024, num_classes)

    def forward(self, x):
        out = self.conv1(x)
        out = self.conv2(out)
        out = self.inception1(out)
        out = self.inception2(out)
        out = self.maxpool(out)
        out = self.inception3(out)
        out = self.inception4(out)
        out = out.view(out.size(0), -1)
        out = self.dropout(out)
        out = self.fc(out)
        return out


# 创建GoogLeNet模型实例
model = GoogLeNet()

在上面的代码中,定义了一个 InceptionModule 类,用于创建GoogLeNet中的Inception模块。然后,我们使用这个自定义模块构建了GoogLeNet模型,其中包括多个 InceptionModule 实例作为模型的层。

可以根据需要自定义 InceptionModule 类的参数,例如输入通道数、各个分支的输出通道数和卷积核大小等。同时,你也可以调整 GoogLeNet 类中的层次结构和参数,以适应你的特定任务。

猜你喜欢

转载自blog.csdn.net/Metal1/article/details/132371542