tensorflow67 《深度学习原理与TensorFlow实战》04 CNN看懂世界 04深度残差网络
2017-07-06 17:45
134 查看
00 环境
#《深度学习原理与TensorFlow实战》04 CNN看懂世界 # 书源码地址:https://github.com/DeepVisionTeam/TensorFlowBook.git # 视频讲座地址:http://edu.csdn.net/course/detail/5222 # win10 Tensorflow1.2.0 python3.6.1 # CUDA v8.0 cudnn-8.0-windows10-x64-v5.1 # https://github.com/tflearn/tflearn/blob/master/examples/images/residual_network_mnist.py # https://github.com/tflearn/tflearn/blob/master/examples/images/residual_network_cifar10.py[/code]01 residual_network_mnist.py
# -*- coding: utf-8 -*- """ Deep Residual Network. Applying a Deep Residual Network to MNIST Dataset classification task. References: - K. He, X. Zhang, S. Ren, and J. Sun. Deep Residual Learning for Image Recognition, 2015. - Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner. "Gradient-based learning applied to document recognition." Proceedings of the IEEE, 86(11):2278-2324, November 1998. Links: - [Deep Residual Network](http://arxiv.org/pdf/1512.03385.pdf) - [MNIST Dataset](http://yann.lecun.com/exdb/mnist/) """ from __future__ import division, print_function, absolute_import import tflearn import tflearn.data_utils as du # Data loading and preprocessing import tflearn.datasets.mnist as mnist X, Y, testX, testY = mnist.load_data(one_hot=True) X = X.reshape([-1, 28, 28, 1]) testX = testX.reshape([-1, 28, 28, 1]) X, mean = du.featurewise_zero_center(X) testX = du.featurewise_zero_center(testX, mean) # Building Residual Network net = tflearn.input_data(shape=[None, 28, 28, 1]) net = tflearn.conv_2d(net, 64, 3, activation='relu', bias=False) # Residual blocks net = tflearn.residual_bottleneck(net, 3, 16, 64) net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=True) net = tflearn.residual_bottleneck(net, 2, 32, 128) net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=True) net = tflearn.residual_bottleneck(net, 2, 64, 256) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 10, activation='softmax') net = tflearn.regression(net, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.1) # Training model = tflearn.DNN(net, checkpoint_path='model_resnet_mnist', max_checkpoints=10, tensorboard_verbose=0) model.fit(X, Y, n_epoch=100, validation_set=(testX, testY), show_metric=True, batch_size=256, run_id='resnet_mnist') ''' Extracting mnist/train-images-idx3-ubyte.gz Extracting mnist/train-labels-idx1-ubyte.gz Extracting mnist/t10k-images-idx3-ubyte.gz Extracting mnist/t10k-labels-idx1-ubyte.gz --------------------------------- Run id: resnet_mnist Log directory: /tmp/tflearn_logs/ --------------------------------- Training samples: 55000 Validation samples: 10000 -- Training Step: 1 | time: 6.395s | Momentum | epoch: 001 | loss: 0.00000 - acc: 0.0000 -- iter: 00256/55000 Training Step: 2 | total loss: 2.07091 | time: 11.896s | Momentum | epoch: 001 | loss: 2.07091 - acc: 0.0844 -- iter: 00512/55000 Training Step: 3 | total loss: 2.25693 | time: 17.365s ... Training Step: 242 | total loss: 0.07294 | time: 156.351s | Momentum | epoch: 002 | loss: 0.07294 - acc: 0.9785 -- iter: 06912/55000 Training Step: 243 | total loss: 0.07384 | time: 162.524s | Momentum | epoch: 002 | loss: 0.07384 - acc: 0.9783 -- iter: 07168/55000 Training Step: 244 | total loss: 0.07520 | time: 168.575s | Momentum | epoch: 002 | loss: 0.07520 - acc: 0.9789 -- iter: 07424/55000 ... '''02 residual_network_cifar10.py
# -*- coding: utf-8 -*- """ Deep Residual Network. Applying a Deep Residual Network to CIFAR-10 Dataset classification task. References: - K. He, X. Zhang, S. Ren, and J. Sun. Deep Residual Learning for Image Recognition, 2015. - Learning Multiple Layers of Features from Tiny Images, A. Krizhevsky, 2009. Links: - [Deep Residual Network](http://arxiv.org/pdf/1512.03385.pdf) - [CIFAR-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar.html) """ from __future__ import division, print_function, absolute_import import tflearn # Residual blocks # 32 layers: n=5, 56 layers: n=9, 110 layers: n=18 n = 5 # Data loading from tflearn.datasets import cifar10 (X, Y), (testX, testY) = cifar10.load_data() Y = tflearn.data_utils.to_categorical(Y, 10) testY = tflearn.data_utils.to_categorical(testY, 10) # Real-time data preprocessing img_prep = tflearn.ImagePreprocessing() img_prep.add_featurewise_zero_center(per_channel=True) # Real-time data augmentation img_aug = tflearn.ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_crop([32, 32], padding=4) # Building Residual Network net = tflearn.input_data(shape=[None, 32, 32, 3], data_preprocessing=img_prep, data_augmentation=img_aug) net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, n, 16) net = tflearn.residual_block(net, 1, 32, downsample=True) net = tflearn.residual_block(net, n-1, 32) net = tflearn.residual_block(net, 1, 64, downsample=True) net = tflearn.residual_block(net, n-1, 64) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 10, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') # Training model = tflearn.DNN(net, checkpoint_path='model_resnet_cifar10', max_checkpoints=10, tensorboard_verbose=0, clip_gradients=0.) model.fit(X, Y, n_epoch=200, validation_set=(testX, testY), snapshot_epoch=False, snapshot_step=500, show_metric=True, batch_size=128, shuffle=True, run_id='resnet_cifar10') ''' # 注意自解压目录,需要手动修改一下目录层次,cifar-10-batches-py==>cifar-10-batches-py/cifar-10-batches-py。 --------------------------------- Run id: resnet_cifar10 Log directory: /tmp/tflearn_logs/ --------------------------------- Preprocessing... Calculating mean over all dataset (this may take long)... Mean: [ 0.49139968 0.48215841 0.44653091] (To avoid repetitive computation, add it to argument 'mean' of `add_featurewise_zero_center`) --------------------------------- Training samples: 50000 Validation samples: 10000 -- Training Step: 1 | time: 3.993s | Momentum | epoch: 001 | loss: 0.00000 - acc: 0.0000 -- iter: 00128/50000 Training Step: 2 | total loss: 2.08295 | time: 7.453s | Momentum | epoch: 001 | loss: 2.08295 - acc: 0.0562 -- iter: 00256/50000 Training Step: 3 | total loss: 2.26373 | time: 10.725s ... Training Step: 121 | total loss: 1.72538 | time: 425.664s | Momentum | epoch: 001 | loss: 1.72538 - acc: 0.3421 -- iter: 15488/50000 Training Step: 122 | total loss: 1.72287 | time: 429.461s | Momentum | epoch: 001 | loss: 1.72287 - acc: 0.3415 -- iter: 15616/50000 Training Step: 123 | total loss: 1.71641 | time: 433.079s | Momentum | epoch: 001 | loss: 1.71641 - acc: 0.3448 -- iter: 15744/50000 ... '''
相关文章推荐
- tensorflow68 《深度学习原理与TensorFlow实战》04 CNN看懂世界 05neural_style.py
- tensorflow65 《深度学习原理与TensorFlow实战》04 CNN看懂世界 02 vgg_network.py
- tensorflow69 《深度学习原理与TensorFlow实战》04 CNN看懂世界 06 fast_neural_style.py
- tensorflow64 《深度学习原理与TensorFlow实战》04 CNN看懂世界 01 alexnet_benchmark.py
- tensorflow66 《深度学习原理与TensorFlow实战》04 CNN看懂世界 03 inception_v3 模型使用
- 看懂卷积神经网络(CNN)
- Myra女士的自行车世界-译文04-(自锁脚踏)
- 一个视频带你看懂区块链将如何改变世界!
- 计算机视觉:让冰冷的机器看懂这个多彩的世界
- 机器之眼,看懂世界
- 机器之眼,看懂世界——36Kr:计算机视觉行业研究报告
- c语言大世界—————04内外兼施之extern
- 十张图看懂未来的大数据世界
- 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-04-基于Python的LeNet之MLP
- Android好奇宝宝_番外篇_看脸的世界_04
- [04]tensorflow实现CNN
- 摄像机标定04_从单幅图像中提取世界坐标
- 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-04-基于Python的LeNet之MLP
- 看懂了这些图你就看懂了整个世界
- 计算机视觉:让冰冷的机器看懂多彩的世界