1 /* Copyright 2015 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 16 // TODO(shlens, sherrym): Consider adding additional tests in image_ops.py in 17 // order to compare the reference implementation for image resizing in Python 18 // Image Library. 19 #include "tensorflow/core/framework/allocator.h" 20 #include "tensorflow/core/framework/fake_input.h" 21 #include "tensorflow/core/framework/node_def_builder.h" 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/tensor.h" 24 #include "tensorflow/core/framework/tensor_testutil.h" 25 #include "tensorflow/core/framework/types.h" 26 #include "tensorflow/core/framework/types.pb.h" 27 #include "tensorflow/core/kernels/ops_testutil.h" 28 #include "tensorflow/core/kernels/ops_util.h" 29 #include "tensorflow/core/lib/core/status_test_util.h" 30 #include "tensorflow/core/platform/test.h" 31 32 namespace tensorflow { 33 34 class ResizeNearestNeighborOpTest : public OpsTestBase { 35 protected: 36 ResizeNearestNeighborOpTest() { 37 TF_EXPECT_OK(NodeDefBuilder("resize_nn", "ResizeNearestNeighbor") 38 .Input(FakeInput(DT_FLOAT)) 39 .Input(FakeInput(DT_INT32)) 40 .Attr("align_corners", false) 41 .Finalize(node_def())); 42 TF_EXPECT_OK(InitOp()); 43 } 44 }; 45 46 class ResizeNearestNeighborOpAlignCornersTest : public OpsTestBase { 47 protected: 48 ResizeNearestNeighborOpAlignCornersTest() { 49 TF_EXPECT_OK(NodeDefBuilder("resize_nn", "ResizeNearestNeighbor") 50 .Input(FakeInput(DT_FLOAT)) 51 .Input(FakeInput(DT_INT32)) 52 .Attr("align_corners", true) 53 .Finalize(node_def())); 54 TF_EXPECT_OK(InitOp()); 55 } 56 }; 57 58 TEST_F(ResizeNearestNeighborOpTest, TestNearest2x2To1x1) { 59 // Input: 60 // 1, 2 61 // 3, 4 62 AddInputFromArray<float>(TensorShape({1, 2, 2, 1}), {1, 2, 3, 4}); 63 AddInputFromArray<int32>(TensorShape({2}), {1, 1}); 64 TF_ASSERT_OK(RunOpKernel()); 65 66 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 1, 1, 1})); 67 68 // clang-format off 69 test::FillValues<float>(&expected, {1}); 70 71 // clang-format on 72 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 73 } 74 75 TEST_F(ResizeNearestNeighborOpAlignCornersTest, 76 TestNearest2x2AlignCornersTo1x1) { 77 // Input: 78 // 1, 2 79 // 3, 4 80 AddInputFromArray<float>(TensorShape({1, 2, 2, 1}), {1, 2, 3, 4}); 81 AddInputFromArray<int32>(TensorShape({2}), {1, 1}); 82 TF_ASSERT_OK(RunOpKernel()); 83 84 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 1, 1, 1})); 85 86 // clang-format off 87 test::FillValues<float>(&expected, {1}); 88 89 // clang-format on 90 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 91 } 92 93 TEST_F(ResizeNearestNeighborOpTest, TestNearest2x2To3x3) { 94 // Input: 95 // 1, 2 96 // 3, 4 97 AddInputFromArray<float>(TensorShape({1, 2, 2, 1}), {1, 2, 3, 4}); 98 AddInputFromArray<int32>(TensorShape({2}), {3, 3}); 99 TF_ASSERT_OK(RunOpKernel()); 100 101 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 3, 3, 1})); 102 103 // clang-format off 104 test::FillValues<float>(&expected, 105 {1, 1, 2, 106 1, 1, 2, 107 3, 3, 4}); 108 109 // clang-format on 110 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 111 } 112 113 TEST_F(ResizeNearestNeighborOpAlignCornersTest, 114 TestNearestAlignCorners2x2To3x3) { 115 // Input: 116 // 1, 2 117 // 3, 4 118 AddInputFromArray<float>(TensorShape({1, 2, 2, 1}), {1, 2, 3, 4}); 119 AddInputFromArray<int32>(TensorShape({2}), {3, 3}); 120 TF_ASSERT_OK(RunOpKernel()); 121 122 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 3, 3, 1})); 123 124 // clang-format off 125 test::FillValues<float>(&expected, 126 {1, 2, 2, 127 3, 4, 4, 128 3, 4, 4}); 129 130 // clang-format on 131 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 132 } 133 134 TEST_F(ResizeNearestNeighborOpTest, TestNearest3x3To2x2) { 135 // Input: 136 // 1, 2, 3 137 // 4, 5, 6 138 // 7, 8, 9 139 AddInputFromArray<float>(TensorShape({1, 3, 3, 1}), 140 {1, 2, 3, 4, 5, 6, 7, 8, 9}); 141 AddInputFromArray<int32>(TensorShape({2}), {2, 2}); 142 TF_ASSERT_OK(RunOpKernel()); 143 144 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 2, 2, 1})); 145 146 // clang-format off 147 test::FillValues<float>(&expected, 148 {1, 2, 149 4, 5}); 150 151 // clang-format on 152 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 153 } 154 155 TEST_F(ResizeNearestNeighborOpAlignCornersTest, 156 TestNearestAlignCorners3x3To2x2) { 157 // Input: 158 // 1, 2, 3 159 // 4, 5, 6 160 // 7, 8, 9 161 AddInputFromArray<float>(TensorShape({1, 3, 3, 1}), 162 {1, 2, 3, 4, 5, 6, 7, 8, 9}); 163 AddInputFromArray<int32>(TensorShape({2}), {2, 2}); 164 TF_ASSERT_OK(RunOpKernel()); 165 166 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 2, 2, 1})); 167 168 // clang-format off 169 test::FillValues<float>(&expected, 170 {1, 3, 171 7, 9}); 172 173 // clang-format on 174 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 175 } 176 177 TEST_F(ResizeNearestNeighborOpTest, TestNearest2x2To2x5) { 178 // Input: 179 // 1, 2 180 // 3, 4 181 AddInputFromArray<float>(TensorShape({1, 2, 2, 1}), {1, 2, 3, 4}); 182 AddInputFromArray<int32>(TensorShape({2}), {2, 5}); 183 TF_ASSERT_OK(RunOpKernel()); 184 185 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 2, 5, 1})); 186 187 // clang-format off 188 test::FillValues<float>(&expected, 189 {1, 1, 1, 2, 2, 190 3, 3, 3, 4, 4}); 191 192 // clang-format on 193 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 194 } 195 196 TEST_F(ResizeNearestNeighborOpTest, TestNearestNeighbor4x4To3x3) { 197 // Input: 198 // 1, 2, 3, 4 199 // 5, 6, 7, 8 200 // 9, 10, 11, 12 201 // 13, 14, 15, 16 202 AddInputFromArray<float>( 203 TensorShape({1, 4, 4, 1}), 204 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}); 205 AddInputFromArray<int32>(TensorShape({2}), {3, 3}); 206 TF_ASSERT_OK(RunOpKernel()); 207 208 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 3, 3, 1})); 209 210 // clang-format off 211 test::FillValues<float>(&expected, 212 {1, 2, 3, 213 5, 6, 7, 214 9, 10, 11}); 215 216 // clang-format on 217 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 218 } 219 220 TEST_F(ResizeNearestNeighborOpAlignCornersTest, 221 TestNearestNeighborAlignCorners4x4To3x3) { 222 // Input: 223 // 1, 2, 3, 4 224 // 5, 6, 7, 8 225 // 9, 10, 11, 12 226 // 13, 14, 15, 16 227 AddInputFromArray<float>( 228 TensorShape({1, 4, 4, 1}), 229 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}); 230 AddInputFromArray<int32>(TensorShape({2}), {3, 3}); 231 TF_ASSERT_OK(RunOpKernel()); 232 233 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 3, 3, 1})); 234 235 // clang-format off 236 test::FillValues<float>(&expected, 237 { 1, 3, 4, 238 9, 11, 12, 239 13, 15, 16}); 240 241 // clang-format on 242 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 243 } 244 245 TEST_F(ResizeNearestNeighborOpTest, TestNearest2x2To5x2) { 246 // Input: 247 // 1, 2 248 // 3, 4 249 AddInputFromArray<float>(TensorShape({1, 2, 2, 1}), {1, 2, 3, 4}); 250 AddInputFromArray<int32>(TensorShape({2}), {5, 2}); 251 TF_ASSERT_OK(RunOpKernel()); 252 253 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 5, 2, 1})); 254 255 // clang-format off 256 test::FillValues<float>(&expected, 257 {1, 2, 258 1, 2, 259 1, 2, 260 3, 4, 261 3, 4}); 262 263 // clang-format on 264 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 265 } 266 267 TEST_F(ResizeNearestNeighborOpTest, TestNearest2x2To4x4) { 268 // Input: 269 // 1, 2 270 // 3, 4 271 AddInputFromArray<float>(TensorShape({1, 2, 2, 1}), {1, 2, 3, 4}); 272 AddInputFromArray<int32>(TensorShape({2}), {4, 4}); 273 TF_ASSERT_OK(RunOpKernel()); 274 275 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 4, 4, 1})); 276 277 // clang-format off 278 test::FillValues<float>(&expected, 279 {1, 1, 2, 2, 280 1, 1, 2, 2, 281 3, 3, 4, 4, 282 3, 3, 4, 4}); 283 284 // clang-format on 285 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 286 } 287 288 TEST_F(ResizeNearestNeighborOpTest, TestNearest2x2x2x2To2x3x3x2) { 289 // Input: 290 // [ [ 1, 1 ], [ 2, 2], 291 // [ 3, 3 ], [ 4, 4] ], 292 // [ [ 5, 5 ], [ 6, 6], 293 // [ 7, 7 ], [ 8, 8] ] 294 AddInputFromArray<float>(TensorShape({2, 2, 2, 2}), 295 {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8}); 296 AddInputFromArray<int32>(TensorShape({2}), {3, 3}); 297 TF_ASSERT_OK(RunOpKernel()); 298 299 Tensor expected(allocator(), DT_FLOAT, TensorShape({2, 3, 3, 2})); 300 301 // clang-format off 302 test::FillValues<float>(&expected, 303 {1, 1, 1, 304 1, 2, 2, 305 1, 1, 1, 306 1, 2, 2, 307 3, 3, 3, 308 3, 4, 4, 309 5, 5, 5, 310 5, 6, 6, 311 5, 5, 5, 312 5, 6, 6, 313 7, 7, 7, 314 7, 8, 8}); 315 316 // clang-format on 317 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 318 } 319 320 } // namespace tensorflow 321