卷积神经网络的网络结构_典型卷积神经网络结构

卷积神经网络的网络结构_典型卷积神经网络结构GoogLeNet原文地址:GoingDeeperwithConvolutions:https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Szegedy_Going_Deeper_With_2015_CVPR_paper.pdfGoogLeNet在2014年由ChristianSzegedy提出,它是一种全新的深度学习结构。GoogLeNet网络的主要创新点在于:提出Inception结构在多个尺寸上同时进行卷积再聚合;

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

GoogLeNet网络简介

GoogLeNet原文地址:Going Deeper with Convolutions:https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Szegedy_Going_Deeper_With_2015_CVPR_paper.pdf
在这里插入图片描述

GoogLeNet在2014年由Christian Szegedy提出,它是一种全新的深度学习结构。

GoogLeNet网络的主要创新点在于:

  1. 提出Inception结构在多个尺寸上同时进行卷积再聚合;
    在这里插入图片描述

  2. 使用1X1的卷积进行降维以及映射处理;

  3. 添加两个辅助分类器帮助训练;
    辅助分类器是将中间某一层的输出用作分类,并按一个较小的权重加到最终分类结果中。

  4. 使用平均池化层代替全连接层,大大减少了参数量。

GoogLeNet网络结构

GoogLeNet的完整网络结构如下所示:
在这里插入图片描述
下面我们将其逐层拆分讲解并结合代码分析

Inception之前的几层结构

在进入Inception结构之前,GoogLeNet网络先堆叠了两个卷积(实则3个,有一个1X1的卷积)和两个最大池化层。

在这里插入图片描述

# input(3,224,224)
self.front = nn.Sequential(
    nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3),   # output(64,112,112)
    nn.ReLU(inplace=True),

    nn.MaxPool2d(kernel_size=3,stride=2,ceil_mode=True),    # output(64,56,56)

    nn.Conv2d(64,64,kernel_size=1),
    nn.Conv2d(64,192,kernel_size=3,stride=1,padding=1),     # output(192,56,56)
    nn.ReLU(inplace=True),

    nn.MaxPool2d(kernel_size=3,stride=2,ceil_mode=True),    # output(192,28,28)
)

Inception结构

在这里插入图片描述

Inception模块只会改变特征图的通道数,而不会改变尺寸大小。

Inception结构相对复杂,我们重新创建一个类来构建此结构,并通过参数不同的参数来控制各层的通道数。

class Inception(nn.Module):
    ''' in_channels: 输入通道数 out1x1:分支1输出通道数 in3x3:分支2的3x3卷积的输入通道数 out3x3:分支2的3x3卷积的输出通道数 in5x5:分支3的5x5卷积的输入通道数 out5x5:分支3的5x5卷积的输出通道数 pool_proj:分支4的最大池化层输出通道数 '''
    def __init__(self,in_channels,out1x1,in3x3,out3x3,in5x5,out5x5,pool_proj):
        super(Inception, 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,in3x3,kernel_size=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in3x3,out3x3,kernel_size=3,padding=1),
            nn.ReLU(inplace=True)
        )
        self.branch3 = nn.Sequential(
            nn.Conv2d(in_channels, in5x5, kernel_size=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in5x5, 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,pool_proj,kernel_size=1),
            nn.ReLU(inplace=True)
        )

    def forward(self,x):
        branch1 = self.branch1(x)
        branch2 = self.branch2(x)
        branch3 = self.branch3(x)
        branch4 = self.branch4(x)

        outputs = [branch1,branch2,branch3,branch4]
        return torch.cat(outputs,1)	# 按通道数叠加

Inception3a模块

在这里插入图片描述

# input(192,28,28)
self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32)		# output(256,28,28)

Inception3b + MaxPool

在这里插入图片描述

# input(256,28,28)
self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64)		# output(480,28,28)
self.maxpool3 = nn.MaxPool2d(3, stride=2, ceil_mode=True)			# output(480,14,14)

