经典网络结构(四):GoogLeNet (Inception v1~v4)

本节参考:《深度学习之PyTorch物体检测实战》
《DIVE INTO DEEP LEARNING》、吴恩达深度学习视频

Inceptionv1

Inception块

(彩蛋:得名于同名电影《盗梦空间》(Inception) – “We need to go deeper”,GoogLenet 同时也向 Lenet 致敬)

在这里插入图片描述
(1×11 \times 1 / 3×33 \times 3 / 5×55\times 5 / MaxPoolMaxPool怎么选? Inception:小孩子才做选择,我全都要)

Inception块里有4条并行的线路。前3条线路使用窗口大小分别是 1×13×35×51×1 、 3×3 和 5×5 的卷积层来抽取不同空间尺寸下的信息,其中中间2个线路会对输入先做 1×11×1 卷积来减少输入通道数(即之前的文章里提到的 bottleneck layer,相比其他大小的卷积核,利用1×11×1 卷积来减少输入通道数能显著降低计算成本),以降低模型复杂度,在类似的测试精度下,GoogLenet的计算复杂度往往更低。第四条线路则使用 3×33×3 最大池化层,后接 1×11×1 卷积层来改变通道数。4条线路都使用了合适的填充来使输入与输出的高和宽一致。最后我们将每条线路的输出在通道维上连结,并输入接下来的层中去。

Inception块中可以自定义的超参数每个层的输出通道数,我们以此来控制模型复杂度。

