【图像分类】【深度学习】【Pytorch版本】 GoogLeNet(InceptionV3)模型算法详解

【图像分类】【深度学习】【Pytorch版本】 GoogLeNet(InceptionV3)模型算法详解

文章目录

  • 【图像分类】【深度学习】【Pytorch版本】 GoogLeNet(InceptionV3)模型算法详解
  • 前言
  • GoogLeNet(InceptionV3)讲解
    • Factorized Convolutions卷积分解
    • InceptionV3结构Ⅰ
    • InceptionV3结构Ⅱ
    • InceptionV3结构Ⅲ
    • InceptionV3特殊结构
    • 辅助分类器
    • GoogLeNet(InceptionV3)模型结构
  • GoogLeNet(InceptionV3) Pytorch代码
  • 完整代码
  • 总结


前言

GoogLeNet(InceptionV3)是由谷歌的Szegedy, Christian等人在《Rethinking the Inception Architecture for Computer Vision【CVPR-2016】》【论文地址】一文中提出的带有Factorized Convolutions(分解卷积)的改进模型,即在InceptionV2的基础上将标准的卷积操作分解为1xN卷积和Nx1卷积两个步骤,这种分解模式可以显著降低参数量和计算量,同时在一定程度上保持了特征提取的能力。


GoogLeNet(InceptionV3)讲解

GoogLeNet团队在给出了一些通用的网络设计准则,以期望在不提高网络参数量的前提下提升网络的表达能力:

  1. 避免特征图(feature map)表达瓶颈:从理论上讲,尺寸(seize)才包含了相关结构等重要因素,维度(channel)仅仅提供了信息内容的粗略估计,因此特征图的尺寸应该从输入到输出慢慢减小,避免使用极端压缩。
  2. 更高的维度特征图更容易获得网络的局部表达:在卷积网络结构中,增加非线性能够使得更多的特征解耦合,相互独立的特征更多,输入的信息就被分解的更彻底,分解的子特征间相关性低,子特征内部相关性高,因此高维特征带有更多的判别信息,会更容易收敛。
  3. 在低维特征上的空间聚合(池化)不会(极少)损失太多信息:相邻的位置的信息具有强相关性,即使进行了降维,也不会带来太多的损失,并且维数的降低,也能够加速网络学习。
  4. 平衡网络的宽度与深度:最优的网络应该在每一层网络宽度和网络深度之间有个很好的平衡。

Factorized Convolutions卷积分解

分解卷积的主要目的是为了减少网络中的参数,主要方法有:大卷积分解成小卷积,小卷积分解为非对称卷积。

大卷积分解成小卷积: 大尺度的卷积可以获得更大的感受野,但是也带来参数量的增加,VggNet表明使用大于大卷积核(大于3×3)完全可以由一系列的3×3卷积核来替代,即使用小卷积核串联来替代大卷积核。因此在InceptionV2中已经通过堆叠两层3×3的卷积核的方式替代一层5×5的卷积核,这样的连接方式在保持感受野范围的同时又减少了参数量,不会造成表达缺失,降低网络性能,并且可以避免表达瓶颈,加深非线性表达能力。
小卷积分解为非对称卷积: 3x3卷积是能够完全获取上下文信息(上、下、左、右)的最小卷积核,是否能把小卷积核分解的更小呢?在InceptionV3中,GoogLeNet团队考虑了非对称卷积分解,引入了将一个较大的二维卷积拆成两个较小的一维卷积的做法,即任意n×n的卷积都可以通过1×n卷积后接n×1卷积来替代,非对称卷积能够降低运算量,并且不会降低模型的整体表征能力。

InceptionV3结构Ⅰ

与InceptionV2结构相同,即5x5卷积使用两个3x3的卷积代替,目的是减少参数量和计算量——大卷积分解成小卷积。

InceptionV3结构Ⅱ

将InceptionV2结构中3x3的卷积使用1x3和3x1的卷积组合来代替,5x5的卷积使用俩个1x3和3x1的卷积组合来代替,目的也是减少参数量和计算量———小卷积分解为非对称卷积。

采用这种分解在模型的早期网络层上不能有效发挥作用,但是在中等特征图大小(m×m,其中m在12和20之间的范围)上取得了非常好的效果。

