1 - 引言
之前我们介绍了LeNet-5和AlexNet,在AlexNet发明之后,卷积神经网络的层数开始越来越复杂,VGG-16就是一个相对前面2个经典卷积神经网络模型层数明显更多了。
VGGNet是牛津大学计算机视觉组(Visual Geometry Group)和Google DeepMind公司的研究员一起研发的深度卷积神经网络。
VGGNet探索了卷积神经网络的深度与其性能之间的关系,通过反复堆叠33的小型卷积核和22的最大池化层,
VGGNet成功地构筑了16~19层深的卷积神经网络。VGGNet相比之前state-of-the-art的网络结构,错误率大幅下降,
VGGNet论文中全部使用了 3 ∗ 3 3*3 3∗3的小型卷积核和 2 ∗ 2 2*2 2∗2的最大池化核,通过不断加深网络结构来提升性能。
VGG最大的优点或者说是创新之处在于:
VGGNet通过反复堆叠3x3的小型卷积核和2x2的最大池化层,构筑了16~19层深的神经网络。在错误率大大降低的同时扩展性很强,迁移到其它图片数据上的泛化能力很好,而且结构简单。
VGGNet拥有从A到E的五个级别,每一级网络都比前一级根深,但是参数并没有增加很多,因为卷积部分消耗参量不大,主要在全连接层。D和E就是常说的VGGNet-16和VGGNet-19。
VGGNet拥有5段卷积,每一段卷积内有2~3个卷积层,同时每段尾部都会连接一个最大池化层用来缩小图片尺寸,5段卷积后有3个全连接层,然后通过softmax()来预测结果。
是一种虽然结构搭建简单却性能十分优异的卷积网络模型。
2 - VGG结构
训练输入为224224大小的RGB图像,需要减去图像均值。用一堆
3
∗
3
,
1
∗
1
3*3,1*1
3∗3,1∗1小卷积核进行卷积,连接max pooling。最后,连接3个全连接层,softmax分类器。
模型A-E:只增加深度,其他不变(增加33卷积核)
在吴恩达课程中,详细的介绍了VGG-16的模型结构:
输入是224x224x3的图像,下面是每一层的大小:
conv1_1 [32, 224, 224, 64]
conv1_2 [32, 224, 224, 64]
pool1 [32, 112, 112, 64]
conv2_1 [32, 112, 112, 128]
conv2_2 [32, 112, 112, 128]
pool2 [32, 56, 56, 128]
conv3_1 [32, 56, 56, 256]
conv3_2 [32, 56, 56, 256]
conv3_3 [32, 56, 56, 256]
pool3 [32, 28, 28, 256]
conv4_1 [32, 28, 28, 512]
conv4_2 [32, 28, 28, 512]
conv4_3 [32, 28, 28, 512]
pool4 [32, 14, 14, 512]
conv5_1 [32, 14, 14, 512]
conv5_2 [32, 14, 14, 512]
conv5_3 [32, 14, 14, 512]
pool5 [32, 7, 7, 512]
fc6 [32, 4096]
fc7 [32, 4096]
fc8 [32, 1000]
3 - TensorFlow搭建VGG-16模型:
# _*_ coding:utf-8 _*_
import math
import time
import tensorflow as tf
from datetime import datetime
batch_size = 32
num_batches = 100
# 卷积层
'''
input_op输入的tensor
kh, kw是卷积核的高和宽
dh, w是步长到的高和宽
p参数列表
'''
def conv_op(input_op, name, kh, kw, n_out, dh, dw, p):
n_in = input_op.get_shape()[-1].value
with tf.name_scope(name) as scope:
kernel = tf.get_variable(scope+'w',
shape=[kh, kw, n_in, n_out], dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer_conv2d())
conv = tf.nn.conv2d(input_op, kernel, strides=[1, dh, dw, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[n_out], dtype=tf.float32), trainable=True, name='biases')
z = tf.nn.bias_add(conv, biases)
activation = tf.nn.relu(z, name=scope)
p += [kernel, biases]
return activation
# 全连接层
def fc_op(input_op, name, n_out, p):
n_in = input_op.get_shape()[-1].value
with tf.name_scope(name) as scope:
kernel = tf.get_variable(scope+'w',
shape=[n_in, n_out], dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer())
biases = tf.Variable(tf.constant(0.1, shape=[n_out], dtype=tf.float32), trainable=True, name='biases')
activation = tf.nn.relu_layer(input_op, kernel, biases, name=scope)
p += [kernel, biases]
return activation
# 最大池化层
def mpool_op(input_op, name, kh, kw, dh, dw):
return tf.nn.max_pool(input_op,
ksize=[1, kh, kw, 1],
strides=[1, dh, dw, 1],
padding='SAME',
name=name)
# 网络结构
def inference_op(input_op, keep_prob):
parameters = []
# 第一段卷积网络
conv1_1 = conv_op(input_op, name='conv1_1', kh=3, kw=3, n_out=64, dh=1, dw=1, p=parameters)
conv1_2 = conv_op(conv1_1, name='conv1_2', kh=3, kw=3, n_out=64, dh=1, dw=1, p=parameters)
pool1 = mpool_op(conv1_2, name='pool1', kh=2, kw=2, dw=2, dh=2)
# 第二段卷积网络
conv2_1 = conv_op(pool1, name='conv2_1', kh=3, kw=3, n_out=128, dh=1, dw=1, p=parameters)
conv2_2 = conv_op(conv2_1, name='conv2_2', kh=3, kw=3, n_out=128, dh=1, dw=1, p=parameters)
pool2 = mpool_op(conv2_2, name='pool2', kh=2, kw=2, dh=2, dw=2)
# 第三段卷积网络
conv3_1 = conv_op(pool2, name='conv3_1', kh=3, kw=3, n_out=256, dh=1, dw=1, p=parameters)
conv3_2 = conv_op(conv3_1, name='conv3_2', kh=3, kw=3, n_out=256, dh=1, dw=1, p=parameters)
conv3_3 = conv_op(conv3_2, name='conv3_3', kh=3, kw=3, n_out=256, dh=1, dw=1, p=parameters)
pool3 = mpool_op(conv3_3, name='pool3', kh=2, kw=2, dh=2, dw=2)
# 第四段卷积网络
conv4_1 = conv_op(pool3, name='conv4_1', kh=3, kw=3, n_out=512, dh=1, dw=1, p=parameters)
conv4_2 = conv_op(conv4_1, name='conv4_2', kh=3, kw=3, n_out=512, dh=1, dw=1, p=parameters)
conv4_3 = conv_op(conv4_2, name='conv4_3', kh=3, kw=3, n_out=512, dh=1, dw=1, p=parameters)
pool4 = mpool_op(conv4_3, name='pool4', kh=2, kw=2, dh=2, dw=2)
# 第五段卷积网络
conv5_1 = conv_op(pool4, name='conv5_1', kh=3, kw=3, n_out=512, dh=1, dw=1, p=parameters)
conv5_2 = conv_op(conv5_1, name='conv5_1', kh=3, kw=3, n_out=512, dh=1, dw=1, p=parameters)
conv5_3 = conv_op(conv5_2, name='conv5_3', kh=3, kw=3, n_out=512, dh=1, dw=1, p=parameters)
pool5 = mpool_op(conv5_3, name='conv5_3', kh=2, kw=2, dh=2, dw=2)
# 扁平化
shp = pool5.get_shape()
flattened_shape = shp[1].value * shp[2].value * shp[3].value
resh1 = tf.reshape(pool5, [-1, flattened_shape], name='resh1')
# 全连接层
fc6 = fc_op(resh1, name='fc6', n_out=4096, p=parameters)
fc6_drop = tf.nn.dropout(fc6, keep_prob, name='fc6_drop')
fc7 = fc_op(fc6_drop, name='fc7', n_out=4096, p=parameters)
fc7_drop = tf.nn.dropout(fc7, keep_prob, name='fc7_drop')
fc8 = fc_op(fc7_drop, name='fc8', n_out=1000, p=parameters)
softmax = tf.nn.softmax(fc8)
predictions = tf.argmax(softmax, 1)
return predictions, softmax, fc8, parameters