class Inceptionv1(nn.Module):
    def __init__(self, in_dim, c1, c2, c3, c4):
        super(Inceptionv1, self).__init__()
        self.branch1x1 = nn.Sequential(
            nn.Conv2d(in_dim, c1, 1),
            nn.ReLU(inplace=True)
        )
        self.branch3x3 = nn.Sequential(
            nn.Conv2d(in_dim, c2[0], 1),
            nn.ReLU(inplace=True),
            nn.Conv2d(c2[0], c2[1], 3, padding=1),
            nn.ReLU(inplace=True)
        )
        self.branch5x5 = nn.Sequential(
            nn.Conv2d(in_dim, c3[0], 1),
            nn.ReLU(inplace=True),
            nn.Conv2d(c3[0], c3[1], 5, padding=2),
            nn.ReLU(inplace=True)
        )
        self.branch_pool = nn.Sequential(
            nn.MaxPool2d(3, stride=1, padding=1),
            nn.Conv2d(in_dim, c4, 1),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        b1 = self.branch1x1(x)
        b2 = self.branch3x3(x)
        b3 = self.branch5x5(x)
        b4 = self.branch_pool(x)
        output = torch.cat((b1, b2, b3, b4), dim=1)
        return output

GoogLeNet模型

在这里插入图片描述
GoogLeNet跟VGG一样,在主体卷积部分中使用5个模块(block),每个模块之间使用步幅为2的 3×33×3 最大池化层来减小输出高宽。

  • 第一模块使用一个64通道的 7×77×7 卷积层
  • 第二模块使用2个卷积层:首先是64通道的 1×11×1 卷积层,然后是将通道增大3倍的 3×33×3 卷积层。它对应Inception块中的第二条线路
  • 第三模块串联2个完整的Inception块。第一个Inception块的输出通道数为 64+128+32+32=25664+128+32+32=256 ,其中4条线路的输出通道数比例为 64:128:32:32=2:4:1:164:128:32:32=2:4:1:1 。其中第二、第三条线路先分别将输入通道数减小至 96/192=1/296/192=1/216/192=1/1216/192=1/12 后,再接上第二层卷积层。第二个Inception块输出通道数增至 128+192+96+64=480128+192+96+64=480 ,每条线路的输出通道数之比为 128:192:96:64=4:6:3:2128:192:96:64=4:6:3:2 。其中第二、第三条线路先分别将输入通道数减小至 128/256=1/2128/256=1/232/256=1/832/256=1/8 (Inception块的通道数分配之比是在ImageNet数据集上通过大量的实验得来的)
  • 第四模块更加复杂。它串联了5个Inception块,其输出通道数分别是 192+208+48+64=512192+208+48+64=512160+224+64+64=512160+224+64+64=512128+256+64+64=512128+256+64+64=512112+288+64+64=528112+288+64+64=528256+320+128+128=832256+320+128+128=832 。这些线路的通道数分配和第三模块中的类似,首先是含 3×33×3 卷积层的第二条线路输出最多通道,其次是仅含 1×11×1 卷积层的第一条线路,之后是含 5×55×5 卷积层的第三条线路和含 3×33×3 最大池化层的第四条线路。其中第二、第三条线路都会先按比例减小通道数。这些比例在各个Inception块中都略有不同。
  • 第五模块有输出通道数为 256+320+128+128=832256+320+128+128=832384+384+128+128=1024384+384+128+128=1024 的两个Inception块。其中每条线路的通道数的分配思路和第三、第四模块中的一致,只是在具体数值上有所不同。需要注意的是,第五模块的后面紧跟输出层,该模块同NiN一样使用全局平均池化层来将每个通道的高和宽变成1。最后我们将输出变成二维数组后接上一个输出个数为标签类别数的全连接层。

在原论文中还有一个细节:如上图所示黄色后接白色的两个方框为全连接层接softmax,可以看到除了最后的输出以外,从隐藏层中也引出了两个该结构,通过引入这两个辅助分类器(Auxiliary Classifier),在第3个与第6个Inception块输出后执行softmax并计算损失,在训练时和最后的损失一起回传。下面引用一下吴恩达在深度学习视频中的解释:
It ensures that the features computed even in the hidden units are not too bad for predicting the output class of an image. (have a regularization effect and help prevent this network from overfitting)

class GoogLenetv1(nn.Module):
    def __init__(self, in_dim, class_num):
        super(GoogLenetv1, self).__init__()
        self.b1 = nn.Sequential(
            nn.Conv2d(in_dim, 64, kernel_size=7, stride=2, padding=3),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(3, stride=2, padding=1),
        )
        self.b2 = nn.Sequential(
            nn.Conv2d(64, 64, 1),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 192, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(3, stride=2, padding=1),
        )
        self.b3 = nn.Sequential(
            Inceptionv1(192, 64, (96, 128), (16, 32), 32),
            Inceptionv1(256, 128, (128, 192), (32, 96), 64),
            nn.MaxPool2d(3, stride=2, padding=1),
        )
        self.b4 = nn.Sequential(
            Inceptionv1(480, 192, (96, 208), (16, 48), 64),
            Inceptionv1(512, 160, (112, 224), (24, 64), 64),
            Inceptionv1(512, 128, (128, 256), (24, 64), 64),
            Inceptionv1(512, 112, (144, 288), (32, 64), 64),
            Inceptionv1(528, 256, (160, 320), (32, 128), 128),
            nn.MaxPool2d(3, stride=2, padding=1),
        )
        self.b5 = nn.Sequential(
            Inceptionv1(832, 256, (160, 320), (32, 128), 128),
            Inceptionv1(832, 384, (192, 384), (48, 128), 128),
            nn.AdaptiveAvgPool2d(1),
        )
        self.net = nn.Sequential()
        for i in range(1, 6):
            exec('self.net.add_module(str(i), self.b' + str(i) + ')')
        self.fc = nn.Linear(1024, class_num)
        
    def forward(self, x):
        output = self.net(x)
        output = output.view(-1, 1024) 
        output = self.fc(output)
        
        return output

Inceptionv2

参考:https://www.jianshu.com/p/4e5b3e652639

Inceptionv2的基础模块结构

在这里插入图片描述
下面再搭建一个Inceptionv2的基础模块结构:

class BasicConv2d(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, padding=0):
        super(BasicConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding)
        self.bn = nn.BatchNorm2d(out_channels, eps=0.001)
    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        return F.relu(x, inplace=True)

class Inceptionv2(nn.Module):
    def __init__(self, in_dim, c1, c2, c3, c4):
        super(Inceptionv2, self).__init__()
        self.branch1 = BasicConv2d(in_dim, c1, 1, 0)
        self.branch2 = nn.Sequential(
            BasicConv2d(in_dim, c2[0], 1, 0),
            BasicConv2d(c2[0], c2[1], 3, 1)
        )
        self.branch3 = nn.Sequential(
            BasicConv2d(in_dim, c3[0], 1, 0),
            BasicConv2d(c3[0], c3[1], 3, 1),
            BasicConv2d(c3[1], c3[2], 3, 1)
        )
        self.branch4 = nn.Sequential(
            nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
            BasicConv2d(in_dim, c4, 1, 0)
        )
    def forward(self, x):
        x0 = self.branch1(x)
        x1 = self.branch2(x)
        x2 = self.branch3(x)
        x3 = self.branch4(x)
        out = torch.cat((x0, x1, x2, x3), 1)
        return out

可以看出,Inceptionv2基础模块结构与Inceptionv1相比,有下面几个改动:

  • 在每个卷积层之后都加入了BN层,可以采用更大的学习率,能使学习更快地进行
  • 5×55\times5 卷积层换成了两个 3×33\times3 卷积层,可以用更少的参数获得相同的感受野
  • 将Max池化改为Average池化

Inceptionv2的改进模块结构

在上面所说的基础上,最终的Inceptionv2还进行了以下的改进:
n×nn\times n的卷积运算分解为1×n1\times nn×1n\times 1的两个卷积运算。按照这种思路将3×33\times 3的卷积运算分解为1×31\times 33×13\times 1的两个卷积运算,这样就进一步将计算成本降低了33%。作者通过测试发现非对称卷积用在网络中靠中间的层级才有较好的效果(特别是feature map的大小在12x12~20x20之间时),Inceptionv2改进模块如下图所示
在这里插入图片描述
在这里插入图片描述

Inceptionv2的并行结构

Inceptionv2还使用并行结构来优化Pooling,使卷积核变得更宽而不是更深,解决表征能力瓶颈问题。下面进行详细介绍:
首先,作者在论文 Rethinking the Inception Architecture for Computer Vision 中提出了要按照一套合理的规则来优化Inception结构,具体如下:

  • 规则1:要防止出现特征描述的瓶颈(representational bottleneck)。所谓特征描述的瓶颈就是中间某层对特征在空间维度进行较大比例的压缩(比如使用pooling时),导致很多特征丢失。虽然Pooling是CNN结构中必须的功能,但我们可以通过一些优化方法来减少Pooling造成的损失。
  • 规则2:特征的数目越多收敛的越快。相互独立的特征越多,输入的信息就被分解的越彻底,分解的子特征间相关性低,子特征内部相关性高,把相关性强的聚集在了一起会更容易收敛。规则2和规则1可以组合在一起理解,特征越多能加快收敛速度,但是无法弥补Pooling造成的特征损失,Pooling造成的representational bottleneck要靠其他方法来解决。
  • 规则3:可以压缩特征维度数,来减少计算量。inception-v1中提出的用1x1卷积先降维再作特征提取就是利用这点。不同维度的信息有相关性,降维可以理解成一种无损或低损压缩,即使维度降低了,仍然可以利用相关性恢复出原有的信息。
  • 规则4:整个网络结构的深度和宽度(特征维度数)要做到平衡。只有等比例的增大深度和维度才能最大限度的提升网络的性能。

而使用并行结构来优化Pooling就是为了解决规则1中提到的 representational bottleneck。representational bottleneck 的一种解决办法就是在Pooling前用1x1卷积把特征数加倍(见下图右侧),这种加倍可以理解加入了冗余的特征,然后再作Pooling就只是把冗余的信息重新去掉,没有减少信息量。这种方法有很好的效果但因为加入了1x1卷积会极大的增大计算量。
在这里插入图片描述
替代的方法是使用两个并行的支路,左路为3×33\times 3,步长为2的卷积,右路是Pooling,最后再在特征维度拼合到一起(见下图)也得到了两倍的通道数。这种方法既有很好的效果,又没有增大计算量。

在这里插入图片描述

Inceptionv2整体结构

在这里插入图片描述
在这里插入图片描述

Inceptionv3

参考:https://www.jianshu.com/p/4e5b3e652639

使用标签平滑(label smoothing)来对网络输出进行正则化

SoftmaxwithLoss层的输出可以用下面公式表示:
在这里插入图片描述在这里插入图片描述
从上述公式可以反推出整个训练过程收敛时Softmax的正确分类的输入aka_k是无穷大,这是一种极其理想的情况,如果让所有的输入都产生这种极其理想的输出,就会造成overfit(回想一下overfit的概念:能对所有的输入进行最理想的分类,鲁棒性差)。

所以为了克服overfit,防止最终出来的正确分类yk=1y_k=1,在输出yky_k时加了个参数deltadelta,生成新的yky_k^{'},用yky_k^{'}来计算loss

yk=(1ϵ)yk+ϵKy_k^{'} = (1 - \epsilon)y_k + \frac{\epsilon}{K}Kϵ0.1K为类别数,\epsilon可取0.1举个例子:假设输出概率值为[0, 0, 0, 1],那么在经过 label smoothing之后,概率值就变成了[0.025, 0.025, 0.025, 0.925],相当于是施加了一个惩罚

关于分支分类器 Auxiliary Classifiers 的纠正

作者还对自己在GoogLeNet论文中提出的 分支分类器 Auxiliary Classifiers 的效果进行了纠正。Szegedy认为自己当时的结论就是错误的,特别是靠近输入侧的那个Auxiliary Classifier,加不加完全没区别,但如果在靠近输出的那个Auxiliary Classifier的全连接层后加个BN,会起到正则化的作用,所以第二个Auxiliary Classifier还是可以保留。起正则化作用的原因Szegedy完全没解释。。。

Inceptionv4

论文链接:Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning
参考:https://blog.csdn.net/kxh123456/article/details/102828148

Inceptionv4将Inception的思想与残差网络进行了结合,显著提升了训练速度与模型准确率.论文中提出了三个网络,分别叫Inception-v4、Inception-ResNet-v1以及Inception-ResNet-v2,其中Inception-v4中没有使用残差模块,但是网络深度比原来更深了。后两个网络则引入的残差模块,但是在设计上还是略有不同。

Scaling of the Residuals

在与前一层激活层求和时,缩小残差有助于稳定训练过程。一般我们会选择固定的缩放因子,见图20:
在这里插入图片描述

残差模块

在这里插入图片描述
在这里插入图片描述

Inception-v4

整体结构

在这里插入图片描述

网络中的各个模块

stem

在这里插入图片描述

Inception-A block

在这里插入图片描述

Inception-B block

在这里插入图片描述

Inception-C block

在这里插入图片描述

Reduction-A

在这里插入图片描述

Reduction-B

在这里插入图片描述

Inception-ResNet-V1

整体结构

在这里插入图片描述

网络中的各个模块

stem

在这里插入图片描述

Inception-ResNet-A

在这里插入图片描述

Inception-ResNet-B

在这里插入图片描述

Inception-ResNet-C

在这里插入图片描述

Reduction-A

在这里插入图片描述

Reduction-B

在这里插入图片描述

Inception-ResNet-V2

整体结构

在这里插入图片描述

网络中的各个模块

stem

在这里插入图片描述

Inception-ResNet-A

在这里插入图片描述

Inception-ResNet-B

在这里插入图片描述

Inception-ResNet-C

在这里插入图片描述

Reduction-A

在这里插入图片描述

Reduction-B

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章