Commit 82e75455 authored by thangvu's avatar thangvu Committed by ThangVu
Browse files

revise norm config

parent 55a4feb5
# model settings # model settings
normalize = dict(
type='GN',
num_groups=32,
frozen=False)
model = dict( model = dict(
type='MaskRCNN', type='MaskRCNN',
pretrained='tools/resnet50-GN.path', pretrained='tools/resnet50-GN.path',
...@@ -9,20 +14,13 @@ model = dict( ...@@ -9,20 +14,13 @@ model = dict(
out_indices=(0, 1, 2, 3), out_indices=(0, 1, 2, 3),
frozen_stages=1, frozen_stages=1,
style='pytorch', style='pytorch',
# Note: eval_mode and frozen are required args for backbone normalize=normalize),
normalize=dict(
type='GN',
num_groups=32,
eval_mode=False,
frozen=False)),
neck=dict( neck=dict(
type='FPN', type='FPN',
in_channels=[256, 512, 1024, 2048], in_channels=[256, 512, 1024, 2048],
out_channels=256, out_channels=256,
num_outs=5, num_outs=5,
normalize=dict( normalize=normalize),
type='GN',
num_groups=32)),
rpn_head=dict( rpn_head=dict(
type='RPNHead', type='RPNHead',
in_channels=256, in_channels=256,
...@@ -50,9 +48,7 @@ model = dict( ...@@ -50,9 +48,7 @@ model = dict(
target_means=[0., 0., 0., 0.], target_means=[0., 0., 0., 0.],
target_stds=[0.1, 0.1, 0.2, 0.2], target_stds=[0.1, 0.1, 0.2, 0.2],
reg_class_agnostic=False, reg_class_agnostic=False,
normalize=dict( normalize=normalize),
type='GN',
num_groups=32)),
mask_roi_extractor=dict( mask_roi_extractor=dict(
type='SingleRoIExtractor', type='SingleRoIExtractor',
roi_layer=dict(type='RoIAlign', out_size=14, sample_num=2), roi_layer=dict(type='RoIAlign', out_size=14, sample_num=2),
...@@ -64,9 +60,7 @@ model = dict( ...@@ -64,9 +60,7 @@ model = dict(
in_channels=256, in_channels=256,
conv_out_channels=256, conv_out_channels=256,
num_classes=81, num_classes=81,
normalize=dict( normalize=normalize))
type='GN',
num_groups=32)))
# model training and testing settings # model training and testing settings
train_cfg = dict( train_cfg = dict(
......
...@@ -236,11 +236,14 @@ class ResNet(nn.Module): ...@@ -236,11 +236,14 @@ class ResNet(nn.Module):
the first 1x1 conv layer. the first 1x1 conv layer.
frozen_stages (int): Stages to be frozen (all param fixed). -1 means frozen_stages (int): Stages to be frozen (all param fixed). -1 means
not freezing any parameters. not freezing any parameters.
normalize (dict): dictionary to construct norm layer. Additionally, normalize (dict): dictionary to construct and config norm layer.
eval mode and gradent freezing are controlled by norm_eval (bool): Whether to set norm layers to eval mode, namely,
eval (bool) and frozen (bool) respectively. freeze running stats (mean and var). Note: Effect on Batch Norm
and its variants only.
with_cp (bool): Use checkpoint or not. Using checkpoint will save some with_cp (bool): Use checkpoint or not. Using checkpoint will save some
memory while slowing down the training speed. memory while slowing down the training speed.
zero_init_residual (bool): whether to use zero init for last norm layer
in resblocks to let them behave as identity.
""" """
arch_settings = { arch_settings = {
...@@ -261,8 +264,8 @@ class ResNet(nn.Module): ...@@ -261,8 +264,8 @@ class ResNet(nn.Module):
frozen_stages=-1, frozen_stages=-1,
normalize=dict( normalize=dict(
type='BN', type='BN',
eval_mode=True,
frozen=False), frozen=False),
norm_eval=True,
with_cp=False, with_cp=False,
zero_init_residual=True): zero_init_residual=True):
super(ResNet, self).__init__() super(ResNet, self).__init__()
...@@ -278,11 +281,9 @@ class ResNet(nn.Module): ...@@ -278,11 +281,9 @@ class ResNet(nn.Module):
assert max(out_indices) < num_stages assert max(out_indices) < num_stages
self.style = style self.style = style
self.frozen_stages = frozen_stages self.frozen_stages = frozen_stages
assert (isinstance(normalize, dict) and 'eval_mode' in normalize
and 'frozen' in normalize)
self.norm_eval = normalize.pop('eval_mode')
self.normalize = normalize self.normalize = normalize
self.with_cp = with_cp self.with_cp = with_cp
self.norm_eval = norm_eval
self.zero_init_residual = zero_init_residual self.zero_init_residual = zero_init_residual
self.block, stage_blocks = self.arch_settings[depth] self.block, stage_blocks = self.arch_settings[depth]
self.stage_blocks = stage_blocks[:num_stages] self.stage_blocks = stage_blocks[:num_stages]
...@@ -350,7 +351,6 @@ class ResNet(nn.Module): ...@@ -350,7 +351,6 @@ class ResNet(nn.Module):
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
constant_init(m, 1) constant_init(m, 1)
# zero init for last norm layer https://arxiv.org/abs/1706.02677
if self.zero_init_residual: if self.zero_init_residual:
for m in self.modules(): for m in self.modules():
if isinstance(m, Bottleneck): if isinstance(m, Bottleneck):
......
...@@ -122,11 +122,14 @@ class ResNeXt(ResNet): ...@@ -122,11 +122,14 @@ class ResNeXt(ResNet):
the first 1x1 conv layer. the first 1x1 conv layer.
frozen_stages (int): Stages to be frozen (all param fixed). -1 means frozen_stages (int): Stages to be frozen (all param fixed). -1 means
not freezing any parameters. not freezing any parameters.
normalize (dict): dictionary to construct norm layer. Additionally, normalize (dict): dictionary to construct and config norm layer.
eval mode and gradent freezing are controlled by norm_eval (bool): Whether to set norm layers to eval mode, namely,
eval (bool) and frozen (bool) respectively. freeze running stats (mean and var). Note: Effect on Batch Norm
and its variants only.
with_cp (bool): Use checkpoint or not. Using checkpoint will save some with_cp (bool): Use checkpoint or not. Using checkpoint will save some
memory while slowing down the training speed. memory while slowing down the training speed.
zero_init_residual (bool): whether to use zero init for last norm layer
in resblocks to let them behave as identity.
""" """
arch_settings = { arch_settings = {
......
...@@ -31,13 +31,6 @@ def build_norm_layer(cfg, num_features, postfix=''): ...@@ -31,13 +31,6 @@ def build_norm_layer(cfg, num_features, postfix=''):
assert isinstance(cfg, dict) and 'type' in cfg assert isinstance(cfg, dict) and 'type' in cfg
cfg_ = cfg.copy() cfg_ = cfg.copy()
# eval_mode is supported and popped out for processing in module
# having pretrained weight only (e.g. backbone)
# raise an exception if eval_mode is in here
if 'eval_mode' in cfg:
raise Exception('eval_mode for modules without pretrained weights '
'is not supported')
layer_type = cfg_.pop('type') layer_type = cfg_.pop('type')
if layer_type not in norm_cfg: if layer_type not in norm_cfg:
raise KeyError('Unrecognized norm type {}'.format(layer_type)) raise KeyError('Unrecognized norm type {}'.format(layer_type))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment