test_bilinear_interp_op.py 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#   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.

from __future__ import print_function

import unittest
import numpy as np
from op_test import OpTest
import paddle.fluid.core as core


23 24 25 26 27 28 29
def bilinear_interp_np(input,
                       out_h,
                       out_w,
                       out_size=None,
                       actual_shape=None,
                       align_corners=True,
                       align_mode=0):
30 31 32 33
    """bilinear interpolation implement in shape [N, C, H, W]"""
    if out_size is not None:
        out_h = out_size[0]
        out_w = out_size[1]
34 35 36
    if actual_shape is not None:
        out_h = actual_shape[0]
        out_w = actual_shape[1]
37
    batch_size, channel, in_h, in_w = input.shape
38 39 40

    ratio_h = ratio_w = 0.0
    if (align_corners and out_h > 1):
41 42
        ratio_h = (in_h - 1.0) / (out_h - 1.0)
    else:
43 44
        ratio_h = 1.0 * in_h / out_h
    if (align_corners and out_w > 1):
45 46
        ratio_w = (in_w - 1.0) / (out_w - 1.0)
    else:
47
        ratio_w = 1.0 * in_w / out_w
48 49

    out = np.zeros((batch_size, channel, out_h, out_w))
50

51
    for i in range(out_h):
52 53 54 55 56
        if (align_mode == 0 and not align_corners):
            h = int(ratio_h * (i + 0.5) - 0.5)
        else:
            h = int(ratio_h * i)

T
tink2123 已提交
57
        h = max(0, h)
58
        hid = 1 if h < in_h - 1 else 0
59 60 61 62
        if (align_mode == 0 and not align_corners):
            h1lambda = ratio_h * (i + 0.5) - 0.5 - h
        else:
            h1lambda = ratio_h * i - h
63 64
        h2lambda = 1.0 - h1lambda
        for j in range(out_w):
65 66 67 68
            if (align_mode == 0 and not align_corners):
                w = int(ratio_w * (j + 0.5) - 0.5)
            else:
                w = int(ratio_w * j)
T
tink2123 已提交
69
            w = max(0, w)
70
            wid = 1 if w < in_w - 1 else 0
71 72 73 74
            if (align_mode == 0 and not align_corners):
                w1lambda = ratio_w * (j + 0.5) - 0.5 - w
            else:
                w1lambda = ratio_w * j - w
75 76 77 78 79 80 81 82 83
            w2lambda = 1.0 - w1lambda

            out[:, :, i, j] = h2lambda*(w2lambda*input[:, :, h, w] +
                                        w1lambda*input[:, :, h, w+wid]) + \
                h1lambda*(w2lambda*input[:, :, h+hid, w] +
                          w1lambda*input[:, :, h+hid, w+wid])
    return out.astype(input.dtype)


84
class TestBilinearInterpOp(OpTest):
85 86
    def setUp(self):
        self.out_size = None
87
        self.actual_shape = None
88
        self.init_test_case()
89
        self.op_type = "bilinear_interp"
90 91
        input_np = np.random.random(self.input_shape).astype("float32")

92
        output_np = bilinear_interp_np(input_np, self.out_h, self.out_w,
93 94
                                       self.out_size, self.actual_shape,
                                       self.align_corners, self.align_mode)
95 96 97
        self.inputs = {'X': input_np}
        if self.out_size is not None:
            self.inputs['OutSize'] = self.out_size
98 99
        if self.actual_shape is not None:
            self.inputs['OutSize'] = self.actual_shape
100 101 102
        self.attrs = {
            'out_h': self.out_h,
            'out_w': self.out_w,
103 104 105
            'interp_method': self.interp_method,
            'align_corners': self.align_corners,
            'align_mode': self.align_mode
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        }
        self.outputs = {'Out': output_np}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out', in_place=True)

    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [2, 3, 4, 4]
        self.out_h = 2
        self.out_w = 2
        self.out_size = np.array([3, 3]).astype("int32")
T
tink2123 已提交
121 122
        self.align_corners = True
        self.align_mode = 1
123 124


125
class TestBilinearInterpCase1(TestBilinearInterpOp):
126 127 128 129 130
    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [4, 1, 7, 8]
        self.out_h = 1
        self.out_w = 1
T
tink2123 已提交
131 132
        self.align_corners = True
        self.align_mode = 1
133 134


135
class TestBilinearInterpCase2(TestBilinearInterpOp):
136 137 138 139 140
    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [3, 3, 9, 6]
        self.out_h = 12
        self.out_w = 12