Inception4a

在这里插入图片描述

# input(480,14,14)
self.inception4a = Inception(480, 192, 96, 208, 16, 48, 64)		# output(512,14,14)

Inception4b

在这里插入图片描述

# input(512,14,14)
self.inception4b = Inception(512, 160, 112, 224, 24, 64, 64)		# output(512,14,14)

Inception4c

在这里插入图片描述

# input(512,14,14)
self.inception4c = Inception(512, 160, 112, 224, 24, 64, 64)		# output(512,14,14)

Inception4d

在这里插入图片描述

# input(512,14,14)
self.inception4d = Inception(512, 112, 144, 288, 32, 64, 64)		# output(528,14,14)

Inception4e+MaxPool

在这里插入图片描述

# input(528,14,14)
self.inception4e = Inception(528, 256, 160, 320, 32, 128, 128)	# output(832,14,14)
self.maxpool4 = nn.MaxPool2d(3, stride=2, ceil_mode=True)		# output(832,7,7)

Inception5a

在这里插入图片描述

# input(832,7,7)
self.inception5a = Inception(832, 256, 160, 320, 32, 128, 128)		# output(832,7,7)

Inception5b

在这里插入图片描述

# input(832,7,7)
self.inception5b = Inception(832, 384, 192, 384, 48, 128, 128)		# output(1024,7,7)

Inception之后的几层结构

在这里插入图片描述

辅助分类模块

除了以上主干网络结构以外,GoogLeNet还提供了两个辅助分类模块,用于将中间某一层的输出用作分类,并按一个较小的权重(0.3)加到最终分类结果。

与Inception模块一样,我们也重新创建一个类来搭建辅助分类模块结构。

class AccClassify(nn.Module):
	# in_channels: 输入通道
	# num_classes: 分类数
    def __init__(self,in_channels,num_classes):
        self.avgpool = nn.AvgPool2d(kernel_size=5, stride=3)
        self.conv = nn.MaxPool2d(in_channels, 128, kernel_size=1)  # output[batch, 128, 4, 4]
        self.relu = nn.ReLU(inplace=True)

        self.fc1 = nn.Linear(2048, 1024)
        self.fc2 = nn.Linear(1024, num_classes)

    def forward(self,x):
        x = self.avgpool(x)
        x = self.conv(x)
        x = self.relu(x)
        x = torch.flatten(x, 1)
        x = F.dropout(x, 0.5, training=self.training)
        x = F.relu(self.fc1(x), inplace=True)
        x = F.dropout(x, 0.5, training=self.training)
        x = self.fc2(x)

        return x

辅助分类模块1

第一个中间层输出位于Inception4a之后,将Inception4a的输出经过平均池化,1X1卷积和全连接后等到分类结果。
在这里插入图片描述

self.acc_classify1 = AccClassify(512,num_classes)

辅助分类模块2

在这里插入图片描述

self.acc_classify2 = AccClassify(528,num_classes)

整体网络结构

pytorch搭建完整代码

""" #-*-coding:utf-8-*- # @author: wangyu a beginner programmer, striving to be the strongest. # @date: 2022/7/5 18:37 """
import torch.nn as nn
import torch
import torch.nn.functional as F


