test_psroi_pool_op.py 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#   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 math
import unittest
17 18

import numpy as np
19 20
from op_test import OpTest

21 22
import paddle

23

24 25 26 27 28 29 30 31 32
def calc_psroi_pool(
    x,
    rois,
    rois_num_per_img,
    output_channels,
    spatial_scale,
    pooled_height,
    pooled_width,
):
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    """
    Psroi_pool implemented by Numpy.
    x: 4-D as (N, C, H, W),
    rois: 2-D as [[x1, y1, x2, y2], ...],
    rois_num_per_img: 1-D as [nums_of_batch_0, nums_of_batch_1,  ...]
    """
    output_shape = (len(rois), output_channels, pooled_height, pooled_width)
    out_data = np.zeros(output_shape)
    batch_id = 0
    rois_num_id = 0
    rois_num_left = rois_num_per_img[rois_num_id]
    for i in range(len(rois)):
        roi = rois[i]
        roi_batch_id = batch_id
        rois_num_left -= 1
        if rois_num_left == 0:
            rois_num_id += 1
            if rois_num_id < len(rois_num_per_img):
                rois_num_left = rois_num_per_img[rois_num_id]
            batch_id += 1
        roi_start_w = round(roi[0]) * spatial_scale
        roi_start_h = round(roi[1]) * spatial_scale
55 56
        roi_end_w = (round(roi[2]) + 1.0) * spatial_scale
        roi_end_h = (round(roi[3]) + 1.0) * spatial_scale
57 58 59 60 61 62 63 64 65 66 67 68 69

        roi_height = max(roi_end_h - roi_start_h, 0.1)
        roi_width = max(roi_end_w - roi_start_w, 0.1)

        bin_size_h = roi_height / float(pooled_height)
        bin_size_w = roi_width / float(pooled_width)

        x_i = x[roi_batch_id]

        for c in range(output_channels):
            for ph in range(pooled_height):
                for pw in range(pooled_width):
                    hstart = int(
70 71
                        math.floor(float(ph) * bin_size_h + roi_start_h)
                    )
72
                    wstart = int(
73 74
                        math.floor(float(pw) * bin_size_w + roi_start_w)
                    )
75
                    hend = int(
76 77
                        math.ceil(float(ph + 1) * bin_size_h + roi_start_h)
                    )
78
                    wend = int(
79 80
                        math.ceil(float(pw + 1) * bin_size_w + roi_start_w)
                    )
81 82 83 84 85 86 87
                    hstart = min(max(hstart, 0), x.shape[2])
                    hend = min(max(hend, 0), x.shape[2])
                    wstart = min(max(wstart, 0), x.shape[3])
                    wend = min(max(wend, 0), x.shape[3])

                    c_in = (c * pooled_height + ph) * pooled_width + pw
                    is_empty = (hend <= hstart) or (wend <= wstart)
88
                    out_sum = 0.0
89 90 91 92
                    for ih in range(hstart, hend):
                        for iw in range(wstart, wend):
                            out_sum += x_i[c_in, ih, iw]
                    bin_area = (hend - hstart) * (wend - wstart)
93 94 95
                    out_data[i, c, ph, pw] = (
                        0.0 if is_empty else (out_sum / float(bin_area))
                    )
96 97 98
    return out_data


99 100
class TestPSROIPoolOp(OpTest):
    def set_data(self):
101
        paddle.enable_static()
102 103
        self.init_test_case()
        self.make_rois()
104 105 106 107 108 109 110 111 112
        self.outs = calc_psroi_pool(
            self.x,
            self.boxes,
            self.boxes_num,
            self.output_channels,
            self.spatial_scale,
            self.pooled_height,
            self.pooled_width,
        ).astype('float64')
113 114
        self.inputs = {
            'X': self.x,
Z
zyfncg 已提交
115
            'ROIs': (self.rois_with_batch_id[:, 1:5], self.rois_lod),
116
            'RoisNum': self.boxes_num,
117
        }
118 119 120 121
        self.attrs = {
            'output_channels': self.output_channels,
            'spatial_scale': self.spatial_scale,
            'pooled_height': self.pooled_height,
122
            'pooled_width': self.pooled_width,
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        }
        self.outputs = {'Out': self.outs}

    def init_test_case(self):
        self.batch_size = 3
        self.channels = 3 * 2 * 2
        self.height = 6
        self.width = 4

        self.x_dim = [self.batch_size, self.channels, self.height, self.width]

        self.spatial_scale = 1.0 / 4.0
        self.output_channels = 3
        self.pooled_height = 2
        self.pooled_width = 2

139
        self.x = np.random.random(self.x_dim).astype('float64')
140 141 142 143 144 145 146 147

    def make_rois(self):
        rois = []
        self.rois_lod = [[]]
        for bno in range(self.batch_size):
            self.rois_lod[0].append(bno + 1)
            for i in range(bno + 1):
                x1 = np.random.random_integers(
148 149
                    0, self.width // self.spatial_scale - self.pooled_width
                )
150
                y1 = np.random.random_integers(
151 152
                    0, self.height // self.spatial_scale - self.pooled_height
                )
153

154 155 156
                x2 = np.random.random_integers(
                    x1 + self.pooled_width, self.width // self.spatial_scale
                )
157
                y2 = np.random.random_integers(
158 159
                    y1 + self.pooled_height, self.height // self.spatial_scale
                )
160 161 162
                roi = [bno, x1, y1, x2, y2]
                rois.append(roi)
        self.rois_num = len(rois)
163 164
        self.rois_with_batch_id = np.array(rois).astype('float64')
        self.boxes = self.rois_with_batch_id[:, 1:]
165 166 167
        self.boxes_num = np.array(
            [bno + 1 for bno in range(self.batch_size)]
        ).astype('int32')
168 169 170

    def setUp(self):
        self.op_type = 'psroi_pool'
171
        self.python_api = lambda x, boxes, boxes_num, pooled_height, pooled_width, output_channels, spatial_scale: paddle.vision.ops.psroi_pool(
172 173
            x, boxes, boxes_num, (pooled_height, pooled_width), spatial_scale
        )
174 175 176
        self.set_data()

    def test_check_output(self):
Z
zyfncg 已提交
177
        self.check_output(check_eager=True)
178 179

    def test_check_grad(self):
Z
zyfncg 已提交
180
        self.check_grad(['X'], 'Out', check_eager=True)
181 182


183 184 185
class TestPSROIPoolDynamicFunctionAPI(unittest.TestCase):
    def setUp(self):
        self.x = np.random.random([2, 490, 28, 28]).astype(np.float32)
186 187 188
        self.boxes = np.array(
            [[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]]
        ).astype(np.float32)
189 190 191 192 193
        self.boxes_num = np.array([1, 2]).astype(np.int32)

    def test_output_size(self):
        def test_output_size_is_int():
            output_size = 7
194 195 196 197 198 199 200 201 202
            out = paddle.vision.ops.psroi_pool(
                paddle.to_tensor(self.x),
                paddle.to_tensor(self.boxes),
                paddle.to_tensor(self.boxes_num),
                output_size,
            ).numpy()
            expect_out = calc_psroi_pool(
                self.x, self.boxes, self.boxes_num, 10, 1.0, 7, 7
            )
203
            np.testing.assert_allclose(out, expect_out, rtol=1e-05)
204 205 206

        def test_output_size_is_tuple():
            output_size = (7, 7)
207 208 209 210 211 212 213 214 215
            out = paddle.vision.ops.psroi_pool(
                paddle.to_tensor(self.x),
                paddle.to_tensor(self.boxes),
                paddle.to_tensor(self.boxes_num),
                output_size,
            ).numpy()
            expect_out = calc_psroi_pool(
                self.x, self.boxes, self.boxes_num, 10, 1.0, 7, 7
            )
