|
| 1 | +# Lint as: python3 |
1 | 2 | # Copyright 2018 Google LLC. All Rights Reserved.
|
2 | 3 | #
|
3 | 4 | # Licensed under the Apache License, Version 2.0 (the "License");
|
|
14 | 15 | # ==============================================================================
|
15 | 16 | """Tests of GDN layer."""
|
16 | 17 |
|
17 |
| -import numpy as np |
18 |
| -import tensorflow.compat.v1 as tf |
| 18 | +import tensorflow.compat.v2 as tf |
19 | 19 |
|
20 |
| -from tensorflow.python.framework import test_util |
21 | 20 | from tensorflow_compression.python.layers import gdn
|
22 | 21 |
|
23 | 22 |
|
24 |
| -@test_util.deprecated_graph_mode_only |
25 | 23 | class GDNTest(tf.test.TestCase):
|
26 | 24 |
|
27 |
| - def _run_gdn(self, x, shape, inverse, rectify, data_format): |
28 |
| - inputs = tf.placeholder(tf.float32, shape) |
29 |
| - layer = gdn.GDN( |
30 |
| - inverse=inverse, rectify=rectify, data_format=data_format) |
31 |
| - outputs = layer(inputs) |
32 |
| - with self.cached_session() as sess: |
33 |
| - tf.global_variables_initializer().run() |
34 |
| - y, = sess.run([outputs], {inputs: x}) |
35 |
| - return y |
36 |
| - |
37 |
| - def test_invalid_data_format(self): |
38 |
| - x = np.random.uniform(size=(1, 2, 3, 4)) |
| 25 | + def test_invalid_data_format_raises_error(self): |
| 26 | + x = tf.random.uniform((1, 2, 3, 4), dtype=tf.float32) |
39 | 27 | with self.assertRaises(ValueError):
|
40 |
| - self._run_gdn(x, x.shape, False, False, "NHWC") |
| 28 | + gdn.GDN(inverse=False, rectify=False, data_format="NHWC")(x) |
41 | 29 |
|
42 |
| - def test_unknown_dim(self): |
43 |
| - x = np.random.uniform(size=(1, 2, 3, 4)) |
| 30 | + def test_vector_input_raises_error(self): |
| 31 | + x = tf.random.uniform((3,), dtype=tf.float32) |
| 32 | + with self.assertRaises(ValueError): |
| 33 | + gdn.GDN(inverse=False, rectify=False, data_format="channels_last")(x) |
44 | 34 | with self.assertRaises(ValueError):
|
45 |
| - self._run_gdn(x, 4 * [None], False, False, "channels_last") |
| 35 | + gdn.GDN(inverse=True, rectify=True, data_format="channels_first")(x) |
46 | 36 |
|
47 |
| - def test_channels_last(self): |
| 37 | + def test_channels_last_has_correct_output(self): |
| 38 | + # This tests that the layer produces the correct output for a number of |
| 39 | + # different input dimensionalities with 'channels_last' data format. |
48 | 40 | for ndim in [2, 3, 4, 5, 6]:
|
49 |
| - x = np.random.uniform(size=(1, 2, 3, 4, 5, 6)[:ndim]) |
50 |
| - y = self._run_gdn(x, x.shape, False, False, "channels_last") |
| 41 | + x = tf.random.uniform((1, 2, 3, 4, 5, 6)[:ndim], dtype=tf.float32) |
| 42 | + y = gdn.GDN(inverse=False, rectify=False, data_format="channels_last")(x) |
51 | 43 | self.assertEqual(x.shape, y.shape)
|
52 |
| - self.assertAllClose(y, x / np.sqrt(1 + .1 * (x ** 2)), rtol=0, atol=1e-6) |
| 44 | + self.assertAllClose(y, x / tf.sqrt(1 + .1 * (x ** 2)), rtol=0, atol=1e-6) |
53 | 45 |
|
54 |
| - def test_channels_first(self): |
| 46 | + def test_channels_first_has_correct_output(self): |
| 47 | + # This tests that the layer produces the correct output for a number of |
| 48 | + # different input dimensionalities with 'channels_first' data format. |
55 | 49 | for ndim in [2, 3, 4, 5, 6]:
|
56 |
| - x = np.random.uniform(size=(6, 5, 4, 3, 2, 1)[:ndim]) |
57 |
| - y = self._run_gdn(x, x.shape, False, False, "channels_first") |
| 50 | + x = tf.random.uniform((6, 5, 4, 3, 2, 1)[:ndim], dtype=tf.float32) |
| 51 | + y = gdn.GDN(inverse=False, rectify=False, data_format="channels_first")(x) |
58 | 52 | self.assertEqual(x.shape, y.shape)
|
59 |
| - self.assertAllClose( |
60 |
| - y, x / np.sqrt(1 + .1 * (x ** 2)), rtol=0, atol=1e-6) |
61 |
| - |
62 |
| - def test_wrong_dims(self): |
63 |
| - x = np.random.uniform(size=(3,)) |
64 |
| - with self.assertRaises(ValueError): |
65 |
| - self._run_gdn(x, x.shape, False, False, "channels_last") |
66 |
| - with self.assertRaises(ValueError): |
67 |
| - self._run_gdn(x, x.shape, True, True, "channels_first") |
| 53 | + self.assertAllClose(y, x / tf.sqrt(1 + .1 * (x ** 2)), rtol=0, atol=1e-6) |
68 | 54 |
|
69 |
| - def test_igdn(self): |
70 |
| - x = np.random.uniform(size=(1, 2, 3, 4)) |
71 |
| - y = self._run_gdn(x, x.shape, True, False, "channels_last") |
| 55 | + def test_igdn_has_correct_output(self): |
| 56 | + x = tf.random.uniform((1, 2, 3, 4), dtype=tf.float32) |
| 57 | + y = gdn.GDN(inverse=True, rectify=False)(x) |
72 | 58 | self.assertEqual(x.shape, y.shape)
|
73 |
| - self.assertAllClose(y, x * np.sqrt(1 + .1 * (x ** 2)), rtol=0, atol=1e-6) |
| 59 | + self.assertAllClose(y, x * tf.sqrt(1 + .1 * (x ** 2)), rtol=0, atol=1e-6) |
74 | 60 |
|
75 |
| - def test_rgdn(self): |
76 |
| - x = np.random.uniform(-.5, .5, size=(1, 2, 3, 4)) |
77 |
| - y = self._run_gdn(x, x.shape, False, True, "channels_last") |
| 61 | + def test_rgdn_has_correct_output(self): |
| 62 | + x = tf.random.uniform((1, 2, 3, 4), -.5, .5, dtype=tf.float32) |
| 63 | + y = gdn.GDN(inverse=False, rectify=True)(x) |
78 | 64 | self.assertEqual(x.shape, y.shape)
|
79 |
| - x = np.maximum(x, 0) |
80 |
| - self.assertAllClose(y, x / np.sqrt(1 + .1 * (x ** 2)), rtol=0, atol=1e-6) |
| 65 | + x = tf.maximum(x, 0) |
| 66 | + self.assertAllClose(y, x / tf.sqrt(1 + .1 * (x ** 2)), rtol=0, atol=1e-6) |
| 67 | + |
| 68 | + def test_variables_receive_gradients(self): |
| 69 | + x = tf.random.uniform((1, 2), dtype=tf.float32) |
| 70 | + layer = gdn.GDN(inverse=False, rectify=True) |
| 71 | + with tf.GradientTape() as g: |
| 72 | + y = layer(x) |
| 73 | + grads = g.gradient(y, layer.trainable_variables) |
| 74 | + self.assertLen(grads, 2) |
| 75 | + self.assertNotIn(None, grads) |
81 | 76 |
|
82 | 77 |
|
83 | 78 | if __name__ == "__main__":
|
|
0 commit comments