Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Fix DeformConv2d bias error and add tests #940

Merged
merged 4 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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