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