216
            np.testing.assert_allclose(out, expect_out, rtol=1e-05)
217 218 219 220 221 222

        def test_dytype_is_float64():
            output_size = (7, 7)
            out = paddle.vision.ops.psroi_pool(
                paddle.to_tensor(self.x, 'float64'),
                paddle.to_tensor(self.boxes, 'float64'),
223 224 225 226 227 228
                paddle.to_tensor(self.boxes_num, 'int32'),
                output_size,
            ).numpy()
            expect_out = calc_psroi_pool(
                self.x, self.boxes, self.boxes_num, 10, 1.0, 7, 7
            )
229
            np.testing.assert_allclose(out, expect_out, rtol=1e-05)
230 231 232 233 234 235 236 237 238 239 240 241 242 243

        places = ['cpu']
        if paddle.fluid.core.is_compiled_with_cuda():
            places.append('gpu')
        for place in places:
            paddle.set_device(place)
            test_output_size_is_int()
            test_output_size_is_tuple()
            test_dytype_is_float64()


class TestPSROIPoolDynamicClassAPI(unittest.TestCase):
    def setUp(self):
        self.x = np.random.random([2, 128, 32, 32]).astype(np.float32)
244 245 246
        self.boxes = np.array(
            [[3, 5, 6, 13], [7, 4, 22, 18], [4, 5, 7, 10], [5, 3, 25, 21]]
        ).astype(np.float32)
247 248 249 250 251
        self.boxes_num = np.array([2, 2]).astype(np.int32)

    def test_output_size(self):
        def test_output_size_is_int():
            psroi_module = paddle.vision.ops.PSRoIPool(8, 1.1)
252 253 254 255 256 257 258 259
            out = psroi_module(
                paddle.to_tensor(self.x),
                paddle.to_tensor(self.boxes),
                paddle.to_tensor(self.boxes_num),
            ).numpy()
            expect_out = calc_psroi_pool(
                self.x, self.boxes, self.boxes_num, 2, 1.1, 8, 8
            )
260
            np.testing.assert_allclose(out, expect_out, rtol=1e-05)
261 262 263

        def test_output_size_is_tuple():
            psroi_pool_module = paddle.vision.ops.PSRoIPool(8, 1.1)
264 265 266 267 268 269 270 271
            out = psroi_pool_module(
                paddle.to_tensor(self.x),
                paddle.to_tensor(self.boxes),
                paddle.to_tensor(self.boxes_num),
            ).numpy()
            expect_out = calc_psroi_pool(
                self.x, self.boxes, self.boxes_num, 2, 1.1, 8, 8
            )
272
            np.testing.assert_allclose(out, expect_out, rtol=1e-05)
273 274 275

        def test_dytype_is_float64():
            psroi_pool_module = paddle.vision.ops.PSRoIPool(8, 1.1)
276 277 278 279 280 281 282 283
            out = psroi_pool_module(
                paddle.to_tensor(self.x, 'float64'),
                paddle.to_tensor(self.boxes, 'float64'),
                paddle.to_tensor(self.boxes_num, 'int32'),
            ).numpy()
            expect_out = calc_psroi_pool(
                self.x, self.boxes, self.boxes_num, 2, 1.1, 8, 8
            )
284
            np.testing.assert_allclose(out, expect_out, rtol=1e-05)
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

        paddle.disable_static()
        places = ['cpu']
        if paddle.fluid.core.is_compiled_with_cuda():
            places.append('gpu')
        for place in places:
            paddle.set_device(place)
            test_output_size_is_int()
            test_output_size_is_tuple()
            test_dytype_is_float64()


class TestPSROIPoolBoxesNumError(unittest.TestCase):
    def setUp(self):
        paddle.disable_static()
        self.x = paddle.uniform([2, 490, 28, 28], dtype='float32')
        self.boxes = paddle.to_tensor(
302 303
            [[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]], 'float32'
        )