T
tink2123 已提交
141 142
        self.align_corners = True
        self.align_mode = 1
143 144


145
class TestBilinearInterpCase3(TestBilinearInterpOp):
146 147 148 149 150
    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [1, 1, 128, 64]
        self.out_h = 64
        self.out_w = 128
T
tink2123 已提交
151 152
        self.align_corners = True
        self.align_mode = 1
153 154


155
class TestBilinearInterpCase4(TestBilinearInterpOp):
156 157 158 159 160 161
    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [4, 1, 7, 8]
        self.out_h = 1
        self.out_w = 1
        self.out_size = np.array([2, 2]).astype("int32")
T
tink2123 已提交
162 163
        self.align_corners = True
        self.align_mode = 1
164 165


166
class TestBilinearInterpCase5(TestBilinearInterpOp):
167 168 169 170 171 172
    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [3, 3, 9, 6]
        self.out_h = 12
        self.out_w = 12
        self.out_size = np.array([11, 11]).astype("int32")
T
tink2123 已提交
173 174
        self.align_corners = True
        self.align_mode = 1
175 176


177
class TestBilinearInterpCase6(TestBilinearInterpOp):
178 179 180 181 182 183
    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [1, 1, 128, 64]
        self.out_h = 64
        self.out_w = 128
        self.out_size = np.array([65, 129]).astype("int32")
T
tink2123 已提交
184 185
        self.align_corners = True
        self.align_mode = 1
186 187


188
class TestBilinearInterpActualShape(TestBilinearInterpOp):
189 190 191 192 193 194
    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [3, 2, 32, 16]
        self.out_h = 64
        self.out_w = 32
        self.out_size = np.array([66, 40]).astype("int32")
T
tink2123 已提交
195 196
        self.align_corners = True
        self.align_mode = 1
197 198


199
class TestBilinearInterpOpUint8(OpTest):
200 201
    def setUp(self):
        self.out_size = None
202
        self.actual_shape = None
203
        self.init_test_case()
204
        self.op_type = "bilinear_interp"
205 206
        input_np = np.random.randint(
            low=0, high=256, size=self.input_shape).astype("uint8")
207
        output_np = bilinear_interp_np(input_np, self.out_h, self.out_w,
208 209
                                       self.out_size, self.actual_shape,
                                       self.align_corners, self.align_mode)
210 211 212 213 214 215
        self.inputs = {'X': input_np}
        if self.out_size is not None:
            self.inputs['OutSize'] = self.out_size
        self.attrs = {
            'out_h': self.out_h,
            'out_w': self.out_w,
216 217 218
            'interp_method': self.interp_method,
            'align_corners': self.align_corners,
            'align_mode': self.align_mode
219 220 221 222 223 224 225 226 227 228 229
        }
        self.outputs = {'Out': output_np}

    def test_check_output(self):
        self.check_output_with_place(place=core.CPUPlace(), atol=1)

    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [1, 3, 9, 6]
        self.out_h = 10
        self.out_w = 9
T
tink2123 已提交
230 231
        self.align_corners = True
        self.align_mode = 1
232 233


234
class TestBilinearInterpCase1Uint8(TestBilinearInterpOpUint8):
235 236 237 238 239
    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [2, 3, 128, 64]
        self.out_h = 120
        self.out_w = 50
T
tink2123 已提交
240 241
        self.align_corners = True
        self.align_mode = 1
242 243


244
class TestBilinearInterpCase2Uint8(TestBilinearInterpOpUint8):
245 246 247 248 249 250
    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [4, 1, 7, 8]
        self.out_h = 5
        self.out_w = 13
        self.out_size = np.array([6, 15]).astype("int32")
T
tink2123 已提交
251 252
        self.align_corners = True
        self.align_mode = 1
253 254 255 256 257


class TestBilinearInterpOtherMethod1(TestBilinearInterpOp):
    def set_align_mode(self):
        self.align_corners = False
T
tink2123 已提交
258
        self.align_mode = 1
259 260 261 262


class TestBilinearInterpWithMethod2(TestBilinearInterpOp):
    def set_align_mode(self):
T
tink2123 已提交
263 264
        self.align_corners = False
        self.align_mode = 0
265 266 267 268 269 270


class TestBilinearInterpWithMethod3(TestBilinearInterpOp):
    def set_align_mode(self):
        self.align_corners = True
        self.align_mode = 0
271 272 273 274


if __name__ == "__main__":
    unittest.main()