[tensorflow学习] [CNN framework] tensorflow 实现 VGGNet

VGGNet

通过反复堆叠 3×3 3 × 3 的小型卷积核 和 2×2 2 × 2 的最大池化层,成功构筑了 16 ~19 层的深层卷积神经网络。经常被用来提取图像特征。在官网上有训练好的模型参数。

VGG中使用的是两个 3*3 的卷积层代替一个 5*5 的卷积层;三个 3*3 卷积层串联可以代替一个 7*7 的卷积层。
531+1=3 5 − 3 1 + 1 = 3 and 331+1=1 3 − 3 1 + 1 = 1 相当于 551+1=1 5 − 5 1 + 1 = 1

训练的时候采用参数复用。使用Multi-Scale 的方法做数据增强,将图像缩放到不同的尺寸S,然后随机裁切 224*224 的图片,方式模型过拟合。 S[256,512] S ∈ [ 256 , 512 ] ,将得到的多个版本的数据合起来进行训练。

预测的时候,采用了Multi-Scale 的方法,将图像scale到一个尺寸Q,并将图像输入卷积网络计算。然后在最后一个卷积层使用滑窗的方式进行分类预测,将不同窗口的分类结果平均。再将不同尺寸的Q的结果平均得到最后结果。

Multi-Scale 方法:
Single-Scale 方法:

作者总结观点:

  • LRN层作用不大
  • 越深的网络效果越好
  • 1*1 的卷积效果有效,但是没有 3*3 的卷积好,大些的卷积核可以得到更大的空间特征

refer:


code

# coding:utf-8

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

'''
    构造卷积层
    n_in:通道数
    kernel:卷积核参数, xavier_initializer_conv2d用作参数初始化
    xavier_initializer_conv2d: 
    [kh, kw, n_in, n_out] --->>> [filter_height, filter_width, in_channels, out_channels]
    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())

        #  Given an input tensor of shape `[batch, in_height, in_width, in_channels]`
        #   and a filter / kernel tensor of shape
        #   `[filter_height, filter_width, in_channels, out_channels]`,
        #  strides = [1, stride, stride, 1]
        conv = tf.nn.conv2d(input_op, kernel, strides=[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

'''
    构造全连接层
    input_op:输入
    n_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')

        # Compute Relu(x * weight + biases).
        # Dimensions typically: batch, out_units.
        activation = tf.nn.relu_layer(input_op, kernel, biases, name=scope)

        p+=[kernel, biases]
        return activation

'''
    最大池化层
    池化尺寸:[kh, kw]
    步长:[dh, dw]
'''
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)

'''
    VGG16 共有6个部分,前5个部分为卷积神经网络,最后一个是全连接层。
    input_op: 输入
    keep_prob: 控制 dropout 的比率
'''
def inference_op(input_op, keep_prob):
    p=[]
    # 第一层
    # input_op = [224, 224, 3]
    # conv1_1 = [224, 224, 64]
    # conv1_2 = [224, 224, 64]
    # pool1 = [112, 112, 64] --->[2, 2]的池化层
    # 两个 3*3 的卷积层代替一个 5*5 的卷积层
    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)

    # 第二层, 两个卷积层 + 一个最大池化层
    # pool2 = [56, 56, 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)

    # 第三层,三个卷积层,+ 一个最大池化层
    # pool3 = [28, 28, 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)

    # 前三层都是在池化的时候将图缩减到原来的四分之一,而通道变为原来的俩倍,每次输出的tensor总尺寸为原来的一半

    # 第四层 三个卷积层 + 一个最大池化层
    # pool4 = [14, 14, 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)

    # 第五层 三个卷积层 + 一个最大池化层
    # pool5 = [7, 7, 512]
    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)

    # 将 pool5 扁平化,转化为全连接层的输入,一维向量
    # [7 * 7 * 512] = [25088]
    shp = pool5.get_shape()
    flattened_shape = shp[1].value * shp[2].value * shp[3].value
    resh1 = tf.reshape(pool5, [-1, flattened_shape], name='resh1')

    # 全连接层 + dropout 层
    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')

    # 最后一层 1000 输出
    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

num_batches = 30
batch_size = 1      # 如果过大,会造成显存不够用
def time_tensorflow_run(session, target, feed, info_string):
    num_step_burn_in = 10
    total_duration = 0.0
    total_duration_squared = 0.0

    for i in range(num_step_burn_in + num_batches):
        start_time = time.time()
        _ = session.run(target, feed_dict = feed)
        duration = time.time() - start_time
        if i >= num_step_burn_in:
            if not i % 10:
                print("%s: step %d, duration = %.3f" % (datetime.now(), i-num_step_burn_in, duration))
            total_duration += duration
            total_duration_squared += duration * duration

        mn = total_duration / num_batches
        vr = total_duration_squared / num_batches
        sd = math.sqrt(vr)
        print("%s: %s across %d steps, %.3f +/- %.3f sec / batch" % (datetime.now(), info_string, num_batches, mn, sd))

'''
    评测forward 和 backward 的性能,并不进行实质的训练和预测
'''
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)
        with tf.device('/cpu:0'):
            predictions, softmax, fc8, p = inference_op(images, keep_prob)

        # 创建Session,初始化全局参数
        init = tf.global_variables_initializer()

        with tf.Session() as sess:
            sess.run(init)
            with tf.device('/cpu:0'):
                time_tensorflow_run(sess, predictions, {keep_prob: 1.0}, 'Forward')
            objective = tf.nn.l2_loss(fc8)

            with tf.device('/cpu:0'):
                grad = tf.gradients(objective, p)
                time_tensorflow_run(sess, grad, {keep_prob: 0.5}, 'Forward-backward')

run_benchmark()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值