Skip to content

Commit

Permalink
Fix nolocal naming (PaddlePaddle#24)
Browse files Browse the repository at this point in the history
* Fix nolocal naming
* Fix `cb_resnet` as well
  • Loading branch information
willthefrog authored and qingqing01 committed Nov 12, 2019
1 parent a992fef commit 3ff1060
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
33 changes: 16 additions & 17 deletions ppdet/modeling/backbones/cb_resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from numbers import Integral

from .name_adapter import NameAdapter
from .nonlocal import add_space_nonlocal
from .nonlocal_helper import add_space_nonlocal

__all__ = ['CBResNet']

Expand Down Expand Up @@ -99,19 +99,19 @@ def __init__(self,
152: ([3, 8, 36, 3], self.bottleneck),
200: ([3, 12, 48, 3], self.bottleneck),
}

self.nonlocal_stages = nonlocal_stages
self.nonlocal_mod_cfg = {
50 : 2,
101 : 5,
152 : 8,
200 : 12,
}

self.stage_filters = [64, 128, 256, 512]
self._c1_out_chan_num = 64
self.na = NameAdapter(self)

def _conv_offset(self, input, filter_size, stride, padding, act=None, name=None):
out_channel = filter_size * filter_size * 3
out = fluid.layers.conv2d(input,
Expand All @@ -126,7 +126,7 @@ def _conv_offset(self, input, filter_size, stride, padding, act=None, name=None)
act=act,
name=name)
return out

def _conv_norm(self,
input,
num_filters,
Expand Down Expand Up @@ -247,7 +247,7 @@ def bottleneck(self, input, num_filters, stride, is_first, name, dcn=False):

conv_name1, conv_name2, conv_name3, \
shortcut_name = self.na.fix_bottleneck_name(name)

conv_def = [[num_filters, 1, stride1, 'relu', 1, conv_name1],
[num_filters, 3, stride2, 'relu', groups, conv_name2],
[num_filters * expand, 1, 1, None, 1, conv_name3]]
Expand Down Expand Up @@ -312,12 +312,12 @@ def layer_warp(self, input, stage_num):
ch_out = self.stage_filters[stage_num - 2]
is_first = False if stage_num != 2 else True
dcn = True if stage_num in self.dcn_v2_stages else False


nonlocal_mod = 1000
if stage_num in self.nonlocal_stages:
nonlocal_mod = self.nonlocal_mod_cfg[self.depth] if stage_num==4 else 2

# Make the layer name and parameter name consistent
# with ImageNet pre-trained model
conv = input
Expand All @@ -332,15 +332,15 @@ def layer_warp(self, input, stage_num):
is_first=is_first,
name=conv_name,
dcn=dcn)

# add non local model
dim_in = conv.shape[1]
nonlocal_name = "nonlocal_conv{}_lvl{}".format( stage_num, self.curr_level )
if i % nonlocal_mod == nonlocal_mod - 1:
conv = add_space_nonlocal(
conv, dim_in, dim_in,
nonlocal_name + '_{}'.format(i), int(dim_in / 2) )
nonlocal_name + '_{}'.format(i), int(dim_in / 2) )

return conv

def c1_stage(self, input):
Expand Down Expand Up @@ -376,7 +376,7 @@ def c1_stage(self, input):
pool_padding=1,
pool_type='max')
return output

def connect( self, left, right, name ):
ch_right = right.shape[1]
conv = self._conv_norm( left,
Expand All @@ -392,7 +392,7 @@ def connect( self, left, right, name ):
out_shape.stop_gradient = True
conv = fluid.layers.resize_nearest(
conv, scale=2., actual_shape=out_shape)

output = fluid.layers.elementwise_add(x=right, y=conv)
return output

Expand All @@ -410,7 +410,7 @@ def __call__(self, input):
res = self.layer_warp(res, i)
if i in self.feature_maps:
res_endpoints.append(res)

for num in range(1, self.repeat_num):
self.curr_level = num
res = self.c1_stage(input)
Expand All @@ -420,7 +420,6 @@ def __call__(self, input):
res_endpoints[i] = res
if self.freeze_at >= i+2:
res.stop_gradient = True

return OrderedDict([('res{}_sum'.format(self.feature_maps[idx]), feat)
for idx, feat in enumerate(res_endpoints)])

File renamed without changes.
2 changes: 1 addition & 1 deletion ppdet/modeling/backbones/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from ppdet.core.workspace import register, serializable
from numbers import Integral

from .nonlocal import add_space_nonlocal
from .nonlocal_helper import add_space_nonlocal
from .name_adapter import NameAdapter

__all__ = ['ResNet', 'ResNetC5']
Expand Down

0 comments on commit 3ff1060

Please sign in to comment.