class GoogLeNet(nn.Module):
    def __init__(self,num_classes=1000,aux_logits=True):
        super(GoogLeNet, self).__init__()
        self.aux_logits = aux_logits

        # input(3,224,224)
        self.front = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3),   # output(64,112,112)
            nn.ReLU(inplace=True),

            nn.MaxPool2d(kernel_size=3,stride=2,ceil_mode=True),    # output(64,56,56)

            nn.Conv2d(64,64,kernel_size=1),
            nn.Conv2d(64,192,kernel_size=3,stride=1,padding=1),     # output(192,56,56)
            nn.ReLU(inplace=True),

            nn.MaxPool2d(kernel_size=3,stride=2,ceil_mode=True),    # output(192,28,28)
        )

        # input(192,28,28)
        self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32)  # output(64+128+32+32=256,28,28)
        self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64)  # output(480,28,28)
        self.maxpool3 = nn.MaxPool2d(3, stride=2, ceil_mode=True)  # output(480,14,14)

        self.inception4a = Inception(480, 192, 96, 208, 16, 48, 64)  # output(512,14,14)
        self.inception4b = Inception(512, 160, 112, 224, 24, 64, 64)  # output(512,14,14)
        self.inception4c = Inception(512, 128, 128, 256, 24, 64, 64)  # output(512,14,14)
        self.inception4d = Inception(512, 112, 144, 288, 32, 64, 64)  # output(528,14,14)
        self.inception4e = Inception(528, 256, 160, 320, 32, 128, 128)  # output(832,14,14)
        self.maxpool4 = nn.MaxPool2d(3, stride=2, ceil_mode=True)  # output(832,7,7)

        self.inception5a = Inception(832, 256, 160, 320, 32, 128, 128)  # output(832,7,7)
        self.inception5b = Inception(832, 384, 192, 384, 48, 128, 128)  # output(1024,7,7)

        if self.training and self.aux_logits:
            self.acc_classify1 = AccClassify(512,num_classes)
            self.acc_classify2 = AccClassify(528,num_classes)

        self.avgpool = nn.AdaptiveAvgPool2d((1,1))        # output(1024,1,1)
        self.dropout = nn.Dropout(0.4)
        self.fc = nn.Linear(1024,num_classes)


    def forward(self,x):
        # input(3,224,224)
        x = self.front(x)       # output(192,28,28)

        x= self.inception3a(x)  # output(256,28,28)
        x = self.inception3b(x)
        x = self.maxpool3(x)

        x = self.inception4a(x)

        if self.training and self.aux_logits:
            classify1 = self.acc_classify1(x)

        x = self.inception4b(x)
        x = self.inception4c(x)
        x = self.inception4d(x)

        if self.training and self.aux_logits:
            classify2 = self.acc_classify2(x)

        x = self.inception4e(x)
        x = self.maxpool4(x)

        x = self.inception5a(x)
        x = self.inception5b(x)

        x = self.avgpool(x)
        x = torch.flatten(x,dims=1)
        x = self.dropout(x)
        x= self.fc(x)

        if self.training and self.aux_logits:
            return x,classify1,classify2

        return x


class Inception(nn.Module):
    ''' in_channels: 输入通道数 out1x1:分支1输出通道数 in3x3:分支2的3x3卷积的输入通道数 out3x3:分支2的3x3卷积的输出通道数 in5x5:分支3的5x5卷积的输入通道数 out5x5:分支3的5x5卷积的输出通道数 pool_proj:分支4的最大池化层输出通道数 '''
    def __init__(self,in_channels,out1x1,in3x3,out3x3,in5x5,out5x5,pool_proj):
        super(Inception, self).__init__()

        # input(192,28,28)
        self.branch1 = nn.Sequential(
            nn.Conv2d(in_channels, out1x1, kernel_size=1),
            nn.ReLU(inplace=True)
        )
        self.branch2 = nn.Sequential(
            nn.Conv2d(in_channels,in3x3,kernel_size=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in3x3,out3x3,kernel_size=3,padding=1),
            nn.ReLU(inplace=True)
        )
        self.branch3 = nn.Sequential(
            nn.Conv2d(in_channels, in5x5, kernel_size=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in5x5, 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,pool_proj,kernel_size=1),
            nn.ReLU(inplace=True)
        )

    def forward(self,x):
        branch1 = self.branch1(x)
        branch2 = self.branch2(x)
        branch3 = self.branch3(x)
        branch4 = self.branch4(x)

        outputs = [branch1,branch2,branch3,branch4]
        return torch.cat(outputs,1)


