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

科普知识

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

785143466bc2862ab2866978dd79049b.png

# 前言

SEP.

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

ebf43b5d5617af6da159d0e2c0be99ba.png

TensorFlow之ResNet实战

09af26b5a08ed245697ded9751ac9a28.png

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

1.数据准备

94984b08b1ab88b4be35852f0ad2d26c.png

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

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

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

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

69add0b716c731ae99103f8b53f704dd.png

2.网络结构

3525755020cb6a8f69f6553443e3488e.png

1d16540289824c9f7a6500e2a9be78aa.png

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

1cf0216b8fc82a2d2205fdfb3ae1d97c.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.训练过程

0fec199c3bac0c1d670c59937a977124.png

75ae9e4505447d9480330035a684ebe9.png

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

e536f7c3965d563a10a7d41bdbb6ff49.gif

END

结语

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

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

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

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

e426339101c3a08168ed583993a6dd8c.jpeg

IT进阶之旅

往期回顾

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

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

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

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

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

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

b9ec7204d15b5c11abdbb07ed0fe0950.gif

点个"赞"再走吧~

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值