446f9ac4d8446ff0eeb5ed9a8d21fa0eb7fabf0b
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / libs / networks / slim_nets / overfeat_test.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 """Tests for slim.slim_nets.overfeat."""
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19
20 import tensorflow as tf
21
22 from nets import overfeat
23
24 slim = tf.contrib.slim
25
26
27 class OverFeatTest(tf.test.TestCase):
28
29   def testBuild(self):
30     batch_size = 5
31     height, width = 231, 231
32     num_classes = 1000
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])
39
40   def testFullyConvolutional(self):
41     batch_size = 1
42     height, width = 281, 281
43     num_classes = 1000
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])
50
51   def testEndPoints(self):
52     batch_size = 5
53     height, width = 231, 231
54     num_classes = 1000
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',
59                         'overfeat/pool1',
60                         'overfeat/conv2',
61                         'overfeat/pool2',
62                         'overfeat/conv3',
63                         'overfeat/conv4',
64                         'overfeat/conv5',
65                         'overfeat/pool5',
66                         'overfeat/fc6',
67                         'overfeat/fc7',
68                         'overfeat/fc8'
69                        ]
70       self.assertSetEqual(set(end_points.keys()), set(expected_names))
71
72   def testModelVariables(self):
73     batch_size = 5
74     height, width = 231, 231
75     num_classes = 1000
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',
95                        ]
96       model_variables = [v.op.name for v in slim.get_model_variables()]
97       self.assertSetEqual(set(model_variables), set(expected_names))
98
99   def testEvaluation(self):
100     batch_size = 2
101     height, width = 231, 231
102     num_classes = 1000
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])
110
111   def testTrainEvalWithReuse(self):
112     train_batch_size = 2
113     eval_batch_size = 1
114     train_height, train_width = 231, 231
115     eval_height, eval_width = 281, 281
116     num_classes = 1000
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])
133
134   def testForward(self):
135     batch_size = 1
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())
143
144 if __name__ == '__main__':
145   tf.test.main()