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
17The definition for the network was obtained from:
18  OverFeat: Integrated Recognition, Localization and Detection using
19  Convolutional Networks
20  Pierre Sermanet, David Eigen, Xiang Zhang, Michael Mathieu, Rob Fergus and
21  Yann LeCun, 2014
22  http://arxiv.org/abs/1312.6229
23
24Usage:
25  with slim.arg_scope(overfeat.overfeat_arg_scope()):
26    outputs, end_points = overfeat.overfeat(inputs)
27
28@@overfeat
29"""
30
31from __future__ import absolute_import
32from __future__ import division
33from __future__ import print_function
34
35from tensorflow.contrib import layers
36from tensorflow.contrib.framework.python.ops import arg_scope
37from tensorflow.contrib.layers.python.layers import layers as layers_lib
38from tensorflow.contrib.layers.python.layers import regularizers
39from tensorflow.contrib.layers.python.layers import utils
40from tensorflow.python.ops import array_ops
41from tensorflow.python.ops import init_ops
42from tensorflow.python.ops import nn_ops
43from tensorflow.python.ops import variable_scope
44
45trunc_normal = lambda stddev: init_ops.truncated_normal_initializer(0.0, stddev)
46
47
48def overfeat_arg_scope(weight_decay=0.0005):
49  with arg_scope(
50      [layers.conv2d, layers_lib.fully_connected],
51      activation_fn=nn_ops.relu,
52      weights_regularizer=regularizers.l2_regularizer(weight_decay),
53      biases_initializer=init_ops.zeros_initializer()):
54    with arg_scope([layers.conv2d], padding='SAME'):
55      with arg_scope([layers_lib.max_pool2d], padding='VALID') as arg_sc:
56        return arg_sc
57
58
59def overfeat(inputs,
60             num_classes=1000,
61             is_training=True,
62             dropout_keep_prob=0.5,
63             spatial_squeeze=True,
64             scope='overfeat'):
65  """Contains the model definition for the OverFeat network.
66
67  The definition for the network was obtained from:
68    OverFeat: Integrated Recognition, Localization and Detection using
69    Convolutional Networks
70    Pierre Sermanet, David Eigen, Xiang Zhang, Michael Mathieu, Rob Fergus and
71    Yann LeCun, 2014
72    http://arxiv.org/abs/1312.6229
73
74  Note: All the fully_connected layers have been transformed to conv2d layers.
75        To use in classification mode, resize input to 231x231. To use in fully
76        convolutional mode, set spatial_squeeze to false.
77
78  Args:
79    inputs: a tensor of size [batch_size, height, width, channels].
80    num_classes: number of predicted classes.
81    is_training: whether or not the model is being trained.
82    dropout_keep_prob: the probability that activations are kept in the dropout
83      layers during training.
84    spatial_squeeze: whether or not should squeeze the spatial dimensions of the
85      outputs. Useful to remove unnecessary dimensions for classification.
86    scope: Optional scope for the variables.
87
88  Returns:
89    the last op containing the log predictions and end_points dict.
90
91  """
92  with variable_scope.variable_scope(scope, 'overfeat', [inputs]) as sc:
93    end_points_collection = sc.name + '_end_points'
94    # Collect outputs for conv2d, fully_connected and max_pool2d
95    with arg_scope(
96        [layers.conv2d, layers_lib.fully_connected, layers_lib.max_pool2d],
97        outputs_collections=end_points_collection):
98      net = layers.conv2d(
99          inputs, 64, [11, 11], 4, padding='VALID', scope='conv1')
100      net = layers_lib.max_pool2d(net, [2, 2], scope='pool1')
101      net = layers.conv2d(net, 256, [5, 5], padding='VALID', scope='conv2')
102      net = layers_lib.max_pool2d(net, [2, 2], scope='pool2')
103      net = layers.conv2d(net, 512, [3, 3], scope='conv3')
104      net = layers.conv2d(net, 1024, [3, 3], scope='conv4')
105      net = layers.conv2d(net, 1024, [3, 3], scope='conv5')
106      net = layers_lib.max_pool2d(net, [2, 2], scope='pool5')
107      with arg_scope(
108          [layers.conv2d],
109          weights_initializer=trunc_normal(0.005),
110          biases_initializer=init_ops.constant_initializer(0.1)):
111        # Use conv2d instead of fully_connected layers.
112        net = layers.conv2d(net, 3072, [6, 6], padding='VALID', scope='fc6')
113        net = layers_lib.dropout(
114            net, dropout_keep_prob, is_training=is_training, scope='dropout6')
115        net = layers.conv2d(net, 4096, [1, 1], scope='fc7')
116        net = layers_lib.dropout(
117            net, dropout_keep_prob, is_training=is_training, scope='dropout7')
118        net = layers.conv2d(
119            net,
120            num_classes, [1, 1],
121            activation_fn=None,
122            normalizer_fn=None,
123            biases_initializer=init_ops.zeros_initializer(),
124            scope='fc8')
125      # Convert end_points_collection into a end_point dict.
126      end_points = utils.convert_collection_to_dict(end_points_collection)
127      if spatial_squeeze:
128        net = array_ops.squeeze(net, [1, 2], name='fc8/squeezed')
129        end_points[sc.name + '/fc8'] = net
130      return net, end_points
131