pcb defect detetcion application
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / libs / networks / slim_nets / inception_v4_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.inception_v4."""
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 inception
23
24
25 class InceptionTest(tf.test.TestCase):
26
27   def testBuildLogits(self):
28     batch_size = 5
29     height, width = 299, 299
30     num_classes = 1000
31     inputs = tf.random_uniform((batch_size, height, width, 3))
32     logits, end_points = inception.inception_v4(inputs, num_classes)
33     auxlogits = end_points['AuxLogits']
34     predictions = end_points['Predictions']
35     self.assertTrue(auxlogits.op.name.startswith('InceptionV4/AuxLogits'))
36     self.assertListEqual(auxlogits.get_shape().as_list(),
37                          [batch_size, num_classes])
38     self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
39     self.assertListEqual(logits.get_shape().as_list(),
40                          [batch_size, num_classes])
41     self.assertTrue(predictions.op.name.startswith(
42         'InceptionV4/Logits/Predictions'))
43     self.assertListEqual(predictions.get_shape().as_list(),
44                          [batch_size, num_classes])
45
46   def testBuildWithoutAuxLogits(self):
47     batch_size = 5
48     height, width = 299, 299
49     num_classes = 1000
50     inputs = tf.random_uniform((batch_size, height, width, 3))
51     logits, endpoints = inception.inception_v4(inputs, num_classes,
52                                                create_aux_logits=False)
53     self.assertFalse('AuxLogits' in endpoints)
54     self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
55     self.assertListEqual(logits.get_shape().as_list(),
56                          [batch_size, num_classes])
57
58   def testAllEndPointsShapes(self):
59     batch_size = 5
60     height, width = 299, 299
61     num_classes = 1000
62     inputs = tf.random_uniform((batch_size, height, width, 3))
63     _, end_points = inception.inception_v4(inputs, num_classes)
64     endpoints_shapes = {'Conv2d_1a_3x3': [batch_size, 149, 149, 32],
65                         'Conv2d_2a_3x3': [batch_size, 147, 147, 32],
66                         'Conv2d_2b_3x3': [batch_size, 147, 147, 64],
67                         'Mixed_3a': [batch_size, 73, 73, 160],
68                         'Mixed_4a': [batch_size, 71, 71, 192],
69                         'Mixed_5a': [batch_size, 35, 35, 384],
70                         # 4 x Inception-A blocks
71                         'Mixed_5b': [batch_size, 35, 35, 384],
72                         'Mixed_5c': [batch_size, 35, 35, 384],
73                         'Mixed_5d': [batch_size, 35, 35, 384],
74                         'Mixed_5e': [batch_size, 35, 35, 384],
75                         # Reduction-A block
76                         'Mixed_6a': [batch_size, 17, 17, 1024],
77                         # 7 x Inception-B blocks
78                         'Mixed_6b': [batch_size, 17, 17, 1024],
79                         'Mixed_6c': [batch_size, 17, 17, 1024],
80                         'Mixed_6d': [batch_size, 17, 17, 1024],
81                         'Mixed_6e': [batch_size, 17, 17, 1024],
82                         'Mixed_6f': [batch_size, 17, 17, 1024],
83                         'Mixed_6g': [batch_size, 17, 17, 1024],
84                         'Mixed_6h': [batch_size, 17, 17, 1024],
85                         # Reduction-A block
86                         'Mixed_7a': [batch_size, 8, 8, 1536],
87                         # 3 x Inception-C blocks
88                         'Mixed_7b': [batch_size, 8, 8, 1536],
89                         'Mixed_7c': [batch_size, 8, 8, 1536],
90                         'Mixed_7d': [batch_size, 8, 8, 1536],
91                         # Logits and predictions
92                         'AuxLogits': [batch_size, num_classes],
93                         'PreLogitsFlatten': [batch_size, 1536],
94                         'Logits': [batch_size, num_classes],
95                         'Predictions': [batch_size, num_classes]}
96     self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
97     for endpoint_name in endpoints_shapes:
98       expected_shape = endpoints_shapes[endpoint_name]
99       self.assertTrue(endpoint_name in end_points)
100       self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
101                            expected_shape)
102
103   def testBuildBaseNetwork(self):
104     batch_size = 5
105     height, width = 299, 299
106     inputs = tf.random_uniform((batch_size, height, width, 3))
107     net, end_points = inception.inception_v4_base(inputs)
108     self.assertTrue(net.op.name.startswith(
109         'InceptionV4/Mixed_7d'))
110     self.assertListEqual(net.get_shape().as_list(), [batch_size, 8, 8, 1536])
111     expected_endpoints = [
112         'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
113         'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
114         'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
115         'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
116         'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
117     self.assertItemsEqual(end_points.keys(), expected_endpoints)
118     for name, op in end_points.iteritems():
119       self.assertTrue(op.name.startswith('InceptionV4/' + name))
120
121   def testBuildOnlyUpToFinalEndpoint(self):
122     batch_size = 5
123     height, width = 299, 299
124     all_endpoints = [
125         'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
126         'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
127         'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
128         'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
129         'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
130     for index, endpoint in enumerate(all_endpoints):
131       with tf.Graph().as_default():
132         inputs = tf.random_uniform((batch_size, height, width, 3))
133         out_tensor, end_points = inception.inception_v4_base(
134             inputs, final_endpoint=endpoint)
135         self.assertTrue(out_tensor.op.name.startswith(
136             'InceptionV4/' + endpoint))
137         self.assertItemsEqual(all_endpoints[:index+1], end_points)
138
139   def testVariablesSetDevice(self):
140     batch_size = 5
141     height, width = 299, 299
142     num_classes = 1000
143     inputs = tf.random_uniform((batch_size, height, width, 3))
144     # Force all Variables to reside on the device.
145     with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
146       inception.inception_v4(inputs, num_classes)
147     with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
148       inception.inception_v4(inputs, num_classes)
149     for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
150       self.assertDeviceEqual(v.device, '/cpu:0')
151     for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
152       self.assertDeviceEqual(v.device, '/gpu:0')
153
154   def testHalfSizeImages(self):
155     batch_size = 5
156     height, width = 150, 150
157     num_classes = 1000
158     inputs = tf.random_uniform((batch_size, height, width, 3))
159     logits, end_points = inception.inception_v4(inputs, num_classes)
160     self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
161     self.assertListEqual(logits.get_shape().as_list(),
162                          [batch_size, num_classes])
163     pre_pool = end_points['Mixed_7d']
164     self.assertListEqual(pre_pool.get_shape().as_list(),
165                          [batch_size, 3, 3, 1536])
166
167   def testUnknownBatchSize(self):
168     batch_size = 1
169     height, width = 299, 299
170     num_classes = 1000
171     with self.test_session() as sess:
172       inputs = tf.placeholder(tf.float32, (None, height, width, 3))
173       logits, _ = inception.inception_v4(inputs, num_classes)
174       self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
175       self.assertListEqual(logits.get_shape().as_list(),
176                            [None, num_classes])
177       images = tf.random_uniform((batch_size, height, width, 3))
178       sess.run(tf.global_variables_initializer())
179       output = sess.run(logits, {inputs: images.eval()})
180       self.assertEquals(output.shape, (batch_size, num_classes))
181
182   def testEvaluation(self):
183     batch_size = 2
184     height, width = 299, 299
185     num_classes = 1000
186     with self.test_session() as sess:
187       eval_inputs = tf.random_uniform((batch_size, height, width, 3))
188       logits, _ = inception.inception_v4(eval_inputs,
189                                          num_classes,
190                                          is_training=False)
191       predictions = tf.argmax(logits, 1)
192       sess.run(tf.global_variables_initializer())
193       output = sess.run(predictions)
194       self.assertEquals(output.shape, (batch_size,))
195
196   def testTrainEvalWithReuse(self):
197     train_batch_size = 5
198     eval_batch_size = 2
199     height, width = 150, 150
200     num_classes = 1000
201     with self.test_session() as sess:
202       train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
203       inception.inception_v4(train_inputs, num_classes)
204       eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
205       logits, _ = inception.inception_v4(eval_inputs,
206                                          num_classes,
207                                          is_training=False,
208                                          reuse=True)
209       predictions = tf.argmax(logits, 1)
210       sess.run(tf.global_variables_initializer())
211       output = sess.run(predictions)
212       self.assertEquals(output.shape, (eval_batch_size,))
213
214
215 if __name__ == '__main__':
216   tf.test.main()