深度学习实战篇之 ( 十六) -- TensorFlow之ResNet

科普知识

NIPS(NeurIPS),全称神经信息处理系统大会(Conference and Workshop on Neural Information Processing Systems),是一个关于机器学习和计算神经科学的国际会议。该会议固定在每年的12月举行,由NIPS基金会主办。NIPS是机器学习领域的顶级会议 。在中国计算机学会的国际学术会议排名中,NIPS为人工智能领域的A类会议。

73866d386989b81242b1ca8c02053d8b.png

# 前言

SEP.

理论篇的上一篇文章中我们学习了残差网络(ResNet),其核心思想是通过跳跃连接构建残差结构,使得网络可以突破深度的限制,从而构建更加深层次的网络。

38fcb207b4df8a5ecb43a0c0cdcc7b8b.png

TensorFlow之ResNet实战

93cfd05c11e034093d4c55cdac179a7f.png

本期实战分享我们主要对ResNet网络结构中常用的ResNet-18进行代码Tensorflow实践, 本次代码十分简洁,希望各位全程跟上。

1.数据准备

c5079c3ed3428526f4155241d5cdcff8.png

本次数据采用RAFDB人脸表情数据集,包含七个分类:平和,开心,悲伤,

惊讶,厌恶,愤怒,害怕。与之前的数据集类似,该人脸表情数据集也包含

训练集合测试集,每个集中没别包含7个文件夹(表情)。与之前的数据集

一样,该数据集包含训练集与测试集,每个集包含七个文件夹(表情)5aeb720ecbdd9de3d7cf4f2f4ea75ee7.png 一些样本展示

ea19af916d70c40b2e9149e74586fa25.png

2.网络结构

a9405985fbc7d41875a40ceb0dfc4e7a.png

1922f8975d61c8a3756a26793fed81f6.png

上图包含了ResNet网络的常见版本,本次分享以18层的ResNet-18为例,网络结构为:

a1d87baa375062c11d8835e4ff6ccf19.png

# 残差块构建
def ResBlock(name, num_blocks, input, inchannel, outchannel, stride):
    conv_input = input
    conv_inchannel = inchannel
    conv_stride = stride
    for i in range(num_blocks):
        out = Conv_layer(names = '{}{}'.format(name, i), input = conv_input , w_shape = [3, 3, conv_inchannel, outchannel], b_shape = [outchannel], strid = [conv_stride, conv_stride])
        conv_input = out
        conv_inchannel = outchannel
        conv_stride = 1
    
    # 残差
    if stride > 1:
        shortcut  = Conv_layer(names = '{}_{}'.format(name,i), input = input , w_shape = [1, 1, inchannel, outchannel], b_shape = [outchannel], strid = [stride, stride])
        out  = out + shortcut 
    return out
    
    # 残差网络构建
    def inference(images, batch_size, n_classes,drop_rate):
    print("******** images {} ".format(images.shape))
    #第一层预处理卷积
    pre_conv1 = Conv_layer(names = 'pre_conv', input = images , w_shape = [7, 7, 3, 64], b_shape = [64], strid = [2, 2])
    print("******** pre_conv1 {} ".format(pre_conv1.shape))


    # 池化层
    pool_1 = Max_pool_lrn(names = 'pooling1', input = pre_conv1 , ksize = [1, 3, 3, 1], is_lrn = False)
    # print("******** pool_1 {} ".format(pool_1.shape))


    # 第一个卷积块(layer1)
    layer1 = ResBlock('Resblock1', 2, pool_1, 64, 64, 1)
    print("******** layer1 {} ".format(layer1.shape))


    # 第二个卷积块(layer2)
    layer2 = ResBlock('Resblock2', 2, layer1, 64, 128, 2)
    print("******** layer2 {} ".format(layer2.shape))


    # 第三个卷积块(layer3)
    layer3 = ResBlock('Resblock3', 2, layer2, 128, 256, 2)
    print("******** layer3 {} ".format(layer3.shape))


    # 第四个卷积块(layer4)
    layer4 = ResBlock('Resblock4', 2, layer3, 256, 512, 2)
    print("******** layer4 {} ".format(layer4.shape))


    # 全局平均池化
    global_avg = tf.nn.avg_pool(layer4, ksize=[1,7,7,1],strides=[1,7,7,1],padding='SAME')
    print("******** global_avg {} ".format(global_avg.shape))


    reshape = tf.reshape(global_avg, shape=[batch_size, -1])
    dim = reshape.get_shape()[1].value


    with tf.variable_scope('softmax_linear') as scope:
        weights = tf.Variable(tf.truncated_normal(shape=[dim, n_classes], stddev=0.005, dtype=tf.float32),
                              name='softmax_linear', dtype=tf.float32)


        biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[n_classes]),
                             name='biases', dtype=tf.float32)


        resnet18_out = tf.add(tf.matmul(reshape, weights), biases, name='softmax_linear')
        print("---------resnet18_out:{}".format(resnet18_out))


    return resnet18_out

3.训练过程

d493683507d523a5430fdc2af3418247.png

f72a8ae50c4da9b6dffd21b3657c207a.png

源码获取:https://gitee.com/fengyuxiexie/res-net-18

82364cbcde38d9f0f7036536a43241be.gif

END

结语

本期分享就到此结束了,残差网络因其卓越的性能深受众多研究者喜爱,由此,残差网络在很多数据集上已经成为了基线模型。

由于残差网络结构实现简单,小编希望各位童鞋能够在看懂网络结构后自己动手实现,深刻体会图像在残差网络中的维度变换,同时学习Tensorflow中卷积的步长与填充的使用,进一步提升Tensorflow的实战能力。

此外,由于Pytorch框架在学术界的受欢迎程度,后面的文章我们或许不再使用Tensorflow,而是直接采用Pytorch实现,希望各位继续喜欢。

编辑:玥怡居士|审核:小圈圈居士

c31d9548f59c36a161e76af34a0a1652.png

IT进阶之旅

往期回顾

深度学习实战篇之 ( 十五) -- TensorFlow之GoogLeNet

深度学习实战篇之 ( 十四) -- TensorFlow之VGG16

深度学习实战篇之 ( 十三) -- TensorFlow之AlexNet

过去的一年,我们都做了啥:

【年终总结】辞旧迎新,2020,我们再出发

【年终总结】2021,辞旧迎新再出发

11931cf613cff89e002ecfe6a3852768.gif

点个"赞"再走吧~

猜你喜欢

转载自blog.csdn.net/xyl666666/article/details/122163836
今日推荐