VGGNet 是牛津计算机视觉组和Google DeepMind公司的研究员一起研发的深度卷积神经网络。VGGNet探索了卷积神经网络的深度与其性能之间的关系。通过反复堆叠3x3的小型卷积核和2x2的最大池化层,VGGNet成功地构建了16~19层深的卷积神经网络。VGGNet的扩展性很强,迁移到其他图片数据上的泛化性能非常好。结构简洁有效,VGGNet到目前为止依然常被用来提取图像特征。VGGNet拥有5段卷积,每一段内有2~3个卷积层,同时每一段尾部会连接一个最大池化层用来缩小图片尺寸。每段内的卷积核数量一样,越靠后的段的卷积核数量越多:64-128-256-512-512。

VGGNet中还用到一个技巧,多个相同的filter堆叠在一起。比如两个3x3堆叠在一起,那就相当于一个5x5的filter。而三个3x3filter堆叠在一起,就等同于一个7x7的filter。除此之外$\frac{3x3x3}{7x7} = 55\%$,参数只有后者的55%。而且,3个3x3的卷积层拥有比1个7x7的卷积层更多的非线性变换(前者可以使用3次ReLU激活函数增加非线性表达能力,而后者只有一次),使得CNN对特征的学习能力更强。

  1. LRN层作用不大
  2. 越深的网络效果越好
  3. 1x1的卷积也是很有效的,但是没有3x3的卷积好,大一些的卷积核可以学习更大的空间特征。

In [ ]:
import tensorflow as tf
import numpy as np
import math
import time
from datetime import datetime
import os

In [ ]:
tf.__version__

卷积函数

先写一个函数conv_op,用来创建卷积层并把本层参数存入参数列表。

  1. input_op是输入的tensor
  2. name是本层的参数的名称
  3. kh 是卷积核(filter)的高
  4. kw 是卷积核(filter)的宽
  5. n_out 是输出的通道数
  6. dh 是移动步长(stride)的高
  7. dw 是移动步长(stride)的宽
  8. p 是参数列表,存储所有参数

使用xavier作为初始化的方法。


In [ ]:
import numpy as npdef 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:
        # 权重系数,使用get_variable方法,如果当前权重变量存在就获取不存在就新建
        kernel = tf.get_variable(
            scope+"w", 
            shape=[kh, kw, n_in, n_out], 
            dtype=tf.float32,
            initializer=tf.contrib.layers.xavier_initializer_conv2d())
        biases = tf.Variable(tf.constant(0.0, shape=[n_out], dtype=tf.float32), trainable=True, name='b')
        # 卷积计算
        conv = tf.nn.conv2d(input=input_op, filter=kernel, strides=[1, dh, dw, 1], padding='SAME')
        z = tf.nn.bias_add(conv, biases)
        # 激活函数使用ReLU方法
        activation = tf.nn.relu(z, name=scope)
        p += [kernel, biases]
        return activation

全连接函数

  1. input_op 是输入层tensor
  2. name 是名称
  3. n_out 是输出的通道数
  4. p 是参数列表

relu_layer操作等同于: tf.nn.relu(tf.matmul(input_op, kernel) + biases)


In [ ]:
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_conv2d())
        biases = tf.Variable(tf.constant(0.1, shape=[n_out], dtype=tf.float32), name='b')
        activation = tf.nn.relu_layer(input_op, kernel, biases, name=scope)
        p += [kernel, biases]
        return activation

最大池化层

  1. input_op 是输入层tensor
  2. name 是名称
  3. kh 是池化块的高
  4. kw 是池化块的宽
  5. dh 是移动步长的高
  6. dw 是移动步长的宽

In [ ]:
def mpool_op(input_op, name, kh, kw, dh, dw):
    return tf.nn.max_pool(value=input_op, ksize=[1, kh, kh, 1], strides=[1, dh, dw, 1], padding='SAME', name=name)

VGGNet-16

接下来就是用以上的函数,组装网络。

VGGNet-16 一共有16层,分为6个部分。前5段为卷积网络,最后一段是全连接网。max_pool没有参数所以不计入层数。

D
input 224x224 RGB image
conv3-64
conv3-64
maxpool
conv3-128
conv3-128
maxpool
conv3-256
conv3-256
conv3-256
maxpool
conv3-512
conv3-512
conv3-512
maxpool
conv3-512
conv3-512
conv3-512
maxpool
FC-4096
FC-4096
FC-1000
softmax

