首页 > 其他分享 >卷积神经网络模型之——GoogLeNet网络结构与代码实现

卷积神经网络模型之——GoogLeNet网络结构与代码实现

时间:2022-12-06 10:31:33浏览次数:49  
标签:14 nn 卷积 self GoogLeNet 64 Inception output 网络结构


文章目录

  • ​​GoogLeNet网络简介​​
  • ​​GoogLeNet网络结构​​
  • ​​Inception之前的几层结构​​
  • ​​Inception结构​​
  • ​​Inception3a模块​​
  • ​​Inception3b + MaxPool​​
  • ​​Inception4a​​
  • ​​Inception4b​​
  • ​​Inception4c​​
  • ​​Inception4d​​
  • ​​Inception4e+MaxPool​​
  • ​​Inception5a​​
  • ​​Inception5b​​
  • ​​Inception之后的几层结构​​
  • ​​辅助分类模块​​
  • ​​辅助分类模块1​​
  • ​​辅助分类模块2​​
  • ​​整体网络结构​​
  • ​​pytorch搭建完整代码​​
  • ​​结构图​​

GoogLeNet网络简介

GoogLeNet原文地址:Going Deeper with Convolutions:

卷积神经网络模型之——GoogLeNet网络结构与代码实现_cnn

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

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

  1. 提出Inception结构在多个尺寸上同时进行卷积再聚合;
  2. 卷积神经网络模型之——GoogLeNet网络结构与代码实现_深度学习_02

  3. 使用1X1的卷积进行降维以及映射处理;
  4. 添加两个辅助分类器帮助训练;
    辅助分类器是将中间某一层的输出用作分类,并按一个较小的权重加到最终分类结果中。
  5. 使用平均池化层代替全连接层,大大减少了参数量。

GoogLeNet网络结构

GoogLeNet的完整网络结构如下所示:

卷积神经网络模型之——GoogLeNet网络结构与代码实现_深度学习_03


下面我们将其逐层拆分讲解并结合代码分析

Inception之前的几层结构

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

卷积神经网络模型之——GoogLeNet网络结构与代码实现_cnn_04

# 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结构

卷积神经网络模型之——GoogLeNet网络结构与代码实现_GoogLeNet_05

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模块

卷积神经网络模型之——GoogLeNet网络结构与代码实现_cnn_06

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

Inception3b + MaxPool

卷积神经网络模型之——GoogLeNet网络结构与代码实现_GoogLeNet_07

# 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

卷积神经网络模型之——GoogLeNet网络结构与代码实现_GoogLeNet_08

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

Inception4b

卷积神经网络模型之——GoogLeNet网络结构与代码实现_pytorch_09

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

Inception4c

卷积神经网络模型之——GoogLeNet网络结构与代码实现_神经网络_10

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

Inception4d

卷积神经网络模型之——GoogLeNet网络结构与代码实现_cnn_11

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

Inception4e+MaxPool

卷积神经网络模型之——GoogLeNet网络结构与代码实现_神经网络_12

# 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

卷积神经网络模型之——GoogLeNet网络结构与代码实现_GoogLeNet_13

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

Inception5b

卷积神经网络模型之——GoogLeNet网络结构与代码实现_神经网络_14

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

Inception之后的几层结构

卷积神经网络模型之——GoogLeNet网络结构与代码实现_pytorch_15

辅助分类模块

除了以上主干网络结构以外,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卷积和全连接后等到分类结果。

卷积神经网络模型之——GoogLeNet网络结构与代码实现_深度学习_16

self.acc_classify1 = AccClassify(512,num_classes)

辅助分类模块2

卷积神经网络模型之——GoogLeNet网络结构与代码实现_神经网络_17

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())

结构图

卷积神经网络模型之——GoogLeNet网络结构与代码实现_pytorch_18


标签:14,nn,卷积,self,GoogLeNet,64,Inception,output,网络结构
From: https://blog.51cto.com/u_15901218/5914794

相关文章

  • 深度学习基础课:卷积神经网络与卷积层的前向传播推导
    大家好~本课程为“深度学习基础班”的线上课程,带领同学从0开始学习全连接和卷积神经网络,进行数学推导,并且实现可以运行的Demo程序线上课程资料:本节课录像回放1加QQ群,获......
  • 对深度学习中全连接层、卷积层的一些理解
    1、全连接层卷积层和全连接层构成了构成了特征提取器,而全连接层构成了分类器,全连接层将特征提取得到的特征图映射成一维特征向量,该特征向量包含所有特征信息,可以转化为分......
  • 机器学习之深度学习-卷积
    人工智能、机器学习和深度学习三者的联系与区别人工智能(Artificialintelligence)简称AI。人工智能是计算机科学的一个分支,它企图了解智能的本质,并生产出一种新的能以人......
  • 卷积神经网络(CNN)(中)
    4.GoogleNet GoogleNet(也可称作Inception)是在2014年由GoogleDeepMind公式的研究员提出的一种全新的深度学习结构,并取得了ILSVRC2014比赛项目的第一名。GooleNet共有22层,并......
  • PyTorch复现GoogleNet学习笔记
    PyTorch复现GoogleNet学习笔记一篇简单的学习笔记,实现五类花分类,这里只介绍复现的一些细节如果想了解更多有关网络的细节,请去看论文《GoingDeeperwithConvolutions》......
  • 从LeNet-5看懂卷积神经网络结构
    LeNet-5出自论文Gradient-BasedLearningAppliedtoDocumentRecognition,是一种用于手写体字符识别的非常高效的卷积神经网络。论文下载一、卷积神经网络(ConvolutionalN......
  • 【学习笔记】狄利克雷卷积
    狄利克雷卷积数论函数:陪域:包含值域的任意集合。数论函数:一类定义域是正整数,陪域为复数的函数。设\(f\),\(g\)为数论函数:加法:\((f+g)(x)=f(x)+g(x)\)数乘:\((......
  • 卷积神经网络
    title:卷积神经网络date:2022-06-1920:46:59tags:-cvcategories:-note-DeepLearning目录:什么是卷积?reference什么是卷积?举个例子:图1:为一个人进食的......
  • CV笔记:卷积与转置卷积
    1.抽丝剥茧理解转置卷积:https://blog.csdn.net/tsyccnh/article/details/87357447阿斯顿2.计算原理与API使用参考https://blog.csdn.net/qq_37541097/article/details/1......
  • 狄利克雷卷积
    补充一下莫比乌斯反演的前置知识狄利克雷乘积(Dirichletproduct)亦称狄利克雷卷积、卷积,是数论函数的重要运算之一。设f(n)、g(n)是两个数论函数,它们的Dirichlet(狄利克......