From 58239152be777a63488eebd3622fba2a129b2dde Mon Sep 17 00:00:00 2001 From: MeowZheng Date: Mon, 12 Apr 2021 18:52:28 +0800 Subject: [PATCH 1/4] [Bug] Fix DeformConv2d bias error and add tests --- mmcv/ops/deform_conv.py | 6 +++--- tests/test_ops/test_deform_conv.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/mmcv/ops/deform_conv.py b/mmcv/ops/deform_conv.py index 2c31a6efb6..451bef2936 100644 --- a/mmcv/ops/deform_conv.py +++ b/mmcv/ops/deform_conv.py @@ -200,7 +200,7 @@ class DeformConv2d(nn.Module): channels to output channels. Default: 1. deform_groups (int): Number of deformable group partitions. bias (bool): If True, adds a learnable bias to the output. - Default: True. + Default: False. """ @@ -234,7 +234,6 @@ def __init__(self, self.dilation = _pair(dilation) self.groups = groups self.deform_groups = deform_groups - self.bias = bias # enable compatibility with nn.Conv2d self.transposed = False self.output_padding = _single(0) @@ -301,7 +300,8 @@ def __repr__(self): s += f'dilation={self.dilation},\n' s += f'groups={self.groups},\n' s += f'deform_groups={self.deform_groups},\n' - s += f'bias={self.bias})' + if not hasattr(self, 'bias'): + s += 'deform_groups=False)' return s diff --git a/tests/test_ops/test_deform_conv.py b/tests/test_ops/test_deform_conv.py index 58b2e9f631..352b955a74 100644 --- a/tests/test_ops/test_deform_conv.py +++ b/tests/test_ops/test_deform_conv.py @@ -1,4 +1,6 @@ +from _pytest.fixtures import get_parametrized_fixture_keys import numpy as np +import pytest import torch input = [[[[1., 2., 3.], [0., 1., 2.], [3., 5., 2.]]]] @@ -56,6 +58,17 @@ def _test_deformconv(self, dtype=torch.float, threshold=1e-3): assert np.allclose(model.weight.grad.detach().cpu().numpy(), gt_deform_weight_grad, threshold) + from mmcv.ops import DeformConv2d + # test bias=True + with pytest.raises(AssertionError): + model = DeformConv2d(1, 1, 2, stride=1, padding=0, bias=True) + # test in_channels % group != 0 + with pytest.raises(AssertionError): + model = DeformConv2d(3, 2, 3, groups=2) + # test out_channels % group != 0 + with pytest.raises(AssertionError): + model = DeformConv2d(3, 4, 3, groups=3) + def test_deformconv(self): self._test_deformconv(torch.double) self._test_deformconv(torch.float) From 271489093a2cdf991815ec3533ae39f44021b396 Mon Sep 17 00:00:00 2001 From: MeowZheng Date: Mon, 12 Apr 2021 18:57:17 +0800 Subject: [PATCH 2/4] fix repr --- mmcv/ops/deform_conv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mmcv/ops/deform_conv.py b/mmcv/ops/deform_conv.py index 451bef2936..5c45bd4ebb 100644 --- a/mmcv/ops/deform_conv.py +++ b/mmcv/ops/deform_conv.py @@ -300,8 +300,8 @@ def __repr__(self): s += f'dilation={self.dilation},\n' s += f'groups={self.groups},\n' s += f'deform_groups={self.deform_groups},\n' - if not hasattr(self, 'bias'): - s += 'deform_groups=False)' + # bias is not supported in DeformConv2d. + s += 'deform_groups=False)' return s From baca10aaad2e4141eec6d88c27bfcd87ddc1db9b Mon Sep 17 00:00:00 2001 From: MeowZheng Date: Mon, 12 Apr 2021 18:58:51 +0800 Subject: [PATCH 3/4] revise tests --- tests/test_ops/test_deform_conv.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_ops/test_deform_conv.py b/tests/test_ops/test_deform_conv.py index 352b955a74..708021e7bc 100644 --- a/tests/test_ops/test_deform_conv.py +++ b/tests/test_ops/test_deform_conv.py @@ -59,6 +59,9 @@ def _test_deformconv(self, dtype=torch.float, threshold=1e-3): gt_deform_weight_grad, threshold) from mmcv.ops import DeformConv2d + # test bias + model = DeformConv2d(1, 1, 2, stride=1, padding=0) + assert not hasattr(model, 'bias') # test bias=True with pytest.raises(AssertionError): model = DeformConv2d(1, 1, 2, stride=1, padding=0, bias=True) From a4088c0b604840d9c567bc1621f2bb995c31954d Mon Sep 17 00:00:00 2001 From: MeowZheng Date: Mon, 12 Apr 2021 19:27:15 +0800 Subject: [PATCH 4/4] lint --- tests/test_ops/test_deform_conv.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_ops/test_deform_conv.py b/tests/test_ops/test_deform_conv.py index 708021e7bc..b99df8d011 100644 --- a/tests/test_ops/test_deform_conv.py +++ b/tests/test_ops/test_deform_conv.py @@ -1,4 +1,3 @@ -from _pytest.fixtures import get_parametrized_fixture_keys import numpy as np import pytest import torch