使用3x3的卷积代替5x5的卷积,输出512通道特征图,输出128通道特征图:
参数量:512×3×3×128+128×3×3×128=737280
计算量:512×3×3×128×W×H+128×3×3×128×W×H=737280×W×H
W×H是特征图尺寸,假设卷积层的输入输出特征图尺寸保持一致
使用1x3和3x1的卷积组合代替5x5的卷积,输出512通道特征图,输出128通道特征图:
参数量:512×1×3×128+128×3×1×128+128×1×3×128+128×3×1×128=344064
计算量:512×1×3×128×W×H+128×3×1×128×W×H+128×1×3×128×W×H+128×3×1×128×W×H=344064×W×H

InceptionV3结构Ⅲ

该结构主要用于扩充通道数,网络变得更宽,该结构被放置在所以放在GoogLeNet(InceptionV3)的最后。

InceptionV3特殊结构

在传统方法中,卷积神经网络使用池化等操作以减小特征图大小。先池化再进行卷积升维会导致瓶颈结构,过程中将丢失很多信息,对于后面输出的特征图提取的图像中的特征将会更少;先卷积升维再进行池化,计算量将增加三倍,增加了计算成本:

GoogLeNet(InceptionV3)的改进方案采用一种并行的降维结构,在扩充通道数的同时下采样减小特征图大小,既减少计算量保证了计算效率又避免了瓶颈结构。
替换GoogLeNet(InceptionV1)模型中的MaxPool。

辅助分类器

GoogLeNet(InceptionV1)引入了辅助分类器的概念,最初的动机是为了将有用的梯度反向传递到网络低层,解决梯度消失的问题,提高网络的收敛能力,保证网络训练正常进行。
GoogLeNet(InceptionV3)的实验则发现1.辅助分类器并不能保证收敛更快,并且有无辅助分类器,训练过程基本保持一致,只有在训练的最后阶段,有辅助分类器略微高于无辅助分类器的网络;2.位于网络低层的辅助分类器对最终结果没有影响;3.辅助分类器充当了正则化器,如果辅助分类器带有BN或Dropout层那么主分类器性能会更好。

GoogLeNet(InceptionV3)中的辅助分类器同样不直接用于最终的预测结果。在训练过程中,辅助分类器的损失函数会被加权,并与主分类器的损失函数相结合。在推理阶段,辅助分类器被舍弃,仅使用主分类器进行预测。

GoogLeNet(InceptionV3)模型结构

下图是原论文给出的关于 GoogLeNet(InceptionV3)模型结构的详细示意图:

GoogLeNet(InceptionV3)在图像分类中分为两部分:backbone部分: 主要由InceptionV3模块、卷积层和池化层(汇聚层)组成,分类器部分: 由主分类器和辅助分类器组成。
博主仿造GoogLeNet(InceptionV1)的结构绘制了以下GoogLeNet(InceptionV3)的结构。


GoogLeNet(InceptionV3) Pytorch代码

卷积层组: 卷积层+BN层+激活函数

# 卷积组: Conv2d+BN+ReLU
class BasicConv2d(nn.Module):def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0):super(BasicConv2d, self).__init__()self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)self.bn = nn.BatchNorm2d(out_channels)self.relu = nn.ReLU(inplace=True)def forward(self, x):x = self.conv(x)x = self.bn(x)x = self.relu(x)return x

InceptionV3模块Ⅰ: 卷积层组+池化层

就是原始的InceptionV2模块

# InceptionV3A:BasicConv2d+MaxPool2d
class InceptionV3A(nn.Module):def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch3x3redX2, ch3x3X2, pool_proj):super(InceptionV3A, self).__init__()# 1×1卷积self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)# 1×1卷积+3×3卷积self.branch2 = nn.Sequential(BasicConv2d(in_channels, ch3x3red, kernel_size=1),BasicConv2d(ch3x3red, ch3x3, kernel_size=3, padding=1)   # 保证输出大小等于输入大小)# 1×1卷积++3×3卷积+3×3卷积self.branch3 = nn.Sequential(BasicConv2d(in_channels, ch3x3redX2, kernel_size=1),BasicConv2d(ch3x3redX2, ch3x3X2, kernel_size=3, padding=1),BasicConv2d(ch3x3X2, ch3x3X2, kernel_size=3, padding=1)         # 保证输出大小等于输入大小)# 3×3池化+1×1卷积self.branch4 = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=1, padding=1),BasicConv2d(in_channels, pool_proj, kernel_size=1))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)

