Deep Learning-TensorFlow (14) CNN卷积神经网络_深度残差网络 ResNet

环境:Win8.1 TensorFlow1.0.1

软件:Anaconda3 (集成Python3及开发环境)

TensorFlow安装:pip install tensorflow (CPU版) pip install tensorflow-gpu (GPU版)

TFLearn安装:pip install tflearn


参考:

Deep Residual Learning for Image Recognition Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun


1. 前言


ResNet(Residual Neural Network)由前微软研究院的 Kaiming He 等4名华人提出,通过使用 Residual Blocks 成功训练152层深的神经网络,在 ILSVRC 2015 比赛中获得了冠军,取得 3.57% 的 top-5 错误率,同时参数量却比 VGGNet 低,效果非常突出。ResNet 的结构可以极快地加速超深神经网络的训练,模型的准确率也有非常大的提升。上一篇博文讲解了 Inception,而 Inception V4 则是将 Inception Module 和 ResNet 相结合。可以看到 ResNet 是一个推广性非常好的网络结构,甚至可以直接应用到 Inception Net 中。


CVPR16 上何凯明、张祥雨、任少卿和孙剑四人的 Deep Residual Learning for Image Recognition 毫无争议地获得了 Best Paper

关于这篇文章细节可参考:

  1. 获奖无数的深度残差学习,清华学霸的又一次No.1 | CVPR2016 最佳论文
  2. 秒懂!何凯明的深度残差网络PPT是这样的|ICML2016 tutorial

2. 问题


作者首先提出的问题是,深度神经网络是不是越深越好? <

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 10
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
好的,让我们开始吧! 首先,我们需要导入必要的库和数据集。这里我们使用MNIST数据集,它包含手写数字的图像和对应的标签。 ```python import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.datasets import mnist # 导入数据集 (x_train, y_train), (x_test, y_test) = mnist.load_data() ``` 接下来,我们将对图像进行预处理,将像素值缩放到0到1之间,并将标签转换为one-hot编码。 ```python # 将像素值缩放到0到1之间 x_train = x_train.astype("float32") / 255.0 x_test = x_test.astype("float32") / 255.0 # 将标签转换为one-hot编码 y_train = keras.utils.to_categorical(y_train, 10) y_test = keras.utils.to_categorical(y_test, 10) ``` 然后,我们将定义ResNet的结构。这里我们使用了经典的ResNet-18结构,包括卷积层、批归一化层、ReLU激活函数、最大池化层、残差块和全局平均池化层等组件。 ```python def conv_block(inputs, filters, strides): x = layers.Conv2D(filters, 3, strides=strides, padding="same")(inputs) x = layers.BatchNormalization()(x) x = layers.ReLU()(x) return x def identity_block(inputs, filters): x = layers.Conv2D(filters, 3, padding="same")(inputs) x = layers.BatchNormalization()(x) x = layers.ReLU()(x) x = layers.Conv2D(filters, 3, padding="same")(x) x = layers.BatchNormalization()(x) x = layers.Add()([inputs, x]) x = layers.ReLU()(x) return x def resnet18(): inputs = keras.Input(shape=(28, 28, 1)) x = conv_block(inputs, 64, strides=1) x = identity_block(x, 64) x = identity_block(x, 64) x = conv_block(x, 128, strides=2) x = identity_block(x, 128) x = identity_block(x, 128) x = conv_block(x, 256, strides=2) x = identity_block(x, 256) x = identity_block(x, 256) x = conv_block(x, 512, strides=2) x = identity_block(x, 512) x = identity_block(x, 512) x = layers.GlobalAveragePooling2D()(x) outputs = layers.Dense(10, activation="softmax")(x) return keras.Model(inputs, outputs) ``` 最后,我们将编译模型并开始训练。这里我们使用交叉熵损失函数和Adam优化器。 ```python # 创建模型 model = resnet18() # 编译模型 model.compile( loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"] ) # 训练模型 model.fit(x_train, y_train, batch_size=128, epochs=10, validation_split=0.1) # 在测试集上评估模型 model.evaluate(x_test, y_test) ``` 恭喜!现在你已经成功地使用TensorFlow(Keras)搭建了卷积神经网络ResNet,实现了手写数字识别。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DiamonJoy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值