test_nearest_interp_op.py 13.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
16

17
import numpy as np
姜永久 已提交
18
from eager_op_test import OpTest
19 20

import paddle.fluid.core as core
21 22


23 24 25 26 27 28 29 30 31
def nearest_neighbor_interp_np(
    X,
    out_h,
    out_w,
    out_size=None,
    actual_shape=None,
    align_corners=True,
    data_layout='NCHW',
):
32
    """nearest neighbor interpolation implement in shape [N, C, H, W]"""
33 34
    if data_layout == "NHWC":
        X = np.transpose(X, (0, 3, 1, 2))  # NHWC => NCHW
35 36 37
    if out_size is not None:
        out_h = out_size[0]
        out_w = out_size[1]
38 39 40
    if actual_shape is not None:
        out_h = actual_shape[0]
        out_w = actual_shape[1]
41 42 43
    n, c, in_h, in_w = X.shape

    ratio_h = ratio_w = 0.0
44 45
    if out_h > 1:
        if align_corners:
T
tink2123 已提交
46 47 48
            ratio_h = (in_h - 1.0) / (out_h - 1.0)
        else:
            ratio_h = 1.0 * in_h / out_h
49 50
    if out_w > 1:
        if align_corners:
T
tink2123 已提交
51 52 53
            ratio_w = (in_w - 1.0) / (out_w - 1.0)
        else:
            ratio_w = 1.0 * in_w / out_w
54 55

    out = np.zeros((n, c, out_h, out_w))
56 57 58 59 60 61 62 63 64 65 66 67 68

    if align_corners:
        for i in range(out_h):
            in_i = int(ratio_h * i + 0.5)
            for j in range(out_w):
                in_j = int(ratio_w * j + 0.5)
                out[:, :, i, j] = X[:, :, in_i, in_j]
    else:
        for i in range(out_h):
            in_i = int(ratio_h * i)
            for j in range(out_w):
                in_j = int(ratio_w * j)
                out[:, :, i, j] = X[:, :, in_i, in_j]
69

70 71 72
    if data_layout == "NHWC":
        out = np.transpose(out, (0, 2, 3, 1))  # NCHW => NHWC

73 74 75
    return out.astype(X.dtype)


76
class TestNearestInterpOp(OpTest):
77 78
    def setUp(self):
        self.out_size = None
79
        self.actual_shape = None
80
        self.data_layout = 'NCHW'
81
        self.init_test_case()
82
        self.op_type = "nearest_interp"
姜永久 已提交
83
        self.check_dygraph = True
84
        input_np = np.random.random(self.input_shape).astype("float64")
85

86 87 88 89 90 91 92
        if self.data_layout == "NCHW":
            in_h = self.input_shape[2]
            in_w = self.input_shape[3]
        else:
            in_h = self.input_shape[1]
            in_w = self.input_shape[2]

D
dengkaipeng 已提交
93
        if self.scale > 0:
94 95
            out_h = int(in_h * self.scale)
            out_w = int(in_w * self.scale)
D
dengkaipeng 已提交
96 97 98 99
        else:
            out_h = self.out_h
            out_w = self.out_w

100 101 102 103 104 105 106 107 108
        output_np = nearest_neighbor_interp_np(
            input_np,
            out_h,
            out_w,
            self.out_size,
            self.actual_shape,
            self.align_corners,
            self.data_layout,
        )
109 110 111
        self.inputs = {'X': input_np}
        if self.out_size is not None:
            self.inputs['OutSize'] = self.out_size
姜永久 已提交
112
            self.check_dygraph = False
113 114
        if self.actual_shape is not None:
            self.inputs['OutSize'] = self.actual_shape
姜永久 已提交
115
            self.check_dygraph = False
116 117 118
        self.attrs = {
            'out_h': self.out_h,
            'out_w': self.out_w,
D
dengkaipeng 已提交
119
            'scale': self.scale,
120 121
            'interp_method': self.interp_method,
            'align_corners': self.align_corners,
122
            'data_layout': self.data_layout,
123 124 125 126
        }
        self.outputs = {'Out': output_np}

    def test_check_output(self):
姜永久 已提交
127
        self.check_output(check_dygraph=self.check_dygraph)
128 129

    def test_check_grad(self):
130
        self.check_grad(
姜永久 已提交
131
            ['X'], 'Out', in_place=True, check_dygraph=self.check_dygraph
132
        )
133 134

    def init_test_case(self):
135
        self.interp_method = 'nearest'
136
        self.input_shape = [2, 3, 4, 5]
137 138
        self.out_h = 2
        self.out_w = 2
139
        self.scale = 0.0
140
        self.out_size = np.array([3, 3]).astype("int32")
141
        self.align_corners = True
142 143


144
class TestNearestNeighborInterpCase1(TestNearestInterpOp):
145
    def init_test_case(self):
146
        self.interp_method = 'nearest'
147 148 149
        self.input_shape = [4, 1, 7, 8]
        self.out_h = 1
        self.out_w = 1
150
        self.scale = 0.0
T
tink2123 已提交
151
        self.align_corners = True
152 153


154
class TestNearestNeighborInterpCase2(TestNearestInterpOp):
155
    def init_test_case(self):
156
        self.interp_method = 'nearest'
157 158 159
        self.input_shape = [3, 3, 9, 6]
        self.out_h = 12
        self.out_w = 12
160
        self.scale = 0.0
161
        self.align_corners = True
162 163


164
class TestNearestNeighborInterpCase3(TestNearestInterpOp):
165
    def init_test_case(self):
166
        self.interp_method = 'nearest'
167
        self.input_shape = [1, 1, 32, 64]
168
        self.out_h = 64
169
        self.out_w = 32
170
        self.scale = 0.0
171
        self.align_corners = True
172 173


174
class TestNearestNeighborInterpCase4(TestNearestInterpOp):
175
    def init_test_case(self):
176
        self.interp_method = 'nearest'
177 178 179
        self.input_shape = [4, 1, 7, 8]
        self.out_h = 1
        self.out_w = 1
180
        self.scale = 0.0
181
        self.out_size = np.array([2, 2]).astype("int32")
182
        self.align_corners = True
183 184


185
class TestNearestNeighborInterpCase5(TestNearestInterpOp):
186
    def init_test_case(self):
187
        self.interp_method = 'nearest'
188 189 190
        self.input_shape = [3, 3, 9, 6]
        self.out_h = 12
        self.out_w = 12
191
        self.scale = 0.0
192
        self.out_size = np.array([11, 11]).astype("int32")
193
        self.align_corners = True
194 195


196
class TestNearestNeighborInterpCase6(TestNearestInterpOp):
197
    def init_test_case(self):
198
        self.interp_method = 'nearest'
199
        self.input_shape = [1, 1, 32, 64]
200
        self.out_h = 64
201
        self.out_w = 32
202
        self.scale = 0.0
203
        self.out_size = np.array([65, 129]).astype("int32")
204
        self.align_corners = True
205 206


K
Kaipeng Deng 已提交
207 208 209
class TestNearestNeighborInterpSame(TestNearestInterpOp):
    def init_test_case(self):
        self.interp_method = 'nearest'
210 211
        self.input_shape = [2, 3, 32, 64]
        self.out_h = 32
K
Kaipeng Deng 已提交
212
        self.out_w = 64
213
        self.scale = 0.0
K
Kaipeng Deng 已提交
214 215 216
        self.align_corners = True


217
class TestNearestNeighborInterpActualShape(TestNearestInterpOp):
218
    def init_test_case(self):
219
        self.interp_method = 'nearest'
220 221 222
        self.input_shape = [3, 2, 32, 16]
        self.out_h = 64
        self.out_w = 32
223
        self.scale = 0.0
224
        self.out_size = np.array([66, 40]).astype("int32")
225
        self.align_corners = True
226 227


228 229 230 231 232 233
class TestNearestNeighborInterpDataLayout(TestNearestInterpOp):
    def init_test_case(self):
        self.interp_method = 'nearest'
        self.input_shape = [2, 4, 4, 5]
        self.out_h = 2
        self.out_w = 2
234
        self.scale = 0.0
235 236 237 238 239
        self.out_size = np.array([3, 8]).astype("int32")
        self.align_corners = True
        self.data_layout = "NHWC"


240
class TestNearestInterpOpUint8(OpTest):
241 242
    def setUp(self):
        self.out_size = None