InceptionV3模块Ⅱ: 卷积层组+池化层

# InceptionV3B:BasicConv2d+MaxPool2d
class InceptionV3B(nn.Module):def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch3x3redX2, ch3x3X2, pool_proj):super(InceptionV3B, self).__init__()# 1×1卷积self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)# 1×1卷积+1×3卷积+3×1卷积self.branch2 = nn.Sequential(BasicConv2d(in_channels, ch3x3red, kernel_size=1),BasicConv2d(ch3x3red, ch3x3, kernel_size=[1, 3], padding=[0, 1]),BasicConv2d(ch3x3, ch3x3, kernel_size=[3, 1], padding=[1, 0])   # 保证输出大小等于输入大小)# 1×1卷积+1×3卷积+3×1卷积+1×3卷积+3×1卷积self.branch3 = nn.Sequential(BasicConv2d(in_channels, ch3x3redX2, kernel_size=1),BasicConv2d(ch3x3red, ch3x3, kernel_size=[1, 3], padding=[0, 1]),BasicConv2d(ch3x3, ch3x3, kernel_size=[3, 1], padding=[1, 0]),BasicConv2d(ch3x3, ch3x3, kernel_size=[1, 3], padding=[0, 1]),BasicConv2d(ch3x3, ch3x3, kernel_size=[3, 1], padding=[1, 0])  # 保证输出大小等于输入大小)# 3×3池化+1×1卷积self.branch4 = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=1, padding=1),BasicConv2d(in_channels, pool_proj, kernel_size=1))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)

InceptionV3模块Ⅲ: 卷积层组+池化层

# InceptionV3C:BasicConv2d+MaxPool2d
class InceptionV3C(nn.Module):def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch3x3redX2, ch3x3X2, pool_proj):super(InceptionV3C, self).__init__()# 1×1卷积self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)# 1×1卷积+1×3卷积+3×1卷积self.branch2_0 = BasicConv2d(in_channels, ch3x3red, kernel_size=1)self.branch2_1 = BasicConv2d(ch3x3red, ch3x3, kernel_size=[1, 3], padding=[0, 1])self.branch2_2 = BasicConv2d(ch3x3red, ch3x3, kernel_size=[3, 1], padding=[1, 0])# 1×1卷积+3×3卷积+1×3卷积+3×1卷积self.branch3_0 = nn.Sequential(BasicConv2d(in_channels, ch3x3redX2, kernel_size=1),BasicConv2d(ch3x3redX2, ch3x3X2, kernel_size=3, padding=1),)self.branch3_1 = BasicConv2d(ch3x3X2, ch3x3X2, kernel_size=[1, 3], padding=[0, 1])self.branch3_2 = BasicConv2d(ch3x3X2, ch3x3X2, kernel_size=[3, 1], padding=[1, 0])# 3×3池化+1×1卷积self.branch4 = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=1, padding=1),BasicConv2d(in_channels, pool_proj, kernel_size=1))def forward(self, x):branch1 = self.branch1(x)branch2_0 = self.branch2_0(x)branch2 = torch.cat([self.branch2_1(branch2_0), self.branch2_2(branch2_0)], dim=1)branch3_0 = self.branch3_0(x)branch3 = torch.cat([self.branch3_1(branch3_0), self.branch3_2(branch3_0)], dim=1)branch4 = self.branch4(x)# 拼接outputs = [branch1, branch2, branch3, branch4]return torch.cat(outputs, 1)

InceptionV3特殊模块(三分支): 卷积层组+池化层

# InceptionV3D:BasicConv2d+MaxPool2d
class InceptionV3D(nn.Module):def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch3x3redX2, ch3x3X2, pool_proj):super(InceptionV3D, self).__init__()# ch1x1:没有1×1卷积# 1×1卷积+3×3卷积,步长为2self.branch1 = nn.Sequential(BasicConv2d(in_channels, ch3x3red, kernel_size=1),BasicConv2d(ch3x3red, ch3x3, kernel_size=3, stride=2)   # 保证输出大小等于输入大小)# 1×1卷积+3×3卷积+3×3卷积,步长为2self.branch2 = nn.Sequential(BasicConv2d(in_channels, ch3x3redX2, kernel_size=1),BasicConv2d(ch3x3redX2, ch3x3X2, kernel_size=3, padding=1),   # 保证输出大小等于输入大小BasicConv2d(ch3x3X2, ch3x3X2, kernel_size=3, stride=2)         # 保证输出大小等于输入大小)# 3×3池化,步长为2self.branch3 = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=2))# pool_proj:池化层后不再接卷积层def forward(self, x):branch1 = self.branch1(x)branch2 = self.branch2(x)branch3 = self.branch3(x)# 拼接outputs = [branch1,branch2, branch3]return torch.cat(outputs, 1)

