美文网首页机器学习实战
深度学习——CNN(3)

深度学习——CNN(3)

作者: 飘涯 | 来源:发表于2018-05-29 09:53 被阅读5次

    前言:前面介绍了最基本的Lenet,下面介绍几种其他的网络结构

    CNN-AlexNet

    网络结构如下图:



    从图中可以看出,采用双gpu训练
    增加LRN归一化层:本质上,这个层也是为了防止激活函数的饱和的。
    采用dropout防止过拟合
    基于AlexNet进行微调 ,诞生了ZF-net


    CNN-GoogleNet

    GoogLeNet借鉴了NIN的特性,在原先的卷积过程中附加了11的 卷积核加上ReLU激活。
    这不仅仅提升了网络的深度,提高了representation power,而且 文中还通过1
    1的卷积来进行降维,减少了更新参数量。

    • NIN模型
      Network-in-Network主要思想是,用全连接的多层感知机去代替 传统的卷积过程,以获取特征更加全面的表达,同时,因为前面已 经做了提升特征表达的过程,传统CNN最后的全连接层也被替换为 一个全局平均池化层,因为作者认为此时的map已经具备分类足够 的可信度了,它可以直接通过softmax来计算loss了。


      NIN模型
    • 在计算要求增加很多的地方应用维度缩减 和预测。即,在3x3和5x5的卷积前用一个1x1的卷积用于减少计算, 还用于修正线性激活。如下图所示,左边是加入维度缩减之前的, 右边是加入维度缩减之后的。



      最终网络结构如下:


    其他网络结构

    还有VGG-net,RES-net等,结构都是在最基本的网络结构上进行改进,比如R esNet结构
    使用了一种连接方式叫做“shortcut connection”,顾名思义, shortcut就是“抄近道”的意思。
    参数更新用了RNN的思想



    下面给一个CNN-VGGNet15层代码的示例,用来判断17种花的一个分类情况

    def vgg_network(x, y):
        net1_kernel_size = 32
        net3_kernel_size = 64
        net5_kernal_size_1 = 128
        net5_kernal_size_2 = 128
        net7_kernal_size_1 = 256
        net7_kernal_size_2 = 256
        net9_kernal_size_1 = 256
        net9_kernal_size_2 = 256
        net11_unit_size = 1000
        net12_unit_size = 1000
        net13_unit_size = 17
    
        # cov3-64 lrn
        with tf.variable_scope('net1'):
            net = tf.nn.conv2d(x, filter=get_variable('w', [3, 3, 3, net1_kernel_size]), strides=[1, 1, 1, 1],
                               padding='SAME')
            net = tf.nn.bias_add(net, get_variable('b', [net1_kernel_size]))
            net = tf.nn.relu(net)
            # lrn(input, depth_radius=5, bias=1, alpha=1, beta=0.5, name=None)
            # 做一个局部响应归一化,是对卷积核的输出值做归一化
            # depth_radius ==> 对应ppt公式上的n,bias => 对应ppt公式上的k, alpha => 对应ppt公式上的α, beta=>对应ppt公式上的β
            net = tf.nn.lrn(net)
        # maxpool
        with tf.variable_scope('net2'):
            net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        # conv3-128
        with tf.variable_scope('net3'):
            net = tf.nn.conv2d(net, filter=get_variable('w', [3, 3, net1_kernel_size, net3_kernel_size]),
                               strides=[1, 1, 1, 1],
                               padding='SAME')
            net = tf.nn.bias_add(net, get_variable('b', [net3_kernel_size]))
            net = tf.nn.relu(net)
        # maxpool
        with tf.variable_scope('net4'):
            net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        # conv3-256 conv3-256
        with tf.variable_scope('net5'):
            net = tf.nn.conv2d(net, filter=get_variable('w1', [3, 3, net3_kernel_size, net5_kernal_size_1]),
                               strides=[1, 1, 1, 1],
                               padding='SAME')
            net = tf.nn.bias_add(net, get_variable('b1', [net5_kernal_size_1]))
            net = tf.nn.relu(net)
    
            net = tf.nn.conv2d(net, filter=get_variable('w2', [3, 3, net5_kernal_size_1, net5_kernal_size_2]),
                               strides=[1, 1, 1, 1],
                               padding='SAME')
            net = tf.nn.bias_add(net, get_variable('b2', [net5_kernal_size_2]))
            net = tf.nn.relu(net)
        # maxpool
        with tf.variable_scope('net6'):
            net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        # conv3-512 conv3-512
        with tf.variable_scope('net7'):
            net = tf.nn.conv2d(net, filter=get_variable('w1', [3, 3, net5_kernal_size_2, net7_kernal_size_1]),
                               strides=[1, 1, 1, 1],
                               padding='SAME')
            net = tf.nn.bias_add(net, get_variable('b1', [net7_kernal_size_1]))
            net = tf.nn.relu(net)
    
            net = tf.nn.conv2d(net, filter=get_variable('w2', [3, 3, net7_kernal_size_1, net7_kernal_size_2]),
                               strides=[1, 1, 1, 1],
                               padding='SAME')
            net = tf.nn.bias_add(net, get_variable('b2', [net7_kernal_size_2]))
            net = tf.nn.relu(net)
        # maxpool
        with tf.variable_scope('net8'):
            net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        # conv3-512 conv3-512
        with tf.variable_scope('net9'):
            net = tf.nn.conv2d(net, filter=get_variable('w1', [3, 3, net7_kernal_size_2, net9_kernal_size_1]),
                               strides=[1, 1, 1, 1],
                               padding='SAME')
            net = tf.nn.bias_add(net, get_variable('b1', [net9_kernal_size_1]))
            net = tf.nn.relu(net)
    
            net = tf.nn.conv2d(net, filter=get_variable('w2', [3, 3, net9_kernal_size_1, net9_kernal_size_2]),
                               strides=[1, 1, 1, 1],
                               padding='SAME')
            net = tf.nn.bias_add(net, get_variable('b2', [net9_kernal_size_2]))
            net = tf.nn.relu(net)
        # maxpool
        with tf.variable_scope('net10'):
            net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        # fc
        with tf.variable_scope('net11'):
            # 将四维的数据转换为两维的数据
            shape = net.get_shape()
            feature_number = shape[1] * shape[2] * shape[3]
            net = tf.reshape(net, shape=[-1, feature_number])
            # 全连接
            net = tf.add(tf.matmul(net, get_variable('w', [feature_number, net11_unit_size])),
                         get_variable('b', [net11_unit_size]))
        # fc
        with tf.variable_scope('net12'):
            # 全连接
            net = tf.add(tf.matmul(net, get_variable('w', [net11_unit_size, net12_unit_size])),
                         get_variable('b', [net12_unit_size]))
        # fc
        with tf.variable_scope('net13'):
            # 全连接
            net = tf.add(tf.matmul(net, get_variable('w', [net12_unit_size, net13_unit_size])),
                         get_variable('b', [net13_unit_size]))
    
        # softmax
        with tf.variable_scope('net14'):
            # softmax
            act = tf.nn.softmax(net)
    
        return act
    

    详细代码见:https://github.com/dctongsheng/Lenet

    相关文章

      网友评论

        本文标题:深度学习——CNN(3)

        本文链接:https://www.haomeiwen.com/subject/ffsojftx.html