test_psroi_pool_op.py 13.3 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.

from __future__ import print_function

17
import paddle
18 19 20 21 22 23
import math
import numpy as np
import unittest
from op_test import OpTest


24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
def calc_psroi_pool(x, rois, rois_num_per_img, output_channels, spatial_scale,
                    pooled_height, pooled_width):
    """
    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
        roi_end_w = (round(roi[2]) + 1.) * spatial_scale
        roi_end_h = (round(roi[3]) + 1.) * spatial_scale

        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(
                        math.floor(float(ph) * bin_size_h + roi_start_h))
                    wstart = int(
                        math.floor(float(pw) * bin_size_w + roi_start_w))
                    hend = int(
                        math.ceil(float(ph + 1) * bin_size_h + roi_start_h))
                    wend = int(
                        math.ceil(float(pw + 1) * bin_size_w + roi_start_w))
                    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)
                    out_sum = 0.
                    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)
                    out_data[i, c, ph, pw] = 0. if is_empty else (
                        out_sum / float(bin_area))
    return out_data


87 88
class TestPSROIPoolOp(OpTest):
    def set_data(self):
89
        paddle.enable_static()
90 91
        self.init_test_case()
        self.make_rois()
92 93 94 95 96 97
        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')
        self.inputs = {
            'X': self.x,
Z
zyfncg 已提交
98 99
            'ROIs': (self.rois_with_batch_id[:, 1:5], self.rois_lod),
            'RoisNum': self.boxes_num
100
        }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        self.attrs = {
            'output_channels': self.output_channels,
            'spatial_scale': self.spatial_scale,
            'pooled_height': self.pooled_height,
            'pooled_width': self.pooled_width
        }
        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

122
        self.x = np.random.random(self.x_dim).astype('float64')
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    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(
                    0, self.width // self.spatial_scale - self.pooled_width)
                y1 = np.random.random_integers(
                    0, self.height // self.spatial_scale - self.pooled_height)

                x2 = np.random.random_integers(x1 + self.pooled_width,
                                               self.width // self.spatial_scale)
                y2 = np.random.random_integers(
                    y1 + self.pooled_height, self.height // self.spatial_scale)
                roi = [bno, x1, y1, x2, y2]
                rois.append(roi)
        self.rois_num = len(rois)
142 143 144 145
        self.rois_with_batch_id = np.array(rois).astype('float64')
        self.boxes = self.rois_with_batch_id[:, 1:]
        self.boxes_num = np.array(
            [bno + 1 for bno in range(self.batch_size)]).astype('int32')
146 147 148

    def setUp(self):
        self.op_type = 'psroi_pool'
Z
zyfncg 已提交
149
        self.python_api = lambda x, boxes, boxes_num, pooled_height, pooled_width, output_channels, spatial_scale: paddle.vision.ops.psroi_pool(x, boxes, boxes_num, (pooled_height, pooled_width), spatial_scale)
150 151 152
        self.set_data()

    def test_check_output(self):
Z
zyfncg 已提交
153
        self.check_output(check_eager=True)
154 155

    def test_check_grad(self):
Z
zyfncg 已提交
156
        self.check_grad(['X'], 'Out', check_eager=True)
157 158


159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
class TestPSROIPoolDynamicFunctionAPI(unittest.TestCase):
    def setUp(self):
        self.x = np.random.random([2, 490, 28, 28]).astype(np.float32)
        self.boxes = np.array(
            [[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]]).astype(np.float32)
        self.boxes_num = np.array([1, 2]).astype(np.int32)

    def test_output_size(self):
        def test_output_size_is_int():
            output_size = 7
            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)
            self.assertTrue(np.allclose(out, expect_out))

        def test_output_size_is_tuple():
            output_size = (7, 7)
            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)
            self.assertTrue(np.allclose(out, expect_out))

        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'),
                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)
            self.assertTrue(np.allclose(out, expect_out))

        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)
        self.boxes = np.array([[3, 5, 6, 13], [7, 4, 22, 18], [4, 5, 7, 10],
                               [5, 3, 25, 21]]).astype(np.float32)
        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)
            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)
            self.assertTrue(np.allclose(out, expect_out))

        def test_output_size_is_tuple():
            psroi_pool_module = paddle.vision.ops.PSRoIPool(8, 1.1)
            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)
            self.assertTrue(np.allclose(out, expect_out))

        def test_dytype_is_float64():
            psroi_pool_module = paddle.vision.ops.PSRoIPool(8, 1.1)
            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)
            self.assertTrue(np.allclose(out, expect_out))

        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(
            [[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]], 'float32')

    def test_errors(self):
        def test_boxes_num_nums_error():
            boxes_num = paddle.to_tensor([1, 5], 'int32')
            out = paddle.vision.ops.psroi_pool(
                self.x, self.boxes, boxes_num, output_size=7)

        self.assertRaises(ValueError, test_boxes_num_nums_error)

        def test_boxes_num_length_error():
            boxes_num = paddle.to_tensor([1, 1, 1], 'int32')
            out = paddle.vision.ops.psroi_pool(
                self.x, self.boxes, boxes_num, output_size=7)

        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(
            [[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]], 'float32')
        self.output_size = 4

    def test_errors(self):
        def test_channel_error():
            boxes_num = paddle.to_tensor([2, 1], 'int32')
            out = paddle.vision.ops.psroi_pool(self.x, self.boxes, boxes_num,
                                               self.output_size)

        self.assertRaises(ValueError, test_channel_error)


class TestPSROIPoolStaticAPI(unittest.TestCase):
    def setUp(self):
        paddle.enable_static()
        self.x_placeholder = paddle.static.data(
            name='x', shape=[2, 490, 28, 28])
        self.x = np.random.random([2, 490, 28, 28]).astype(np.float32)
        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)
        self.boxes_num = np.array([1, 2]).astype(np.int32)

    def test_function_in_static(self):
        output_size = 7
        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)
        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)
            boxes_lod_data = paddle.fluid.create_lod_tensor(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])
            self.assertTrue(np.allclose(out_res, expect_out))


329 330
if __name__ == '__main__':
    unittest.main()