test_prroi_pool_op.py 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#   Copyright (c) 2019 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 numpy as np
import unittest
from py_precise_roi_pool import PyPrRoIPool
from op_test import OpTest
19
import paddle
20
import paddle.fluid as fluid
21
from paddle.fluid import Program, program_guard
22 23 24 25 26 27 28


class TestPRROIPoolOp(OpTest):
    def set_data(self):
        self.init_test_case()
        self.make_rois()
        self.prRoIPool = PyPrRoIPool()
29 30 31 32 33 34 35 36
        self.outs = self.prRoIPool.compute(
            self.x,
            self.rois,
            self.output_channels,
            self.spatial_scale,
            self.pooled_height,
            self.pooled_width,
        ).astype('float32')
37 38 39 40 41
        self.inputs = {'X': self.x, 'ROIs': (self.rois[:, 1:5], self.rois_lod)}
        self.attrs = {
            'output_channels': self.output_channels,
            'spatial_scale': self.spatial_scale,
            'pooled_height': self.pooled_height,
42
            'pooled_width': self.pooled_width,
43 44 45 46 47 48
        }
        self.outputs = {'Out': self.outs}

    def init_test_case(self):
        self.batch_size = 3
        self.channels = 3 * 2 * 2
49 50
        self.height = 12
        self.width = 16
51 52 53

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

54
        self.spatial_scale = 1.0 / 2.0
55
        self.output_channels = self.channels
56 57
        self.pooled_height = 4
        self.pooled_width = 4
58

59
        self.x = np.random.random(self.x_dim).astype('float32')
60 61 62 63 64 65 66

    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):
67
                x1 = np.random.uniform(
68 69
                    0, self.width // self.spatial_scale - self.pooled_width
                )
70
                y1 = np.random.uniform(
71 72 73 74 75 76 77 78 79
                    0, self.height // self.spatial_scale - self.pooled_height
                )

                x2 = np.random.uniform(
                    x1 + self.pooled_width, self.width // self.spatial_scale
                )
                y2 = np.random.uniform(
                    y1 + self.pooled_height, self.height // self.spatial_scale
                )
80 81 82
                roi = [bno, x1, y1, x2, y2]
                rois.append(roi)
        self.rois_num = len(rois)
83
        self.rois = np.array(rois).astype('float32')
84 85 86 87 88 89

    def setUp(self):
        self.op_type = 'prroi_pool'
        self.set_data()

    def test_check_output(self):
90
        self.check_output(check_eager=True)
91 92

    def test_backward(self):
93 94 95 96
        places = [fluid.CPUPlace()]
        if fluid.core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
97
            self.check_grad_with_place(place, ['X'], 'Out', check_eager=True)
98 99 100 101 102 103

    def run_net(self, place):
        with program_guard(Program(), Program()):
            x = fluid.layers.data(
                name="X",
                shape=[self.channels, self.height, self.width],
104 105 106 107 108
                dtype="float32",
            )
            rois = fluid.layers.data(
                name="ROIs", shape=[4], dtype="float32", lod_level=1
            )
109
            output = fluid.layers.prroi_pool(x, rois, 0.25, 2, 2)
110
            loss = paddle.mean(output)
111 112 113
            optimizer = fluid.optimizer.SGD(learning_rate=1e-3)
            optimizer.minimize(loss)
            input_x = fluid.create_lod_tensor(self.x, [], place)
114 115 116
            input_rois = fluid.create_lod_tensor(
                self.rois[:, 1:5], self.rois_lod, place
            )
117 118
            exe = fluid.Executor(place)
            exe.run(fluid.default_startup_program())
119 120 121
            exe.run(
                fluid.default_main_program(), {'X': input_x, "ROIs": input_rois}
            )
122 123 124 125 126 127 128 129 130 131

    def test_net(self):
        places = [fluid.CPUPlace()]
        if fluid.core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
            self.run_net(place)

    def test_errors(self):
        with program_guard(Program(), Program()):
132 133 134 135 136 137
            x = fluid.layers.data(
                name="x", shape=[245, 30, 30], dtype="float32"
            )
            rois = fluid.layers.data(
                name="rois", shape=[4], dtype="float32", lod_level=1
            )
138
            # spatial_scale must be float type
139 140 141
            self.assertRaises(
                TypeError, fluid.layers.prroi_pool, x, rois, 2, 7, 7
            )
142
            # pooled_height must be int type
143 144 145
            self.assertRaises(
                TypeError, fluid.layers.prroi_pool, x, rois, 0.25, 0.7, 7
            )
146
            # pooled_width must be int type
147 148 149
            self.assertRaises(
                TypeError, fluid.layers.prroi_pool, x, rois, 0.25, 7, 0.7
            )
150 151 152 153 154 155 156


class TestPRROIPoolOpTensorRoIs(OpTest):
    def set_data(self):
        self.init_test_case()
        self.make_rois()
        self.prRoIPool = PyPrRoIPool()
157 158 159 160 161 162 163 164
        self.outs = self.prRoIPool.compute(
            self.x,
            self.rois,
            self.output_channels,
            self.spatial_scale,
            self.pooled_height,
            self.pooled_width,
        ).astype('float32')
