-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
add act_cfg #2239
Merged
Merged
add act_cfg #2239
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import torch.nn as nn | ||
|
||
activation_cfg = { | ||
# format: layer_type: (abbreviation, module) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is incorrect. |
||
'relu': nn.ReLU, | ||
'leaky_relu': nn.LeakyReLU, | ||
xvjiarui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'p_relu': nn.PReLU, | ||
'r_relu': nn.RReLU, | ||
'relu6': nn.ReLU6, | ||
'selu': nn.SELU, | ||
'celu': nn.CELU | ||
} | ||
|
||
|
||
def build_activation_layer(cfg): | ||
""" Build activation layer | ||
|
||
Args: | ||
cfg (dict): cfg should contain: | ||
type (str): Identify activation layer type. | ||
hellock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
layer args: args needed to instantiate a activation layer. | ||
|
||
Returns: | ||
layer (nn.Module): Created activation layer | ||
""" | ||
assert isinstance(cfg, dict) and 'type' in cfg | ||
cfg_ = cfg.copy() | ||
|
||
layer_type = cfg_.pop('type') | ||
if layer_type not in activation_cfg: | ||
raise KeyError('Unrecognized activation type {}'.format(layer_type)) | ||
else: | ||
activation = activation_cfg[layer_type] | ||
if activation is None: | ||
raise NotImplementedError | ||
|
||
layer = activation(**cfg_) | ||
return layer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from mmcv.cnn import constant_init, kaiming_init | ||
|
||
from mmdet.ops import DeformConvPack, ModulatedDeformConvPack | ||
from .activation import build_activation_layer | ||
from .conv_ws import ConvWS2d | ||
from .norm import build_norm_layer | ||
|
||
|
@@ -60,7 +61,7 @@ class ConvModule(nn.Module): | |
False. | ||
conv_cfg (dict): Config dict for convolution layer. | ||
norm_cfg (dict): Config dict for normalization layer. | ||
activation (str or None): Activation type, "ReLU" by default. | ||
act_cfg (str or None): Activation type, "ReLU" by default. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is no longer a str. |
||
inplace (bool): Whether to use inplace mode for activation. | ||
order (tuple[str]): The order of conv/norm/activation layers. It is a | ||
sequence of "conv", "norm" and "act". Examples are | ||
|
@@ -78,22 +79,23 @@ def __init__(self, | |
bias='auto', | ||
conv_cfg=None, | ||
norm_cfg=None, | ||
activation='relu', | ||
act_cfg=dict(type='relu'), | ||
inplace=True, | ||
order=('conv', 'norm', 'act')): | ||
super(ConvModule, self).__init__() | ||
assert conv_cfg is None or isinstance(conv_cfg, dict) | ||
assert norm_cfg is None or isinstance(norm_cfg, dict) | ||
assert act_cfg is None or isinstance(act_cfg, dict) | ||
self.conv_cfg = conv_cfg | ||
self.norm_cfg = norm_cfg | ||
self.activation = activation | ||
self.act_cfg = act_cfg | ||
self.inplace = inplace | ||
self.order = order | ||
assert isinstance(self.order, tuple) and len(self.order) == 3 | ||
assert set(order) == set(['conv', 'norm', 'act']) | ||
|
||
self.with_norm = norm_cfg is not None | ||
self.with_activation = activation is not None | ||
self.with_activation = act_cfg is not None | ||
# if the conv layer is before a norm layer, bias is unnecessary. | ||
if bias == 'auto': | ||
bias = False if self.with_norm else True | ||
|
@@ -136,12 +138,9 @@ def __init__(self, | |
|
||
# build activation layer | ||
if self.with_activation: | ||
# TODO: introduce `act_cfg` and supports more activation layers | ||
if self.activation not in ['relu']: | ||
raise ValueError('{} is currently not supported.'.format( | ||
self.activation)) | ||
if self.activation == 'relu': | ||
self.activate = nn.ReLU(inplace=inplace) | ||
act_cfg_ = act_cfg.copy() | ||
act_cfg_.setdefault('inplace', inplace) | ||
self.activate = build_activation_layer(act_cfg_) | ||
|
||
# Use msra init by default | ||
self.init_weights() | ||
|
@@ -151,7 +150,10 @@ def norm(self): | |
return getattr(self, self.norm_name) | ||
|
||
def init_weights(self): | ||
nonlinearity = 'relu' if self.activation is None else self.activation | ||
if self.with_activation and self.act_cfg['type'] == 'leaky_relu': | ||
nonlinearity = 'leaky_relu' | ||
else: | ||
nonlinearity = 'relu' | ||
kaiming_init(self.conv, nonlinearity=nonlinearity) | ||
if self.with_norm: | ||
constant_init(self.norm, 1, bias=0) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may follow the argument order: conv_cfg, norm_cfg, act_cfg.