天天看點

GoogLeNet網絡結構詳解及代碼複現

1. GoogLeNet論文詳解

Abstract:

提出了

GoogLeNet

網絡結構——22層,此設計允許在保證計算預算不變的前提下,增加網絡的深度和寬度,這個網絡結構是基于

Hebbian

原則和多尺度處理,并且在

ILSVRC 2014

中的分類任務中獲得第一名。

對于大型資料集,最近的趨勢是增加層數和每一層的尺寸,同時使用

dropout

來解決過拟合問題

  • 層尺寸的增大意味着需要更大數量的參數,這會使得網絡更容易過拟合,尤其是對于資料集小的情況下
  • 層深度的增加會大大增加計算資源的使用,尤其是卷積層的權重為0時,會浪費大量計算資源

1x1 卷積 & 全局平均池化

這兩種方法都是為了提高卷積網絡的表達能力,改善網絡結構的。在Network-in-network中被提出的。

GoogLeNet網絡結構詳解及代碼複現

1x1卷積:

  • 可以通過設定1x1卷積核的數量來實作降維或升維
  • 實作特征圖的通道間的聚合
GoogLeNet網絡結構詳解及代碼複現

全局平均池化:

傳統CNN網絡中,前面堆疊卷積層提取特征,最後通過全連接配接層分類提取出的特征,但是全連接配接層很容易導緻模型過拟合,并且其參數比較多,為了解決這個問題,出現了

dropout

,但是在Network-in-network中,作者提出了全局平均池化來解決此問題

将卷積層提取出來的特征圖(Feature map)進行相加求平均,然後将這些特征圖對應的平均值作為某一類的置信度輸入到

softmax

進行分類(要控制卷積層的最後一層的特征圖數量與最終分類數量保持一緻)

此方法的好處

  • 減少了參數(相對于全連接配接層)
  • 減輕過拟合
  • 求和取平均操作綜合了空間資訊,提高模型的魯棒性

缺點:

對特征圖的簡單相加求平均可能會丢失一些有用資訊

網絡結構細節:

  1. Inception
GoogLeNet網絡結構詳解及代碼複現

1x1卷積應用于3x3卷積和5x5卷積之前,主要作用:降維,降低參數數量

inception(3a)

3x3卷積

為例:

input

:

28x28x192

  • 不使用

    1x1

    卷積

    其參數數量為:

    192x3x3x128=221184

  • 使用

    1x1

    卷積

    其參數數量為:

    192x1x1x96+96x3x3x128=184320

    相當于先将

    channel

    的維數從

    192

    維降到

    96

  1. GoogLeNet參數
GoogLeNet網絡結構詳解及代碼複現
  • 所有的卷積層都包含

    ReLu

    激活層
  • #3x3 reduce:表示

    Inception

    結構中

    3x3

    卷積層前的

    1x1

    卷積核的數量
  • #5x5 reduce:表示

    Inception

    結構中的

    5x5

    卷積前的

    1x1

    卷積核的數量
  • pool proj:表示

    Inception

    結構中的最大池化層後的

    1x1

    卷積核的數量
  1. GoogLeNet網絡結構
GoogLeNet網絡結構詳解及代碼複現
  • 紅色:池化層
  • 藍色:卷積層+ReLu
  • 綠色:拼接操作
  • 黃色:softmax激活函數

由于網絡的深度相對比較大,能夠在所有層保證梯度能傳播是一個問題。對此我們增加了2個輔助分類器,在訓練期間輔助分類器的權重為0.3,在預測時,這些層會被丢棄。

輔助分類器結構:

GoogLeNet網絡結構詳解及代碼複現
  • AveragePool:濾波器大小(5,5)、步長:3。輸出:4*4*512(from 4a)、4*4*528(from 4d)
  • Conv:1x1卷積、卷積核數量:128、步長:1
  • FC:(128*4*4,1024)
  • FC:(1024,1000)

2. 基于Pytorch代碼複現:

2.1 模型搭建

