mmcv/tests/test_ops/test_modulated_deform_conv.py
zhuyuanhao c0f5492ee9
add ext ops, support parrots (#310)
* add ext ops, support parrots

* fix lint

* fix lint

* update op from mmdetection

* support non-pytorch env

* fix import bug

* test not import mmcv.op

* rename mmcv.op to mmcv.ops

* fix compile warning

* 1. fix syncbn warning in pytorch 1.5
2. support only cpu compile
3. add point_sample from mmdet

* fix text bug

* update docstrings

* fix line endings

* minor updates

* remove non_local from ops

* bug fix for nonlocal2d

* rename ops_ext to _ext and _ext to _flow_warp_ext

* update the doc

* try clang-format github action

* fix github action

* add ops to api.rst

* fix cpp format

* fix clang format issues

* remove .clang-format

Co-authored-by: Kai Chen <chenkaidev@gmail.com>
2020-06-28 23:15:47 +08:00

65 lines
2.4 KiB
Python

import os
import numpy
import torch
cur_dir = os.path.dirname(os.path.abspath(__file__))
input_t = [[[[1., 2., 3.], [1., 2., 3.], [1., 2., 3.]]]]
output_t = [[[[0.5, 1.5, 2.5, 1.5], [1.0, 3.0, 5.0, 3.0], [1.0, 3.0, 5.0, 3.0],
[0.5, 1.5, 2.5, 1.5]]]]
input_grad = [[[[2., 2., 2.], [2., 2., 2.], [2., 2., 2.]]]]
dcn_w_grad = [[[[9., 9.], [9., 9.]]]]
dcn_offset_w_grad = [[[[-7.0, -4.0], [0.0, 0.0]]], [[[-9.0, 7.5], [-6.0,
5.0]]],
[[[-4.0, -7.0], [0.0, 0.0]]],
[[[-7.5, -9.0], [-5.0, -6.0]]],
[[[-7.0, -4.0], [-7.0, -4.0]]],
[[[-6.0, 5.0], [-9.0, 7.5]]],
[[[-4.0, -7.0], [-4.0, -7.0]]],
[[[-5.0, -6.0], [-7.5, -9.0]]], [[[10.5, 6.0], [7.0,
4.0]]],
[[[6.0, 10.5], [4.0, 7.0]]], [[[7.0, 4.0], [10.5, 6.0]]],
[[[4.0, 7.0], [6.0, 10.5]]]]
dcn_offset_b_grad = [
-3.0, -1.5, -3.0, -1.5, -3.0, -1.5, -3.0, -1.5, 4.5, 4.5, 4.5, 4.5
]
class TestMdconv(object):
def _test_mdconv(self, dtype=torch.float):
if not torch.cuda.is_available():
return
from mmcv.ops import ModulatedDeformConv2dPack
input = torch.tensor(input_t).cuda().type(dtype)
input.requires_grad = True
dcn = ModulatedDeformConv2dPack(
1,
1,
kernel_size=(2, 2),
stride=1,
padding=1,
deform_groups=1,
bias=False).cuda()
dcn.weight.data.fill_(1.)
dcn.type(dtype)
output = dcn(input)
output.sum().backward()
assert numpy.allclose(output.cpu().detach().numpy(), output_t, 1e-2)
assert numpy.allclose(input.grad.cpu().detach().numpy(), input_grad,
1e-2)
assert numpy.allclose(dcn.weight.grad.cpu().detach().numpy(),
dcn_w_grad, 1e-2)
assert numpy.allclose(
dcn.conv_offset.weight.grad.cpu().detach().numpy(),
dcn_offset_w_grad, 1e-2)
assert numpy.allclose(dcn.conv_offset.bias.grad.cpu().detach().numpy(),
dcn_offset_b_grad, 1e-2)
def test_mdconv(self):
self._test_mdconv(torch.double)
self._test_mdconv(torch.float)
self._test_mdconv(torch.half)