未验证 提交 1475bb05 编写于 作者: K Kaipeng Deng 提交者: GitHub

exit 1 when travis check failed (#214)

* exit1 in travis failed
上级 fcfdbd2e
...@@ -26,6 +26,7 @@ script: ...@@ -26,6 +26,7 @@ script:
- .travis/precommit.sh || exit_code=$(( exit_code | $? )) - .travis/precommit.sh || exit_code=$(( exit_code | $? ))
- docker run -i --rm -v "$PWD:/py_unittest" paddlepaddle/paddle:latest /bin/bash -c - docker run -i --rm -v "$PWD:/py_unittest" paddlepaddle/paddle:latest /bin/bash -c
'cd /py_unittest; sh .travis/unittest.sh' || exit_code=$(( exit_code | $? )) 'cd /py_unittest; sh .travis/unittest.sh' || exit_code=$(( exit_code | $? ))
- if [ $exit_code -eq 0 ]; then true; else exit 1; fi;
notifications: notifications:
email: email:
......
# add python requirements for unittests here, note install Cython
# and pycocotools directly is not supported in travis ci.
tqdm
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
abort(){ abort(){
echo "Run unittest failed" 1>&2 echo "Run unittest failed" 1>&2
echo "Please check your code" 1>&2 echo "Please check your code" 1>&2
echo " 1. you can run unit tests by 'bash .travis/unittest.sh' locally" 1>&2
echo " 2. you can add python requirements in .travis/requirements.txt if you use new requirements in unit tests" 1>&2
exit 1 exit 1
} }
...@@ -18,10 +20,11 @@ unittest(){ ...@@ -18,10 +20,11 @@ unittest(){
trap 'abort' 0 trap 'abort' 0
set -e set -e
# install python dependencies # install travis python dependencies
if [ -f "requirements.txt" ]; then if [ -f ".travis/requirements.txt" ]; then
pip install -r requirements.txt pip install -r .travis/requirements.txt
fi fi
export PYTHONPATH=`pwd`:$PYTHONPATH export PYTHONPATH=`pwd`:$PYTHONPATH
unittest . unittest .
......
...@@ -134,8 +134,7 @@ _DETECTIONBOX = _descriptor.Descriptor( ...@@ -134,8 +134,7 @@ _DETECTIONBOX = _descriptor.Descriptor(
extension_ranges=[], extension_ranges=[],
oneofs=[], oneofs=[],
serialized_start=43, serialized_start=43,
serialized_end=175, serialized_end=175)
)
_DETECTIONRESULT = _descriptor.Descriptor( _DETECTIONRESULT = _descriptor.Descriptor(
name='DetectionResult', name='DetectionResult',
...@@ -186,8 +185,7 @@ _DETECTIONRESULT = _descriptor.Descriptor( ...@@ -186,8 +185,7 @@ _DETECTIONRESULT = _descriptor.Descriptor(
extension_ranges=[], extension_ranges=[],
oneofs=[], oneofs=[],
serialized_start=177, serialized_start=177,
serialized_end=267, serialized_end=267)
)
_DETECTIONRESULT.fields_by_name['detection_boxes'].message_type = _DETECTIONBOX _DETECTIONRESULT.fields_by_name['detection_boxes'].message_type = _DETECTIONBOX
DESCRIPTOR.message_types_by_name['DetectionBox'] = _DETECTIONBOX DESCRIPTOR.message_types_by_name['DetectionBox'] = _DETECTIONBOX
...@@ -195,20 +193,22 @@ DESCRIPTOR.message_types_by_name['DetectionResult'] = _DETECTIONRESULT ...@@ -195,20 +193,22 @@ DESCRIPTOR.message_types_by_name['DetectionResult'] = _DETECTIONRESULT
DetectionBox = _reflection.GeneratedProtocolMessageType( DetectionBox = _reflection.GeneratedProtocolMessageType(
'DetectionBox', 'DetectionBox',
(_message.Message,), (_message.Message, ),
dict(DESCRIPTOR=_DETECTIONBOX, dict(
__module__='detection_result_pb2' DESCRIPTOR=_DETECTIONBOX,
# @@protoc_insertion_point(class_scope:PaddleSolution.DetectionBox) __module__='detection_result_pb2'
)) # @@protoc_insertion_point(class_scope:PaddleSolution.DetectionBox)
))
_sym_db.RegisterMessage(DetectionBox) _sym_db.RegisterMessage(DetectionBox)
DetectionResult = _reflection.GeneratedProtocolMessageType( DetectionResult = _reflection.GeneratedProtocolMessageType(
'DetectionResult', 'DetectionResult',
(_message.Message,), (_message.Message, ),
dict(DESCRIPTOR=_DETECTIONRESULT, dict(
__module__='detection_result_pb2' DESCRIPTOR=_DETECTIONRESULT,
# @@protoc_insertion_point(class_scope:PaddleSolution.DetectionResult) __module__='detection_result_pb2'
)) # @@protoc_insertion_point(class_scope:PaddleSolution.DetectionResult)
))
_sym_db.RegisterMessage(DetectionResult) _sym_db.RegisterMessage(DetectionResult)
# @@protoc_insertion_point(module_scope) # @@protoc_insertion_point(module_scope)
...@@ -85,8 +85,8 @@ if __name__ == "__main__": ...@@ -85,8 +85,8 @@ if __name__ == "__main__":
for box in detection_result.detection_boxes: for box in detection_result.detection_boxes:
if box.score >= Flags.threshold: if box.score >= Flags.threshold:
box_class = getattr(box, 'class') box_class = getattr(box, 'class')
text_class_score_str = "%s %.2f" % (class2LabelMap.get( text_class_score_str = "%s %.2f" % (
str(box_class)), box.score) class2LabelMap.get(str(box_class)), box.score)
text_point = (int(box.left_top_x), int(box.left_top_y)) text_point = (int(box.left_top_x), int(box.left_top_y))
ptLeftTop = (int(box.left_top_x), int(box.left_top_y)) ptLeftTop = (int(box.left_top_x), int(box.left_top_y))
...@@ -106,8 +106,8 @@ if __name__ == "__main__": ...@@ -106,8 +106,8 @@ if __name__ == "__main__":
text_box_left_top = (text_point[0], text_box_left_top = (text_point[0],
text_point[1] - text_size[0][1]) text_point[1] - text_size[0][1])
text_box_right_bottom = (text_point[0] + text_box_right_bottom = (
text_size[0][0], text_point[1]) text_point[0] + text_size[0][0], text_point[1])
cv2.rectangle(img, text_box_left_top, cv2.rectangle(img, text_box_left_top,
text_box_right_bottom, color, -1, 8) text_box_right_bottom, color, -1, 8)
......
...@@ -23,14 +23,19 @@ import re ...@@ -23,14 +23,19 @@ import re
try: try:
from docstring_parser import parse as doc_parse from docstring_parser import parse as doc_parse
except Exception: except Exception:
def doc_parse(*args): def doc_parse(*args):
pass pass
try: try:
from typeguard import check_type from typeguard import check_type
except Exception: except Exception:
def check_type(*args): def check_type(*args):
pass pass
__all__ = ['SchemaValue', 'SchemaDict', 'SharedConfig', 'extract_schema'] __all__ = ['SchemaValue', 'SchemaDict', 'SharedConfig', 'extract_schema']
......
...@@ -25,11 +25,11 @@ import shutil ...@@ -25,11 +25,11 @@ import shutil
import numpy as np import numpy as np
import PIL.ImageDraw import PIL.ImageDraw
label_to_num = {} label_to_num = {}
categories_list = [] categories_list = []
labels_list = [] labels_list = []
class MyEncoder(json.JSONEncoder): class MyEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj):
if isinstance(obj, np.integer): if isinstance(obj, np.integer):
...@@ -287,16 +287,14 @@ def main(): ...@@ -287,16 +287,14 @@ def main():
indent=4, indent=4,
cls=MyEncoder) cls=MyEncoder)
if args.val_proportion != 0: if args.val_proportion != 0:
val_data_coco = deal_json(args.dataset_type, val_data_coco = deal_json(args.dataset_type, args.output_dir + '/val',
args.output_dir + '/val',
args.json_input_dir) args.json_input_dir)
val_json_path = osp.join(args.output_dir + '/annotations', val_json_path = osp.join(args.output_dir + '/annotations',
'instance_val.json') 'instance_val.json')
json.dump( json.dump(
val_data_coco, open(val_json_path, 'w'), indent=4, cls=MyEncoder) val_data_coco, open(val_json_path, 'w'), indent=4, cls=MyEncoder)
if args.test_proportion != 0: if args.test_proportion != 0:
test_data_coco = deal_json(args.dataset_type, test_data_coco = deal_json(args.dataset_type, args.output_dir + '/test',
args.output_dir + '/test',
args.json_input_dir) args.json_input_dir)
test_json_path = osp.join(args.output_dir + '/annotations', test_json_path = osp.join(args.output_dir + '/annotations',
'instance_test.json') 'instance_test.json')
......
...@@ -64,8 +64,8 @@ class CBResNet(object): ...@@ -64,8 +64,8 @@ class CBResNet(object):
variant='b', variant='b',
feature_maps=[2, 3, 4, 5], feature_maps=[2, 3, 4, 5],
dcn_v2_stages=[], dcn_v2_stages=[],
nonlocal_stages = [], nonlocal_stages=[],
repeat_num = 2): repeat_num=2):
super(CBResNet, self).__init__() super(CBResNet, self).__init__()
if isinstance(feature_maps, Integral): if isinstance(feature_maps, Integral):
...@@ -102,19 +102,26 @@ class CBResNet(object): ...@@ -102,19 +102,26 @@ class CBResNet(object):
self.nonlocal_stages = nonlocal_stages self.nonlocal_stages = nonlocal_stages
self.nonlocal_mod_cfg = { self.nonlocal_mod_cfg = {
50 : 2, 50: 2,
101 : 5, 101: 5,
152 : 8, 152: 8,
200 : 12, 200: 12,
} }
self.stage_filters = [64, 128, 256, 512] self.stage_filters = [64, 128, 256, 512]
self._c1_out_chan_num = 64 self._c1_out_chan_num = 64
self.na = NameAdapter(self) self.na = NameAdapter(self)
def _conv_offset(self, input, filter_size, stride, padding, act=None, name=None): def _conv_offset(self,
input,
filter_size,
stride,
padding,
act=None,
name=None):
out_channel = filter_size * filter_size * 3 out_channel = filter_size * filter_size * 3
out = fluid.layers.conv2d(input, out = fluid.layers.conv2d(
input,
num_filters=out_channel, num_filters=out_channel,
filter_size=filter_size, filter_size=filter_size,
stride=stride, stride=stride,
...@@ -145,7 +152,8 @@ class CBResNet(object): ...@@ -145,7 +152,8 @@ class CBResNet(object):
padding=(filter_size - 1) // 2, padding=(filter_size - 1) // 2,
groups=groups, groups=groups,
act=None, act=None,
param_attr=ParamAttr(name=name + "_weights_"+str(self.curr_level)), param_attr=ParamAttr(
name=name + "_weights_" + str(self.curr_level)),
bias_attr=False) bias_attr=False)
else: else:
offset_mask = self._conv_offset( offset_mask = self._conv_offset(
...@@ -155,8 +163,8 @@ class CBResNet(object): ...@@ -155,8 +163,8 @@ class CBResNet(object):
padding=(filter_size - 1) // 2, padding=(filter_size - 1) // 2,
act=None, act=None,
name=name + "_conv_offset_" + str(self.curr_level)) name=name + "_conv_offset_" + str(self.curr_level))
offset_channel = filter_size ** 2 * 2 offset_channel = filter_size**2 * 2
mask_channel = filter_size ** 2 mask_channel = filter_size**2
offset, mask = fluid.layers.split( offset, mask = fluid.layers.split(
input=offset_mask, input=offset_mask,
num_or_sections=[offset_channel, mask_channel], num_or_sections=[offset_channel, mask_channel],
...@@ -173,7 +181,8 @@ class CBResNet(object): ...@@ -173,7 +181,8 @@ class CBResNet(object):
groups=groups, groups=groups,
deformable_groups=1, deformable_groups=1,
im2col_step=1, im2col_step=1,
param_attr=ParamAttr(name=name + "_weights_"+str(self.curr_level)), param_attr=ParamAttr(
name=name + "_weights_" + str(self.curr_level)),
bias_attr=False) bias_attr=False)
bn_name = self.na.fix_conv_norm_name(name) bn_name = self.na.fix_conv_norm_name(name)
...@@ -181,11 +190,11 @@ class CBResNet(object): ...@@ -181,11 +190,11 @@ class CBResNet(object):
norm_lr = 0. if self.freeze_norm else 1. norm_lr = 0. if self.freeze_norm else 1.
norm_decay = self.norm_decay norm_decay = self.norm_decay
pattr = ParamAttr( pattr = ParamAttr(
name=bn_name + '_scale_'+str(self.curr_level), name=bn_name + '_scale_' + str(self.curr_level),
learning_rate=norm_lr, learning_rate=norm_lr,
regularizer=L2Decay(norm_decay)) regularizer=L2Decay(norm_decay))
battr = ParamAttr( battr = ParamAttr(
name=bn_name + '_offset_'+str(self.curr_level), name=bn_name + '_offset_' + str(self.curr_level),
learning_rate=norm_lr, learning_rate=norm_lr,
regularizer=L2Decay(norm_decay)) regularizer=L2Decay(norm_decay))
...@@ -194,11 +203,12 @@ class CBResNet(object): ...@@ -194,11 +203,12 @@ class CBResNet(object):
out = fluid.layers.batch_norm( out = fluid.layers.batch_norm(
input=conv, input=conv,
act=act, act=act,
name=bn_name + '.output.1_'+str(self.curr_level), name=bn_name + '.output.1_' + str(self.curr_level),
param_attr=pattr, param_attr=pattr,
bias_attr=battr, bias_attr=battr,
moving_mean_name=bn_name + '_mean_'+str(self.curr_level), moving_mean_name=bn_name + '_mean_' + str(self.curr_level),
moving_variance_name=bn_name + '_variance_'+str(self.curr_level), moving_variance_name=bn_name + '_variance_' +
str(self.curr_level),
use_global_stats=global_stats) use_global_stats=global_stats)
scale = fluid.framework._get_var(pattr.name) scale = fluid.framework._get_var(pattr.name)
bias = fluid.framework._get_var(battr.name) bias = fluid.framework._get_var(battr.name)
...@@ -262,7 +272,7 @@ class CBResNet(object): ...@@ -262,7 +272,7 @@ class CBResNet(object):
act=act, act=act,
groups=g, groups=g,
name=_name, name=_name,
dcn=(i==1 and dcn)) dcn=(i == 1 and dcn))
short = self._shortcut( short = self._shortcut(
input, input,
num_filters * expand, num_filters * expand,
...@@ -273,8 +283,7 @@ class CBResNet(object): ...@@ -273,8 +283,7 @@ class CBResNet(object):
if callable(getattr(self, '_squeeze_excitation', None)): if callable(getattr(self, '_squeeze_excitation', None)):
residual = self._squeeze_excitation( residual = self._squeeze_excitation(
input=residual, num_channels=num_filters, name='fc' + name) input=residual, num_channels=num_filters, name='fc' + name)
return fluid.layers.elementwise_add( return fluid.layers.elementwise_add(x=short, y=residual, act='relu')
x=short, y=residual, act='relu')
def basicblock(self, input, num_filters, stride, is_first, name, dcn=False): def basicblock(self, input, num_filters, stride, is_first, name, dcn=False):
assert dcn is False, "Not implemented yet." assert dcn is False, "Not implemented yet."
...@@ -313,10 +322,10 @@ class CBResNet(object): ...@@ -313,10 +322,10 @@ class CBResNet(object):
is_first = False if stage_num != 2 else True is_first = False if stage_num != 2 else True
dcn = True if stage_num in self.dcn_v2_stages else False dcn = True if stage_num in self.dcn_v2_stages else False
nonlocal_mod = 1000 nonlocal_mod = 1000
if stage_num in self.nonlocal_stages: if stage_num in self.nonlocal_stages:
nonlocal_mod = self.nonlocal_mod_cfg[self.depth] if stage_num==4 else 2 nonlocal_mod = self.nonlocal_mod_cfg[
self.depth] if stage_num == 4 else 2
# Make the layer name and parameter name consistent # Make the layer name and parameter name consistent
# with ImageNet pre-trained model # with ImageNet pre-trained model
...@@ -335,11 +344,12 @@ class CBResNet(object): ...@@ -335,11 +344,12 @@ class CBResNet(object):
# add non local model # add non local model
dim_in = conv.shape[1] dim_in = conv.shape[1]
nonlocal_name = "nonlocal_conv{}_lvl{}".format( stage_num, self.curr_level ) nonlocal_name = "nonlocal_conv{}_lvl{}".format(stage_num,
self.curr_level)
if i % nonlocal_mod == nonlocal_mod - 1: if i % nonlocal_mod == nonlocal_mod - 1:
conv = add_space_nonlocal( conv = add_space_nonlocal(conv, dim_in, dim_in,
conv, dim_in, dim_in, nonlocal_name + '_{}'.format(i),
nonlocal_name + '_{}'.format(i), int(dim_in / 2) ) int(dim_in / 2))
return conv return conv
...@@ -349,9 +359,9 @@ class CBResNet(object): ...@@ -349,9 +359,9 @@ class CBResNet(object):
conv1_name = self.na.fix_c1_stage_name() conv1_name = self.na.fix_c1_stage_name()
if self.variant in ['c', 'd']: if self.variant in ['c', 'd']:
conv1_1_name= "conv1_1" conv1_1_name = "conv1_1"
conv1_2_name= "conv1_2" conv1_2_name = "conv1_2"
conv1_3_name= "conv1_3" conv1_3_name = "conv1_3"
conv_def = [ conv_def = [
[out_chan // 2, 3, 2, conv1_1_name], [out_chan // 2, 3, 2, conv1_1_name],
[out_chan // 2, 3, 1, conv1_2_name], [out_chan // 2, 3, 1, conv1_2_name],
...@@ -377,14 +387,15 @@ class CBResNet(object): ...@@ -377,14 +387,15 @@ class CBResNet(object):
pool_type='max') pool_type='max')
return output return output
def connect( self, left, right, name ): def connect(self, left, right, name):
ch_right = right.shape[1] ch_right = right.shape[1]
conv = self._conv_norm( left, conv = self._conv_norm(
num_filters=ch_right, left,
filter_size=1, num_filters=ch_right,
stride=1, filter_size=1,
act="relu", stride=1,
name=name+"_connect") act="relu",
name=name + "_connect")
shape = fluid.layers.shape(right) shape = fluid.layers.shape(right)
shape_hw = fluid.layers.slice(shape, axes=[0], starts=[2], ends=[4]) shape_hw = fluid.layers.slice(shape, axes=[0], starts=[2], ends=[4])
out_shape_ = shape_hw out_shape_ = shape_hw
...@@ -414,11 +425,11 @@ class CBResNet(object): ...@@ -414,11 +425,11 @@ class CBResNet(object):
for num in range(1, self.repeat_num): for num in range(1, self.repeat_num):
self.curr_level = num self.curr_level = num
res = self.c1_stage(input) res = self.c1_stage(input)
for i in range( len(res_endpoints) ): for i in range(len(res_endpoints)):
res = self.connect( res_endpoints[i], res, "test_c"+str(i+1) ) res = self.connect(res_endpoints[i], res, "test_c" + str(i + 1))
res = self.layer_warp(res, i+2) res = self.layer_warp(res, i + 2)
res_endpoints[i] = res res_endpoints[i] = res
if self.freeze_at >= i+2: if self.freeze_at >= i + 2:
res.stop_gradient = True res.stop_gradient = True
return OrderedDict([('res{}_sum'.format(self.feature_maps[idx]), feat) return OrderedDict([('res{}_sum'.format(self.feature_maps[idx]), feat)
......
...@@ -40,35 +40,36 @@ class HRFPN(object): ...@@ -40,35 +40,36 @@ class HRFPN(object):
spatial_scale (list): feature map scaling factor spatial_scale (list): feature map scaling factor
""" """
def __init__(self, def __init__(
num_chan=256, self,
pooling_type="avg", num_chan=256,
share_conv=False, pooling_type="avg",
spatial_scale=[1./64, 1./32, 1./16, 1./8, 1./4], share_conv=False,
): spatial_scale=[1. / 64, 1. / 32, 1. / 16, 1. / 8, 1. / 4], ):
self.num_chan = num_chan self.num_chan = num_chan
self.pooling_type = pooling_type self.pooling_type = pooling_type
self.share_conv = share_conv self.share_conv = share_conv
self.spatial_scale = spatial_scale self.spatial_scale = spatial_scale
return return
def get_output(self, body_dict): def get_output(self, body_dict):
num_out = len(self.spatial_scale) num_out = len(self.spatial_scale)
body_name_list = list(body_dict.keys()) body_name_list = list(body_dict.keys())
num_backbone_stages = len(body_name_list) num_backbone_stages = len(body_name_list)
outs = [] outs = []
outs.append(body_dict[body_name_list[0]]) outs.append(body_dict[body_name_list[0]])
# resize # resize
for i in range(1, len(body_dict)): for i in range(1, len(body_dict)):
resized = self.resize_input_tensor(body_dict[body_name_list[i]], outs[0], 2**i) resized = self.resize_input_tensor(body_dict[body_name_list[i]],
outs.append( resized ) outs[0], 2**i)
outs.append(resized)
# concat # concat
out = fluid.layers.concat( outs, axis=1 ) out = fluid.layers.concat(outs, axis=1)
# reduction # reduction
out = fluid.layers.conv2d( out = fluid.layers.conv2d(
input=out, input=out,
...@@ -78,34 +79,40 @@ class HRFPN(object): ...@@ -78,34 +79,40 @@ class HRFPN(object):
padding=0, padding=0,
param_attr=ParamAttr(name='hrfpn_reduction_weights'), param_attr=ParamAttr(name='hrfpn_reduction_weights'),
bias_attr=False) bias_attr=False)
# conv # conv
outs = [out] outs = [out]
for i in range(1, num_out): for i in range(1, num_out):
outs.append(self.pooling(out, size=2**i, stride=2**i, pooling_type=self.pooling_type)) outs.append(
self.pooling(
out, size=2**i, stride=2**i,
pooling_type=self.pooling_type))
outputs = [] outputs = []
for i in range(num_out): for i in range(num_out):
conv_name = "shared_fpn_conv" if self.share_conv else "shared_fpn_conv_"+str(i) conv_name = "shared_fpn_conv" if self.share_conv else "shared_fpn_conv_" + str(
i)
conv = fluid.layers.conv2d( conv = fluid.layers.conv2d(
input=outs[i], input=outs[i],
num_filters=self.num_chan, num_filters=self.num_chan,
filter_size=3, filter_size=3,
stride=1, stride=1,
padding=1, padding=1,
param_attr=ParamAttr(name=conv_name+"_weights"), param_attr=ParamAttr(name=conv_name + "_weights"),
bias_attr=False) bias_attr=False)
outputs.append( conv ) outputs.append(conv)
for idx in range(0, num_out-len(body_name_list)): for idx in range(0, num_out - len(body_name_list)):
body_name_list.append("fpn_res5_sum_subsampled_{}x".format( 2**(idx+1) )) body_name_list.append("fpn_res5_sum_subsampled_{}x".format(2**(idx +
1)))
outputs = outputs[::-1] outputs = outputs[::-1]
body_name_list = body_name_list[::-1] body_name_list = body_name_list[::-1]
res_dict = OrderedDict([(body_name_list[k], outputs[k]) for k in range(len(body_name_list))]) res_dict = OrderedDict([(body_name_list[k], outputs[k])
for k in range(len(body_name_list))])
return res_dict, self.spatial_scale return res_dict, self.spatial_scale
def resize_input_tensor(self, body_input, ref_output, scale): def resize_input_tensor(self, body_input, ref_output, scale):
shape = fluid.layers.shape(ref_output) shape = fluid.layers.shape(ref_output)
shape_hw = fluid.layers.slice(shape, axes=[0], starts=[2], ends=[4]) shape_hw = fluid.layers.slice(shape, axes=[0], starts=[2], ends=[4])
...@@ -115,12 +122,11 @@ class HRFPN(object): ...@@ -115,12 +122,11 @@ class HRFPN(object):
body_output = fluid.layers.resize_bilinear( body_output = fluid.layers.resize_bilinear(
body_input, scale=scale, actual_shape=out_shape) body_input, scale=scale, actual_shape=out_shape)
return body_output return body_output
def pooling(self, input, size, stride, pooling_type): def pooling(self, input, size, stride, pooling_type):
pool = fluid.layers.pool2d(input=input, pool = fluid.layers.pool2d(
input=input,
pool_size=size, pool_size=size,
pool_stride=stride, pool_stride=stride,
pool_type=pooling_type) pool_type=pooling_type)
return pool return pool
\ No newline at end of file
...@@ -64,7 +64,7 @@ class HRNet(object): ...@@ -64,7 +64,7 @@ class HRNet(object):
assert 0 <= freeze_at <= 4, "freeze_at should be 0, 1, 2, 3 or 4" assert 0 <= freeze_at <= 4, "freeze_at should be 0, 1, 2, 3 or 4"
assert len(feature_maps) > 0, "need one or more feature maps" assert len(feature_maps) > 0, "need one or more feature maps"
assert norm_type in ['bn', 'sync_bn'] assert norm_type in ['bn', 'sync_bn']
self.width = width self.width = width
self.has_se = has_se self.has_se = has_se
self.channels = { self.channels = {
...@@ -76,7 +76,7 @@ class HRNet(object): ...@@ -76,7 +76,7 @@ class HRNet(object):
48: [[48, 96], [48, 96, 192], [48, 96, 192, 384]], 48: [[48, 96], [48, 96, 192], [48, 96, 192, 384]],
60: [[60, 120], [60, 120, 240], [60, 120, 240, 480]], 60: [[60, 120], [60, 120, 240], [60, 120, 240, 480]],
64: [[64, 128], [64, 128, 256], [64, 128, 256, 512]], 64: [[64, 128], [64, 128, 256], [64, 128, 256, 512]],
} }
self.freeze_at = freeze_at self.freeze_at = freeze_at
self.norm_type = norm_type self.norm_type = norm_type
...@@ -86,24 +86,26 @@ class HRNet(object): ...@@ -86,24 +86,26 @@ class HRNet(object):
self.feature_maps = feature_maps self.feature_maps = feature_maps
self.end_points = [] self.end_points = []
return return
def net(self, input, class_dim=1000): def net(self, input, class_dim=1000):
width = self.width width = self.width
channels_2, channels_3, channels_4 = self.channels[width] channels_2, channels_3, channels_4 = self.channels[width]
num_modules_2, num_modules_3, num_modules_4 = 1, 4, 3 num_modules_2, num_modules_3, num_modules_4 = 1, 4, 3
x = self.conv_bn_layer(input=input, x = self.conv_bn_layer(
filter_size=3, input=input,
num_filters=64, filter_size=3,
stride=2, num_filters=64,
if_act=True, stride=2,
name='layer1_1') if_act=True,
x = self.conv_bn_layer(input=x, name='layer1_1')
filter_size=3, x = self.conv_bn_layer(
num_filters=64, input=x,
stride=2, filter_size=3,
if_act=True, num_filters=64,
name='layer1_2') stride=2,
if_act=True,
name='layer1_2')
la1 = self.layer1(x, name='layer2') la1 = self.layer1(x, name='layer2')
tr1 = self.transition_layer([la1], [256], channels_2, name='tr1') tr1 = self.transition_layer([la1], [256], channels_2, name='tr1')
...@@ -112,19 +114,20 @@ class HRNet(object): ...@@ -112,19 +114,20 @@ class HRNet(object):
st3 = self.stage(tr2, num_modules_3, channels_3, name='st3') st3 = self.stage(tr2, num_modules_3, channels_3, name='st3')
tr3 = self.transition_layer(st3, channels_3, channels_4, name='tr3') tr3 = self.transition_layer(st3, channels_3, channels_4, name='tr3')
st4 = self.stage(tr3, num_modules_4, channels_4, name='st4') st4 = self.stage(tr3, num_modules_4, channels_4, name='st4')
self.end_points = st4 self.end_points = st4
return st4[-1] return st4[-1]
def layer1(self, input, name=None): def layer1(self, input, name=None):
conv = input conv = input
for i in range(4): for i in range(4):
conv = self.bottleneck_block(conv, conv = self.bottleneck_block(
num_filters=64, conv,
downsample=True if i == 0 else False, num_filters=64,
name=name+'_'+str(i+1)) downsample=True if i == 0 else False,
name=name + '_' + str(i + 1))
return conv return conv
def transition_layer(self, x, in_channels, out_channels, name=None): def transition_layer(self, x, in_channels, out_channels, name=None):
num_in = len(in_channels) num_in = len(in_channels)
num_out = len(out_channels) num_out = len(out_channels)
...@@ -132,19 +135,21 @@ class HRNet(object): ...@@ -132,19 +135,21 @@ class HRNet(object):
for i in range(num_out): for i in range(num_out):
if i < num_in: if i < num_in:
if in_channels[i] != out_channels[i]: if in_channels[i] != out_channels[i]:
residual = self.conv_bn_layer(x[i], residual = self.conv_bn_layer(
filter_size=3, x[i],
num_filters=out_channels[i], filter_size=3,
name=name+'_layer_'+str(i+1)) num_filters=out_channels[i],
name=name + '_layer_' + str(i + 1))
out.append(residual) out.append(residual)
else: else:
out.append(x[i]) out.append(x[i])
else: else:
residual = self.conv_bn_layer(x[-1], residual = self.conv_bn_layer(
filter_size=3, x[-1],
num_filters=out_channels[i], filter_size=3,
stride=2, num_filters=out_channels[i],
name=name+'_layer_'+str(i+1)) stride=2,
name=name + '_layer_' + str(i + 1))
out.append(residual) out.append(residual)
return out return out
...@@ -153,9 +158,11 @@ class HRNet(object): ...@@ -153,9 +158,11 @@ class HRNet(object):
for i in range(len(channels)): for i in range(len(channels)):
residual = x[i] residual = x[i]
for j in range(block_num): for j in range(block_num):
residual = self.basic_block(residual, residual = self.basic_block(
channels[i], residual,
name=name+'_branch_layer_'+str(i+1)+'_'+str(j+1)) channels[i],
name=name + '_branch_layer_' + str(i + 1) + '_' +
str(j + 1))
out.append(residual) out.append(residual)
return out return out
...@@ -165,167 +172,215 @@ class HRNet(object): ...@@ -165,167 +172,215 @@ class HRNet(object):
residual = x[i] residual = x[i]
for j in range(len(channels)): for j in range(len(channels)):
if j > i: if j > i:
y = self.conv_bn_layer(x[j], y = self.conv_bn_layer(
filter_size=1, x[j],
num_filters=channels[i], filter_size=1,
if_act=False, num_filters=channels[i],
name=name+'_layer_'+str(i+1)+'_'+str(j+1)) if_act=False,
y = fluid.layers.resize_nearest(input=y, scale=2 ** (j - i)) name=name + '_layer_' + str(i + 1) + '_' + str(j + 1))
y = fluid.layers.resize_nearest(input=y, scale=2**(j - i))
residual = fluid.layers.elementwise_add( residual = fluid.layers.elementwise_add(
x=residual, y=y, act=None) x=residual, y=y, act=None)
elif j < i: elif j < i:
y = x[j] y = x[j]
for k in range(i - j): for k in range(i - j):
if k == i - j - 1: if k == i - j - 1:
y = self.conv_bn_layer(y, y = self.conv_bn_layer(
filter_size=3, y,
num_filters=channels[i], filter_size=3,
stride=2,if_act=False, num_filters=channels[i],
name=name+'_layer_'+str(i+1)+'_'+str(j+1)+'_'+str(k+1)) stride=2,
if_act=False,
name=name + '_layer_' + str(i + 1) + '_' +
str(j + 1) + '_' + str(k + 1))
else: else:
y = self.conv_bn_layer(y, y = self.conv_bn_layer(
filter_size=3, y,
num_filters=channels[j], filter_size=3,
stride=2, num_filters=channels[j],
name=name+'_layer_'+str(i+1)+'_'+str(j+1)+'_'+str(k+1)) stride=2,
name=name + '_layer_' + str(i + 1) + '_' +
str(j + 1) + '_' + str(k + 1))
residual = fluid.layers.elementwise_add( residual = fluid.layers.elementwise_add(
x=residual, y=y, act=None) x=residual, y=y, act=None)
residual = fluid.layers.relu(residual) residual = fluid.layers.relu(residual)
out.append(residual) out.append(residual)
return out return out
def high_resolution_module(self, x, channels, multi_scale_output=True, name=None): def high_resolution_module(self,
x,
channels,
multi_scale_output=True,
name=None):
residual = self.branches(x, 4, channels, name=name) residual = self.branches(x, 4, channels, name=name)
out = self.fuse_layers(residual, channels, multi_scale_output=multi_scale_output, name=name) out = self.fuse_layers(
residual,
channels,
multi_scale_output=multi_scale_output,
name=name)
return out return out
def stage(self, x, num_modules, channels, multi_scale_output=True, name=None): def stage(self,
x,
num_modules,
channels,
multi_scale_output=True,
name=None):
out = x out = x
for i in range(num_modules): for i in range(num_modules):
if i == num_modules - 1 and multi_scale_output == False: if i == num_modules - 1 and multi_scale_output == False:
out = self.high_resolution_module(out, out = self.high_resolution_module(
channels, out,
multi_scale_output=False, channels,
name=name+'_'+str(i+1)) multi_scale_output=False,
name=name + '_' + str(i + 1))
else: else:
out = self.high_resolution_module(out, out = self.high_resolution_module(
channels, out, channels, name=name + '_' + str(i + 1))
name=name+'_'+str(i+1))
return out return out
def last_cls_out(self, x, name=None): def last_cls_out(self, x, name=None):
out = [] out = []
num_filters_list = [128, 256, 512, 1024] num_filters_list = [128, 256, 512, 1024]
for i in range(len(x)): for i in range(len(x)):
out.append(self.conv_bn_layer(input=x[i], out.append(
filter_size=1, self.conv_bn_layer(
num_filters=num_filters_list[i], input=x[i],
name=name+'conv_'+str(i+1))) filter_size=1,
num_filters=num_filters_list[i],
name=name + 'conv_' + str(i + 1)))
return out return out
def basic_block(self,
def basic_block(self, input, num_filters, stride=1, downsample=False, name=None): input,
num_filters,
stride=1,
downsample=False,
name=None):
residual = input residual = input
conv = self.conv_bn_layer(input=input, conv = self.conv_bn_layer(
filter_size=3, input=input,
num_filters=num_filters, filter_size=3,
stride=stride, num_filters=num_filters,
name=name+'_conv1') stride=stride,
conv = self.conv_bn_layer(input=conv, name=name + '_conv1')
filter_size=3, conv = self.conv_bn_layer(
num_filters=num_filters, input=conv,
if_act=False, filter_size=3,
name=name+'_conv2') num_filters=num_filters,
if_act=False,
name=name + '_conv2')
if downsample: if downsample:
residual = self.conv_bn_layer(input=input, residual = self.conv_bn_layer(
filter_size=1, input=input,
num_filters=num_filters, filter_size=1,
if_act=False, num_filters=num_filters,
name=name+'_downsample') if_act=False,
name=name + '_downsample')
if self.has_se: if self.has_se:
conv = self.squeeze_excitation( conv = self.squeeze_excitation(
input=conv, input=conv,
num_channels=num_filters, num_channels=num_filters,
reduction_ratio=16, reduction_ratio=16,
name='fc'+name) name='fc' + name)
return fluid.layers.elementwise_add(x=residual, y=conv, act='relu') return fluid.layers.elementwise_add(x=residual, y=conv, act='relu')
def bottleneck_block(self, input, num_filters, stride=1, downsample=False, name=None): def bottleneck_block(self,
input,
num_filters,
stride=1,
downsample=False,
name=None):
residual = input residual = input
conv = self.conv_bn_layer(input=input, conv = self.conv_bn_layer(
filter_size=1, input=input,
num_filters=num_filters, filter_size=1,
name=name+'_conv1') num_filters=num_filters,
conv = self.conv_bn_layer(input=conv, name=name + '_conv1')
filter_size=3, conv = self.conv_bn_layer(
num_filters=num_filters, input=conv,
stride=stride, filter_size=3,
name=name+'_conv2') num_filters=num_filters,
conv = self.conv_bn_layer(input=conv, stride=stride,
filter_size=1, name=name + '_conv2')
num_filters=num_filters*4, conv = self.conv_bn_layer(
if_act=False, input=conv,
name=name+'_conv3') filter_size=1,
num_filters=num_filters * 4,
if_act=False,
name=name + '_conv3')
if downsample: if downsample:
residual = self.conv_bn_layer(input=input, residual = self.conv_bn_layer(
filter_size=1, input=input,
num_filters=num_filters*4, filter_size=1,
if_act=False, num_filters=num_filters * 4,
name=name+'_downsample') if_act=False,
name=name + '_downsample')
if self.has_se: if self.has_se:
conv = self.squeeze_excitation( conv = self.squeeze_excitation(
input=conv, input=conv,
num_channels=num_filters * 4, num_channels=num_filters * 4,
reduction_ratio=16, reduction_ratio=16,
name='fc'+name) name='fc' + name)
return fluid.layers.elementwise_add(x=residual, y=conv, act='relu') return fluid.layers.elementwise_add(x=residual, y=conv, act='relu')
def squeeze_excitation(self, input, num_channels, reduction_ratio, name=None): def squeeze_excitation(self,
input,
num_channels,
reduction_ratio,
name=None):
pool = fluid.layers.pool2d( pool = fluid.layers.pool2d(
input=input, pool_size=0, pool_type='avg', global_pooling=True) input=input, pool_size=0, pool_type='avg', global_pooling=True)
stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0) stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0)
squeeze = fluid.layers.fc(input=pool, squeeze = fluid.layers.fc(
size=num_channels / reduction_ratio, input=pool,
act='relu', size=num_channels / reduction_ratio,
param_attr=fluid.param_attr.ParamAttr( act='relu',
initializer=fluid.initializer.Uniform( param_attr=fluid.param_attr.ParamAttr(
-stdv, stdv),name=name+'_sqz_weights'), initializer=fluid.initializer.Uniform(-stdv, stdv),
bias_attr=ParamAttr(name=name+'_sqz_offset')) name=name + '_sqz_weights'),
bias_attr=ParamAttr(name=name + '_sqz_offset'))
stdv = 1.0 / math.sqrt(squeeze.shape[1] * 1.0) stdv = 1.0 / math.sqrt(squeeze.shape[1] * 1.0)
excitation = fluid.layers.fc(input=squeeze, excitation = fluid.layers.fc(
size=num_channels, input=squeeze,
act='sigmoid', size=num_channels,
param_attr=fluid.param_attr.ParamAttr( act='sigmoid',
initializer=fluid.initializer.Uniform( param_attr=fluid.param_attr.ParamAttr(
-stdv, stdv),name=name+'_exc_weights'), initializer=fluid.initializer.Uniform(-stdv, stdv),
bias_attr=ParamAttr(name=name+'_exc_offset')) name=name + '_exc_weights'),
bias_attr=ParamAttr(name=name + '_exc_offset'))
scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0) scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0)
return scale return scale
def conv_bn_layer(self,input, filter_size, num_filters, stride=1, padding=1, num_groups=1, if_act=True, name=None): def conv_bn_layer(self,
input,
filter_size,
num_filters,
stride=1,
padding=1,
num_groups=1,
if_act=True,
name=None):
conv = fluid.layers.conv2d( conv = fluid.layers.conv2d(
input=input, input=input,
num_filters=num_filters, num_filters=num_filters,
filter_size=filter_size, filter_size=filter_size,
stride=stride, stride=stride,
padding=(filter_size-1)//2, padding=(filter_size - 1) // 2,
groups=num_groups, groups=num_groups,
act=None, act=None,
param_attr=ParamAttr(initializer=MSRA(), name=name+'_weights'), param_attr=ParamAttr(
initializer=MSRA(), name=name + '_weights'),
bias_attr=False) bias_attr=False)
bn_name = name + '_bn' bn_name = name + '_bn'
bn = self._bn( input=conv, bn_name=bn_name ) bn = self._bn(input=conv, bn_name=bn_name)
if if_act: if if_act:
bn = fluid.layers.relu(bn) bn = fluid.layers.relu(bn)
return bn return bn
def _bn(self, def _bn(self, input, act=None, bn_name=None):
input,
act=None,
bn_name=None):
norm_lr = 0. if self.freeze_norm else 1. norm_lr = 0. if self.freeze_norm else 1.
norm_decay = self.norm_decay norm_decay = self.norm_decay
pattr = ParamAttr( pattr = ParamAttr(
...@@ -336,7 +391,7 @@ class HRNet(object): ...@@ -336,7 +391,7 @@ class HRNet(object):
name=bn_name + '_offset', name=bn_name + '_offset',
learning_rate=norm_lr, learning_rate=norm_lr,
regularizer=L2Decay(norm_decay)) regularizer=L2Decay(norm_decay))
global_stats = True if self.freeze_norm else False global_stats = True if self.freeze_norm else False
out = fluid.layers.batch_norm( out = fluid.layers.batch_norm(
input=input, input=input,
...@@ -353,7 +408,7 @@ class HRNet(object): ...@@ -353,7 +408,7 @@ class HRNet(object):
scale.stop_gradient = True scale.stop_gradient = True
bias.stop_gradient = True bias.stop_gradient = True
return out return out
def __call__(self, input): def __call__(self, input):
assert isinstance(input, Variable) assert isinstance(input, Variable)
assert not (set(self.feature_maps) - set([2, 3, 4, 5])), \ assert not (set(self.feature_maps) - set([2, 3, 4, 5])), \
...@@ -363,15 +418,14 @@ class HRNet(object): ...@@ -363,15 +418,14 @@ class HRNet(object):
res = input res = input
feature_maps = self.feature_maps feature_maps = self.feature_maps
self.net( input ) self.net(input)
for i in feature_maps: for i in feature_maps:
res = self.end_points[i-2] res = self.end_points[i - 2]
if i in self.feature_maps: if i in self.feature_maps:
res_endpoints.append(res) res_endpoints.append(res)
if self.freeze_at >= i: if self.freeze_at >= i:
res.stop_gradient = True res.stop_gradient = True
return OrderedDict([('res{}_sum'.format(self.feature_maps[idx]), feat) return OrderedDict([('res{}_sum'.format(self.feature_maps[idx]), feat)
for idx, feat in enumerate(res_endpoints)]) for idx, feat in enumerate(res_endpoints)])
...@@ -54,31 +54,34 @@ class Res2Net(ResNet): ...@@ -54,31 +54,34 @@ class Res2Net(ResNet):
""" """
__shared__ = ['norm_type', 'freeze_norm', 'weight_prefix_name'] __shared__ = ['norm_type', 'freeze_norm', 'weight_prefix_name']
def __init__(self, def __init__(
depth=50, self,
width=26, depth=50,
scales=4, width=26,
freeze_at=2, scales=4,
norm_type='bn', freeze_at=2,
freeze_norm=True, norm_type='bn',
norm_decay=0., freeze_norm=True,
variant='b', norm_decay=0.,
feature_maps=[2, 3, 4, 5], variant='b',
dcn_v2_stages=[], feature_maps=[2, 3, 4, 5],
weight_prefix_name='', dcn_v2_stages=[],
nonlocal_stages=[],): weight_prefix_name='',
super(Res2Net, self).__init__(depth=depth, nonlocal_stages=[], ):
freeze_at=freeze_at, super(Res2Net, self).__init__(
norm_type=norm_type, depth=depth,
freeze_norm=freeze_norm, freeze_at=freeze_at,
norm_decay=norm_decay, norm_type=norm_type,
variant=variant, freeze_norm=freeze_norm,
feature_maps=feature_maps, norm_decay=norm_decay,
dcn_v2_stages=dcn_v2_stages, variant=variant,
weight_prefix_name=weight_prefix_name, feature_maps=feature_maps,
nonlocal_stages=nonlocal_stages) dcn_v2_stages=dcn_v2_stages,
weight_prefix_name=weight_prefix_name,
assert depth >= 50, "just support depth>=50 in res2net, but got depth=".format(depth) nonlocal_stages=nonlocal_stages)
assert depth >= 50, "just support depth>=50 in res2net, but got depth=".format(
depth)
# res2net config # res2net config
self.scales = scales self.scales = scales
self.width = width self.width = width
...@@ -96,60 +99,62 @@ class Res2Net(ResNet): ...@@ -96,60 +99,62 @@ class Res2Net(ResNet):
name, name,
dcn_v2=False): dcn_v2=False):
conv0 = self._conv_norm( conv0 = self._conv_norm(
input=input, input=input,
num_filters=num_filters1, num_filters=num_filters1,
filter_size=1, filter_size=1,
stride=1, stride=1,
act='relu', act='relu',
name=name + '_branch2a') name=name + '_branch2a')
xs = fluid.layers.split(conv0, self.scales, 1) xs = fluid.layers.split(conv0, self.scales, 1)
ys = [] ys = []
for s in range(self.scales - 1): for s in range(self.scales - 1):
if s == 0 or stride == 2: if s == 0 or stride == 2:
ys.append(self._conv_norm(input=xs[s], ys.append(
num_filters=num_filters1//self.scales, self._conv_norm(
stride=stride, input=xs[s],
filter_size=3, num_filters=num_filters1 // self.scales,
act='relu', stride=stride,
name=name+ '_branch2b_' + str(s+1), filter_size=3,
dcn_v2=dcn_v2)) act='relu',
name=name + '_branch2b_' + str(s + 1),
dcn_v2=dcn_v2))
else: else:
ys.append(self._conv_norm(input=xs[s]+ys[-1], ys.append(
num_filters=num_filters1//self.scales, self._conv_norm(
stride=stride, input=xs[s] + ys[-1],
filter_size=3, num_filters=num_filters1 // self.scales,
act='relu', stride=stride,
name=name+ '_branch2b_' + str(s+1), filter_size=3,
dcn_v2=dcn_v2)) act='relu',
name=name + '_branch2b_' + str(s + 1),
dcn_v2=dcn_v2))
if stride == 1: if stride == 1:
ys.append(xs[-1]) ys.append(xs[-1])
else: else:
ys.append(fluid.layers.pool2d(input=xs[-1], ys.append(
pool_size=3, fluid.layers.pool2d(
pool_stride=stride, input=xs[-1],
pool_padding=1, pool_size=3,
pool_type='avg')) pool_stride=stride,
pool_padding=1,
pool_type='avg'))
conv1 = fluid.layers.concat(ys, axis=1) conv1 = fluid.layers.concat(ys, axis=1)
conv2 = self._conv_norm( conv2 = self._conv_norm(
input=conv1, input=conv1,
num_filters=num_filters2, num_filters=num_filters2,
filter_size=1, filter_size=1,
act=None, act=None,
name=name+"_branch2c") name=name + "_branch2c")
short = self._shortcut(input, short = self._shortcut(
num_filters2, input, num_filters2, stride, is_first, name=name + "_branch1")
stride,
is_first,
name=name + "_branch1")
return fluid.layers.elementwise_add( return fluid.layers.elementwise_add(
x=short, y=conv2, act='relu', name=name + ".add.output.5") x=short, y=conv2, act='relu', name=name + ".add.output.5")
def layer_warp(self, input, stage_num): def layer_warp(self, input, stage_num):
""" """
Args: Args:
...@@ -167,14 +172,15 @@ class Res2Net(ResNet): ...@@ -167,14 +172,15 @@ class Res2Net(ResNet):
ch_out = self.stage_filters[stage_num - 2] ch_out = self.stage_filters[stage_num - 2]
is_first = False if stage_num != 2 else True is_first = False if stage_num != 2 else True
dcn_v2 = True if stage_num in self.dcn_v2_stages else False dcn_v2 = True if stage_num in self.dcn_v2_stages else False
num_filters1 = self.num_filters1[stage_num-2] num_filters1 = self.num_filters1[stage_num - 2]
num_filters2 = self.num_filters2[stage_num-2] num_filters2 = self.num_filters2[stage_num - 2]
nonlocal_mod = 1000 nonlocal_mod = 1000
if stage_num in self.nonlocal_stages: if stage_num in self.nonlocal_stages:
nonlocal_mod = self.nonlocal_mod_cfg[self.depth] if stage_num==4 else 2 nonlocal_mod = self.nonlocal_mod_cfg[
self.depth] if stage_num == 4 else 2
# Make the layer name and parameter name consistent # Make the layer name and parameter name consistent
# with ImageNet pre-trained model # with ImageNet pre-trained model
conv = input conv = input
...@@ -190,14 +196,14 @@ class Res2Net(ResNet): ...@@ -190,14 +196,14 @@ class Res2Net(ResNet):
is_first=is_first, is_first=is_first,
name=conv_name, name=conv_name,
dcn_v2=dcn_v2) dcn_v2=dcn_v2)
# add non local model # add non local model
dim_in = conv.shape[1] dim_in = conv.shape[1]
nonlocal_name = "nonlocal_conv{}".format( stage_num ) nonlocal_name = "nonlocal_conv{}".format(stage_num)
if i % nonlocal_mod == nonlocal_mod - 1: if i % nonlocal_mod == nonlocal_mod - 1:
conv = add_space_nonlocal( conv = add_space_nonlocal(conv, dim_in, dim_in,
conv, dim_in, dim_in, nonlocal_name + '_{}'.format(i),
nonlocal_name + '_{}'.format(i), int(dim_in / 2) ) int(dim_in / 2))
return conv return conv
...@@ -217,7 +223,7 @@ class Res2NetC5(Res2Net): ...@@ -217,7 +223,7 @@ class Res2NetC5(Res2Net):
variant='b', variant='b',
feature_maps=[5], feature_maps=[5],
weight_prefix_name=''): weight_prefix_name=''):
super(Res2NetC5, self).__init__(depth, width, scales, super(Res2NetC5, self).__init__(depth, width, scales, freeze_at,
freeze_at, norm_type, freeze_norm, norm_type, freeze_norm, norm_decay,
norm_decay, variant, feature_maps) variant, feature_maps)
self.severed_head = True self.severed_head = True
...@@ -36,16 +36,25 @@ class IouLoss(object): ...@@ -36,16 +36,25 @@ class IouLoss(object):
max_height (int): max height of input to support random shape input max_height (int): max height of input to support random shape input
max_width (int): max width of input to support random shape input max_width (int): max width of input to support random shape input
""" """
def __init__(self,
loss_weight=2.5, def __init__(self, loss_weight=2.5, max_height=608, max_width=608):
max_height=608,
max_width=608):
self._loss_weight = loss_weight self._loss_weight = loss_weight
self._MAX_HI = max_height self._MAX_HI = max_height
self._MAX_WI = max_width self._MAX_WI = max_width
def __call__(self, x, y, w, h, tx, ty, tw, th, def __call__(self,
anchors, downsample_ratio, batch_size, eps=1.e-10): x,
y,
w,
h,
tx,
ty,
tw,
th,
anchors,
downsample_ratio,
batch_size,
eps=1.e-10):
''' '''
Args: Args:
x | y | w | h ([Variables]): the output of yolov3 for encoded x|y|w|h x | y | w | h ([Variables]): the output of yolov3 for encoded x|y|w|h
...@@ -55,10 +64,10 @@ class IouLoss(object): ...@@ -55,10 +64,10 @@ class IouLoss(object):
batch_size (int): training batch size batch_size (int): training batch size
eps (float): the decimal to prevent the denominator eqaul zero eps (float): the decimal to prevent the denominator eqaul zero
''' '''
x1, y1, x2, y2 = self._bbox_transform(x, y, w, h, anchors, x1, y1, x2, y2 = self._bbox_transform(
downsample_ratio, batch_size, False) x, y, w, h, anchors, downsample_ratio, batch_size, False)
x1g, y1g, x2g, y2g = self._bbox_transform(tx, ty, tw, th, x1g, y1g, x2g, y2g = self._bbox_transform(
anchors, downsample_ratio, batch_size, True) tx, ty, tw, th, anchors, downsample_ratio, batch_size, True)
x2 = fluid.layers.elementwise_max(x1, x2) x2 = fluid.layers.elementwise_max(x1, x2)
y2 = fluid.layers.elementwise_max(y1, y2) y2 = fluid.layers.elementwise_max(y1, y2)
...@@ -76,14 +85,16 @@ class IouLoss(object): ...@@ -76,14 +85,16 @@ class IouLoss(object):
intsctk = (xkis2 - xkis1) * (ykis2 - ykis1) intsctk = (xkis2 - xkis1) * (ykis2 - ykis1)
intsctk = intsctk * fluid.layers.greater_than( intsctk = intsctk * fluid.layers.greater_than(
xkis2, xkis1) * fluid.layers.greater_than(ykis2, ykis1) xkis2, xkis1) * fluid.layers.greater_than(ykis2, ykis1)
unionk = (x2 - x1) * (y2 - y1) + (x2g - x1g) * (y2g - y1g) - intsctk + eps unionk = (x2 - x1) * (y2 - y1) + (x2g - x1g) * (y2g - y1g
) - intsctk + eps
iouk = intsctk / unionk iouk = intsctk / unionk
loss_iou = 1. - iouk * iouk loss_iou = 1. - iouk * iouk
loss_iou = loss_iou * self._loss_weight loss_iou = loss_iou * self._loss_weight
return loss_iou return loss_iou
def _bbox_transform(self, dcx, dcy, dw, dh, anchors, downsample_ratio, batch_size, is_gt): def _bbox_transform(self, dcx, dcy, dw, dh, anchors, downsample_ratio,
batch_size, is_gt):
grid_x = int(self._MAX_WI / downsample_ratio) grid_x = int(self._MAX_WI / downsample_ratio)
grid_y = int(self._MAX_HI / downsample_ratio) grid_y = int(self._MAX_HI / downsample_ratio)
an_num = len(anchors) // 2 an_num = len(anchors) // 2
...@@ -125,14 +136,16 @@ class IouLoss(object): ...@@ -125,14 +136,16 @@ class IouLoss(object):
anchor_w_np = np.array(anchor_w_) anchor_w_np = np.array(anchor_w_)
anchor_w_np = np.reshape(anchor_w_np, newshape=[1, an_num, 1, 1]) anchor_w_np = np.reshape(anchor_w_np, newshape=[1, an_num, 1, 1])
anchor_w_np = np.tile(anchor_w_np, reps=[batch_size, 1, grid_y, grid_x]) anchor_w_np = np.tile(anchor_w_np, reps=[batch_size, 1, grid_y, grid_x])
anchor_w_max = self._create_tensor_from_numpy(anchor_w_np.astype(np.float32)) anchor_w_max = self._create_tensor_from_numpy(
anchor_w_np.astype(np.float32))
anchor_w = fluid.layers.crop(x=anchor_w_max, shape=dcx) anchor_w = fluid.layers.crop(x=anchor_w_max, shape=dcx)
anchor_w.stop_gradient = True anchor_w.stop_gradient = True
anchor_h_ = [anchors[i] for i in range(0, len(anchors)) if i % 2 == 1] anchor_h_ = [anchors[i] for i in range(0, len(anchors)) if i % 2 == 1]
anchor_h_np = np.array(anchor_h_) anchor_h_np = np.array(anchor_h_)
anchor_h_np = np.reshape(anchor_h_np, newshape=[1, an_num, 1, 1]) anchor_h_np = np.reshape(anchor_h_np, newshape=[1, an_num, 1, 1])
anchor_h_np = np.tile(anchor_h_np, reps=[batch_size, 1, grid_y, grid_x]) anchor_h_np = np.tile(anchor_h_np, reps=[batch_size, 1, grid_y, grid_x])
anchor_h_max = self._create_tensor_from_numpy(anchor_h_np.astype(np.float32)) anchor_h_max = self._create_tensor_from_numpy(
anchor_h_np.astype(np.float32))
anchor_h = fluid.layers.crop(x=anchor_h_max, shape=dcx) anchor_h = fluid.layers.crop(x=anchor_h_max, shape=dcx)
anchor_h.stop_gradient = True anchor_h.stop_gradient = True
# e^tw e^th # e^tw e^th
...@@ -147,7 +160,6 @@ class IouLoss(object): ...@@ -147,7 +160,6 @@ class IouLoss(object):
exp_dh.stop_gradient = True exp_dh.stop_gradient = True
pw.stop_gradient = True pw.stop_gradient = True
ph.stop_gradient = True ph.stop_gradient = True
x1 = cx - 0.5 * pw x1 = cx - 0.5 * pw
y1 = cy - 0.5 * ph y1 = cy - 0.5 * ph
...@@ -169,4 +181,3 @@ class IouLoss(object): ...@@ -169,4 +181,3 @@ class IouLoss(object):
default_initializer=NumpyArrayInitializer(numpy_array)) default_initializer=NumpyArrayInitializer(numpy_array))
paddle_array.stop_gradient = True paddle_array.stop_gradient = True
return paddle_array return paddle_array
...@@ -131,8 +131,8 @@ class YOLOv3Loss(object): ...@@ -131,8 +131,8 @@ class YOLOv3Loss(object):
loss_h = fluid.layers.abs(h - th) * tscale_tobj loss_h = fluid.layers.abs(h - th) * tscale_tobj
loss_h = fluid.layers.reduce_sum(loss_h, dim=[1, 2, 3]) loss_h = fluid.layers.reduce_sum(loss_h, dim=[1, 2, 3])
if self._iou_loss is not None: if self._iou_loss is not None:
loss_iou = self._iou_loss(x, y, w, h, tx, ty, tw, th, loss_iou = self._iou_loss(x, y, w, h, tx, ty, tw, th, anchors,
anchors, downsample, self._batch_size) downsample, self._batch_size)
loss_iou = loss_iou * tscale_tobj loss_iou = loss_iou * tscale_tobj
loss_iou = fluid.layers.reduce_sum(loss_iou, dim=[1, 2, 3]) loss_iou = fluid.layers.reduce_sum(loss_iou, dim=[1, 2, 3])
loss_ious.append(fluid.layers.reduce_mean(loss_iou)) loss_ious.append(fluid.layers.reduce_mean(loss_iou))
......
...@@ -219,29 +219,33 @@ class CascadeBBoxHead(object): ...@@ -219,29 +219,33 @@ class CascadeBBoxHead(object):
return {'bbox': box_out, 'score': boxes_cls_prob_mean} return {'bbox': box_out, 'score': boxes_cls_prob_mean}
pred_result = self.nms(bboxes=box_out, scores=boxes_cls_prob_mean) pred_result = self.nms(bboxes=box_out, scores=boxes_cls_prob_mean)
return {"bbox": pred_result} return {"bbox": pred_result}
def get_prediction_cls_aware(self, def get_prediction_cls_aware(self, im_info, im_shape, cascade_cls_prob,
im_info, cascade_decoded_box, cascade_bbox_reg_weights):
im_shape,
cascade_cls_prob,
cascade_decoded_box,
cascade_bbox_reg_weights):
''' '''
get_prediction_cls_aware: predict bbox for each class get_prediction_cls_aware: predict bbox for each class
''' '''
cascade_num_stage = 3 cascade_num_stage = 3
cascade_eval_weight = [0.2, 0.3, 0.5] cascade_eval_weight = [0.2, 0.3, 0.5]
# merge 3 stages results # merge 3 stages results
sum_cascade_cls_prob = sum([ prob*cascade_eval_weight[idx] for idx, prob in enumerate(cascade_cls_prob) ]) sum_cascade_cls_prob = sum([
sum_cascade_decoded_box = sum([ bbox*cascade_eval_weight[idx] for idx, bbox in enumerate(cascade_decoded_box) ]) prob * cascade_eval_weight[idx]
for idx, prob in enumerate(cascade_cls_prob)
])
sum_cascade_decoded_box = sum([
bbox * cascade_eval_weight[idx]
for idx, bbox in enumerate(cascade_decoded_box)
])
self.im_scale = fluid.layers.slice(im_info, [1], starts=[2], ends=[3]) self.im_scale = fluid.layers.slice(im_info, [1], starts=[2], ends=[3])
im_scale_lod = fluid.layers.sequence_expand(self.im_scale, sum_cascade_decoded_box) im_scale_lod = fluid.layers.sequence_expand(self.im_scale,
sum_cascade_decoded_box)
sum_cascade_decoded_box = sum_cascade_decoded_box / im_scale_lod sum_cascade_decoded_box = sum_cascade_decoded_box / im_scale_lod
decoded_bbox = sum_cascade_decoded_box decoded_bbox = sum_cascade_decoded_box
decoded_bbox = fluid.layers.reshape(decoded_bbox, shape=(-1, self.num_classes, 4) ) decoded_bbox = fluid.layers.reshape(
decoded_bbox, shape=(-1, self.num_classes, 4))
box_out = fluid.layers.box_clip(input=decoded_bbox, im_info=im_shape) box_out = fluid.layers.box_clip(input=decoded_bbox, im_info=im_shape)
pred_result = self.nms(bboxes=box_out, scores=sum_cascade_cls_prob) pred_result = self.nms(bboxes=box_out, scores=sum_cascade_cls_prob)
return {"bbox": pred_result} return {"bbox": pred_result}
......
...@@ -26,519 +26,518 @@ from .coco_eval import bbox2out ...@@ -26,519 +26,518 @@ from .coco_eval import bbox2out
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
__all__ = ['bbox2out', 'get_category_info'] __all__ = ['bbox2out', 'get_category_info']
def get_category_info(anno_file=None, def get_category_info(anno_file=None,
with_background=True, with_background=True,
use_default_label=False): use_default_label=False):
clsid2catid = { k:k for k in range(1, 501) } clsid2catid = {k: k for k in range(1, 501)}
catid2name = { catid2name = {
0:"background", 0: "background",
1:"Infant bed", 1: "Infant bed",
2:"Rose", 2: "Rose",
3:"Flag", 3: "Flag",
4:"Flashlight", 4: "Flashlight",
5:"Sea turtle", 5: "Sea turtle",
6:"Camera", 6: "Camera",
7:"Animal", 7: "Animal",
8:"Glove", 8: "Glove",
9:"Crocodile", 9: "Crocodile",
10:"Cattle", 10: "Cattle",
11:"House", 11: "House",
12:"Guacamole", 12: "Guacamole",
13:"Penguin", 13: "Penguin",
14:"Vehicle registration plate", 14: "Vehicle registration plate",
15:"Bench", 15: "Bench",
16:"Ladybug", 16: "Ladybug",
17:"Human nose", 17: "Human nose",
18:"Watermelon", 18: "Watermelon",
19:"Flute", 19: "Flute",
20:"Butterfly", 20: "Butterfly",
21:"Washing machine", 21: "Washing machine",
22:"Raccoon", 22: "Raccoon",
23:"Segway", 23: "Segway",
24:"Taco", 24: "Taco",
25:"Jellyfish", 25: "Jellyfish",
26:"Cake", 26: "Cake",
27:"Pen", 27: "Pen",
28:"Cannon", 28: "Cannon",
29:"Bread", 29: "Bread",
30:"Tree", 30: "Tree",
31:"Shellfish", 31: "Shellfish",
32:"Bed", 32: "Bed",
33:"Hamster", 33: "Hamster",
34:"Hat", 34: "Hat",
35:"Toaster", 35: "Toaster",
36:"Sombrero", 36: "Sombrero",
37:"Tiara", 37: "Tiara",
38:"Bowl", 38: "Bowl",
39:"Dragonfly", 39: "Dragonfly",
40:"Moths and butterflies", 40: "Moths and butterflies",
41:"Antelope", 41: "Antelope",
42:"Vegetable", 42: "Vegetable",
43:"Torch", 43: "Torch",
44:"Building", 44: "Building",
45:"Power plugs and sockets", 45: "Power plugs and sockets",
46:"Blender", 46: "Blender",
47:"Billiard table", 47: "Billiard table",
48:"Cutting board", 48: "Cutting board",
49:"Bronze sculpture", 49: "Bronze sculpture",
50:"Turtle", 50: "Turtle",
51:"Broccoli", 51: "Broccoli",
52:"Tiger", 52: "Tiger",
53:"Mirror", 53: "Mirror",
54:"Bear", 54: "Bear",
55:"Zucchini", 55: "Zucchini",
56:"Dress", 56: "Dress",
57:"Volleyball", 57: "Volleyball",
58:"Guitar", 58: "Guitar",
59:"Reptile", 59: "Reptile",
60:"Golf cart", 60: "Golf cart",
61:"Tart", 61: "Tart",
62:"Fedora", 62: "Fedora",
63:"Carnivore", 63: "Carnivore",
64:"Car", 64: "Car",
65:"Lighthouse", 65: "Lighthouse",
66:"Coffeemaker", 66: "Coffeemaker",
67:"Food processor", 67: "Food processor",
68:"Truck", 68: "Truck",
69:"Bookcase", 69: "Bookcase",
70:"Surfboard", 70: "Surfboard",
71:"Footwear", 71: "Footwear",
72:"Bench", 72: "Bench",
73:"Necklace", 73: "Necklace",
74:"Flower", 74: "Flower",
75:"Radish", 75: "Radish",
76:"Marine mammal", 76: "Marine mammal",
77:"Frying pan", 77: "Frying pan",
78:"Tap", 78: "Tap",
79:"Peach", 79: "Peach",
80:"Knife", 80: "Knife",
81:"Handbag", 81: "Handbag",
82:"Laptop", 82: "Laptop",
83:"Tent", 83: "Tent",
84:"Ambulance", 84: "Ambulance",
85:"Christmas tree", 85: "Christmas tree",
86:"Eagle", 86: "Eagle",
87:"Limousine", 87: "Limousine",
88:"Kitchen & dining room table", 88: "Kitchen & dining room table",
89:"Polar bear", 89: "Polar bear",
90:"Tower", 90: "Tower",
91:"Football", 91: "Football",
92:"Willow", 92: "Willow",
93:"Human head", 93: "Human head",
94:"Stop sign", 94: "Stop sign",
95:"Banana", 95: "Banana",
96:"Mixer", 96: "Mixer",
97:"Binoculars", 97: "Binoculars",
98:"Dessert", 98: "Dessert",
99:"Bee", 99: "Bee",
100:"Chair", 100: "Chair",
101:"Wood-burning stove", 101: "Wood-burning stove",
102:"Flowerpot", 102: "Flowerpot",
103:"Beaker", 103: "Beaker",
104:"Oyster", 104: "Oyster",
105:"Woodpecker", 105: "Woodpecker",
106:"Harp", 106: "Harp",
107:"Bathtub", 107: "Bathtub",
108:"Wall clock", 108: "Wall clock",
109:"Sports uniform", 109: "Sports uniform",
110:"Rhinoceros", 110: "Rhinoceros",
111:"Beehive", 111: "Beehive",
112:"Cupboard", 112: "Cupboard",
113:"Chicken", 113: "Chicken",
114:"Man", 114: "Man",
115:"Blue jay", 115: "Blue jay",
116:"Cucumber", 116: "Cucumber",
117:"Balloon", 117: "Balloon",
118:"Kite", 118: "Kite",
119:"Fireplace", 119: "Fireplace",
120:"Lantern", 120: "Lantern",
121:"Missile", 121: "Missile",
122:"Book", 122: "Book",
123:"Spoon", 123: "Spoon",
124:"Grapefruit", 124: "Grapefruit",
125:"Squirrel", 125: "Squirrel",
126:"Orange", 126: "Orange",
127:"Coat", 127: "Coat",
128:"Punching bag", 128: "Punching bag",
129:"Zebra", 129: "Zebra",
130:"Billboard", 130: "Billboard",
131:"Bicycle", 131: "Bicycle",
132:"Door handle", 132: "Door handle",
133:"Mechanical fan", 133: "Mechanical fan",
134:"Ring binder", 134: "Ring binder",
135:"Table", 135: "Table",
136:"Parrot", 136: "Parrot",
137:"Sock", 137: "Sock",
138:"Vase", 138: "Vase",
139:"Weapon", 139: "Weapon",
140:"Shotgun", 140: "Shotgun",
141:"Glasses", 141: "Glasses",
142:"Seahorse", 142: "Seahorse",
143:"Belt", 143: "Belt",
144:"Watercraft", 144: "Watercraft",
145:"Window", 145: "Window",
146:"Giraffe", 146: "Giraffe",
147:"Lion", 147: "Lion",
148:"Tire", 148: "Tire",
149:"Vehicle", 149: "Vehicle",
150:"Canoe", 150: "Canoe",
151:"Tie", 151: "Tie",
152:"Shelf", 152: "Shelf",
153:"Picture frame", 153: "Picture frame",
154:"Printer", 154: "Printer",
155:"Human leg", 155: "Human leg",
156:"Boat", 156: "Boat",
157:"Slow cooker", 157: "Slow cooker",
158:"Croissant", 158: "Croissant",
159:"Candle", 159: "Candle",
160:"Pancake", 160: "Pancake",
161:"Pillow", 161: "Pillow",
162:"Coin", 162: "Coin",
163:"Stretcher", 163: "Stretcher",
164:"Sandal", 164: "Sandal",
165:"Woman", 165: "Woman",
166:"Stairs", 166: "Stairs",
167:"Harpsichord", 167: "Harpsichord",
168:"Stool", 168: "Stool",
169:"Bus", 169: "Bus",
170:"Suitcase", 170: "Suitcase",
171:"Human mouth", 171: "Human mouth",
172:"Juice", 172: "Juice",
173:"Skull", 173: "Skull",
174:"Door", 174: "Door",
175:"Violin", 175: "Violin",
176:"Chopsticks", 176: "Chopsticks",
177:"Digital clock", 177: "Digital clock",
178:"Sunflower", 178: "Sunflower",
179:"Leopard", 179: "Leopard",
180:"Bell pepper", 180: "Bell pepper",
181:"Harbor seal", 181: "Harbor seal",
182:"Snake", 182: "Snake",
183:"Sewing machine", 183: "Sewing machine",
184:"Goose", 184: "Goose",
185:"Helicopter", 185: "Helicopter",
186:"Seat belt", 186: "Seat belt",
187:"Coffee cup", 187: "Coffee cup",
188:"Microwave oven", 188: "Microwave oven",
189:"Hot dog", 189: "Hot dog",
190:"Countertop", 190: "Countertop",
191:"Serving tray", 191: "Serving tray",
192:"Dog bed", 192: "Dog bed",
193:"Beer", 193: "Beer",
194:"Sunglasses", 194: "Sunglasses",
195:"Golf ball", 195: "Golf ball",
196:"Waffle", 196: "Waffle",
197:"Palm tree", 197: "Palm tree",
198:"Trumpet", 198: "Trumpet",
199:"Ruler", 199: "Ruler",
200:"Helmet", 200: "Helmet",
201:"Ladder", 201: "Ladder",
202:"Office building", 202: "Office building",
203:"Tablet computer", 203: "Tablet computer",
204:"Toilet paper", 204: "Toilet paper",
205:"Pomegranate", 205: "Pomegranate",
206:"Skirt", 206: "Skirt",
207:"Gas stove", 207: "Gas stove",
208:"Cookie", 208: "Cookie",
209:"Cart", 209: "Cart",
210:"Raven", 210: "Raven",
211:"Egg", 211: "Egg",
212:"Burrito", 212: "Burrito",
213:"Goat", 213: "Goat",
214:"Kitchen knife", 214: "Kitchen knife",
215:"Skateboard", 215: "Skateboard",
216:"Salt and pepper shakers", 216: "Salt and pepper shakers",
217:"Lynx", 217: "Lynx",
218:"Boot", 218: "Boot",
219:"Platter", 219: "Platter",
220:"Ski", 220: "Ski",
221:"Swimwear", 221: "Swimwear",
222:"Swimming pool", 222: "Swimming pool",
223:"Drinking straw", 223: "Drinking straw",
224:"Wrench", 224: "Wrench",
225:"Drum", 225: "Drum",
226:"Ant", 226: "Ant",
227:"Human ear", 227: "Human ear",
228:"Headphones", 228: "Headphones",
229:"Fountain", 229: "Fountain",
230:"Bird", 230: "Bird",
231:"Jeans", 231: "Jeans",
232:"Television", 232: "Television",
233:"Crab", 233: "Crab",
234:"Microphone", 234: "Microphone",
235:"Home appliance", 235: "Home appliance",
236:"Snowplow", 236: "Snowplow",
237:"Beetle", 237: "Beetle",
238:"Artichoke", 238: "Artichoke",
239:"Jet ski", 239: "Jet ski",
240:"Stationary bicycle", 240: "Stationary bicycle",
241:"Human hair", 241: "Human hair",
242:"Brown bear", 242: "Brown bear",
243:"Starfish", 243: "Starfish",
244:"Fork", 244: "Fork",
245:"Lobster", 245: "Lobster",
246:"Corded phone", 246: "Corded phone",
247:"Drink", 247: "Drink",
248:"Saucer", 248: "Saucer",
249:"Carrot", 249: "Carrot",
250:"Insect", 250: "Insect",
251:"Clock", 251: "Clock",
252:"Castle", 252: "Castle",
253:"Tennis racket", 253: "Tennis racket",
254:"Ceiling fan", 254: "Ceiling fan",
255:"Asparagus", 255: "Asparagus",
256:"Jaguar", 256: "Jaguar",
257:"Musical instrument", 257: "Musical instrument",
258:"Train", 258: "Train",
259:"Cat", 259: "Cat",
260:"Rifle", 260: "Rifle",
261:"Dumbbell", 261: "Dumbbell",
262:"Mobile phone", 262: "Mobile phone",
263:"Taxi", 263: "Taxi",
264:"Shower", 264: "Shower",
265:"Pitcher", 265: "Pitcher",
266:"Lemon", 266: "Lemon",
267:"Invertebrate", 267: "Invertebrate",
268:"Turkey", 268: "Turkey",
269:"High heels", 269: "High heels",
270:"Bust", 270: "Bust",
271:"Elephant", 271: "Elephant",
272:"Scarf", 272: "Scarf",
273:"Barrel", 273: "Barrel",
274:"Trombone", 274: "Trombone",
275:"Pumpkin", 275: "Pumpkin",
276:"Box", 276: "Box",
277:"Tomato", 277: "Tomato",
278:"Frog", 278: "Frog",
279:"Bidet", 279: "Bidet",
280:"Human face", 280: "Human face",
281:"Houseplant", 281: "Houseplant",
282:"Van", 282: "Van",
283:"Shark", 283: "Shark",
284:"Ice cream", 284: "Ice cream",
285:"Swim cap", 285: "Swim cap",
286:"Falcon", 286: "Falcon",
287:"Ostrich", 287: "Ostrich",
288:"Handgun", 288: "Handgun",
289:"Whiteboard", 289: "Whiteboard",
290:"Lizard", 290: "Lizard",
291:"Pasta", 291: "Pasta",
292:"Snowmobile", 292: "Snowmobile",
293:"Light bulb", 293: "Light bulb",
294:"Window blind", 294: "Window blind",
295:"Muffin", 295: "Muffin",
296:"Pretzel", 296: "Pretzel",
297:"Computer monitor", 297: "Computer monitor",
298:"Horn", 298: "Horn",
299:"Furniture", 299: "Furniture",
300:"Sandwich", 300: "Sandwich",
301:"Fox", 301: "Fox",
302:"Convenience store", 302: "Convenience store",
303:"Fish", 303: "Fish",
304:"Fruit", 304: "Fruit",
305:"Earrings", 305: "Earrings",
306:"Curtain", 306: "Curtain",
307:"Grape", 307: "Grape",
308:"Sofa bed", 308: "Sofa bed",
309:"Horse", 309: "Horse",
310:"Luggage and bags", 310: "Luggage and bags",
311:"Desk", 311: "Desk",
312:"Crutch", 312: "Crutch",
313:"Bicycle helmet", 313: "Bicycle helmet",
314:"Tick", 314: "Tick",
315:"Airplane", 315: "Airplane",
316:"Canary", 316: "Canary",
317:"Spatula", 317: "Spatula",
318:"Watch", 318: "Watch",
319:"Lily", 319: "Lily",
320:"Kitchen appliance", 320: "Kitchen appliance",
321:"Filing cabinet", 321: "Filing cabinet",
322:"Aircraft", 322: "Aircraft",
323:"Cake stand", 323: "Cake stand",
324:"Candy", 324: "Candy",
325:"Sink", 325: "Sink",
326:"Mouse", 326: "Mouse",
327:"Wine", 327: "Wine",
328:"Wheelchair", 328: "Wheelchair",
329:"Goldfish", 329: "Goldfish",
330:"Refrigerator", 330: "Refrigerator",
331:"French fries", 331: "French fries",
332:"Drawer", 332: "Drawer",
333:"Treadmill", 333: "Treadmill",
334:"Picnic basket", 334: "Picnic basket",
335:"Dice", 335: "Dice",
336:"Cabbage", 336: "Cabbage",
337:"Football helmet", 337: "Football helmet",
338:"Pig", 338: "Pig",
339:"Person", 339: "Person",
340:"Shorts", 340: "Shorts",
341:"Gondola", 341: "Gondola",
342:"Honeycomb", 342: "Honeycomb",
343:"Doughnut", 343: "Doughnut",
344:"Chest of drawers", 344: "Chest of drawers",
345:"Land vehicle", 345: "Land vehicle",
346:"Bat", 346: "Bat",
347:"Monkey", 347: "Monkey",
348:"Dagger", 348: "Dagger",
349:"Tableware", 349: "Tableware",
350:"Human foot", 350: "Human foot",
351:"Mug", 351: "Mug",
352:"Alarm clock", 352: "Alarm clock",
353:"Pressure cooker", 353: "Pressure cooker",
354:"Human hand", 354: "Human hand",
355:"Tortoise", 355: "Tortoise",
356:"Baseball glove", 356: "Baseball glove",
357:"Sword", 357: "Sword",
358:"Pear", 358: "Pear",
359:"Miniskirt", 359: "Miniskirt",
360:"Traffic sign", 360: "Traffic sign",
361:"Girl", 361: "Girl",
362:"Roller skates", 362: "Roller skates",
363:"Dinosaur", 363: "Dinosaur",
364:"Porch", 364: "Porch",
365:"Human beard", 365: "Human beard",
366:"Submarine sandwich", 366: "Submarine sandwich",
367:"Screwdriver", 367: "Screwdriver",
368:"Strawberry", 368: "Strawberry",
369:"Wine glass", 369: "Wine glass",
370:"Seafood", 370: "Seafood",
371:"Racket", 371: "Racket",
372:"Wheel", 372: "Wheel",
373:"Sea lion", 373: "Sea lion",
374:"Toy", 374: "Toy",
375:"Tea", 375: "Tea",
376:"Tennis ball", 376: "Tennis ball",
377:"Waste container", 377: "Waste container",
378:"Mule", 378: "Mule",
379:"Cricket ball", 379: "Cricket ball",
380:"Pineapple", 380: "Pineapple",
381:"Coconut", 381: "Coconut",
382:"Doll", 382: "Doll",
383:"Coffee table", 383: "Coffee table",
384:"Snowman", 384: "Snowman",
385:"Lavender", 385: "Lavender",
386:"Shrimp", 386: "Shrimp",
387:"Maple", 387: "Maple",
388:"Cowboy hat", 388: "Cowboy hat",
389:"Goggles", 389: "Goggles",
390:"Rugby ball", 390: "Rugby ball",
391:"Caterpillar", 391: "Caterpillar",
392:"Poster", 392: "Poster",
393:"Rocket", 393: "Rocket",
394:"Organ", 394: "Organ",
395:"Saxophone", 395: "Saxophone",
396:"Traffic light", 396: "Traffic light",
397:"Cocktail", 397: "Cocktail",
398:"Plastic bag", 398: "Plastic bag",
399:"Squash", 399: "Squash",
400:"Mushroom", 400: "Mushroom",
401:"Hamburger", 401: "Hamburger",
402:"Light switch", 402: "Light switch",
403:"Parachute", 403: "Parachute",
404:"Teddy bear", 404: "Teddy bear",
405:"Winter melon", 405: "Winter melon",
406:"Deer", 406: "Deer",
407:"Musical keyboard", 407: "Musical keyboard",
408:"Plumbing fixture", 408: "Plumbing fixture",
409:"Scoreboard", 409: "Scoreboard",
410:"Baseball bat", 410: "Baseball bat",
411:"Envelope", 411: "Envelope",
412:"Adhesive tape", 412: "Adhesive tape",
413:"Briefcase", 413: "Briefcase",
414:"Paddle", 414: "Paddle",
415:"Bow and arrow", 415: "Bow and arrow",
416:"Telephone", 416: "Telephone",
417:"Sheep", 417: "Sheep",
418:"Jacket", 418: "Jacket",
419:"Boy", 419: "Boy",
420:"Pizza", 420: "Pizza",
421:"Otter", 421: "Otter",
422:"Office supplies", 422: "Office supplies",
423:"Couch", 423: "Couch",
424:"Cello", 424: "Cello",
425:"Bull", 425: "Bull",
426:"Camel", 426: "Camel",
427:"Ball", 427: "Ball",
428:"Duck", 428: "Duck",
429:"Whale", 429: "Whale",
430:"Shirt", 430: "Shirt",
431:"Tank", 431: "Tank",
432:"Motorcycle", 432: "Motorcycle",
433:"Accordion", 433: "Accordion",
434:"Owl", 434: "Owl",
435:"Porcupine", 435: "Porcupine",
436:"Sun hat", 436: "Sun hat",
437:"Nail", 437: "Nail",
438:"Scissors", 438: "Scissors",
439:"Swan", 439: "Swan",
440:"Lamp", 440: "Lamp",
441:"Crown", 441: "Crown",
442:"Piano", 442: "Piano",
443:"Sculpture", 443: "Sculpture",
444:"Cheetah", 444: "Cheetah",
445:"Oboe", 445: "Oboe",
446:"Tin can", 446: "Tin can",
447:"Mango", 447: "Mango",
448:"Tripod", 448: "Tripod",
449:"Oven", 449: "Oven",
450:"Mouse", 450: "Mouse",
451:"Barge", 451: "Barge",
452:"Coffee", 452: "Coffee",
453:"Snowboard", 453: "Snowboard",
454:"Common fig", 454: "Common fig",
455:"Salad", 455: "Salad",
456:"Marine invertebrates", 456: "Marine invertebrates",
457:"Umbrella", 457: "Umbrella",
458:"Kangaroo", 458: "Kangaroo",
459:"Human arm", 459: "Human arm",
460:"Measuring cup", 460: "Measuring cup",
461:"Snail", 461: "Snail",
462:"Loveseat", 462: "Loveseat",
463:"Suit", 463: "Suit",
464:"Teapot", 464: "Teapot",
465:"Bottle", 465: "Bottle",
466:"Alpaca", 466: "Alpaca",
467:"Kettle", 467: "Kettle",
468:"Trousers", 468: "Trousers",
469:"Popcorn", 469: "Popcorn",
470:"Centipede", 470: "Centipede",
471:"Spider", 471: "Spider",
472:"Sparrow", 472: "Sparrow",
473:"Plate", 473: "Plate",
474:"Bagel", 474: "Bagel",
475:"Personal care", 475: "Personal care",
476:"Apple", 476: "Apple",
477:"Brassiere", 477: "Brassiere",
478:"Bathroom cabinet", 478: "Bathroom cabinet",
479:"studio couch", 479: "studio couch",
480:"Computer keyboard", 480: "Computer keyboard",
481:"Table tennis racket", 481: "Table tennis racket",
482:"Sushi", 482: "Sushi",
483:"Cabinetry", 483: "Cabinetry",
484:"Street light", 484: "Street light",
485:"Towel", 485: "Towel",
486:"Nightstand", 486: "Nightstand",
487:"Rabbit", 487: "Rabbit",
488:"Dolphin", 488: "Dolphin",
489:"Dog", 489: "Dog",
490:"Jug", 490: "Jug",
491:"Wok", 491: "Wok",
492:"Fire hydrant", 492: "Fire hydrant",
493:"Human eye", 493: "Human eye",
494:"Skyscraper", 494: "Skyscraper",
495:"Backpack", 495: "Backpack",
496:"Potato", 496: "Potato",
497:"Paper towel", 497: "Paper towel",
498:"Lifejacket", 498: "Lifejacket",
499:"Bicycle wheel", 499: "Bicycle wheel",
500:"Toilet", 500: "Toilet",
} }
if not with_background: if not with_background:
clsid2catid = {k - 1: v for k, v in clsid2catid.items()} clsid2catid = {k - 1: v for k, v in clsid2catid.items()}
return clsid2catid, catid2name return clsid2catid, catid2name
\ No newline at end of file
...@@ -35,7 +35,6 @@ set_paddle_flags( ...@@ -35,7 +35,6 @@ set_paddle_flags(
FLAGS_eager_delete_tensor_gb=0, # enable GC to save memory FLAGS_eager_delete_tensor_gb=0, # enable GC to save memory
) )
from paddle import fluid from paddle import fluid
from ppdet.experimental import mixed_precision_context from ppdet.experimental import mixed_precision_context
from ppdet.core.workspace import load_config, merge_config, create from ppdet.core.workspace import load_config, merge_config, create
...@@ -85,11 +84,16 @@ def main(): ...@@ -85,11 +84,16 @@ def main():
eval_prog = eval_prog.clone(True) eval_prog = eval_prog.clone(True)
if FLAGS.print_params: if FLAGS.print_params:
print("-------------------------All parameters in current graph----------------------") print(
"-------------------------All parameters in current graph----------------------"
)
for block in eval_prog.blocks: for block in eval_prog.blocks:
for param in block.all_parameters(): for param in block.all_parameters():
print("parameter name: {}\tshape: {}".format(param.name, param.shape)) print("parameter name: {}\tshape: {}".format(param.name,
print("------------------------------------------------------------------------------") param.shape))
print(
"------------------------------------------------------------------------------"
)
return return
eval_reader = create_reader(cfg.EvalReader) eval_reader = create_reader(cfg.EvalReader)
...@@ -104,7 +108,7 @@ def main(): ...@@ -104,7 +108,7 @@ def main():
if cfg.metric == 'WIDERFACE': if cfg.metric == 'WIDERFACE':
extra_keys = ['im_id', 'im_shape', 'gt_box'] extra_keys = ['im_id', 'im_shape', 'gt_box']
eval_keys, eval_values, eval_cls = parse_fetches(fetches, eval_prog, eval_keys, eval_values, eval_cls = parse_fetches(fetches, eval_prog,
extra_keys) extra_keys)
exe.run(startup_prog) exe.run(startup_prog)
...@@ -133,16 +137,16 @@ def main(): ...@@ -133,16 +137,16 @@ def main():
compiled_eval_prog = fluid.compiler.CompiledProgram(program) compiled_eval_prog = fluid.compiler.CompiledProgram(program)
results = eval_run(exe, compiled_eval_prog, eval_loader, results = eval_run(exe, compiled_eval_prog, eval_loader, eval_keys,
eval_keys, eval_values, eval_cls) eval_values, eval_cls)
resolution = None resolution = None
if 'mask' in results[0]: if 'mask' in results[0]:
resolution = model.mask_head.resolution resolution = model.mask_head.resolution
dataset = cfg['EvalReader']['dataset'] dataset = cfg['EvalReader']['dataset']
box_ap_stats = eval_results( box_ap_stats = eval_results(
results, results,
cfg.metric, cfg.metric,
cfg.num_classes, cfg.num_classes,
resolution, resolution,
is_bbox_normalized, is_bbox_normalized,
FLAGS.output_eval, FLAGS.output_eval,
...@@ -151,18 +155,21 @@ def main(): ...@@ -151,18 +155,21 @@ def main():
return box_ap_stats[0] return box_ap_stats[0]
pruned_params = FLAGS.pruned_params pruned_params = FLAGS.pruned_params
assert (FLAGS.pruned_params is not None), "FLAGS.pruned_params is empty!!! Please set it by '--pruned_params' option." assert (
FLAGS.pruned_params is not None
), "FLAGS.pruned_params is empty!!! Please set it by '--pruned_params' option."
pruned_params = FLAGS.pruned_params.strip().split(",") pruned_params = FLAGS.pruned_params.strip().split(",")
logger.info("pruned params: {}".format(pruned_params)) logger.info("pruned params: {}".format(pruned_params))
pruned_ratios = [float(n) for n in FLAGS.pruned_ratios.strip().split(" ")] pruned_ratios = [float(n) for n in FLAGS.pruned_ratios.strip().split(" ")]
logger.info("pruned ratios: {}".format(pruned_ratios)) logger.info("pruned ratios: {}".format(pruned_ratios))
sensitivity(eval_prog, sensitivity(
place, eval_prog,
pruned_params, place,
test, pruned_params,
sensitivities_file=FLAGS.sensitivities_file, test,
pruned_ratios=pruned_ratios) sensitivities_file=FLAGS.sensitivities_file,
pruned_ratios=pruned_ratios)
if __name__ == '__main__': if __name__ == '__main__':
...@@ -195,7 +202,8 @@ if __name__ == '__main__': ...@@ -195,7 +202,8 @@ if __name__ == '__main__':
"--pruned_ratios", "--pruned_ratios",
default="0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9", default="0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9",
type=str, type=str,
help="The ratios pruned iteratively for each parameter when calculating sensitivities.") help="The ratios pruned iteratively for each parameter when calculating sensitivities."
)
parser.add_argument( parser.add_argument(
"-P", "-P",
"--print_params", "--print_params",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册