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.vgg."""
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
20 import tensorflow as tf
24 slim = tf.contrib.slim
27 class VGGATest(tf.test.TestCase):
31 height, width = 224, 224
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])
40 def testFullyConvolutional(self):
42 height, width = 256, 256
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])
51 def testEndPoints(self):
53 height, width = 224, 224
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',
60 'vgg_a/conv2/conv2_1',
62 'vgg_a/conv3/conv3_1',
63 'vgg_a/conv3/conv3_2',
65 'vgg_a/conv4/conv4_1',
66 'vgg_a/conv4/conv4_2',
68 'vgg_a/conv5/conv5_1',
69 'vgg_a/conv5/conv5_2',
75 self.assertSetEqual(set(end_points.keys()), set(expected_names))
77 def testModelVariables(self):
79 height, width = 224, 224
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',
107 model_variables = [v.op.name for v in slim.get_model_variables()]
108 self.assertSetEqual(set(model_variables), set(expected_names))
110 def testEvaluation(self):
112 height, width = 224, 224
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])
122 def testTrainEvalWithReuse(self):
125 train_height, train_width = 224, 224
126 eval_height, eval_width = 256, 256
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])
145 def testForward(self):
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())
156 class VGG16Test(tf.test.TestCase):
160 height, width = 224, 224
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])
169 def testFullyConvolutional(self):
171 height, width = 256, 256
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])
180 def testEndPoints(self):
182 height, width = 224, 224
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',
190 'vgg_16/conv2/conv2_1',
191 'vgg_16/conv2/conv2_2',
193 'vgg_16/conv3/conv3_1',
194 'vgg_16/conv3/conv3_2',
195 'vgg_16/conv3/conv3_3',
197 'vgg_16/conv4/conv4_1',
198 'vgg_16/conv4/conv4_2',
199 'vgg_16/conv4/conv4_3',
201 'vgg_16/conv5/conv5_1',
202 'vgg_16/conv5/conv5_2',
203 'vgg_16/conv5/conv5_3',
209 self.assertSetEqual(set(end_points.keys()), set(expected_names))
211 def testModelVariables(self):
213 height, width = 224, 224
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',
246 'vgg_16/fc7/weights',
248 'vgg_16/fc8/weights',
251 model_variables = [v.op.name for v in slim.get_model_variables()]
252 self.assertSetEqual(set(model_variables), set(expected_names))
254 def testEvaluation(self):
256 height, width = 224, 224
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])
266 def testTrainEvalWithReuse(self):
269 train_height, train_width = 224, 224
270 eval_height, eval_width = 256, 256
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])
289 def testForward(self):
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())
300 class VGG19Test(tf.test.TestCase):
304 height, width = 224, 224
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])
313 def testFullyConvolutional(self):
315 height, width = 256, 256
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])
324 def testEndPoints(self):
326 height, width = 224, 224
328 with self.test_session():
329 inputs = tf.random_uniform((batch_size, height, width, 3))
330 _, end_points = vgg.vgg_19(inputs, num_classes)
332 'vgg_19/conv1/conv1_1',
333 'vgg_19/conv1/conv1_2',
335 'vgg_19/conv2/conv2_1',
336 'vgg_19/conv2/conv2_2',
338 'vgg_19/conv3/conv3_1',
339 'vgg_19/conv3/conv3_2',
340 'vgg_19/conv3/conv3_3',
341 'vgg_19/conv3/conv3_4',
343 'vgg_19/conv4/conv4_1',
344 'vgg_19/conv4/conv4_2',
345 'vgg_19/conv4/conv4_3',
346 'vgg_19/conv4/conv4_4',
348 'vgg_19/conv5/conv5_1',
349 'vgg_19/conv5/conv5_2',
350 'vgg_19/conv5/conv5_3',
351 'vgg_19/conv5/conv5_4',
357 self.assertSetEqual(set(end_points.keys()), set(expected_names))
359 def testModelVariables(self):
361 height, width = 224, 224
363 with self.test_session():
364 inputs = tf.random_uniform((batch_size, height, width, 3))
365 vgg.vgg_19(inputs, num_classes)
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',
401 'vgg_19/fc7/weights',
403 'vgg_19/fc8/weights',
406 model_variables = [v.op.name for v in slim.get_model_variables()]
407 self.assertSetEqual(set(model_variables), set(expected_names))
409 def testEvaluation(self):
411 height, width = 224, 224
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])
421 def testTrainEvalWithReuse(self):
424 train_height, train_width = 224, 224
425 eval_height, eval_width = 256, 256
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])
444 def testForward(self):
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())
454 if __name__ == '__main__':