Skip to content

Commit

Permalink
[Bug] Fix DeformConv2d bias error and add tests (#940)
Browse files Browse the repository at this point in the history
* [Bug] Fix DeformConv2d bias error and add tests

* fix repr

* revise tests

* lint
  • Loading branch information
MeowZheng committed Apr 12, 2021
1 parent 3bcc796 commit 79f8cbd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions mmcv/ops/deform_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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})'
# bias is not supported in DeformConv2d.
s += 'deform_groups=False)'
return s


Expand Down
15 changes: 15 additions & 0 deletions tests/test_ops/test_deform_conv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest
import torch

input = [[[[1., 2., 3.], [0., 1., 2.], [3., 5., 2.]]]]
Expand Down Expand Up @@ -56,6 +57,20 @@ 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
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)
# 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)
Expand Down

0 comments on commit 79f8cbd

Please sign in to comment.