|
| 1 | +import unittest |
| 2 | +from flax.pooling import pool,avg_pool, max_pool, min_pool |
| 3 | +import numpy as np |
| 4 | +import jax.numpy as jnp |
| 5 | +from absl.testing import absltest, parameterized |
| 6 | +import jax |
| 7 | + |
| 8 | +jax.config.parse_flags_with_absl() |
| 9 | + |
| 10 | + |
| 11 | +class PoolTest(parameterized.TestCase): |
| 12 | + def test_pool_custom_reduce(self): |
| 13 | + x = jnp.full((1, 3, 3, 1), 2.0) |
| 14 | + mul_reduce = lambda x, y: x * y |
| 15 | + y = pool(x, 1.0, mul_reduce, (2, 2), (1, 1), 'VALID') |
| 16 | + np.testing.assert_allclose(y, np.full((1, 2, 2, 1), 2.0**4)) |
| 17 | + |
| 18 | + @parameterized.parameters( |
| 19 | + {'count_include_pad': True}, {'count_include_pad': False} |
| 20 | + ) |
| 21 | + def test_avg_pool(self, count_include_pad): |
| 22 | + x = jnp.full((1, 3, 3, 1), 2.0) |
| 23 | + pool = lambda x: avg_pool(x, (2, 2), count_include_pad=count_include_pad) |
| 24 | + y = pool(x) |
| 25 | + np.testing.assert_allclose(y, np.full((1, 2, 2, 1), 2.0)) |
| 26 | + y_grad = jax.grad(lambda x: pool(x).sum())(x) |
| 27 | + expected_grad = jnp.array( |
| 28 | + [ |
| 29 | + [0.25, 0.5, 0.25], |
| 30 | + [0.5, 1.0, 0.5], |
| 31 | + [0.25, 0.5, 0.25], |
| 32 | + ] |
| 33 | + ).reshape((1, 3, 3, 1)) |
| 34 | + np.testing.assert_allclose(y_grad, expected_grad) |
| 35 | + |
| 36 | + @parameterized.parameters( |
| 37 | + {'count_include_pad': True}, {'count_include_pad': False} |
| 38 | + ) |
| 39 | + def test_avg_pool_no_batch(self, count_include_pad): |
| 40 | + x = jnp.full((3, 3, 1), 2.0) |
| 41 | + pool = lambda x: avg_pool(x, (2, 2), count_include_pad=count_include_pad) |
| 42 | + y = pool(x) |
| 43 | + np.testing.assert_allclose(y, np.full((2, 2, 1), 2.0)) |
| 44 | + y_grad = jax.grad(lambda x: pool(x).sum())(x) |
| 45 | + expected_grad = jnp.array( |
| 46 | + [ |
| 47 | + [0.25, 0.5, 0.25], |
| 48 | + [0.5, 1.0, 0.5], |
| 49 | + [0.25, 0.5, 0.25], |
| 50 | + ] |
| 51 | + ).reshape((3, 3, 1)) |
| 52 | + np.testing.assert_allclose(y_grad, expected_grad) |
| 53 | + |
| 54 | + def test_max_pool(self): |
| 55 | + x = jnp.arange(9).reshape((1, 3, 3, 1)).astype(jnp.float32) |
| 56 | + pool = lambda x: max_pool(x, (2, 2)) |
| 57 | + expected_y = jnp.array( |
| 58 | + [ |
| 59 | + [4.0, 5.0], |
| 60 | + [7.0, 8.0], |
| 61 | + ] |
| 62 | + ).reshape((1, 2, 2, 1)) |
| 63 | + y = pool(x) |
| 64 | + np.testing.assert_allclose(y, expected_y) |
| 65 | + y_grad = jax.grad(lambda x: pool(x).sum())(x) |
| 66 | + expected_grad = jnp.array( |
| 67 | + [ |
| 68 | + [0.0, 0.0, 0.0], |
| 69 | + [0.0, 1.0, 1.0], |
| 70 | + [0.0, 1.0, 1.0], |
| 71 | + ] |
| 72 | + ).reshape((1, 3, 3, 1)) |
| 73 | + np.testing.assert_allclose(y_grad, expected_grad) |
| 74 | + |
| 75 | + @parameterized.parameters( |
| 76 | + {'count_include_pad': True}, {'count_include_pad': False} |
| 77 | + ) |
| 78 | + def test_avg_pool_padding_same(self, count_include_pad): |
| 79 | + x = jnp.array([1.0, 2.0, 3.0, 4.0]).reshape((1, 2, 2, 1)) |
| 80 | + pool = lambda x: avg_pool( |
| 81 | + x, (2, 2), padding='SAME', count_include_pad=count_include_pad |
| 82 | + ) |
| 83 | + y = pool(x) |
| 84 | + if count_include_pad: |
| 85 | + expected_y = jnp.array([10.0 / 4, 6.0 / 4, 7.0 / 4, 4.0 / 4]).reshape( |
| 86 | + (1, 2, 2, 1) |
| 87 | + ) |
| 88 | + else: |
| 89 | + expected_y = jnp.array([10.0 / 4, 6.0 / 2, 7.0 / 2, 4.0 / 1]).reshape( |
| 90 | + (1, 2, 2, 1) |
| 91 | + ) |
| 92 | + np.testing.assert_allclose(y, expected_y) |
| 93 | + |
| 94 | + def test_pooling_variable_batch_dims(self): |
| 95 | + x = jnp.zeros((1, 8, 32, 32, 3), dtype=jnp.float32) |
| 96 | + y = max_pool(x, (2, 2), (2, 2)) |
| 97 | + |
| 98 | + assert y.shape == (1, 8, 16, 16, 3) |
| 99 | + |
| 100 | + def test_pooling_no_batch_dims(self): |
| 101 | + x = jnp.zeros((32, 32, 3), dtype=jnp.float32) |
| 102 | + y = max_pool(x, (2, 2), (2, 2)) |
| 103 | + |
| 104 | + assert y.shape == (16, 16, 3) |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + unittest.main() |
0 commit comments