2018-08-07VGGNet-16实现详解

A结构

B代码

A结构:

结构为8段。(不包含LRN与池化层)

conv1_1+conv1_2+pool1 ----->conv2_1+conv2_2+pool2----->conv3_1+conv3_2+conv3_3+pool3----->conv4_1+conv4_2+conv4_3+pool4------>conv5_1+conv5_2+conv5_3+pool5---->3个全连接(fc6&dropout---->fc7&dropout---->fc8)

其中输入结构【32,224,224,3】其中32为batchsize,224x224是图像大小,深度为3
conv1_1输出的结构【32,224,224,64】
pool1输出的结构【32,112,112,64】

也就是说 第一段统称co4nv1输出的结构是【32,112,112,64】

conv2输出的结构【32,56,56,128】

conv3输出的结构【32,28,28,256】
conv4输出的结构【32,14,14,512】
conv5输出的结构【32,7,7,512】一共25088个向量
fc6 4096
fc7 4096
fc8 1000

从上面结构也可以看出,前四层每一段卷积都将边长缩小一半,输出通道翻倍。

上述中
卷积结构【3,3,,64】,步长结构【1,1,1,1】
卷积结构【3,3,
,64】,步长结构【1,1,1,1】
池化结构【1,2,2,1】,步长结构【1,2,2,1】

卷积结构【3,3,,128】,步长结构【1,1,1,1】
卷积结构【3,3,
,128】,步长结构【1,1,1,1】
池化结构【1,2,2,1】,步长结构【1,2,2,1】

卷积结构【3,3,,256】,步长结构【1,1,1,1】
卷积结构【3,3,
,256】,步长结构【1,1,1,1】
卷积结构【3,3,_,256】,步长结构【1,1,1,1】
池化结构【1,2,2,1】,步长结构【1,2,2,1】

卷积结构【3,3,,512】,步长结构【1,1,1,1】
卷积结构【3,3,
,512】,步长结构【1,1,1,1】
卷积结构【3,3,_,512】,步长结构【1,1,1,1】
池化结构【1,2,2,1】,步长结构【1,2,2,1】

卷积结构【3,3,,512】,步长结构【1,1,1,1】
卷积结构【3,3,
,512】,步长结构【1,1,1,1】
卷积结构【3,3,_,512】,步长结构【1,1,1,1】
池化结构【1,2,2,1】,步长结构【1,2,2,1】

全链接3层节点分别4096,4096,1000。到这里,是不是可以根据所有信息自己实现代码了呢~

B代码:

测试结果:
书 GPU:每10步0.15分钟
我CPU:每10步26分钟

from datetime import datetime
import math
import time
import tensorflow as tf


#用来创建卷积层并把参数存入参数列表
#输入的tensor
#这一层的名字
#kh是卷积核的高
#kw是卷积核的宽
#n_out是卷积核的数量,输出通道数
#dh是步长的高
#dw是步长的宽
#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,(1,dh,dw,1),padding='SAME')
        bias_init_val = tf.constant(0.0,shape=[n_out],dtype=tf.float32)
        biases = tf.Variable(bias_init_val,trainable=True,name='b')
        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),name='b')
        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):
    p = []
    # assume input_op shape is 224x224x3

    # block 1 -- outputs 112x112x64
    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, dw=2, dh=2)

    # block 2 -- outputs 56x56x128
    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)

    # # block 3 -- outputs 28x28x256
    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)

    # block 4 -- outputs 14x14x512
    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)

    # block 5 -- outputs 7x7x512
    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, dw=2, dh=2)

    # flatten
    shp = pool5.get_shape()
    flattened_shape = shp[1].value * shp[2].value * shp[3].value
    resh1 = tf.reshape(pool5, [-1, flattened_shape], name="resh1")

    # fully connected
    fc6 = fc_op(resh1, name="fc6", n_out=4096, p=p)
    fc6_drop = tf.nn.dropout(fc6, 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, name="fc7_drop")

    fc8 = fc_op(fc7_drop, name="fc8", n_out=1000, p=p)
    softmax = tf.nn.softmax(fc8)
    predictions = tf.argmax(softmax, 1)
    return predictions, softmax, fc8, p
    
    


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



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=1e-1))

        keep_prob = tf.placeholder(tf.float32)
        predictions, softmax, fc8, p = inference_op(images, keep_prob)

        init = tf.global_variables_initializer()

        config = tf.ConfigProto()
        config.gpu_options.allocator_type = 'BFC'
        sess = tf.Session(config=config)
        sess.run(init)

        time_tensorflow_run(sess, predictions, {keep_prob:1.0}, "Forward")

        objective = tf.nn.l2_loss(fc8)
        grad = tf.gradients(objective, p)
        time_tensorflow_run(sess, grad, {keep_prob:0.5}, "Forward-backward")

batch_size=32
num_batches=100
run_benchmark()

你可能感兴趣的:(2018-08-07VGGNet-16实现详解)