In [ ]:
def inference_op(input_op, keep_prob):
    p = []
    
    # 第一段:两个conv64,都是3x3
    conv1_1 = conv_op(input_op, name='conv1_1', kh=3, kw=3, n_out=64, dh=1, dw=1, p=p)
    conv1_2 = conv_op(conv1_1, name='conv1_2', kh=3, kw=3, n_out=64, dh=1, dw=1, p=p)
    pool1 = mpool_op(conv1_2, name='pool1', kh=2, kw=2, dh=2, dw=2)
    
    # 第二段:两个卷积层加一个最大池化层,输出通道都是128
    conv2_1 = conv_op(pool1, name='conv2_1', kh=3, kw=3, n_out=128, dh=1, dw=1, p=p)
    conv2_2 = conv_op(conv2_1, name='conv2_2', kh=3, kw=3, n_out=128, dh=1, dw=1, p=p)
    pool2 = mpool_op(conv2_2, name='pool2', kh=2, kw=2, dh=2, dw=2)
    
    # 第三段:三个卷积层加一个最大池化层,输出通道都是256
    conv3_1 = conv_op(pool2, name='conv3_1', kh=3, kw=3, n_out=256, dh=1, dw=1, p=p)
    conv3_2 = conv_op(conv3_1, name='conv3_2', kh=3, kw=3, n_out=256, dh=1, dw=1, p=p)
    conv3_3 = conv_op(conv3_2, name='conv3_3', kh=3, kw=3, n_out=256, dh=1, dw=1, p=p)
    pool3 = mpool_op(conv3_3, name='pool3', kh=2, kw=2, dh=2, dw=2)
    
    # 第四段:三个卷积层加一个最大池化层,输出通道都是512
    conv4_1 = conv_op(pool3, name='conv4_1', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    conv4_2 = conv_op(conv4_1, name='conv4_2', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    conv4_3 = conv_op(conv4_2, name='conv4_3', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    pool4 = mpool_op(conv4_3, name='pool4', kh=2, kw=2, dh=2, dw=2)
    
    # 最后一个卷积网络: 通道数量维持在512,3个卷积层加一个最大池化层,卷积核尺寸3x3,步长为1x1
    conv5_1 = conv_op(pool4, name='conv5_1', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    conv5_2 = conv_op(conv5_1, name='conv5_2', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    conv5_3 = conv_op(conv5_2, name='conv5_3', kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)
    pool5 = mpool_op(conv5_3, name='pool5', kh=2, kw=2, dh=2, dw=2)
    
    # 扁平化,使用tf.reshape来拉伸权重
    shp = pool5.get_shape()
    flattened_shape = shp[1:4].num_elements() # 累乘
    flatten = tf.reshape(pool5, shape=[-1, flattened_shape], name='flatten')
    
    # 接下来开始全连接层:4096。加上drop层。
    fc6 = fc_op(flatten, name='fc6', n_out=4096, p=p)
    fc6_drop = tf.nn.dropout(fc6, keep_prob=keep_prob, name='fc6_drop')
    
    # 再来一个全连接层
    fc7 = fc_op(fc6_drop, name='fc7', n_out=4096, p=p)
    fc7_drop = tf.nn.dropout(fc7, keep_prob=keep_prob, name='fc7_drop')
    
    # 最后连接1000个节点,作为类别输出
    fc8 = fc_op(fc7_drop, name='fc8', n_out=1000, p=p)
    softmax = tf.nn.softmax(fc8)
    predictions = tf.argmax(softmax, axis=1)
    
    return predictions, softmax, fc8, p

In [ ]:
import numpy as npdef time_tensorflow_run(session, target, feed, info_string):
    num_steps_burn_in = 10
    total_duration = 0.0
    total_duration_squared = 0.0
    for i in range(num_batches + num_steps_burn_in):
        start_time = time.time()
        _ = session.run(target, feed_dict=feed)
        duration = time.time() - start_time
        if i >= num_steps_burn_in:
            if not i % 10:
                print ('%s: step %d, duration = %.3f' %
                      (datetime.now(), i - num_steps_burn_in, duration))
            total_duration += duration
            total_duration_squared += duration * duration
    mn = total_duration / num_batches
    vr = total_duration_squared / num_batches - mn * mn
    sd = math.sqrt(vr)
    print ('%s: %s across %d steps, %.3f +- %.3f sec / batch' %
          (datetime.now(), info_string, num_batches, mn, sd))

In [ ]:
def run_benchmark():
    with tf.Graph().as_default():
        image_size = 224
        images = tf.Variable(tf.random_normal([batch_size, image_size, image_size, 3], dtype=tf.float32, stddev=0.1))
        keep_prob = tf.placeholder(tf.float32)
        predictions, softmax, fc8, p = inference_op(images, keep_prob)
        sess = tf.Session()
        init = tf.global_variables_initializer()
        sess.run(init)
        # forawrd
        time_tensorflow_run(sess, predictions, {keep_prob: 1.0}, 'Forward')
        # backward
        objective = tf.nn.l2_loss(fc8)
        grad = tf.gradients(objective, p) # gradients(ys, xs)
        time_tensorflow_run(sess, grad, {keep_prob: 0.5}, 'Forward-backward')

在GPU上运行benchmark,结果如下:


In [ ]:
batch_size = 32
num_batches = 100

In [ ]:
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# for x in range(1, 10):
#     batch_size = x
#     run_benchmark()
#     print ("\n")
run_benchmark()

在CPU上运行benchmark,结果如下:


In [ ]:
# os.environ['CUDA_VISIBLE_DEVICES'] = ''
# run_benchmark()

In [ ]:
fx = range(1, 10)
fy_new = [0.055, 0.060, 0.072, 0.086, 0.101, 0.115, 0.133, 0.146, 0.166]
fy_old = [0.925, 1.532, 2.047, 2.645, 3.117, 3.703, 4.892, 5.887, 6.799]

In [ ]:
import matplotlib.pyplot as plt

In [ ]:
lines = plt.plot(fx, fy_new, '-*', fx, fy_old, '--o')
plt.xlim(1, 10)
plt.ylim(-0.01, 7)
plt.title('VggNet Benchmark on desktop and macbook')
plt.xlabel('Batch Size')s
plt.legend(lines, ('desktop', 'macbook'))
plt.ylabel('Forward-backward time/batch')
plt.show()

In [ ]:
plt.plot(fx, np.array(fy_old)/np.array(fy_new), ls='-', color='orange', marker='*')
plt.xlabel('Batch Size')
plt.ylabel('Times')
plt.show()

In [ ]: