Skip to content

Commit

Permalink
[Fix] Restrict the warning message (#1267)
Browse files Browse the repository at this point in the history
* restrict the warning message

* and an important keyword in warning description

* a more elegant way of condition

* link format code too long

* fix the stupid spelling mistake

* Use issubclass to restrict warning message.

* maybe this version is more elegant.

* conv + bias + norm warning pytest

* 'created' a warning, hahaha

* isort and yapf format revision

* isort and yapf format revision

* flake8 fail issue

* I have to right this way in order to solve the conflicts between yapf and flake8, sigh...

* fixed test bug

* Add ruby windows installer source.

* Simplified the code and remove ruby source from CONTRIBUTING.md

* use _BatchNorm to simplify the code

* bug fix and add instanceNorm case into warning

* change the warning message to make it more clear

* fix unit test
  • Loading branch information
yyz561 authored Sep 7, 2021
1 parent eed9cce commit 99088c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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

0 comments on commit 99088c8

Please sign in to comment.