304 305 306 307

    def test_errors(self):
        def test_boxes_num_nums_error():
            boxes_num = paddle.to_tensor([1, 5], 'int32')
308 309 310
            out = paddle.vision.ops.psroi_pool(
                self.x, self.boxes, boxes_num, output_size=7
            )
311 312 313 314 315

        self.assertRaises(ValueError, test_boxes_num_nums_error)

        def test_boxes_num_length_error():
            boxes_num = paddle.to_tensor([1, 1, 1], 'int32')
316 317 318
            out = paddle.vision.ops.psroi_pool(
                self.x, self.boxes, boxes_num, output_size=7
            )
319 320 321 322 323 324 325 326 327

        self.assertRaises(ValueError, test_boxes_num_length_error)


class TestPSROIPoolChannelError(unittest.TestCase):
    def setUp(self):
        paddle.disable_static()
        self.x = paddle.uniform([2, 490, 28, 28], dtype='float32')
        self.boxes = paddle.to_tensor(
328 329
            [[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]], 'float32'
        )
330 331 332 333 334
        self.output_size = 4

    def test_errors(self):
        def test_channel_error():
            boxes_num = paddle.to_tensor([2, 1], 'int32')
335 336 337
            out = paddle.vision.ops.psroi_pool(
                self.x, self.boxes, boxes_num, self.output_size
            )
338 339 340 341

        self.assertRaises(ValueError, test_channel_error)


342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
class TestPSROIPoolZeroDivError(unittest.TestCase):
    def setUp(self):
        paddle.disable_static()
        self.x = paddle.uniform([2, 490, 28, 28], dtype='float32')
        self.boxes = paddle.to_tensor(
            [[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]], dtype='float32'
        )
        self.boxes_num = paddle.to_tensor([1, 2], dtype='int32')

    def test_errors(self):
        def test_zero_div_error():
            paddle.vision.ops.psroi_pool(self.x, self.boxes, self.boxes_num, 0)

        self.assertRaises(ValueError, test_zero_div_error)


358 359 360
class TestPSROIPoolStaticAPI(unittest.TestCase):
    def setUp(self):
        paddle.enable_static()
361 362 363
        self.x_placeholder = paddle.static.data(
            name='x', shape=[2, 490, 28, 28]
        )
364
        self.x = np.random.random([2, 490, 28, 28]).astype(np.float32)
365 366 367 368 369 370
        self.boxes_placeholder = paddle.static.data(
            name='boxes', shape=[3, 4], lod_level=1
        )
        self.boxes = np.array(
            [[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]]
        ).astype(np.float32)
371 372 373 374
        self.boxes_num = np.array([1, 2]).astype(np.int32)

    def test_function_in_static(self):
        output_size = 7
375 376 377 378 379 380 381 382 383
        out = paddle.vision.ops.psroi_pool(
            self.x_placeholder,
            self.boxes_placeholder,
            self.boxes_num,
            output_size,
        )
        expect_out = calc_psroi_pool(
            self.x, self.boxes, self.boxes_num, 10, 1.0, 7, 7
        )
384 385 386 387 388
        places = [paddle.CPUPlace()]
        if paddle.fluid.core.is_compiled_with_cuda():
            places.append(paddle.CUDAPlace(0))
        for place in places:
            exe = paddle.static.Executor(place)
389
            boxes_lod_data = paddle.fluid.create_lod_tensor(
390 391 392 393 394 395 396
                self.boxes, [[1, 2]], place
            )
            (out_res,) = exe.run(
                paddle.static.default_main_program(),
                feed={'x': self.x, 'boxes': boxes_lod_data},
                fetch_list=[out.name],
            )
397
            np.testing.assert_allclose(out_res, expect_out, rtol=1e-05)
398 399


400 401
if __name__ == '__main__':
    unittest.main()