1 # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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 """Tests for slim.slim_nets.overfeat."""
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
20 import tensorflow as tf
22 from nets import overfeat
24 slim = tf.contrib.slim
27 class OverFeatTest(tf.test.TestCase):
31 height, width = 231, 231
33 with self.test_session():
34 inputs = tf.random_uniform((batch_size, height, width, 3))
35 logits, _ = overfeat.overfeat(inputs, num_classes)
36 self.assertEquals(logits.op.name, 'overfeat/fc8/squeezed')
37 self.assertListEqual(logits.get_shape().as_list(),
38 [batch_size, num_classes])
40 def testFullyConvolutional(self):
42 height, width = 281, 281
44 with self.test_session():
45 inputs = tf.random_uniform((batch_size, height, width, 3))
46 logits, _ = overfeat.overfeat(inputs, num_classes, spatial_squeeze=False)
47 self.assertEquals(logits.op.name, 'overfeat/fc8/BiasAdd')
48 self.assertListEqual(logits.get_shape().as_list(),
49 [batch_size, 2, 2, num_classes])
51 def testEndPoints(self):
53 height, width = 231, 231
55 with self.test_session():
56 inputs = tf.random_uniform((batch_size, height, width, 3))
57 _, end_points = overfeat.overfeat(inputs, num_classes)
58 expected_names = ['overfeat/conv1',
70 self.assertSetEqual(set(end_points.keys()), set(expected_names))
72 def testModelVariables(self):
74 height, width = 231, 231
76 with self.test_session():
77 inputs = tf.random_uniform((batch_size, height, width, 3))
78 overfeat.overfeat(inputs, num_classes)
79 expected_names = ['overfeat/conv1/weights',
80 'overfeat/conv1/biases',
81 'overfeat/conv2/weights',
82 'overfeat/conv2/biases',
83 'overfeat/conv3/weights',
84 'overfeat/conv3/biases',
85 'overfeat/conv4/weights',
86 'overfeat/conv4/biases',
87 'overfeat/conv5/weights',
88 'overfeat/conv5/biases',
89 'overfeat/fc6/weights',
90 'overfeat/fc6/biases',
91 'overfeat/fc7/weights',
92 'overfeat/fc7/biases',
93 'overfeat/fc8/weights',
94 'overfeat/fc8/biases',
96 model_variables = [v.op.name for v in slim.get_model_variables()]
97 self.assertSetEqual(set(model_variables), set(expected_names))
99 def testEvaluation(self):
101 height, width = 231, 231
103 with self.test_session():
104 eval_inputs = tf.random_uniform((batch_size, height, width, 3))
105 logits, _ = overfeat.overfeat(eval_inputs, is_training=False)
106 self.assertListEqual(logits.get_shape().as_list(),
107 [batch_size, num_classes])
108 predictions = tf.argmax(logits, 1)
109 self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
111 def testTrainEvalWithReuse(self):
114 train_height, train_width = 231, 231
115 eval_height, eval_width = 281, 281
117 with self.test_session():
118 train_inputs = tf.random_uniform(
119 (train_batch_size, train_height, train_width, 3))
120 logits, _ = overfeat.overfeat(train_inputs)
121 self.assertListEqual(logits.get_shape().as_list(),
122 [train_batch_size, num_classes])
123 tf.get_variable_scope().reuse_variables()
124 eval_inputs = tf.random_uniform(
125 (eval_batch_size, eval_height, eval_width, 3))
126 logits, _ = overfeat.overfeat(eval_inputs, is_training=False,
127 spatial_squeeze=False)
128 self.assertListEqual(logits.get_shape().as_list(),
129 [eval_batch_size, 2, 2, num_classes])
130 logits = tf.reduce_mean(logits, [1, 2])
131 predictions = tf.argmax(logits, 1)
132 self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
134 def testForward(self):
136 height, width = 231, 231
137 with self.test_session() as sess:
138 inputs = tf.random_uniform((batch_size, height, width, 3))
139 logits, _ = overfeat.overfeat(inputs)
140 sess.run(tf.global_variables_initializer())
141 output = sess.run(logits)
142 self.assertTrue(output.any())
144 if __name__ == '__main__':