aihot  2017-12-09 23:01:43  机器学习 |   查看评论   

Deep Residual Network tflearn实现

 

      tflearn官方有一个cifar10的实现, 代码如下:

  1.  from __future__ import division, print_function, absolute_import

  2.  import tflearn

  3.  # Residual blocks
  4.  # 32 layers: n=5, 56 layers: n=9, 110 layers: n=18
  5.  n = 5

  6.  # Data loading
  7.  from tflearn.datasets import cifar10
  8.  (X, Y), (testX, testY) = cifar10.load_data()
  9.  Y = tflearn.data_utils.to_categorical(Y, 10)
  10.  testY = tflearn.data_utils.to_categorical(testY, 10)

  11.  # Real-time data preprocessing
  12.  img_prep = tflearn.ImagePreprocessing()
  13.  img_prep.add_featurewise_zero_center(per_channel=True)

  14.  # Real-time data augmentation
  15.  img_aug = tflearn.ImageAugmentation()
  16.  img_aug.add_random_flip_leftright()
  17.  img_aug.add_random_crop([32, 32], padding=4)

  18.  # Building Residual Network
  19.  net = tflearn.input_data(shape=[None, 32, 32, 3],
  20.                           data_preprocessing=img_prep,
  21.                           data_augmentation=img_aug)
  22.  net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)
  23.  net = tflearn.residual_block(net, n, 16)
  24.  net = tflearn.residual_block(net, 1, 32, downsample=True)
  25.  net = tflearn.residual_block(net, n-1, 32)
  26.  net = tflearn.residual_block(net, 1, 64, downsample=True)
  27.  net = tflearn.residual_block(net, n-1, 64)
  28.  net = tflearn.batch_normalization(net)
  29.  net = tflearn.activation(net, 'relu')
  30.  net = tflearn.global_avg_pool(net)
  31.  # Regression
  32.  net = tflearn.fully_connected(net, 10, activation='softmax')
  33.  mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
  34.  net = tflearn.regression(net, optimizer=mom,
  35.                           loss='categorical_crossentropy')
  36.  # Training
  37.  model = tflearn.DNN(net, checkpoint_path='model_resnet_cifar10',
  38.                      max_checkpoints=10, tensorboard_verbose=0,
  39.                      clip_gradients=0.)

  40.  model.fit(X, Y, n_epoch=200, validation_set=(testX, testY),
  41.            snapshot_epoch=False, snapshot_step=500,
  42.            show_metric=True, batch_size=128, shuffle=True,
  43.            run_id='resnet_cifar10')

       其中,residual_block实现了shortcut,代码写的十分棒:

  1.  def residual_block(incoming, nb_blocks, out_channels, downsample=False,
  2.                     downsample_strides=2, activation='relu', batch_norm=True,
  3.                     bias=True, weights_init='variance_scaling',
  4.                     bias_init='zeros', regularizer='L2', weight_decay=0.0001,
  5.                     trainable=True, restore=True, reuse=False, scope=None,
  6.                     name="ResidualBlock"):
  7.      """ Residual Block.

  8.      A residual block as described in MSRA's Deep Residual Network paper.
  9.      Full pre-activation architecture is used here.

  10.      Input:
  11.          4-D Tensor [batch, height, width, in_channels].

  12.      Output:
  13.          4-D Tensor [batch, new height, new width, nb_filter].

  14.      Arguments:
  15.          incoming: `Tensor`. Incoming 4-D Layer.
  16.          nb_blocks: `int`. Number of layer blocks.
  17.          out_channels: `int`. The number of convolutional filters of the
  18.              convolution layers.
  19.          downsample: `bool`. If True, apply downsampling using
  20.              'downsample_strides' for strides.
  21.          downsample_strides: `int`. The strides to use when downsampling.
  22.          activation: `str` (name) or `function` (returning a `Tensor`).
  23.              Activation applied to this layer (see tflearn.activations).
  24.              Default: 'linear'.
  25.          batch_norm: `bool`. If True, apply batch normalization.
  26.          bias: `bool`. If True, a bias is used.
  27.          weights_init: `str` (name) or `Tensor`. Weights initialization.
  28.              (see tflearn.initializations) Default: 'uniform_scaling'.
  29.          bias_init: `str` (name) or `tf.Tensor`. Bias initialization.
  30.              (see tflearn.initializations) Default: 'zeros'.
  31.          regularizer: `str` (name) or `Tensor`. Add a regularizer to this
  32.              layer weights (see tflearn.regularizers). Default: None.
  33.          weight_decay: `float`. Regularizer decay parameter. Default: 0.001.
  34.          trainable: `bool`. If True, weights will be trainable.
  35.          restore: `bool`. If True, this layer weights will be restored when
  36.              loading a model.
  37.          reuse: `bool`. If True and 'scope' is provided, this layer variables
  38.              will be reused (shared).
  39.          scope: `str`. Define this layer scope (optional). A scope can be
  40.              used to share variables between layers. Note that scope will
  41.              override name.
  42.          name: A name for this layer (optional). Default: 'ShallowBottleneck'.

  43.      References:
  44.          - Deep Residual Learning for Image Recognition. Kaiming He, Xiangyu
  45.              Zhang, Shaoqing Ren, Jian Sun. 2015.
  46.          - Identity Mappings in Deep Residual Networks. Kaiming He, Xiangyu
  47.              Zhang, Shaoqing Ren, Jian Sun. 2015.

  48.      Links:
  49.          - [http://arxiv.org/pdf/1512.03385v1.pdf]
  50.              ([http://arxiv.org/pdf/1512.03385v1.pdf](http://arxiv.org/pdf/1512.03385v1.pdf))
  51.          - [Identity Mappings in Deep Residual Networks]
  52.              ([http://arxiv.org/pdf/1603.05027v2.pdf](http://arxiv.org/pdf/1603.05027v2.pdf))

  53.      """
  54.      resnet = incoming
  55.      in_channels = incoming.get_shape().as_list()[-1]

  56.      with tf.variable_op_scope([incoming], scope, name, reuse=reuse) as scope:
  57.          name = scope.name #TODO

  58.          for i in range(nb_blocks):

  59.              identity = resnet

  60.              if not downsample:
  61.                  downsample_strides = 1

  62.              if batch_norm:
  63.                  resnet = tflearn.batch_normalization(resnet)
  64.              resnet = tflearn.activation(resnet, activation)

  65.              resnet = conv_2d(resnet, out_channels, 3,
  66.                               downsample_strides, 'same', 'linear',
  67.                               bias, weights_init, bias_init,
  68.                               regularizer, weight_decay, trainable,
  69.                               restore)

  70.              if batch_norm:
  71.                  resnet = tflearn.batch_normalization(resnet)
  72.              resnet = tflearn.activation(resnet, activation)

  73.              resnet = conv_2d(resnet, out_channels, 3, 1, 'same',
  74.                               'linear', bias, weights_init,
  75.                               bias_init, regularizer, weight_decay,
  76.                               trainable, restore)

  77.              # Downsampling
  78.              if downsample_strides > 1:
  79.                  identity = tflearn.avg_pool_2d(identity, 1,
  80.                                                 downsample_strides)

  81.              # Projection to new dimension
  82.              if in_channels != out_channels:
  83.                  ch = (out_channels - in_channels)//2
  84.                  identity = tf.pad(identity,
  85.                                    [[0, 0], [0, 0], [0, 0], [ch, ch]])
  86.                  in_channels = out_channels

  87.              resnet = resnet + identity

  88.      return resnet

 

 

除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自机器学习进阶笔记之五 | 深入理解VGG\Residual Network

留言与评论(共有 0 条评论)
   
验证码:
[lianlun]1[/lianlun]