辅助分类器: 池化层+卷积层组+全连接层+dropout

# 辅助分类器:AvgPool2d+BasicConv2d+Linear+dropout
class InceptionAux(nn.Module):def __init__(self, in_channels, out_channels):super(InceptionAux, self).__init__()self.averagePool = nn.AvgPool2d(kernel_size=5, stride=3)self.conv1 = BasicConv2d(in_channels=in_channels, out_channels=128, kernel_size=1)self.conv2 = BasicConv2d(in_channels=128, out_channels=768, kernel_size=5, stride=1)self.dropout = nn.Dropout(p=0.7)self.linear = nn.Linear(in_features=768, out_features=out_channels)def forward(self, x):# N x 768 x 17 x 17x = self.averagePool(x)# N x 768 x 5 x 5x = self.conv1(x)# N x 128 x 5 x 5x = self.conv2(x)# N x 768 x 1 x 1x = x.view(x.size(0), -1)# N x 768out = self.linear(self.dropout(x))# N x num_classesreturn out

完整代码

GoogLeNet(InceptionV3)的输入图像尺寸是299×299

import torch.nn as nn
import torch
from torchsummary import summaryclass GoogLeNetV3(nn.Module):def __init__(self, num_classes=1000, aux_logits=True, init_weights=False):super(GoogLeNetV3, self).__init__()self.aux_logits = aux_logits# 3个3×3卷积替代7×7卷积self.conv1_1 = BasicConv2d(3, 32, kernel_size=3, stride=2)self.conv1_2 = BasicConv2d(32, 32, kernel_size=3, stride=1)self.conv1_3 = BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1)# 池化层self.maxpool1 = nn.MaxPool2d(3, stride=2, ceil_mode=True)self.conv2 = BasicConv2d(64, 80, kernel_size=3)self.conv3 = BasicConv2d(80, 192, kernel_size=3, stride=2)self.conv4 = BasicConv2d(192, 192, kernel_size=3, padding=1)self.inception3a = InceptionV3A(192, 64, 48, 64, 64, 96, 32)self.inception3b = InceptionV3A(256, 64, 48, 64, 64, 96, 64)self.inception3c = InceptionV3A(288, 64, 48, 64, 64, 96, 64)self.inception4a = InceptionV3D(288, 0, 384, 384, 64, 96, 0)self.inception4b = InceptionV3B(768, 192, 128, 192, 128, 192, 192)self.inception4c = InceptionV3B(768, 192, 160, 192, 160, 192, 192)self.inception4d = InceptionV3B(768, 192, 160, 192, 160, 192, 192)self.inception4e = InceptionV3D(768, 0, 384, 384, 64, 128, 0)if self.aux_logits == True:self.aux = InceptionAux(in_channels=768, out_channels=num_classes)self.inception5a = InceptionV3C(1280, 320, 384, 384, 448, 384, 192)self.inception5b = InceptionV3C(2048, 320, 384, 384, 448, 384, 192)self.avgpool = nn.AdaptiveAvgPool2d((1, 1))self.dropout = nn.Dropout(0.5)self.fc = nn.Linear(2048, num_classes)if init_weights:self._initialize_weights()def forward(self, x):# N x 3 x 299 x 299x = self.conv1_1(x)# N x 32 x 149 x 149x = self.conv1_2(x)# N x 32 x 147 x 147x = self.conv1_3(x)#  N x 32 x 147 x 147x = self.maxpool1(x)# N x 64 x 73 x 73x = self.conv2(x)# N x 80 x 71 x 71x = self.conv3(x)# N x 192 x 35 x 35x = self.conv4(x)# N x 192 x 35 x 35x = self.inception3a(x)# N x 256 x 35 x 35x = self.inception3b(x)# N x 288 x 35 x 35x = self.inception3c(x)# N x 288 x 35x 35x = self.inception4a(x)# N x 768 x 17 x 17x = self.inception4b(x)# N x 768 x 17 x 17x = self.inception4c(x)# N x 768 x 17 x 17x = self.inception4d(x)# N x 768 x 17 x 17if self.training and self.aux_logits:    # eval model lose this layeraux = self.aux(x)# N x 768 x 17 x 17x = self.inception4e(x)# N x 1280 x 8 x 8x = self.inception5a(x)# N x 2048 x 8 x 8x = self.inception5b(x)# N x 2048 x 7 x 7x = self.avgpool(x)# N x 2048 x 1 x 1x = torch.flatten(x, 1)# N x 1024x = self.dropout(x)x = self.fc(x)# N x 1000(num_classes)if self.training and self.aux_logits:  # 训练阶段使用return x, auxreturn x# 对模型的权重进行初始化操作def _initialize_weights(self):for m in self.modules():if isinstance(m, nn.Conv2d):nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')if m.bias is not None:nn.init.constant_(m.bias, 0)elif isinstance(m, nn.Linear):nn.init.normal_(m.weight, 0, 0.01)nn.init.constant_(m.bias, 0)# InceptionV3A:BasicConv2d+MaxPool2d
class InceptionV3A(nn.Module):def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch3x3redX2, ch3x3X2, pool_proj):super(InceptionV3A, self).__init__()# 1×1卷积self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)# 1×1卷积+3×3卷积self.branch2 = nn.Sequential(BasicConv2d(in_channels, ch3x3red, kernel_size=1),BasicConv2d(ch3x3red, ch3x3, kernel_size=3, padding=1)   # 保证输出大小等于输入大小)# 1×1卷积++3×3卷积+3×3卷积self.branch3 = nn.Sequential(BasicConv2d(in_channels, ch3x3redX2, kernel_size=1),BasicConv2d(ch3x3redX2, ch3x3X2, kernel_size=3, padding=1),BasicConv2d(ch3x3X2, ch3x3X2, kernel_size=3, padding=1)         # 保证输出大小等于输入大小)# 3×3池化+1×1卷积self.branch4 = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=1, padding=1),BasicConv2d(in_channels, pool_proj, kernel_size=1))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)# InceptionV3B:BasicConv2d+MaxPool2d
class InceptionV3B(nn.Module):def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch3x3redX2, ch3x3X2, pool_proj):super(InceptionV3B, self).__init__()# 1×1卷积self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)# 1×1卷积+1×3卷积+3×1卷积self.branch2 = nn.Sequential(BasicConv2d(in_channels, ch3x3red, kernel_size=1),BasicConv2d(ch3x3red, ch3x3, kernel_size=[1, 3], padding=[0, 1]),BasicConv2d(ch3x3, ch3x3, kernel_size=[3, 1], padding=[1, 0])   # 保证输出大小等于输入大小)# 1×1卷积+1×3卷积+3×1卷积+1×3卷积+3×1卷积self.branch3 = nn.Sequential(BasicConv2d(in_channels, ch3x3redX2, kernel_size=1),BasicConv2d(ch3x3red, ch3x3, kernel_size=[1, 3], padding=[0, 1]),BasicConv2d(ch3x3, ch3x3, kernel_size=[3, 1], padding=[1, 0]),BasicConv2d(ch3x3, ch3x3, kernel_size=[1, 3], padding=[0, 1]),BasicConv2d(ch3x3, ch3x3, kernel_size=[3, 1], padding=[1, 0])  # 保证输出大小等于输入大小)# 3×3池化+1×1卷积self.branch4 = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=1, padding=1),BasicConv2d(in_channels, pool_proj, kernel_size=1))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)# InceptionV3C:BasicConv2d+MaxPool2d
class InceptionV3C(nn.Module):def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch3x3redX2, ch3x3X2, pool_proj):super(InceptionV3C, self).__init__()# 1×1卷积self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)# 1×1卷积+1×3卷积+3×1卷积self.branch2_0 = BasicConv2d(in_channels, ch3x3red, kernel_size=1)self.branch2_1 = BasicConv2d(ch3x3red, ch3x3, kernel_size=[1, 3], padding=[0, 1])self.branch2_2 = BasicConv2d(ch3x3red, ch3x3, kernel_size=[3, 1], padding=[1, 0])# 1×1卷积+3×3卷积+1×3卷积+3×1卷积self.branch3_0 = nn.Sequential(BasicConv2d(in_channels, ch3x3redX2, kernel_size=1),BasicConv2d(ch3x3redX2, ch3x3X2, kernel_size=3, padding=1),)self.branch3_1 = BasicConv2d(ch3x3X2, ch3x3X2, kernel_size=[1, 3], padding=[0, 1])self.branch3_2 = BasicConv2d(ch3x3X2, ch3x3X2, kernel_size=[3, 1], padding=[1, 0])# 3×3池化+1×1卷积self.branch4 = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=1, padding=1),BasicConv2d(in_channels, pool_proj, kernel_size=1))def forward(self, x):branch1 = self.branch1(x)branch2_0 = self.branch2_0(x)branch2 = torch.cat([self.branch2_1(branch2_0), self.branch2_2(branch2_0)], dim=1)branch3_0 = self.branch3_0(x)branch3 = torch.cat([self.branch3_1(branch3_0), self.branch3_2(branch3_0)], dim=1)branch4 = self.branch4(x)# 拼接outputs = [branch1, branch2, branch3, branch4]return torch.cat(outputs, 1)# InceptionV3D:BasicConv2d+MaxPool2d
class InceptionV3D(nn.Module):def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch3x3redX2, ch3x3X2, pool_proj):super(InceptionV3D, self).__init__()# ch1x1:没有1×1卷积# 1×1卷积+3×3卷积,步长为2self.branch1 = nn.Sequential(BasicConv2d(in_channels, ch3x3red, kernel_size=1),BasicConv2d(ch3x3red, ch3x3, kernel_size=3, stride=2)   # 保证输出大小等于输入大小)# 1×1卷积+3×3卷积+3×3卷积,步长为2self.branch2 = nn.Sequential(BasicConv2d(in_channels, ch3x3redX2, kernel_size=1),BasicConv2d(ch3x3redX2, ch3x3X2, kernel_size=3, padding=1),   # 保证输出大小等于输入大小BasicConv2d(ch3x3X2, ch3x3X2, kernel_size=3, stride=2)         # 保证输出大小等于输入大小)# 3×3池化,步长为2self.branch3 = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=2))# pool_proj:池化层后不再接卷积层def forward(self, x):branch1 = self.branch1(x)branch2 = self.branch2(x)branch3 = self.branch3(x)# 拼接outputs = [branch1,branch2, branch3]return torch.cat(outputs, 1)# 辅助分类器:AvgPool2d+BasicConv2d+Linear+dropout
class InceptionAux(nn.Module):def __init__(self, in_channels, out_channels):super(InceptionAux, self).__init__()self.averagePool = nn.AvgPool2d(kernel_size=5, stride=3)self.conv1 = BasicConv2d(in_channels=in_channels, out_channels=128, kernel_size=1)self.conv2 = BasicConv2d(in_channels=128, out_channels=768, kernel_size=5, stride=1)self.dropout = nn.Dropout(p=0.7)self.linear = nn.Linear(in_features=768, out_features=out_channels)def forward(self, x):# N x 768 x 17 x 17x = self.averagePool(x)# N x 768 x 5 x 5x = self.conv1(x)# N x 128 x 5 x 5x = self.conv2(x)# N x 768 x 1 x 1x = x.view(x.size(0), -1)# N x 768out = self.linear(self.dropout(x))# N x num_classesreturn out# 卷积组: Conv2d+BN+ReLU
class BasicConv2d(nn.Module):def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0):super(BasicConv2d, self).__init__()self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)self.bn = nn.BatchNorm2d(out_channels)self.relu = nn.ReLU(inplace=True)def forward(self, x):x = self.conv(x)x = self.bn(x)x = self.relu(x)return xif __name__ == '__main__':device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")model = GoogLeNetV3().to(device)summary(model, input_size=(3, 299, 299))

