|  | 
|  | 1 | +# Copyright 2021 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 MaxUnpooling2DV2 layers.""" | 
|  | 16 | + | 
|  | 17 | +import numpy as np | 
|  | 18 | +import pytest | 
|  | 19 | +import tensorflow as tf | 
|  | 20 | +from tensorflow_addons.layers.max_unpooling_2d_v2 import MaxUnpooling2DV2 | 
|  | 21 | + | 
|  | 22 | + | 
|  | 23 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") | 
|  | 24 | +def test_simple(): | 
|  | 25 | +    valid_input = np.array([13, 4]).astype(np.float32) | 
|  | 26 | +    valid_input = np.reshape(valid_input, (1, 1, 2, 1)) | 
|  | 27 | +    indices = np.array([1, 6]).astype(np.float32) | 
|  | 28 | +    indices = np.reshape(indices, (1, 1, 2, 1)) | 
|  | 29 | +    output_shape = (1, 2, 4, 1) | 
|  | 30 | +    expected_output = np.array([0, 13, 0, 0, 0, 0, 4, 0]).astype(np.float32) | 
|  | 31 | +    expected_output = np.reshape(expected_output, output_shape) | 
|  | 32 | + | 
|  | 33 | +    output = MaxUnpooling2DV2(output_shape)(valid_input, indices).numpy() | 
|  | 34 | +    np.testing.assert_array_equal(expected_output, output) | 
|  | 35 | + | 
|  | 36 | + | 
|  | 37 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") | 
|  | 38 | +def test_complex(): | 
|  | 39 | +    valid_input = np.array([1, 2, 3, 4, 5, 6, 7, 8]).astype(np.float32) | 
|  | 40 | +    valid_input = np.reshape(valid_input, (1, 2, 2, 2)) | 
|  | 41 | +    indices = np.array([0, 3, 4, 7, 8, 11, 12, 15]).astype(np.float32) | 
|  | 42 | +    indices = np.reshape(indices, (1, 2, 2, 2)) | 
|  | 43 | +    output_shape = (1, 4, 2, 2) | 
|  | 44 | +    expected_output = np.array([1, 0, 0, 2, 3, 0, 0, 4, 5, 0, 0, 6, 7, 0, 0, 8]).astype( | 
|  | 45 | +        np.float32 | 
|  | 46 | +    ) | 
|  | 47 | +    expected_output = np.reshape(expected_output, output_shape) | 
|  | 48 | + | 
|  | 49 | +    output = MaxUnpooling2DV2(output_shape)(valid_input, indices).numpy() | 
|  | 50 | +    np.testing.assert_array_equal(expected_output, output) | 
|  | 51 | + | 
|  | 52 | + | 
|  | 53 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") | 
|  | 54 | +def test_batch(): | 
|  | 55 | +    valid_input = np.array( | 
|  | 56 | +        # fmt: off | 
|  | 57 | +        [ | 
|  | 58 | +            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, | 
|  | 59 | +            22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 | 
|  | 60 | +        ] | 
|  | 61 | +        # fmt: on | 
|  | 62 | +    ).astype(np.float32) | 
|  | 63 | +    valid_input = np.reshape(valid_input, (2, 2, 4, 2)) | 
|  | 64 | +    indices = np.array( | 
|  | 65 | +        # fmt: off | 
|  | 66 | +        [ | 
|  | 67 | +            2, 23, 8, 9, 12, 15, 40, 43, 44, 47, 72, 75, 80, 79, 62, 65, 0, 1, 30, 7, | 
|  | 68 | +            14, 35, 42, 21, 68, 69, 50, 51, 56, 5, 86, 63 | 
|  | 69 | +        ] | 
|  | 70 | +        # fmt: on | 
|  | 71 | +    ).astype(np.float32) | 
|  | 72 | +    indices = np.reshape(indices, (2, 2, 4, 2)) | 
|  | 73 | +    output_shape = (2, 4, 12, 2) | 
|  | 74 | +    expected_output = np.array( | 
|  | 75 | +        # fmt: off | 
|  | 76 | +        [ | 
|  | 77 | +            0, 0, 1, 0, 0, 0, 0, 0, 3, 4, 0, 0, 5, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, | 
|  | 78 | +            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 8, 9, 0, 0, 10, 0, | 
|  | 79 | +            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 16, 0, 0, 0, 0, 0, 0, 11, | 
|  | 80 | +            0, 0, 12, 0, 0, 0, 14, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 81 | +            17, 18, 0, 0, 0, 30, 0, 20, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 24, 0, | 
|  | 82 | +            0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, | 
|  | 83 | +            0, 0, 0, 27, 28, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 25, 26, | 
|  | 84 | +            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 0, 0, 0, 0, | 
|  | 85 | +            0, 0 | 
|  | 86 | +        ] | 
|  | 87 | +        # fmt: on | 
|  | 88 | +    ).astype(np.float32) | 
|  | 89 | +    expected_output = np.reshape(expected_output, output_shape) | 
|  | 90 | + | 
|  | 91 | +    output = MaxUnpooling2DV2(output_shape)(valid_input, indices) | 
|  | 92 | +    np.testing.assert_array_equal(expected_output, output) | 
|  | 93 | + | 
|  | 94 | + | 
|  | 95 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") | 
|  | 96 | +def test_with_pooling_simple(): | 
|  | 97 | +    valid_input = np.array([1, 2, 3, 4, 5, 6, 7, 8]).astype(np.float32) | 
|  | 98 | +    valid_input = np.reshape(valid_input, (1, 2, 4, 1)) | 
|  | 99 | +    updates, indices = tf.nn.max_pool_with_argmax( | 
|  | 100 | +        valid_input, ksize=[2, 2], strides=[2, 2], padding="SAME" | 
|  | 101 | +    ) | 
|  | 102 | +    expected_output = np.array([0, 0, 0, 0, 0, 6, 0, 8]).astype(np.float32) | 
|  | 103 | +    expected_output = np.reshape(expected_output, valid_input.shape) | 
|  | 104 | + | 
|  | 105 | +    output = MaxUnpooling2DV2(valid_input.shape)(updates, indices).numpy() | 
|  | 106 | +    np.testing.assert_array_equal(expected_output, output) | 
|  | 107 | + | 
|  | 108 | + | 
|  | 109 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") | 
|  | 110 | +def test_with_pooling(): | 
|  | 111 | +    valid_input = np.array( | 
|  | 112 | +        [1, 2, 4, 3, 8, 6, 7, 5, 9, 10, 12, 11, 13, 16, 15, 14] | 
|  | 113 | +    ).astype(np.float32) | 
|  | 114 | +    valid_input = np.reshape(valid_input, (1, 4, 4, 1)) | 
|  | 115 | +    updates, indices = tf.nn.max_pool_with_argmax( | 
|  | 116 | +        valid_input, ksize=[2, 2], strides=[2, 2], padding="SAME" | 
|  | 117 | +    ) | 
|  | 118 | +    expected_output = np.array( | 
|  | 119 | +        [0, 0, 0, 0, 8, 0, 7, 0, 0, 0, 0, 0, 0, 16, 15, 0] | 
|  | 120 | +    ).astype(np.float32) | 
|  | 121 | +    expected_output = np.reshape(expected_output, valid_input.shape) | 
|  | 122 | + | 
|  | 123 | +    output = MaxUnpooling2DV2(valid_input.shape)(updates, indices).numpy() | 
|  | 124 | +    np.testing.assert_array_equal(expected_output, output) | 
|  | 125 | + | 
|  | 126 | + | 
|  | 127 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") | 
|  | 128 | +def test_symbolic_tensor_shape(): | 
|  | 129 | +    valid_input = tf.keras.layers.Input((None, None, 1)) | 
|  | 130 | +    updates, indices = tf.nn.max_pool_with_argmax( | 
|  | 131 | +        valid_input, ksize=[2, 2], strides=[2, 2], padding="SAME" | 
|  | 132 | +    ) | 
|  | 133 | +    with pytest.raises(ValueError): | 
|  | 134 | +        MaxUnpooling2DV2(valid_input.shape)(updates, indices) | 
0 commit comments