VGGNet詳解

本博文主要介紹了VGGNET的網(wǎng)絡(luò)結(jié)構(gòu),并在cifar10數(shù)據(jù)集上實(shí)現(xiàn)了

VGGNET詳解

??VGG Net由牛津大學(xué)的視覺幾何組(Visual Geometry Group)和 Google DeepMind公司的研究員一起研發(fā)的的深度卷積神經(jīng)網(wǎng)絡(luò),在 ILSVRC 2014 上取得了第二名的成績,將 Top-5錯誤率降到7.3%。它主要的貢獻(xiàn)是展示出網(wǎng)絡(luò)的深度(depth)是算法優(yōu)良性能的關(guān)鍵部分。
??VGGNET的網(wǎng)絡(luò)結(jié)構(gòu)如下圖所示,VGGNET包含多層網(wǎng)絡(luò),深度從11層到19層不等,較為常用的是VGG16和VGG19,接下來我們以VGG16為例,即下圖中的D,介紹VGGNET。

VGGNET網(wǎng)絡(luò)結(jié)構(gòu)
  1. 輸入尺寸為224\times224\times3的圖片,用64個3\times3的卷積核作兩次卷積和ReLU,卷積后的尺寸變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=224%5Ctimes224%5Ctimes64" alt="224\times224\times64" mathimg="1">。
  2. 池化層,使用max pooling,池化單元大小為2\times2,池化后尺寸變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=112%5Ctimes112%5Ctimes64" alt="112\times112\times64" mathimg="1">。
  3. 輸入尺寸為112\times112\times64,使用128個3\times3的卷積核作兩次卷積和ReLU,尺寸改變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=112%5Ctimes112%5Ctimes128" alt="112\times112\times128" mathimg="1">。
  4. 池化層,使用max pooling,池化單元大小為2\times2,池化后尺寸變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=56%5Ctimes56%5Ctimes128" alt="56\times56\times128" mathimg="1">。
  5. 輸入尺寸為56\times56\times128,使用256個3\times3的卷積核作三次卷積和ReLU,尺寸改變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=56%5Ctimes56%5Ctimes256" alt="56\times56\times256" mathimg="1">。
  6. 池化層,使用max pooling,池化單元大小為2\times2,池化后尺寸變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=28%5Ctimes28%5Ctimes256" alt="28\times28\times256" mathimg="1">。
  7. 輸入尺寸為28\times28\times256,使用512個3\times3的卷積核作三次卷積和ReLU,尺寸改變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=28%5Ctimes28%5Ctimes512" alt="28\times28\times512" mathimg="1">。
  8. 池化層,使用max pooling,池化單元大小為2\times2,池化后尺寸變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=14%5Ctimes14%5Ctimes512" alt="14\times14\times512" mathimg="1">。
  9. 輸入尺寸為14\times14\times512,使用512個3\times3的卷積核作三次卷積和ReLU,尺寸改變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=14%5Ctimes14%5Ctimes512" alt="14\times14\times512" mathimg="1">。
  10. 池化層,使用max pooling,池化單元大小為2\times2,池化后尺寸變?yōu)?img class="math-inline" src="https://math.jianshu.com/math?formula=7%5Ctimes7%5Ctimes512" alt="7\times7\times512" mathimg="1">。
  11. 與兩層1x1x4096,一層1x1x1000進(jìn)行全連接+ReLU(共三層)。
  12. 通過softmax輸出1000個預(yù)測結(jié)果。

VGGNET的特點(diǎn)

  1. VGGNET全部使用3\times3的卷積核和2\times2的池化核,通過不斷加深網(wǎng)絡(luò)深度來提升性能。作者認(rèn)為,兩個3\times3卷積層的串聯(lián)相當(dāng)于1個5\times5的卷積層,3個3\times3的卷積層串聯(lián)相當(dāng)于1個7*7的卷積層,即3個3\times3卷積層的感受野大小相當(dāng)于1個7\times7的卷積層。但是3個3\times3的卷積層參數(shù)量只有7\times7的一半左右,同時前者可以有3個非線性操作,而后者只有1個非線性操作,這樣使得前者對于特征的學(xué)習(xí)能力更強(qiáng)。

  2. VGGNet的卷積層有一個顯著的特點(diǎn):特征圖的空間分辨率單調(diào)遞減,特征圖的通道數(shù)單調(diào)遞增。

代碼實(shí)現(xiàn)

import torch.nn as nn


cfg = {
    'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
    'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
    'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
    'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}


class VGG(nn.Module):
    def __init__(self, vgg_name):
        super(VGG, self).__init__()
        self.features = self._make_layers(cfg[vgg_name])
        self.classifier = nn.Linear(512, 10)

    def forward(self, x):
        out = self.features(x)
        out = out.view(out.size(0), -1)
        out = self.classifier(out)
        return out

    def _make_layers(self, cfg):
        layers = []
        in_channels = 3
        for x in cfg:
            if x == 'M':
                layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
            else:
                layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
                           nn.BatchNorm2d(x),
                           nn.ReLU(inplace=True)]
                in_channels = x
        layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
        return nn.Sequential(*layers)


def VGG11():
    return VGG('VGG11')


def VGG13():
    return VGG('VGG13')


def VGG16():
    return VGG('VGG16')


def VGG19():
    return VGG('VGG19')

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容