summary可以打印网络结构和参数,方便查看搭建好的网络结构。


总结

尽可能简单、详细的介绍了小卷积分解为非对称卷积的原理和在卷积神经网络中的作用,讲解了GoogLeNet(InceptionV3)模型的结构和pytorch代码。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/178440.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2020年五一杯数学建模C题饲料混合加工问题解题全过程文档及程序

2020年五一杯数学建模 C题 饲料混合加工问题 原题再现 饲料加工厂需要加工一批动物能量饲料。饲料加工需要原料,如加工猪饲料需要玉米、荞麦、稻谷等。加工厂从不同的产区收购了原料,原料在收购的过程中由于运输、保鲜以及产品本身属性等原因&#xff…

Spring中的BeanFactory和ApplicationContext的区别

我用一个例子去测试BeanFactory和ApplicationContext的区别 首先建立一个bean public class User { //声明无参构造,打印一句话,监测对象创建时机public User(){System.out.println("User对象初始化");} } 然后再建立测试类 ublic class User…

【Linux】Linux 中关于 MySQL 的相关操作

Linux 中关于 MySQL 的相关操作 Linux 系统与 MySQL 数据库是目前互联网开发中最为流行的组合之一。Linux 作为开源的操作系统,具有运行效率高、安全性好等优点;而 MySQL 作为开源的数据库,具有运行速度快、可靠性高等特点。 (1&…

HarmonyOS 学习记录

时光荏苒,岁月如梭,韶华不负,未来可期。转眼间已经30岁了,学习的重要性不言而喻,在接下来的日子里记录下自己学习HarmonyOS的过程。增加一下知识储备,防患于未然嘛 不得不说华为的开发文档写的不错,开发工具直接安装后自动配置环境…

程序员的护城河:技术深度、创新精神与软实力的完美结合

文章目录 1. 技术深度:建立坚实的技术基石2. 创新精神:应对变革的利器3. 软实力:沟通协作构筑团队防线4. 结合三者构筑完美护城河 🎉程序员的护城河:技术深度、创新精神与软实力的完美结合 ☆* o(≧▽≦)o *☆嗨~我是I…

清晨早安问候祝福语精选,相互牵挂,祝福无价!

1、有一种祝福,时刻都在心里。无论相隔多远,都能温暖彼此的心灵。珍惜美丽相遇,珍藏这份情意。忙碌不是忘记,牵挂一直都在心里。送上一份心的祝福,书写一份心的牵挂,坦诚相待,温暖相伴&#xff…

PHP项目学习笔记-萤火商城-增加一个模块(表涉及到的操作和文件)

背景 是在store的后台添加一个页面,显示的如满意度调查的页面 在router.config.js里面配置一个新的菜单 路径:yoshop2.0-store\src\config\router.config.js 代码如下,很简单,定义了这菜单点击的时候进入的页面,和下面…

springboot 2.1.0.RELEASE 项目加入swagger接口文档

Release v2.1.0.RELEASE spring-projects/spring-boot GitHub springboot 2.1.0.RELEASE发行日期是2018年10月30日(Oct 30, 2018) 不要使用过高的swagger版本,如SpringFox Boot Starter 3.0.0,否则报错: spring-…

HDFS入门--学习笔记

1,大数据介绍 定义 数据指的是:一种可以被鉴别的、对客观事件进行记录的符号,除了可以是最简单的 数字外,也可以是各类符号、文字、图像、声音等。 通俗地说,数据就是对人类的行为及发生事件的一种记录。 存在的价值…

HTML5学习系列之标题和正文、描述性信息

HTML5学习系列之标题和正文、描述性信息 标题和正文标题段落 描述性信息强调注解备选上下标术语代码预定义格式缩写词编辑提示引用引述换行显示修饰非文本注解 总结 标题和正文 标题 按语义轻重排列&#xff1a;h1\h2\h3\h4\h5\h6 <h1>诗词介绍</h1> <h2>…

2023亚太杯数学建模思路 - 复盘:人力资源安排的最优化模型

文章目录 0 赛题思路1 描述2 问题概括3 建模过程3.1 边界说明3.2 符号约定3.3 分析3.4 模型建立3.5 模型求解 4 模型评价与推广5 实现代码 建模资料 0 赛题思路 &#xff08;赛题出来以后第一时间在CSDN分享&#xff09; https://blog.csdn.net/dc_sinor?typeblog 1 描述 …

zookeeper的安装部署

目录 简介 Zookeeper架构设计及原理 1.Zookeeper定义 2.Zookeeper的特点 3.Zookeeper的基本架构 4.Zookeeper的工作原理 5.Zookeeper的数据模型 &#xff08;1&#xff09;临时节点 &#xff08;2&#xff09;顺序节点 &#xff08;3&#xff09;观察机制 Zookeeper集…