CNN架构(3)-- VGGNet  --2014

VGGNet — Simonyan et al

2014年IMAGENET挑战赛的亚军。因为这种统一架构十分轻巧,不少新人将之作为深度卷积神经网络的简单形式。
在下面的文章中,我们将会学习这种最常用的网络架构之一是如何从图片中提取特征的(提取图像信息将之转化为包含图片重要信息的低维数组)。
CNN架构(3)-- VGGNet  --2014

VGGNet — Architecture

VGGNet有两条需要遵守的简单经验法则:

1.每个卷积层的配置为:kernel size = 3×3, stride = 1×1, padding = same.唯一不同的是核数量。

2.每个最大池化层的配置为:windows size = 2×2 and stride = 2×2.因此,我们在每个池化层将图片尺寸降为一半。

输入是224*224的RGB图像,所以输入尺寸为224x224x3

CNN架构(3)-- VGGNet  --2014

总参数为138,000,000.这些参数的大部分都来自于全连接层:

第一层全连接层包含了4096 * (7 * 7 * 512) + 4096 = 102,764,544个参数

第二层全连接层包含了4096 * 4096 + 4096 = 16,781,312个参数

第三层全连接层包含了4096 * 1000 + 4096 = 4,100,096个参数

全连接层共包含了123,645,952个参数。

VGGNet — 代码

from keras import layers
from keras.models import Model, Sequential

from functools import partial

conv3 = partial(layers.Conv2D,
               kernel_size=3,
               strides=1,
               padding='same',
               activation='relu')

def block(in_tensor, filters, n_convs):
   conv_block = in_tensor
   for _ in range(n_convs):
       conv_block = conv3(filters=filters)(conv_block)
   return conv_block

def _vgg(in_shape=(227,227,3),
        n_classes=1000,
        opt='sgd',
        n_stages_per_blocks=[2, 2, 3, 3, 3]):
   in_layer = layers.Input(in_shape)

   block1 = block(in_layer, 64, n_stages_per_blocks[0])
   pool1 = layers.MaxPool2D()(block1)
   block2 = block(pool1, 128, n_stages_per_blocks[1])
   pool2 = layers.MaxPool2D()(block2)
   block3 = block(pool2, 256, n_stages_per_blocks[2])
   pool3 = layers.MaxPool2D()(block3)
   block4 = block(pool3, 512, n_stages_per_blocks[3])
   pool4 = layers.MaxPool2D()(block4)
   block5 = block(pool4, 512, n_stages_per_blocks[4])
   pool5 = layers.MaxPool2D()(block5)
   flattened = layers.GlobalAvgPool2D()(pool5)

   dense1 = layers.Dense(4096, activation='relu')(flattened)
   dense2 = layers.Dense(4096, activation='relu')(dense1)
   preds = layers.Dense(1000, activation='softmax')(dense2)

   model = Model(in_layer, preds)
   model.compile(loss="categorical_crossentropy", optimizer=opt,
               metrics=["accuracy"])
   return model

def vgg16(in_shape=(227,227,3), n_classes=1000, opt='sgd'):
   return _vgg(in_shape, n_classes, opt)

def vgg19(in_shape=(227,227,3), n_classes=1000, opt='sgd'):
   return _vgg(in_shape, n_classes, opt, [2, 2, 4, 4, 4])

if __name__ == '__main__':
   model = vgg19()
   print(model.summary())