243
        self.actual_shape = None
244
        self.init_test_case()
245
        self.op_type = "nearest_interp"
姜永久 已提交
246
        self.check_dygraph = True
247 248 249
        input_np = np.random.randint(
            low=0, high=256, size=self.input_shape
        ).astype("uint8")
D
dengkaipeng 已提交
250 251 252 253 254 255 256 257

        if self.scale > 0:
            out_h = int(self.input_shape[2] * self.scale)
            out_w = int(self.input_shape[3] * self.scale)
        else:
            out_h = self.out_h
            out_w = self.out_w

258 259 260 261 262 263 264 265
        output_np = nearest_neighbor_interp_np(
            input_np,
            out_h,
            out_w,
            self.out_size,
            self.actual_shape,
            self.align_corners,
        )
266 267 268
        self.inputs = {'X': input_np}
        if self.out_size is not None:
            self.inputs['OutSize'] = self.out_size
姜永久 已提交
269
            self.check_dygraph = False
270 271 272
        self.attrs = {
            'out_h': self.out_h,
            'out_w': self.out_w,
D
dengkaipeng 已提交
273
            'scale': self.scale,
274
            'interp_method': self.interp_method,
275
            'align_corners': self.align_corners,
276 277 278 279
        }
        self.outputs = {'Out': output_np}

    def test_check_output(self):
280
        self.check_output_with_place(
姜永久 已提交
281
            place=core.CPUPlace(), atol=1, check_dygraph=self.check_dygraph
282
        )
283 284

    def init_test_case(self):
285
        self.interp_method = 'nearest'
286 287 288
        self.input_shape = [1, 3, 9, 6]
        self.out_h = 10
        self.out_w = 9
289
        self.scale = 0.0
290
        self.align_corners = True
291 292


293
class TestNearestNeighborInterpCase1Uint8(TestNearestInterpOpUint8):
294 295
    def init_test_case(self):
        self.interp_method = 'nearest'
296 297 298
        self.input_shape = [2, 3, 32, 64]
        self.out_h = 80
        self.out_w = 40
299
        self.scale = 0.0
T
tink2123 已提交
300
        self.align_corners = True
301 302


303
class TestNearestNeighborInterpCase2Uint8(TestNearestInterpOpUint8):
304 305 306 307 308
    def init_test_case(self):
        self.interp_method = 'nearest'
        self.input_shape = [4, 1, 7, 8]
        self.out_h = 5
        self.out_w = 13
309
        self.scale = 0.0
310
        self.out_size = np.array([6, 15]).astype("int32")
311 312 313 314 315 316
        self.align_corners = True


class TestNearestInterpWithoutCorners(TestNearestInterpOp):
    def set_align_corners(self):
        self.align_corners = False
317 318


D
dengkaipeng 已提交
319 320 321
class TestNearestNeighborInterpScale1(TestNearestInterpOp):
    def init_test_case(self):
        self.interp_method = 'nearest'
322
        self.input_shape = [3, 2, 7, 5]
D
dengkaipeng 已提交
323 324
        self.out_h = 64
        self.out_w = 32
325
        self.scale = 2.0
D
dengkaipeng 已提交
326 327 328 329 330 331 332
        self.out_size = np.array([66, 40]).astype("int32")
        self.align_corners = True


class TestNearestNeighborInterpScale2(TestNearestInterpOp):
    def init_test_case(self):
        self.interp_method = 'nearest'
333
        self.input_shape = [3, 2, 5, 7]
D
dengkaipeng 已提交
334 335 336 337 338 339 340 341 342 343
        self.out_h = 64
        self.out_w = 32
        self.scale = 1.5
        self.out_size = np.array([66, 40]).astype("int32")
        self.align_corners = True


class TestNearestNeighborInterpScale3(TestNearestInterpOp):
    def init_test_case(self):
        self.interp_method = 'nearest'
344
        self.input_shape = [3, 2, 7, 5]
D
dengkaipeng 已提交
345 346
        self.out_h = 64
        self.out_w = 32
347
        self.scale = 1.0
D
dengkaipeng 已提交
348 349 350 351
        self.out_size = np.array([66, 40]).astype("int32")
        self.align_corners = True