import torch
import torch.nn as nn
import torchvision.models as models
from torchsummary import summary
import torch.optim as optim


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

        self.conv1 = BasicConv2d(3, 64, kernel_size=7, stride=2, padding=3)
        self.maxpool1 = nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)
        self.conv2 = BasicConv2d(64, 64, kernel_size=1)
        self.conv3 = BasicConv2d(64, 192, kernel_size=3, padding=1)
        self.maxpool2 = nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)

        self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32)
        self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64)
        self.maxpool3 = nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)

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

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

        if self.aux_logits:
            self.aux1 = InceptionAux(512, num_classes)
            self.aux2 = InceptionAux(528, num_classes)

        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.dropout = nn.Dropout(p=0.2)
        self.fc = nn.Linear(1024, num_classes)
        if init_weights:
            self._init_weight()

    def forward(self, x):
        # N x 3 x 224 x 224
        x = self.conv1(x)
        # N x 64 x 112 x 112
        x = self.maxpool1(x)
        # N x 64 x 56 x 56
        x = self.conv2(x)
        # N x 64 x 56 x 56
        x = self.conv3(x)
        # N x 192 x 56 x 56
        x = self.maxpool2(x)

        # N x 192 x 28 x 28
        x = self.inception3a(x)
        # N x 256 x 28 x 28
        x = self.inception3b(x)
        # N x 480 x 28 x 28
        x = self.maxpool3(x)
        # N x 480 x 14 x 14
        x = self.inception4a(x)
        # N x 512 x 14 x 14
        if self.aux_logits and self.training:
            aux1 = self.aux1(x)

        x = self.inception4b(x)
        # N x 512 x 14 x 14
        x = self.inception4c(x)
        # N x 512 x 14 x 14
        x = self.inception4d(x)
        # N x 528 x 14 x 14
        if self.aux_logits and self.training:
            aux2 = self.aux2(x)

        x = self.inception4e(x)
        # N x 832 x 14 x 14
        x = self.maxpool4(x)
        # N x 832 x 7 x 7
        x = self.inception5a(x)
        # N x 832 x 7 x 7
        x = self.inception5b(x)
        # N x 1024 x 7 x 7

        x = self.avgpool(x)
        # N x 1024 x 1 x 1
        x = torch.flatten(x, start_dim=1)
        # N x 1024
        x = self.dropout(x)
        x = self.fc(x)

        if self.aux_logits and self.training:
            return x, aux2, aux1
        return x

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


class Inception(nn.Module):
    def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj):
        super(Inception, self).__init__()

        self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)

        self.branch2 = nn.Sequential(
            BasicConv2d(in_channels, ch3x3red, kernel_size=1),
            BasicConv2d(ch3x3red, ch3x3, kernel_size=3, padding=1)
        )

        self.branch3 = nn.Sequential(
            BasicConv2d(in_channels, ch5x5red, kernel_size=1),
            BasicConv2d(ch5x5red, ch5x5, kernel_size=5, padding=2)
        )

        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, dim=1)


class InceptionAux(nn.Module):
    def __init__(self, in_channels, num_classes):
        super(InceptionAux, self).__init__()
        self.averagePool = nn.AdaptiveAvgPool2d((4, 4))
        self.conv = BasicConv2d(in_channels, 128, kernel_size=1)

        self.aux_classifier = nn.Sequential(
            nn.Linear(128 * 4 * 4, 1024),
            nn.Dropout(p=0.5),
            nn.ReLU(inplace=True),
            nn.Linear(1024, num_classes)
        )

    def forward(self, x):
        x = self.averagePool(x)
        x = self.conv(x)
        x = torch.flatten(x, start_dim=1)
        x = self.aux_classifier(x)
        return x


class BasicConv2d(nn.Module):
    def __init__(self, in_channels, out_channels, **kwargs):
        super(BasicConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, **kwargs)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        x = self.conv(x)
        x = self.relu(x)
        return x

           

2.2 訓練結果如下

  1. 訓練資料集與驗證集大小以及訓練參數
Using 3306 images for training, 364 images for validation
Using cuda GeForce RTX 2060 device for training
lr: 0.0001
batch_size: 16
           
  1. 使用自己定義的網絡訓練結果
[epoch 1/10] train_loss: 2.350 val_acc: 0.407
[epoch 2/10] train_loss: 1.912 val_acc: 0.505
[epoch 3/10] train_loss: 1.842 val_acc: 0.511
[epoch 4/10] train_loss: 1.769 val_acc: 0.560
[epoch 5/10] train_loss: 1.746 val_acc: 0.566
[epoch 6/10] train_loss: 1.670 val_acc: 0.621
[epoch 7/10] train_loss: 1.595 val_acc: 0.635
[epoch 8/10] train_loss: 1.538 val_acc: 0.621
[epoch 9/10] train_loss: 1.509 val_acc: 0.681
[epoch 10/10] train_loss: 1.456 val_acc: 0.657
Best acc: 0.681
Finished Training
Train 耗時為:277.0s
           
  1. 使用預訓練模型參數訓練結果
[epoch 1/10] train_loss: 0.668 val_acc: 0.871
[epoch 2/10] train_loss: 0.359 val_acc: 0.901
[epoch 3/10] train_loss: 0.298 val_acc: 0.923
[epoch 4/10] train_loss: 0.268 val_acc: 0.920
[epoch 5/10] train_loss: 0.252 val_acc: 0.904
[epoch 6/10] train_loss: 0.228 val_acc: 0.923
[epoch 7/10] train_loss: 0.196 val_acc: 0.915
[epoch 8/10] train_loss: 0.210 val_acc: 0.92
[epoch 9/10] train_loss: 0.169 val_acc: 0.918
[epoch 10/10] train_loss: 0.179 val_acc: 0.93

Best acc: 0.931
Finished Training
Train 耗時為:239.9s
           

上一篇:VggNet

下一篇:ResNet

完整代碼:https://github.com/codecat0/Classical_Network

繼續閱讀