|
18 | 18 | from __future__ import division |
19 | 19 | from __future__ import print_function |
20 | 20 |
|
| 21 | +import numpy as np |
| 22 | + |
21 | 23 | from tensorflow.python.framework import ops |
22 | 24 | from tensorflow.python.layers import convolutional as conv_layers |
| 25 | +from tensorflow.python.ops import init_ops |
23 | 26 | from tensorflow.python.ops import math_ops |
24 | 27 | from tensorflow.python.ops import nn_ops |
25 | 28 | from tensorflow.python.ops import random_ops |
| 29 | +from tensorflow.python.ops import variable_scope |
| 30 | +from tensorflow.python.ops import variables |
26 | 31 | from tensorflow.python.platform import test |
27 | 32 |
|
28 | 33 |
|
@@ -191,21 +196,45 @@ def testFunctionalConv2DReuse(self): |
191 | 196 | height, width = 7, 9 |
192 | 197 | images = random_ops.random_uniform((5, height, width, 3), seed=1) |
193 | 198 | conv_layers.conv2d(images, 32, [3, 3], name='conv1') |
194 | | - self.assertEqual( |
195 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 2) |
| 199 | + self.assertEqual(len(variables.trainable_variables()), 2) |
196 | 200 | conv_layers.conv2d(images, 32, [3, 3], name='conv1', reuse=True) |
197 | | - self.assertEqual( |
198 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 2) |
| 201 | + self.assertEqual(len(variables.trainable_variables()), 2) |
| 202 | + |
| 203 | + def testFunctionalConv2DReuseFromScope(self): |
| 204 | + with variable_scope.variable_scope('scope'): |
| 205 | + height, width = 7, 9 |
| 206 | + images = random_ops.random_uniform((5, height, width, 3), seed=1) |
| 207 | + conv_layers.conv2d(images, 32, [3, 3], name='conv1') |
| 208 | + self.assertEqual(len(variables.trainable_variables()), 2) |
| 209 | + with variable_scope.variable_scope('scope', reuse=True): |
| 210 | + conv_layers.conv2d(images, 32, [3, 3], name='conv1') |
| 211 | + self.assertEqual(len(variables.trainable_variables()), 2) |
| 212 | + |
| 213 | + def testFunctionalConv2DInitializerFromScope(self): |
| 214 | + with self.test_session() as sess: |
| 215 | + with variable_scope.variable_scope( |
| 216 | + 'scope', initializer=init_ops.ones_initializer()): |
| 217 | + height, width = 7, 9 |
| 218 | + images = random_ops.random_uniform((5, height, width, 3), seed=1) |
| 219 | + conv_layers.conv2d(images, 32, [3, 3], name='conv1') |
| 220 | + weights = variables.trainable_variables() |
| 221 | + # Check the names of weights in order. |
| 222 | + self.assertTrue('kernel' in weights[0].name) |
| 223 | + self.assertTrue('bias' in weights[1].name) |
| 224 | + sess.run(variables.global_variables_initializer()) |
| 225 | + weights = sess.run(weights) |
| 226 | + # Check that the kernel weights got initialized to ones (from scope) |
| 227 | + self.assertAllClose(weights[0], np.ones((3, 3, 3, 32))) |
| 228 | + # Check that the bias still got initialized to zeros. |
| 229 | + self.assertAllClose(weights[1], np.zeros((32))) |
199 | 230 |
|
200 | 231 | def testFunctionalConv2DNoReuse(self): |
201 | 232 | height, width = 7, 9 |
202 | 233 | images = random_ops.random_uniform((5, height, width, 3), seed=1) |
203 | 234 | conv_layers.conv2d(images, 32, [3, 3]) |
204 | | - self.assertEqual( |
205 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 2) |
| 235 | + self.assertEqual(len(variables.trainable_variables()), 2) |
206 | 236 | conv_layers.conv2d(images, 32, [3, 3]) |
207 | | - self.assertEqual( |
208 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 4) |
| 237 | + self.assertEqual(len(variables.trainable_variables()), 4) |
209 | 238 |
|
210 | 239 |
|
211 | 240 | class SeparableConv2DTest(test.TestCase): |
@@ -323,22 +352,48 @@ def testFunctionalConv2DReuse(self): |
323 | 352 | height, width = 7, 9 |
324 | 353 | images = random_ops.random_uniform((5, height, width, 3), seed=1) |
325 | 354 | conv_layers.separable_conv2d(images, 32, [3, 3], name='sepconv1') |
326 | | - self.assertEqual( |
327 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 3) |
| 355 | + self.assertEqual(len(variables.trainable_variables()), 3) |
328 | 356 | conv_layers.separable_conv2d( |
329 | 357 | images, 32, [3, 3], name='sepconv1', reuse=True) |
330 | | - self.assertEqual( |
331 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 3) |
| 358 | + self.assertEqual(len(variables.trainable_variables()), 3) |
| 359 | + |
| 360 | + def testFunctionalConv2DReuseFromScope(self): |
| 361 | + with variable_scope.variable_scope('scope'): |
| 362 | + height, width = 7, 9 |
| 363 | + images = random_ops.random_uniform((5, height, width, 3), seed=1) |
| 364 | + conv_layers.separable_conv2d(images, 32, [3, 3], name='sepconv1') |
| 365 | + self.assertEqual(len(variables.trainable_variables()), 3) |
| 366 | + with variable_scope.variable_scope('scope', reuse=True): |
| 367 | + conv_layers.separable_conv2d(images, 32, [3, 3], name='sepconv1') |
| 368 | + self.assertEqual(len(variables.trainable_variables()), 3) |
| 369 | + |
| 370 | + def testFunctionalConv2DInitializerFromScope(self): |
| 371 | + with self.test_session() as sess: |
| 372 | + with variable_scope.variable_scope( |
| 373 | + 'scope', initializer=init_ops.ones_initializer()): |
| 374 | + height, width = 7, 9 |
| 375 | + images = random_ops.random_uniform((5, height, width, 3), seed=1) |
| 376 | + conv_layers.separable_conv2d(images, 32, [3, 3], name='sepconv1') |
| 377 | + weights = variables.trainable_variables() |
| 378 | + # Check the names of weights in order. |
| 379 | + self.assertTrue('depthwise_kernel' in weights[0].name) |
| 380 | + self.assertTrue('pointwise_kernel' in weights[1].name) |
| 381 | + self.assertTrue('bias' in weights[2].name) |
| 382 | + sess.run(variables.global_variables_initializer()) |
| 383 | + weights = sess.run(weights) |
| 384 | + # Check that the kernel weights got initialized to ones (from scope) |
| 385 | + self.assertAllClose(weights[0], np.ones((3, 3, 3, 1))) |
| 386 | + self.assertAllClose(weights[1], np.ones((1, 1, 3, 32))) |
| 387 | + # Check that the bias still got initialized to zeros. |
| 388 | + self.assertAllClose(weights[2], np.zeros((32))) |
332 | 389 |
|
333 | 390 | def testFunctionalConv2DNoReuse(self): |
334 | 391 | height, width = 7, 9 |
335 | 392 | images = random_ops.random_uniform((5, height, width, 3), seed=1) |
336 | 393 | conv_layers.separable_conv2d(images, 32, [3, 3]) |
337 | | - self.assertEqual( |
338 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 3) |
| 394 | + self.assertEqual(len(variables.trainable_variables()), 3) |
339 | 395 | conv_layers.separable_conv2d(images, 32, [3, 3]) |
340 | | - self.assertEqual( |
341 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 6) |
| 396 | + self.assertEqual(len(variables.trainable_variables()), 6) |
342 | 397 |
|
343 | 398 | def testSeparableConv2DDepthwiseRegularizer(self): |
344 | 399 | height, width = 7, 9 |
@@ -511,21 +566,45 @@ def testFunctionalConv2DTransposeReuse(self): |
511 | 566 | height, width = 7, 9 |
512 | 567 | images = random_ops.random_uniform((5, height, width, 3), seed=1) |
513 | 568 | conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1') |
514 | | - self.assertEqual( |
515 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 2) |
| 569 | + self.assertEqual(len(variables.trainable_variables()), 2) |
516 | 570 | conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1', reuse=True) |
517 | | - self.assertEqual( |
518 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 2) |
| 571 | + self.assertEqual(len(variables.trainable_variables()), 2) |
| 572 | + |
| 573 | + def testFunctionalConv2DTransposeReuseFromScope(self): |
| 574 | + with variable_scope.variable_scope('scope'): |
| 575 | + height, width = 7, 9 |
| 576 | + images = random_ops.random_uniform((5, height, width, 3), seed=1) |
| 577 | + conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1') |
| 578 | + self.assertEqual(len(variables.trainable_variables()), 2) |
| 579 | + with variable_scope.variable_scope('scope', reuse=True): |
| 580 | + conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1') |
| 581 | + self.assertEqual(len(variables.trainable_variables()), 2) |
| 582 | + |
| 583 | + def testFunctionalConv2DTransposeInitializerFromScope(self): |
| 584 | + with self.test_session() as sess: |
| 585 | + with variable_scope.variable_scope( |
| 586 | + 'scope', initializer=init_ops.ones_initializer()): |
| 587 | + height, width = 7, 9 |
| 588 | + images = random_ops.random_uniform((5, height, width, 3), seed=1) |
| 589 | + conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1') |
| 590 | + weights = variables.trainable_variables() |
| 591 | + # Check the names of weights in order. |
| 592 | + self.assertTrue('kernel' in weights[0].name) |
| 593 | + self.assertTrue('bias' in weights[1].name) |
| 594 | + sess.run(variables.global_variables_initializer()) |
| 595 | + weights = sess.run(weights) |
| 596 | + # Check that the kernel weights got initialized to ones (from scope) |
| 597 | + self.assertAllClose(weights[0], np.ones((3, 3, 32, 3))) |
| 598 | + # Check that the bias still got initialized to zeros. |
| 599 | + self.assertAllClose(weights[1], np.zeros((32))) |
519 | 600 |
|
520 | 601 | def testFunctionalConv2DTransposeNoReuse(self): |
521 | 602 | height, width = 7, 9 |
522 | 603 | images = random_ops.random_uniform((5, height, width, 3), seed=1) |
523 | 604 | conv_layers.conv2d_transpose(images, 32, [3, 3]) |
524 | | - self.assertEqual( |
525 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 2) |
| 605 | + self.assertEqual(len(variables.trainable_variables()), 2) |
526 | 606 | conv_layers.conv2d_transpose(images, 32, [3, 3]) |
527 | | - self.assertEqual( |
528 | | - len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 4) |
| 607 | + self.assertEqual(len(variables.trainable_variables()), 4) |
529 | 608 |
|
530 | 609 |
|
531 | 610 | if __name__ == '__main__': |
|
0 commit comments