8e383b379c1d8acfdeb322430d7c07737fd292df
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / libs / networks / slim_nets / vgg_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.vgg."""
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 vgg
23
24 slim = tf.contrib.slim
25
26
27 class VGGATest(tf.test.TestCase):
28
29   def testBuild(self):
30     batch_size = 5
31     height, width = 224, 224
32     num_classes = 1000
33     with self.test_session():
34       inputs = tf.random_uniform((batch_size, height, width, 3))
35       logits, _ = vgg.vgg_a(inputs, num_classes)
36       self.assertEquals(logits.op.name, 'vgg_a/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 = 256, 256
43     num_classes = 1000
44     with self.test_session():
45       inputs = tf.random_uniform((batch_size, height, width, 3))
46       logits, _ = vgg.vgg_a(inputs, num_classes, spatial_squeeze=False)
47       self.assertEquals(logits.op.name, 'vgg_a/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 = 224, 224
54     num_classes = 1000
55     with self.test_session():
56       inputs = tf.random_uniform((batch_size, height, width, 3))
57       _, end_points = vgg.vgg_a(inputs, num_classes)
58       expected_names = ['vgg_a/conv1/conv1_1',
59                         'vgg_a/pool1',
60                         'vgg_a/conv2/conv2_1',
61                         'vgg_a/pool2',
62                         'vgg_a/conv3/conv3_1',
63                         'vgg_a/conv3/conv3_2',
64                         'vgg_a/pool3',
65                         'vgg_a/conv4/conv4_1',
66                         'vgg_a/conv4/conv4_2',
67                         'vgg_a/pool4',
68                         'vgg_a/conv5/conv5_1',
69                         'vgg_a/conv5/conv5_2',
70                         'vgg_a/pool5',
71                         'vgg_a/fc6',
72                         'vgg_a/fc7',
73                         'vgg_a/fc8'
74                        ]
75       self.assertSetEqual(set(end_points.keys()), set(expected_names))
76
77   def testModelVariables(self):
78     batch_size = 5
79     height, width = 224, 224
80     num_classes = 1000
81     with self.test_session():
82       inputs = tf.random_uniform((batch_size, height, width, 3))
83       vgg.vgg_a(inputs, num_classes)
84       expected_names = ['vgg_a/conv1/conv1_1/weights',
85                         'vgg_a/conv1/conv1_1/biases',
86                         'vgg_a/conv2/conv2_1/weights',
87                         'vgg_a/conv2/conv2_1/biases',
88                         'vgg_a/conv3/conv3_1/weights',
89                         'vgg_a/conv3/conv3_1/biases',
90                         'vgg_a/conv3/conv3_2/weights',
91                         'vgg_a/conv3/conv3_2/biases',
92                         'vgg_a/conv4/conv4_1/weights',
93                         'vgg_a/conv4/conv4_1/biases',
94                         'vgg_a/conv4/conv4_2/weights',
95                         'vgg_a/conv4/conv4_2/biases',
96                         'vgg_a/conv5/conv5_1/weights',
97                         'vgg_a/conv5/conv5_1/biases',
98                         'vgg_a/conv5/conv5_2/weights',
99                         'vgg_a/conv5/conv5_2/biases',
100                         'vgg_a/fc6/weights',
101                         'vgg_a/fc6/biases',
102                         'vgg_a/fc7/weights',
103                         'vgg_a/fc7/biases',
104                         'vgg_a/fc8/weights',
105                         'vgg_a/fc8/biases',
106                        ]
107       model_variables = [v.op.name for v in slim.get_model_variables()]
108       self.assertSetEqual(set(model_variables), set(expected_names))
109
110   def testEvaluation(self):
111     batch_size = 2
112     height, width = 224, 224
113     num_classes = 1000
114     with self.test_session():
115       eval_inputs = tf.random_uniform((batch_size, height, width, 3))
116       logits, _ = vgg.vgg_a(eval_inputs, is_training=False)
117       self.assertListEqual(logits.get_shape().as_list(),
118                            [batch_size, num_classes])
119       predictions = tf.argmax(logits, 1)
120       self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
121
122   def testTrainEvalWithReuse(self):
123     train_batch_size = 2
124     eval_batch_size = 1
125     train_height, train_width = 224, 224
126     eval_height, eval_width = 256, 256
127     num_classes = 1000
128     with self.test_session():
129       train_inputs = tf.random_uniform(
130           (train_batch_size, train_height, train_width, 3))
131       logits, _ = vgg.vgg_a(train_inputs)
132       self.assertListEqual(logits.get_shape().as_list(),
133                            [train_batch_size, num_classes])
134       tf.get_variable_scope().reuse_variables()
135       eval_inputs = tf.random_uniform(
136           (eval_batch_size, eval_height, eval_width, 3))
137       logits, _ = vgg.vgg_a(eval_inputs, is_training=False,
138                             spatial_squeeze=False)
139       self.assertListEqual(logits.get_shape().as_list(),
140                            [eval_batch_size, 2, 2, num_classes])
141       logits = tf.reduce_mean(logits, [1, 2])
142       predictions = tf.argmax(logits, 1)
143       self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
144
145   def testForward(self):
146     batch_size = 1
147     height, width = 224, 224
148     with self.test_session() as sess:
149       inputs = tf.random_uniform((batch_size, height, width, 3))
150       logits, _ = vgg.vgg_a(inputs)
151       sess.run(tf.global_variables_initializer())
152       output = sess.run(logits)
153       self.assertTrue(output.any())
154
155
156 class VGG16Test(tf.test.TestCase):
157
158   def testBuild(self):
159     batch_size = 5
160     height, width = 224, 224
161     num_classes = 1000
162     with self.test_session():
163       inputs = tf.random_uniform((batch_size, height, width, 3))
164       logits, _ = vgg.vgg_16(inputs, num_classes)
165       self.assertEquals(logits.op.name, 'vgg_16/fc8/squeezed')
166       self.assertListEqual(logits.get_shape().as_list(),
167                            [batch_size, num_classes])
168
169   def testFullyConvolutional(self):
170     batch_size = 1
171     height, width = 256, 256
172     num_classes = 1000
173     with self.test_session():
174       inputs = tf.random_uniform((batch_size, height, width, 3))
175       logits, _ = vgg.vgg_16(inputs, num_classes, spatial_squeeze=False)
176       self.assertEquals(logits.op.name, 'vgg_16/fc8/BiasAdd')
177       self.assertListEqual(logits.get_shape().as_list(),
178                            [batch_size, 2, 2, num_classes])
179
180   def testEndPoints(self):
181     batch_size = 5
182     height, width = 224, 224
183     num_classes = 1000
184     with self.test_session():
185       inputs = tf.random_uniform((batch_size, height, width, 3))
186       _, end_points = vgg.vgg_16(inputs, num_classes)
187       expected_names = ['vgg_16/conv1/conv1_1',
188                         'vgg_16/conv1/conv1_2',
189                         'vgg_16/pool1',
190                         'vgg_16/conv2/conv2_1',
191                         'vgg_16/conv2/conv2_2',
192                         'vgg_16/pool2',
193                         'vgg_16/conv3/conv3_1',
194                         'vgg_16/conv3/conv3_2',
195                         'vgg_16/conv3/conv3_3',
196                         'vgg_16/pool3',
197                         'vgg_16/conv4/conv4_1',
198                         'vgg_16/conv4/conv4_2',
199                         'vgg_16/conv4/conv4_3',
200                         'vgg_16/pool4',
201                         'vgg_16/conv5/conv5_1',
202                         'vgg_16/conv5/conv5_2',
203                         'vgg_16/conv5/conv5_3',
204                         'vgg_16/pool5',
205                         'vgg_16/fc6',
206                         'vgg_16/fc7',
207                         'vgg_16/fc8'
208                        ]
209       self.assertSetEqual(set(end_points.keys()), set(expected_names))
210
211   def testModelVariables(self):
212     batch_size = 5
213     height, width = 224, 224
214     num_classes = 1000
215     with self.test_session():
216       inputs = tf.random_uniform((batch_size, height, width, 3))
217       vgg.vgg_16(inputs, num_classes)
218       expected_names = ['vgg_16/conv1/conv1_1/weights',
219                         'vgg_16/conv1/conv1_1/biases',
220                         'vgg_16/conv1/conv1_2/weights',
221                         'vgg_16/conv1/conv1_2/biases',
222                         'vgg_16/conv2/conv2_1/weights',
223                         'vgg_16/conv2/conv2_1/biases',
224                         'vgg_16/conv2/conv2_2/weights',
225                         'vgg_16/conv2/conv2_2/biases',
226                         'vgg_16/conv3/conv3_1/weights',
227                         'vgg_16/conv3/conv3_1/biases',
228                         'vgg_16/conv3/conv3_2/weights',
229                         'vgg_16/conv3/conv3_2/biases',
230                         'vgg_16/conv3/conv3_3/weights',
231                         'vgg_16/conv3/conv3_3/biases',
232                         'vgg_16/conv4/conv4_1/weights',
233                         'vgg_16/conv4/conv4_1/biases',
234                         'vgg_16/conv4/conv4_2/weights',
235                         'vgg_16/conv4/conv4_2/biases',
236                         'vgg_16/conv4/conv4_3/weights',
237                         'vgg_16/conv4/conv4_3/biases',
238                         'vgg_16/conv5/conv5_1/weights',
239                         'vgg_16/conv5/conv5_1/biases',
240                         'vgg_16/conv5/conv5_2/weights',
241                         'vgg_16/conv5/conv5_2/biases',
242                         'vgg_16/conv5/conv5_3/weights',
243                         'vgg_16/conv5/conv5_3/biases',
244                         'vgg_16/fc6/weights',
245                         'vgg_16/fc6/biases',
246                         'vgg_16/fc7/weights',
247                         'vgg_16/fc7/biases',
248                         'vgg_16/fc8/weights',
249                         'vgg_16/fc8/biases',
250                        ]
251       model_variables = [v.op.name for v in slim.get_model_variables()]
252       self.assertSetEqual(set(model_variables), set(expected_names))
253
254   def testEvaluation(self):
255     batch_size = 2
256     height, width = 224, 224
257     num_classes = 1000
258     with self.test_session():
259       eval_inputs = tf.random_uniform((batch_size, height, width, 3))
260       logits, _ = vgg.vgg_16(eval_inputs, is_training=False)
261       self.assertListEqual(logits.get_shape().as_list(),
262                            [batch_size, num_classes])
263       predictions = tf.argmax(logits, 1)
264       self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
265
266   def testTrainEvalWithReuse(self):
267     train_batch_size = 2
268     eval_batch_size = 1
269     train_height, train_width = 224, 224
270     eval_height, eval_width = 256, 256
271     num_classes = 1000
272     with self.test_session():
273       train_inputs = tf.random_uniform(
274           (train_batch_size, train_height, train_width, 3))
275       logits, _ = vgg.vgg_16(train_inputs)
276       self.assertListEqual(logits.get_shape().as_list(),
277                            [train_batch_size, num_classes])
278       tf.get_variable_scope().reuse_variables()
279       eval_inputs = tf.random_uniform(
280           (eval_batch_size, eval_height, eval_width, 3))
281       logits, _ = vgg.vgg_16(eval_inputs, is_training=False,
282                              spatial_squeeze=False)
283       self.assertListEqual(logits.get_shape().as_list(),
284                            [eval_batch_size, 2, 2, num_classes])
285       logits = tf.reduce_mean(logits, [1, 2])
286       predictions = tf.argmax(logits, 1)
287       self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
288
289   def testForward(self):
290     batch_size = 1
291     height, width = 224, 224
292     with self.test_session() as sess:
293       inputs = tf.random_uniform((batch_size, height, width, 3))
294       logits, _ = vgg.vgg_16(inputs)
295       sess.run(tf.global_variables_initializer())
296       output = sess.run(logits)
297       self.assertTrue(output.any())
298
299
300 class VGG19Test(tf.test.TestCase):
301
302   def testBuild(self):
303     batch_size = 5
304     height, width = 224, 224
305     num_classes = 1000
306     with self.test_session():
307       inputs = tf.random_uniform((batch_size, height, width, 3))
308       logits, _ = vgg.vgg_19(inputs, num_classes)
309       self.assertEquals(logits.op.name, 'vgg_19/fc8/squeezed')
310       self.assertListEqual(logits.get_shape().as_list(),
311                            [batch_size, num_classes])
312
313   def testFullyConvolutional(self):
314     batch_size = 1
315     height, width = 256, 256
316     num_classes = 1000
317     with self.test_session():
318       inputs = tf.random_uniform((batch_size, height, width, 3))
319       logits, _ = vgg.vgg_19(inputs, num_classes, spatial_squeeze=False)
320       self.assertEquals(logits.op.name, 'vgg_19/fc8/BiasAdd')
321       self.assertListEqual(logits.get_shape().as_list(),
322                            [batch_size, 2, 2, num_classes])
323
324   def testEndPoints(self):
325     batch_size = 5
326     height, width = 224, 224
327     num_classes = 1000
328     with self.test_session():
329       inputs = tf.random_uniform((batch_size, height, width, 3))
330       _, end_points = vgg.vgg_19(inputs, num_classes)
331       expected_names = [
332           'vgg_19/conv1/conv1_1',
333           'vgg_19/conv1/conv1_2',
334           'vgg_19/pool1',
335           'vgg_19/conv2/conv2_1',
336           'vgg_19/conv2/conv2_2',
337           'vgg_19/pool2',
338           'vgg_19/conv3/conv3_1',
339           'vgg_19/conv3/conv3_2',
340           'vgg_19/conv3/conv3_3',
341           'vgg_19/conv3/conv3_4',
342           'vgg_19/pool3',
343           'vgg_19/conv4/conv4_1',
344           'vgg_19/conv4/conv4_2',
345           'vgg_19/conv4/conv4_3',
346           'vgg_19/conv4/conv4_4',
347           'vgg_19/pool4',
348           'vgg_19/conv5/conv5_1',
349           'vgg_19/conv5/conv5_2',
350           'vgg_19/conv5/conv5_3',
351           'vgg_19/conv5/conv5_4',
352           'vgg_19/pool5',
353           'vgg_19/fc6',
354           'vgg_19/fc7',
355           'vgg_19/fc8'
356       ]
357       self.assertSetEqual(set(end_points.keys()), set(expected_names))
358
359   def testModelVariables(self):
360     batch_size = 5
361     height, width = 224, 224
362     num_classes = 1000
363     with self.test_session():
364       inputs = tf.random_uniform((batch_size, height, width, 3))
365       vgg.vgg_19(inputs, num_classes)
366       expected_names = [
367           'vgg_19/conv1/conv1_1/weights',
368           'vgg_19/conv1/conv1_1/biases',
369           'vgg_19/conv1/conv1_2/weights',
370           'vgg_19/conv1/conv1_2/biases',
371           'vgg_19/conv2/conv2_1/weights',
372           'vgg_19/conv2/conv2_1/biases',
373           'vgg_19/conv2/conv2_2/weights',
374           'vgg_19/conv2/conv2_2/biases',
375           'vgg_19/conv3/conv3_1/weights',
376           'vgg_19/conv3/conv3_1/biases',
377           'vgg_19/conv3/conv3_2/weights',
378           'vgg_19/conv3/conv3_2/biases',
379           'vgg_19/conv3/conv3_3/weights',
380           'vgg_19/conv3/conv3_3/biases',
381           'vgg_19/conv3/conv3_4/weights',
382           'vgg_19/conv3/conv3_4/biases',
383           'vgg_19/conv4/conv4_1/weights',
384           'vgg_19/conv4/conv4_1/biases',
385           'vgg_19/conv4/conv4_2/weights',
386           'vgg_19/conv4/conv4_2/biases',
387           'vgg_19/conv4/conv4_3/weights',
388           'vgg_19/conv4/conv4_3/biases',
389           'vgg_19/conv4/conv4_4/weights',
390           'vgg_19/conv4/conv4_4/biases',
391           'vgg_19/conv5/conv5_1/weights',
392           'vgg_19/conv5/conv5_1/biases',
393           'vgg_19/conv5/conv5_2/weights',
394           'vgg_19/conv5/conv5_2/biases',
395           'vgg_19/conv5/conv5_3/weights',
396           'vgg_19/conv5/conv5_3/biases',
397           'vgg_19/conv5/conv5_4/weights',
398           'vgg_19/conv5/conv5_4/biases',
399           'vgg_19/fc6/weights',
400           'vgg_19/fc6/biases',
401           'vgg_19/fc7/weights',
402           'vgg_19/fc7/biases',
403           'vgg_19/fc8/weights',
404           'vgg_19/fc8/biases',
405       ]
406       model_variables = [v.op.name for v in slim.get_model_variables()]
407       self.assertSetEqual(set(model_variables), set(expected_names))
408
409   def testEvaluation(self):
410     batch_size = 2
411     height, width = 224, 224
412     num_classes = 1000
413     with self.test_session():
414       eval_inputs = tf.random_uniform((batch_size, height, width, 3))
415       logits, _ = vgg.vgg_19(eval_inputs, is_training=False)
416       self.assertListEqual(logits.get_shape().as_list(),
417                            [batch_size, num_classes])
418       predictions = tf.argmax(logits, 1)
419       self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
420
421   def testTrainEvalWithReuse(self):
422     train_batch_size = 2
423     eval_batch_size = 1
424     train_height, train_width = 224, 224
425     eval_height, eval_width = 256, 256
426     num_classes = 1000
427     with self.test_session():
428       train_inputs = tf.random_uniform(
429           (train_batch_size, train_height, train_width, 3))
430       logits, _ = vgg.vgg_19(train_inputs)
431       self.assertListEqual(logits.get_shape().as_list(),
432                            [train_batch_size, num_classes])
433       tf.get_variable_scope().reuse_variables()
434       eval_inputs = tf.random_uniform(
435           (eval_batch_size, eval_height, eval_width, 3))
436       logits, _ = vgg.vgg_19(eval_inputs, is_training=False,
437                              spatial_squeeze=False)
438       self.assertListEqual(logits.get_shape().as_list(),
439                            [eval_batch_size, 2, 2, num_classes])
440       logits = tf.reduce_mean(logits, [1, 2])
441       predictions = tf.argmax(logits, 1)
442       self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
443
444   def testForward(self):
445     batch_size = 1
446     height, width = 224, 224
447     with self.test_session() as sess:
448       inputs = tf.random_uniform((batch_size, height, width, 3))
449       logits, _ = vgg.vgg_19(inputs)
450       sess.run(tf.global_variables_initializer())
451       output = sess.run(logits)
452       self.assertTrue(output.any())
453
454 if __name__ == '__main__':
455   tf.test.main()