class AccClassify(nn.Module):
    def __init__(self,in_channels,num_classes):
        self.avgpool = nn.AvgPool2d(kernel_size=5, stride=3)
        self.conv = nn.MaxPool2d(in_channels, 128, kernel_size=1)  # output[batch, 128, 4, 4]
        self.relu = nn.ReLU(inplace=True)

        self.fc1 = nn.Linear(2048, 1024)
        self.fc2 = nn.Linear(1024, num_classes)

    def forward(self,x):
        x = self.avgpool(x)
        x = self.conv(x)
        x = self.relu(x)
        x = torch.flatten(x, 1)
        x = F.dropout(x, 0.5, training=self.training)
        x = F.relu(self.fc1(x), inplace=True)
        x = F.dropout(x, 0.5, training=self.training)
        x = self.fc2(x)

        return x

# print(GoogLeNet())

结构图

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170439.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • IDEA打jar包在服务器运行出现Error:Invalid or corrupt jarfile xxx.jar 报错+如何使用IDEA打jar包

    IDEA打jar包在服务器运行出现Error:Invalid or corrupt jarfile xxx.jar 报错+如何使用IDEA打jar包JAR包错误:Error:Invalidorcorruptjarfilexxx.jar错误背景:IDEA打包springboot项目为jar包。打包方法百度了好几种,结果都在运行时报了Error:Invalidorcorruptjarfilexxx.jar错误。…

    2022年8月22日
    7
  • 目标检测—利用labelimg制作自己的深度学习目标检测数据集

    目标检测—利用labelimg制作自己的深度学习目标检测数据集看了网上的xml转txt的博客很多上来就给代码,关于怎么用都不说,有的也用不了,所以这里自己写了一份代码

    2022年6月15日
    46
  • 一个标准的x.509数字证书包括哪些内容?(数字证书的功能是)

    1、什么叫数字签名数字签名:将报文按双方约定的HASH算法计算得到一个固定位数的报文摘要。在数学上保证:只要改动报文中任何一位,重新计算出的报文摘要值就会与原先的值不相符。这样就保证了报文的不可更改性。将该报文摘要值用发送者的私人密钥加密,然后连同原报文一起发送给接收者,而产生的报文即称数字签名2、什么叫数字证书数字证书:数字证书就是互联网通讯中标志通讯各方身份信息的一系列数据,提供了一种在In

    2022年4月15日
    125
  • Allure趋势图本地显示

    Allure趋势图本地显示Allure趋势图本地显示众所周知,allure趋势图在本地运行的时候,总是显示的空白,但与Jenkins集成后,生成的报告却显示了整个趋势如果不与Jenkins集成就真的没办法展示趋势图吗?答案是NO,没有趋势图我们就自己写????一、首先看下Jenkins集成allure展示的趋势图是什么样子的展示了每次运行的结果对应构建的次数点击可以跳转到对应的构建结果报告整体趋势一目了然二、研究Jenkins生成的allure报告有什么规律1、打开家目录中的.jenkins/jobs/te

    2022年7月26日
    34
  • js call方法_recall

    js call方法_recallJacvaScript中的call()方法和apply()方法,1.每个函数都包含两个非继承而来的方法:call()方法和apply()方法。2.相同点:这两个方法的作用是一样的。都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖以运行的作用域。

    2025年8月9日
    1
  • vue 引用mockjs 模拟真实的数据

    vue 引用mockjs 模拟真实的数据vue mockjs 模拟数据 实现前后端分离开发在项目中尝试了 mockjs mock 数据 实现前后端分离开发 关于 mockjs 官网描述的是 1 前后端分离 2 不需要修改既有代码 就可以拦截 Ajax 请求 返回模拟的响应数据 3 数据类型丰富 4 通过随机数据 模拟各种场景 等等优点 总结起来就是在后端接口没有开发完成之前 前端可以用已有的接口文档 在真实的请求上拦

    2025年8月26日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号