352 353 354 355 356 357 358 359 360 361 362 363
class TestNearestInterpOp_attr_tensor(OpTest):
    def setUp(self):
        self.out_size = None
        self.actual_shape = None
        self.init_test_case()
        self.op_type = "nearest_interp"
        self.shape_by_1Dtensor = False
        self.scale_by_1Dtensor = False
        self.attrs = {
            'interp_method': self.interp_method,
            'align_corners': self.align_corners,
        }
364
        # NOTE(dev): some AsDispensible input is not used under imperative mode.
姜永久 已提交
365 366
        # Skip check_dygraph while found them in Inputs.
        self.check_dygraph = True
367

368
        input_np = np.random.random(self.input_shape).astype("float64")
369 370 371
        self.inputs = {'X': input_np}

        if self.scale_by_1Dtensor:
372
            self.inputs['Scale'] = np.array([self.scale]).astype("float64")
373 374 375 376 377 378 379 380 381 382
        elif self.scale > 0:
            out_h = int(self.input_shape[2] * self.scale)
            out_w = int(self.input_shape[3] * self.scale)
            self.attrs['scale'] = self.scale
        else:
            out_h = self.out_h
            out_w = self.out_w

        if self.shape_by_1Dtensor:
            self.inputs['OutSize'] = self.out_size
姜永久 已提交
383
            self.check_dygraph = False
384 385 386
        elif self.out_size is not None:
            size_tensor = []
            for index, ele in enumerate(self.out_size):
387 388 389
                size_tensor.append(
                    ("x" + str(index), np.ones((1)).astype('int32') * ele)
                )
390
            self.inputs['SizeTensor'] = size_tensor
姜永久 已提交
391
            self.check_dygraph = False
392 393 394

        self.attrs['out_h'] = self.out_h
        self.attrs['out_w'] = self.out_w
395 396 397 398 399 400 401 402
        output_np = nearest_neighbor_interp_np(
            input_np,
            out_h,
            out_w,
            self.out_size,
            self.actual_shape,
            self.align_corners,
        )
403 404 405
        self.outputs = {'Out': output_np}

    def test_check_output(self):
姜永久 已提交
406
        self.check_output(check_dygraph=self.check_dygraph)
407 408

    def test_check_grad(self):
409
        self.check_grad(
姜永久 已提交
410
            ['X'], 'Out', in_place=True, check_dygraph=self.check_dygraph
411
        )
412 413 414

    def init_test_case(self):
        self.interp_method = 'nearest'
Z
zhupengyang 已提交
415
        self.input_shape = [2, 5, 4, 4]
416 417
        self.out_h = 3
        self.out_w = 3
418
        self.scale = 0.0
419 420 421 422 423 424 425 426 427 428 429
        self.out_size = [3, 3]
        self.align_corners = True


# out_size is a tensor list
class TestNearestInterp_attr_tensor_Case1(TestNearestInterpOp_attr_tensor):
    def init_test_case(self):
        self.interp_method = 'nearest'
        self.input_shape = [3, 3, 9, 6]
        self.out_h = 12
        self.out_w = 12
430
        self.scale = 0.0
431 432 433 434 435 436 437 438 439 440 441
        self.out_size = [8, 12]
        self.align_corners = True


# out_size is a 1-D tensor
class TestNearestInterp_attr_tensor_Case2(TestNearestInterpOp_attr_tensor):
    def init_test_case(self):
        self.interp_method = 'nearest'
        self.input_shape = [3, 2, 32, 16]
        self.out_h = 64
        self.out_w = 32
442
        self.scale = 0.0
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
        self.out_size = np.array([66, 40]).astype("int32")
        self.align_corners = True
        self.shape_by_1Dtensor = True


# scale is a 1-D tensor
class TestNearestInterp_attr_tensor_Case3(TestNearestInterpOp_attr_tensor):
    def init_test_case(self):
        self.interp_method = 'nearest'
        self.input_shape = [3, 2, 32, 16]
        self.out_h = 64
        self.out_w = 32
        self.scale = 2.0
        self.out_size = None
        self.align_corners = True
        self.scale_by_1Dtensor = True


461
if __name__ == "__main__":
462
    import paddle
463

464
    paddle.enable_static()
465
    unittest.main()