165 166 167 168 169

        self.rois_index = np.array(self.rois_lod).reshape([-1]).astype(np.int64)
        self.inputs = {
            'X': self.x,
            'ROIs': self.rois[:, 1:5],
170
            'BatchRoINums': self.rois_index,
171 172 173 174 175
        }
        self.attrs = {
            'output_channels': self.output_channels,
            'spatial_scale': self.spatial_scale,
            'pooled_height': self.pooled_height,
176
            'pooled_width': self.pooled_width,
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        }
        self.outputs = {'Out': self.outs}

    def init_test_case(self):
        self.batch_size = 3
        self.channels = 3 * 2 * 2
        self.height = 12
        self.width = 16

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

        self.spatial_scale = 1.0 / 2.0
        self.output_channels = self.channels
        self.pooled_height = 4
        self.pooled_width = 4

193
        self.x = np.random.random(self.x_dim).astype('float32')
194 195 196 197 198 199 200 201

    def make_rois(self):
        rois = []
        self.rois_lod = []
        for bno in range(self.batch_size):
            self.rois_lod.append(bno + 1)
            for i in range(bno + 1):
                x1 = np.random.uniform(
202 203
                    0, self.width // self.spatial_scale - self.pooled_width
                )
204
                y1 = np.random.uniform(
205 206 207 208 209 210 211 212 213
                    0, self.height // self.spatial_scale - self.pooled_height
                )

                x2 = np.random.uniform(
                    x1 + self.pooled_width, self.width // self.spatial_scale
                )
                y2 = np.random.uniform(
                    y1 + self.pooled_height, self.height // self.spatial_scale
                )
214 215 216
                roi = [bno, x1, y1, x2, y2]
                rois.append(roi)
        self.rois_num = len(rois)
217
        self.rois = np.array(rois).astype('float32')
218 219 220 221 222 223

    def setUp(self):
        self.op_type = 'prroi_pool'
        self.set_data()

    def test_check_output(self):
224
        self.check_output(check_eager=True)
225 226 227 228 229 230

    def test_backward(self):
        places = [fluid.CPUPlace()]
        if fluid.core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
231
            self.check_grad_with_place(place, ['X'], 'Out', check_eager=True)
232 233 234 235 236 237

    def run_net(self, place):
        with program_guard(Program(), Program()):
            x = fluid.layers.data(
                name="X",
                shape=[self.channels, self.height, self.width],
238 239
                dtype="float32",
            )
240
            rois = fluid.layers.data(name="ROIs", shape=[4], dtype="float32")
241 242 243 244 245 246
            rois_index = fluid.layers.data(
                name='rois_idx', shape=[], dtype="int64"
            )
            output = fluid.layers.prroi_pool(
                x, rois, 0.25, 2, 2, batch_roi_nums=rois_index
            )
247
            loss = paddle.mean(output)
248 249 250 251
            optimizer = fluid.optimizer.SGD(learning_rate=1e-3)
            optimizer.minimize(loss)
            exe = fluid.Executor(place)
            exe.run(fluid.default_startup_program())
252 253 254 255 256 257 258 259
            exe.run(
                fluid.default_main_program(),
                {
                    'X': self.x,
                    "ROIs": self.rois[:, 1:5],
                    "rois_idx": self.rois_index,
                },
            )
260 261 262 263 264 265 266 267 268 269

    def test_net(self):
        places = [fluid.CPUPlace()]
        if fluid.core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
            self.run_net(place)

    def test_errors(self):
        with program_guard(Program(), Program()):
270 271 272 273 274 275
            x = fluid.layers.data(
                name="x", shape=[245, 30, 30], dtype="float32"
            )
            rois = fluid.layers.data(
                name="rois", shape=[4], dtype="float32", lod_level=1
            )
276
            # spatial_scale must be float type
277 278 279
            self.assertRaises(
                TypeError, fluid.layers.prroi_pool, x, rois, 2, 7, 7
            )
280
            # pooled_height must be int type
281 282 283
            self.assertRaises(
                TypeError, fluid.layers.prroi_pool, x, rois, 0.25, 0.7, 7
            )
284
            # pooled_width must be int type
285 286 287
            self.assertRaises(
                TypeError, fluid.layers.prroi_pool, x, rois, 0.25, 7, 0.7
            )
288

289
            def test_bad_x():
290 291 292 293 294 295 296 297 298 299 300 301 302
                x = fluid.layers.data(
                    name='data1',
                    shape=[2, 3, 16, 16],
                    dtype='int64',
                    append_batch_size=False,
                )
                label = fluid.layers.data(
                    name='label1',
                    shape=[2, 4],
                    dtype='float32',
                    lod_level=1,
                    append_batch_size=False,
                )
303 304 305 306 307
                output = fluid.layers.prroi_pool(x, label, 0.25, 2, 2)

            self.assertRaises(TypeError, test_bad_x)

            def test_bad_y():
308 309 310 311 312 313
                x = fluid.layers.data(
                    name='data2',
                    shape=[2, 3, 16, 16],
                    dtype='float32',
                    append_batch_size=False,
                )
314 315 316 317 318
                label = [[1, 2, 3, 4], [2, 3, 4, 5]]
                output = fluid.layers.prroi_pool(x, label, 0.25, 2, 2)

            self.assertRaises(TypeError, test_bad_y)

319 320

if __name__ == '__main__':
321
    import paddle
322

323
    paddle.enable_static()
324
    unittest.main()