test_deformable_conv_op.py 15.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

15
import paddle
16 17 18
import unittest
import numpy as np
from op_test import OpTest
19 20 21
from paddle.fluid.framework import _test_eager_guard

paddle.enable_static()
22 23 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


def dmc_bilinear(data_im, height, width, h, w):
    h_low = int(np.floor(h))
    w_low = int(np.floor(w))
    h_high = h_low + 1
    w_high = w_low + 1

    lh = h - h_low
    lw = w - w_low
    hh = 1 - lh
    hw = 1 - lw

    v1 = 0
    if h_low >= 0 and w_low >= 0:
        v1 = data_im[h_low, w_low]
    v2 = 0
    if h_low >= 0 and w_high <= width - 1:
        v2 = data_im[h_low, w_high]
    v3 = 0
    if h_high <= height - 1 and w_low >= 0:
        v3 = data_im[h_high, w_low]
    v4 = 0
    if h_high <= height - 1 and w_high <= width - 1:
        v4 = data_im[h_high, w_high]

    w1, w2, w3, w4 = hh * hw, hh * lw, lh * hw, lh * lw
    val = w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4

    return val


def dconv_im2col_gemm(input, offset, mask, filter, group, conv_param):
    in_n, in_c, in_h, in_w = input.shape
    out_c, f_c, f_h, f_w = filter.shape

    assert offset.shape == (in_n, 2 * f_h * f_w, in_h, in_w)
    assert mask.shape == (in_n, f_h * f_w, in_h, in_w)
    assert f_c * group == in_c
    assert np.mod(out_c, group) == 0

63 64 65 66 67
    stride, pad, dilation = (
        conv_param['stride'],
        conv_param['pad'],
        conv_param['dilation'],
    )
68 69 70 71 72 73 74 75 76 77 78 79
    out_h = 1 + (in_h + 2 * pad[0] - (dilation[0] * (f_h - 1) + 1)) // stride[0]
    out_w = 1 + (in_w + 2 * pad[1] - (dilation[1] * (f_w - 1) + 1)) // stride[1]
    assert out_h == in_h
    assert out_w == in_w

    col_buffer = np.zeros((in_n, in_c * f_h * f_w, in_h * in_w))
    for n in range(in_n):
        for c in range(in_c):
            for h in range(out_h):
                for w in range(out_w):
                    for kh in range(f_h):
                        for kw in range(f_w):
80 81 82 83 84 85 86
                            offset_h_table = offset[n, ::2, h, w].reshape(
                                f_h, f_w
                            )
                            offset_w_table = offset[n, 1::2, h, w].reshape(
                                f_h, f_w
                            )
                            mask_table = mask[n, :, h, w].reshape(f_h, f_w)
87 88 89
                            offset_h = offset_h_table[kh, kw]
                            offset_w = offset_w_table[kh, kw]
                            val = 0
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                            im_h = (
                                h * stride[0]
                                + kh * dilation[0]
                                + offset_h
                                - pad[0]
                            )
                            im_w = (
                                w * stride[0]
                                + kw * dilation[0]
                                + offset_w
                                - pad[1]
                            )
                            if (
                                im_h > -1
                                and im_w > -1
                                and im_h < in_h
                                and im_w < in_h
                            ):
                                val = dmc_bilinear(
                                    input[n, c], in_h, in_w, im_h, im_w
                                )
111
                            val_out = val * mask_table[kh, kw]
112 113 114
                            col_buffer[
                                n, c * f_h * f_w + kh * f_w + kw, h * in_w + w
                            ] = val_out
115 116 117 118

    out = np.zeros((in_n, group, int(out_c // group), out_h * out_w))
    weight = filter.reshape(group, int(out_c // group), f_c * f_h * f_w)
    col_buffer = col_buffer.reshape(
119 120
        (in_n, group, int(in_c // group * f_h * f_w), in_h * in_w)
    )
121 122 123 124 125 126 127
    for n in range(in_n):
        for g in range(group):
            out[n, g] = np.matmul(weight[g], col_buffer[n, g])
    out = out.reshape(in_n, out_c, out_h, out_w)
    return out


128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
def deform_conv2d_wrapper(
    x,
    offset,
    weight,
    mask=None,
    stride=1,
    padding=0,
    dilation=1,
    deformable_groups=1,
    groups=1,
    im2col_step=1,
):
    return paddle.vision.ops.deform_conv2d(
        x,
        offset,
        weight,
        None,
        stride,
        padding,
        dilation,
        deformable_groups,
        groups,
        mask,
    )
152 153


154 155
class TestModulatedDeformableConvOp(OpTest):
    def setUp(self):
156
        self.python_api = deform_conv2d_wrapper
157
        self.op_type = "deformable_conv"
158
        self.init_type()
159 160 161 162 163 164 165
        self.init_group()
        self.init_dilation()
        self.init_test_case()

        conv_param = {
            'stride': self.stride,
            'pad': self.pad,
166
            'dilation': self.dilations,
167 168 169 170 171 172 173
        }

        input = np.random.random(self.input_size).astype(self.dtype)
        offset = 10 * np.random.random(self.offset_size).astype(self.dtype)
        mask = 10 * np.random.random(self.mask_size).astype(self.dtype)
        filter = np.random.random(self.filter_size).astype(self.dtype)

174 175 176
        output = dconv_im2col_gemm(
            input, offset, mask, filter, self.groups, conv_param
        )
177 178 179 180 181 182
        output = output.astype(self.dtype)

        self.inputs = {
            'Input': OpTest.np_dtype_to_fluid_dtype(input),
            'Offset': OpTest.np_dtype_to_fluid_dtype(offset),
            'Mask': OpTest.np_dtype_to_fluid_dtype(mask),
183
            'Filter': OpTest.np_dtype_to_fluid_dtype(filter),
184 185 186 187 188 189 190 191 192 193 194 195
        }
        self.attrs = {
            'strides': self.stride,
            'paddings': self.pad,
            'groups': self.groups,
            'deformable_groups': self.deformable_groups,
            'im2col_step': self.im2col_step,
            'dilations': self.dilations,
        }
        self.outputs = {'Output': output}

    def test_check_output(self):
196
        self.check_output(check_eager=True)
197 198

    def test_check_grad(self):
199 200 201 202 203 204
        self.check_grad(
            {'Input', 'Offset', 'Mask', 'Filter'},
            'Output',
            max_relative_error=0.05,
            check_eager=True,
        )
205 206 207 208 209

    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.dilations = [1, 1]
210
        self.input_size = [2, 8, 4, 4]  # NCHW
211 212 213 214 215
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [4, f_c, 3, 3]
        self.im2col_step = 1
        self.deformable_groups = 1
216 217 218 219 220 221 222 223 224
        offset_c = (
            2
            * self.deformable_groups
            * self.filter_size[2]
            * self.filter_size[3]
        )
        mask_c = (
            self.deformable_groups * self.filter_size[2] * self.filter_size[3]
        )
225
        self.offset_size = [
226 227 228 229
            self.input_size[0],
            offset_c,
            self.input_size[2],
            self.input_size[3],
230 231
        ]
        self.mask_size = [
232 233 234 235
            self.input_size[0],
            mask_c,
            self.input_size[2],
            self.input_size[3],
236 237 238 239 240 241 242 243
        ]

    def init_dilation(self):
        self.dilations = [1, 1]

    def init_group(self):
        self.groups = 1

244 245 246
    def init_type(self):
        self.dtype = np.float32

247 248 249 250 251 252 253 254 255 256 257

class TestWithStride(TestModulatedDeformableConvOp):
    def init_test_case(self):
        self.pad = [3, 3]
        self.stride = [2, 2]
        self.input_size = [2, 3, 5, 5]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 3, 3]
        self.im2col_step = 1
        self.deformable_groups = 1
258 259 260 261 262 263 264 265 266
        offset_c = (
            2
            * self.deformable_groups
            * self.filter_size[2]
            * self.filter_size[3]
        )
        mask_c = (
            self.deformable_groups * self.filter_size[2] * self.filter_size[3]
        )
267
        self.offset_size = [
268 269 270 271
            self.input_size[0],
            offset_c,
            self.input_size[2],
            self.input_size[3],
272 273
        ]
        self.mask_size = [
274 275 276 277
            self.input_size[0],
            mask_c,
            self.input_size[2],
            self.input_size[3],
278 279 280 281 282 283 284
        ]


class TestWithDilation(TestModulatedDeformableConvOp):
    def init_test_case(self):
        self.pad = [2, 2]
        self.stride = [1, 1]
285
        self.input_size = [4, 3, 4, 4]  # NCHW
286 287 288 289 290
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 3, 3]
        self.im2col_step = 1
        self.deformable_groups = 1
291 292 293 294 295 296 297 298 299
        offset_c = (
            2
            * self.deformable_groups
            * self.filter_size[2]
            * self.filter_size[3]
        )
        mask_c = (
            self.deformable_groups * self.filter_size[2] * self.filter_size[3]
        )
300
        self.offset_size = [
301 302 303 304
            self.input_size[0],
            offset_c,
            self.input_size[2],
            self.input_size[3],
305 306
        ]
        self.mask_size = [
307 308 309 310
            self.input_size[0],
            mask_c,
            self.input_size[2],
            self.input_size[3],
311 312 313 314 315 316
        ]

    def init_dilation(self):
        self.dilations = [2, 2]


317
class TestWith3x3(TestModulatedDeformableConvOp):
318
    def init_test_case(self):
319
        self.pad = [1, 1]
320 321 322 323
        self.stride = [1, 1]
        self.input_size = [2, 3, 5, 5]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
324
        self.filter_size = [6, f_c, 3, 3]
325 326
        self.im2col_step = 1
        self.deformable_groups = 1
327 328 329 330 331 332 333 334 335
        offset_c = (
            2
            * self.deformable_groups
            * self.filter_size[2]
            * self.filter_size[3]
        )
        mask_c = (
            self.deformable_groups * self.filter_size[2] * self.filter_size[3]
        )
336
        self.offset_size = [
337 338 339 340
            self.input_size[0],
            offset_c,
            self.input_size[2],
            self.input_size[3],
341 342
        ]
        self.mask_size = [
343 344 345 346
            self.input_size[0],
            mask_c,
            self.input_size[2],
            self.input_size[3],
347 348 349 350 351 352 353 354
        ]


class TestWithGroup(TestModulatedDeformableConvOp):
    def init_group(self):
        self.groups = 2


355 356 357 358 359 360 361 362 363 364 365 366 367 368
class TestWithDouble(TestModulatedDeformableConvOp):
    def init_type(self):
        self.dtype = np.float64

    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.dilations = [1, 1]
        self.input_size = [2, 6, 4, 4]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [4, f_c, 3, 3]
        self.im2col_step = 1
        self.deformable_groups = 1
369 370 371 372 373 374 375 376 377
        offset_c = (
            2
            * self.deformable_groups
            * self.filter_size[2]
            * self.filter_size[3]
        )
        mask_c = (
            self.deformable_groups * self.filter_size[2] * self.filter_size[3]
        )
378
        self.offset_size = [
379 380 381 382
            self.input_size[0],
            offset_c,
            self.input_size[2],
            self.input_size[3],
383 384
        ]
        self.mask_size = [
385 386 387 388
            self.input_size[0],
            mask_c,
            self.input_size[2],
            self.input_size[3],
389 390 391
        ]


392 393 394
class TestModulatedDeformableConvInvalidInput(unittest.TestCase):
    def test_error(self):
        def test_invalid_input():
395
            paddle.enable_static()
396
            input = [1, 3, 32, 32]
397
            offset = paddle.static.data(
398 399
                name='offset', shape=[None, 3, 32, 32], dtype='float32'
            )
400
            mask = paddle.static.data(
401 402
                name='mask', shape=[None, 3, 32, 32], dtype='float32'
            )
403
            loss = paddle.static.nn.common.deformable_conv(
404 405
                input, offset, mask, num_filters=4, filter_size=1
            )
406 407 408 409

        self.assertRaises(TypeError, test_invalid_input)

        def test_invalid_offset():
410
            paddle.enable_static()
411
            input = paddle.static.data(
412 413
                name='input', shape=[None, 3, 32, 32], dtype='int32'
            )
414
            offset = paddle.static.data(
415 416
                name='offset', shape=[None, 3, 32, 32], dtype='float32'
            )
417
            mask = paddle.static.data(
418 419
                name='mask', shape=[None, 3, 32, 32], dtype='float32'
            )
420
            loss = paddle.static.nn.common.deformable_conv(
421 422
                input, offset, mask, num_filters=4, filter_size=1
            )
423 424 425

        self.assertRaises(TypeError, test_invalid_offset)

426 427
        def test_invalid_filter():
            paddle.enable_static()
428
            input = paddle.static.data(
429 430
                name='input_filter', shape=[None, 3, 32, 32], dtype='float32'
            )
431
            offset = paddle.static.data(
432 433
                name='offset_filter', shape=[None, 3, 32, 32], dtype='float32'
            )
434
            mask = paddle.static.data(
435 436
                name='mask_filter', shape=[None, 3, 32, 32], dtype='float32'
            )
437
            loss = paddle.static.nn.common.deformable_conv(
438 439
                input, offset, mask, num_filters=4, filter_size=0
            )
440 441 442

        self.assertRaises(ValueError, test_invalid_filter)

443 444 445 446
    def test_error_with_eager_guard(self):
        with _test_eager_guard():
            self.test_error()

447

C
cnn 已提交
448
class TestDeformConv2DAPI(unittest.TestCase):
449 450 451
    def test_api(self):
        def test_deform_conv2d_v1():
            paddle.enable_static()
452 453 454 455 456 457 458 459 460 461 462
            input = paddle.static.data(
                name='input_v1', shape=[None, 3, 32, 32], dtype='float32'
            )
            offset = paddle.static.data(
                name='offset_v1', shape=[None, 4, 32, 32], dtype='float32'
            )
            out = paddle.static.nn.deform_conv2d(
                input, offset, None, num_filters=4, filter_size=1
            )

            assert out.shape == (-1, 4, 32, 32)
463 464 465 466 467

        test_deform_conv2d_v1()

        def test_deform_conv2d_v2():
            paddle.enable_static()
468 469 470 471 472 473 474 475 476 477 478 479 480 481
            input = paddle.static.data(
                name='input_v2', shape=[None, 3, 32, 32], dtype='float32'
            )
            offset = paddle.static.data(
                name='offset_v2', shape=[None, 4, 32, 32], dtype='float32'
            )
            mask = paddle.static.data(
                name='mask_v2', shape=[None, 2, 32, 32], dtype='float32'
            )
            out = paddle.static.nn.deform_conv2d(
                input, offset, mask, num_filters=4, filter_size=1
            )

            assert out.shape == (-1, 4, 32, 32)
482 483 484

        test_deform_conv2d_v2()

485 486 487 488
    def test_api_with_eager_guard(self):
        with _test_eager_guard():
            self.test_api()

489

490 491
if __name__ == '__main__':
    unittest.main()