当前位置:网站首页>Convolutional neural network model -- googlenet network structure and code implementation
Convolutional neural network model -- googlenet network structure and code implementation
2022-07-25 13:08:00 【1 + 1= Wang】
List of articles
GoogLeNet Network profile
GoogLeNet Original address :Going Deeper with Convolutions:https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Szegedy_Going_Deeper_With_2015_CVPR_paper.pdf
GoogLeNet stay 2014 Year by year Christian Szegedy Put forward , It is a new deep learning structure .
GoogLeNet The main innovation of the network is :
Put forward Inception The structure is convoluted and repolymerized simultaneously on multiple sizes ;

Use 1X1 The convolution of is used for dimension reduction and mapping ;
Add two auxiliary classifiers to help train ;
Auxiliary classifier is to use the output of a certain middle layer as a classification , And add a smaller weight to the final classification result .Use the average pool layer instead of the full connection layer , Greatly reduced the number of parameters .
GoogLeNet Network structure
GoogLeNet The complete network structure of is as follows :
Next, we will explain it layer by layer and combine it with code analysis
Inception Previous layers
When entering Inception Before the structure ,GoogLeNet The network first stacks two convolutions ( But in fact 3 individual , There is one 1X1 Convolution of ) And two maximum pooling layers .

# 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 structure

Inception The module will only change the number of channels of the characteristic graph , Without changing the size .
Inception The structure is relatively complex , We recreate a class to build this structure , The number of channels in each layer is controlled by different parameters .
class Inception(nn.Module):
''' in_channels: Enter the number of channels out1x1: Branch 1 Number of output channels in3x3: Branch 2 Of 3x3 Number of input channels for convolution out3x3: Branch 2 Of 3x3 The number of output channels of convolution in5x5: Branch 3 Of 5x5 Number of input channels for convolution out5x5: Branch 3 Of 5x5 The number of output channels of convolution pool_proj: Branch 4 Maximum number of pooled layer output channels '''
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) # Stack by channel number
Inception3a modular

# 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 The next several layers

Auxiliary classification module
In addition to the above backbone network structure ,GoogLeNet It also provides two auxiliary classification modules , Used to use the output of an intermediate layer as a classification , And press a smaller weight (0.3) Add to the final classification result .
And Inception The modules are the same , We also re create a class to build the auxiliary classification module structure .
class AccClassify(nn.Module):
# in_channels: Input channel
# num_classes: Number of categories
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
Auxiliary classification module 1
The first m-server output is located at Inception4a after , take Inception4a The output of is average pooled ,1X1 After convolution and full connection, wait until the classification result .
self.acc_classify1 = AccClassify(512,num_classes)
Auxiliary classification module 2

self.acc_classify2 = AccClassify(528,num_classes)
Overall network structure
pytorch Build complete code
""" #-*-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: Enter the number of channels out1x1: Branch 1 Number of output channels in3x3: Branch 2 Of 3x3 Number of input channels for convolution out3x3: Branch 2 Of 3x3 The number of output channels of convolution in5x5: Branch 3 Of 5x5 Number of input channels for convolution out5x5: Branch 3 Of 5x5 The number of output channels of convolution pool_proj: Branch 4 Maximum number of pooled layer output channels '''
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())
chart

边栏推荐
- “蔚来杯“2022牛客暑期多校训练营2 补题题解(G、J、K、L)
- [机器学习] 实验笔记 – 表情识别(emotion recognition)
- Oran special series-21: major players (equipment manufacturers) and their respective attitudes and areas of expertise
- Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)
- Deep learning MEMC framing paper list
- Go: Gin custom log output format
- [operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+
- [CSDN year-end summary] end and start, always on the way - "2021 summary of" 1+1= Wang "
- Deployment of Apache website services and implementation of access control
- Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem
猜你喜欢
![[review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]](/img/fb/6ca8e0eb57c76c515e2aae68e9e549.png)
[review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]

What is ci/cd?

yum和vim须掌握的常用操作

卷积核越大性能越强?一文解读RepLKNet模型

网络空间安全 渗透攻防9(PKI)
![[problem solving] org.apache.ibatis.exceptions PersistenceException: Error building SqlSession. 1-byte word of UTF-8 sequence](/img/fd/245306273e464c04f3292132fbfa2f.png)
[problem solving] org.apache.ibatis.exceptions PersistenceException: Error building SqlSession. 1-byte word of UTF-8 sequence

【AI4Code】《GraphCodeBERT: Pre-Training Code Representations With DataFlow》 ICLR 2021

A turbulent life

【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21

Shell common script: check whether a domain name and IP address are connected
随机推荐
机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理
2022.07.24 (lc_6125_equal row and column pairs)
[300 opencv routines] 239. accurate positioning of Harris corner detection (cornersubpix)
Docekr学习 - MySQL8主从复制搭建部署
Substance Designer 2021软件安装包下载及安装教程
7行代码让B站崩溃3小时,竟因“一个诡计多端的0”
Oran special series-21: major players (equipment manufacturers) and their respective attitudes and areas of expertise
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
Perf performance debugging
[机器学习] 实验笔记 – 表情识别(emotion recognition)
【运维、实施精品】月薪10k+的技术岗位面试技巧
mysql函数汇总之日期和时间函数
Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier
录制和剪辑视频,如何解决占用空间过大的问题?
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
[review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]
The larger the convolution kernel, the stronger the performance? An interpretation of replknet model
Microsoft proposed CodeT: a new SOTA for code generation, with 20 points of performance improvement
The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep
【问题解决】ibatis.binding.BindingException: Type interface xxDao is not known to the MapperRegistry.