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

[Fix] Restrict the warning message #1267

Merged
merged 22 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
8 changes: 5 additions & 3 deletions mmcv/cnn/bricks/conv_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import torch.nn as nn

from mmcv.utils import _BatchNorm, _InstanceNorm
from ..utils import constant_init, kaiming_init
from .activation import build_activation_layer
from .conv import build_conv_layer
Expand Down Expand Up @@ -104,9 +105,6 @@ def __init__(self,
bias = not self.with_norm
self.with_bias = bias

if self.with_norm and self.with_bias:
warnings.warn('ConvModule has norm and bias at the same time')

if self.with_explicit_padding:
pad_cfg = dict(type=padding_mode)
self.padding_layer = build_padding_layer(pad_cfg, padding)
Expand Down Expand Up @@ -147,6 +145,10 @@ def __init__(self,
norm_channels = in_channels
self.norm_name, norm = build_norm_layer(norm_cfg, norm_channels)
self.add_module(self.norm_name, norm)
if self.with_bias:
if isinstance(norm, (_BatchNorm, _InstanceNorm)):
warnings.warn(
'Unnecessary conv bias before batch/instance norm')
else:
self.norm_name = None

Expand Down
20 changes: 18 additions & 2 deletions tests/test_cnn/test_conv_module.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -161,12 +162,27 @@ def test_bias():
conv = ConvModule(3, 8, 2, bias=False)
assert conv.conv.bias is None

# bias: True, with norm
# bias: True, with batch norm
with pytest.warns(UserWarning) as record:
ConvModule(3, 8, 2, bias=True, norm_cfg=dict(type='BN'))
assert len(record) == 1
assert record[0].message.args[
0] == 'ConvModule has norm and bias at the same time'
0] == 'Unnecessary conv bias before batch/instance norm'

# bias: True, with instance norm
with pytest.warns(UserWarning) as record:
ConvModule(3, 8, 2, bias=True, norm_cfg=dict(type='IN'))
assert len(record) == 1
assert record[0].message.args[
0] == 'Unnecessary conv bias before batch/instance norm'

# bias: True, with other norm
with pytest.warns(UserWarning) as record:
norm_cfg = dict(type='GN', num_groups=1)
ConvModule(3, 8, 2, bias=True, norm_cfg=norm_cfg)
warnings.warn('No warnings')
assert len(record) == 1
assert record[0].message.args[0] == 'No warnings'


def conv_forward(self, x):
Expand Down