overfeat.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Contains the model definition for the OverFeat network.
  16. The definition for the network was obtained from:
  17. OverFeat: Integrated Recognition, Localization and Detection using
  18. Convolutional Networks
  19. Pierre Sermanet, David Eigen, Xiang Zhang, Michael Mathieu, Rob Fergus and
  20. Yann LeCun, 2014
  21. http://arxiv.org/abs/1312.6229
  22. Usage:
  23. with slim.arg_scope(overfeat.overfeat_arg_scope()):
  24. outputs, end_points = overfeat.overfeat(inputs)
  25. @@overfeat
  26. """
  27. from __future__ import absolute_import
  28. from __future__ import division
  29. from __future__ import print_function
  30. import tensorflow as tf
  31. slim = tf.contrib.slim
  32. trunc_normal = lambda stddev: tf.truncated_normal_initializer(0.0, stddev)
  33. def overfeat_arg_scope(weight_decay=0.0005):
  34. with slim.arg_scope([slim.conv2d, slim.fully_connected],
  35. activation_fn=tf.nn.relu,
  36. weights_regularizer=slim.l2_regularizer(weight_decay),
  37. biases_initializer=tf.zeros_initializer()):
  38. with slim.arg_scope([slim.conv2d], padding='SAME'):
  39. with slim.arg_scope([slim.max_pool2d], padding='VALID') as arg_sc:
  40. return arg_sc
  41. def overfeat(inputs,
  42. num_classes=1000,
  43. is_training=True,
  44. dropout_keep_prob=0.5,
  45. spatial_squeeze=True,
  46. scope='overfeat'):
  47. """Contains the model definition for the OverFeat network.
  48. The definition for the network was obtained from:
  49. OverFeat: Integrated Recognition, Localization and Detection using
  50. Convolutional Networks
  51. Pierre Sermanet, David Eigen, Xiang Zhang, Michael Mathieu, Rob Fergus and
  52. Yann LeCun, 2014
  53. http://arxiv.org/abs/1312.6229
  54. Note: All the fully_connected layers have been transformed to conv2d layers.
  55. To use in classification mode, resize input to 231x231. To use in fully
  56. convolutional mode, set spatial_squeeze to false.
  57. Args:
  58. inputs: a tensor of size [batch_size, height, width, channels].
  59. num_classes: number of predicted classes.
  60. is_training: whether or not the model is being trained.
  61. dropout_keep_prob: the probability that activations are kept in the dropout
  62. layers during training.
  63. spatial_squeeze: whether or not should squeeze the spatial dimensions of the
  64. outputs. Useful to remove unnecessary dimensions for classification.
  65. scope: Optional scope for the variables.
  66. Returns:
  67. the last op containing the log predictions and end_points dict.
  68. """
  69. with tf.variable_scope(scope, 'overfeat', [inputs]) as sc:
  70. end_points_collection = sc.name + '_end_points'
  71. # Collect outputs for conv2d, fully_connected and max_pool2d
  72. with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d],
  73. outputs_collections=end_points_collection):
  74. net = slim.conv2d(inputs, 64, [11, 11], 4, padding='VALID',
  75. scope='conv1')
  76. net = slim.max_pool2d(net, [2, 2], scope='pool1')
  77. net = slim.conv2d(net, 256, [5, 5], padding='VALID', scope='conv2')
  78. net = slim.max_pool2d(net, [2, 2], scope='pool2')
  79. net = slim.conv2d(net, 512, [3, 3], scope='conv3')
  80. net = slim.conv2d(net, 1024, [3, 3], scope='conv4')
  81. net = slim.conv2d(net, 1024, [3, 3], scope='conv5')
  82. net = slim.max_pool2d(net, [2, 2], scope='pool5')
  83. with slim.arg_scope([slim.conv2d],
  84. weights_initializer=trunc_normal(0.005),
  85. biases_initializer=tf.constant_initializer(0.1)):
  86. # Use conv2d instead of fully_connected layers.
  87. net = slim.conv2d(net, 3072, [6, 6], padding='VALID', scope='fc6')
  88. net = slim.dropout(net, dropout_keep_prob, is_training=is_training,
  89. scope='dropout6')
  90. net = slim.conv2d(net, 4096, [1, 1], scope='fc7')
  91. net = slim.dropout(net, dropout_keep_prob, is_training=is_training,
  92. scope='dropout7')
  93. net = slim.conv2d(net, num_classes, [1, 1],
  94. activation_fn=None,
  95. normalizer_fn=None,
  96. biases_initializer=tf.zeros_initializer(),
  97. scope='fc8')
  98. # Convert end_points_collection into a end_point dict.
  99. end_points = slim.utils.convert_collection_to_dict(end_points_collection)
  100. if spatial_squeeze:
  101. net = tf.squeeze(net, [1, 2], name='fc8/squeezed')
  102. end_points[sc.name + '/fc8'] = net
  103. return net, end_points
  104. overfeat.default_image_size = 231