From 9da6f332c1bd525579b1ba99d7ea24aa80af559b Mon Sep 17 00:00:00 2001 From: wangguanzhong Date: Wed, 27 May 2020 20:19:44 +0800 Subject: [PATCH] [cherry-pick] add version check (#750) * cherry-pick add_version_check * add python version for cornerpool --- configs/anchor_free/README.md | 1 + ppdet/ext_op/cornerpool_lib.py | 148 +++++++++++++----- .../architectures/cornernet_squeeze.py | 2 + ppdet/utils/check.py | 8 +- 4 files changed, 114 insertions(+), 45 deletions(-) diff --git a/configs/anchor_free/README.md b/configs/anchor_free/README.md index 184ca2a6e..81e9d0843 100644 --- a/configs/anchor_free/README.md +++ b/configs/anchor_free/README.md @@ -35,6 +35,7 @@ **注意:** - 模型FPS在Tesla V100单卡环境中通过tools/eval.py进行测试 +- CornerNet-Squeeze要求使用PaddlePaddle1.8及以上版本或适当的develop版本 - CornerNet-Squeeze中使用ResNet结构的骨干网络时,加入了FPN结构,骨干网络的输出feature map采用FPN中的P3层输出。 - \*CornerNet-Squeeze-dcn-mixup-cosine是基于原版CornerNet-Squeeze优化效果最好的模型,在ResNet的骨干网络基础上增加mixup预处理和使用cosine_decay - FCOS使用GIoU loss、用location分支预测centerness、左上右下角点偏移量归一化和ground truth中心匹配策略 diff --git a/ppdet/ext_op/cornerpool_lib.py b/ppdet/ext_op/cornerpool_lib.py index c56fc661a..4cae5a088 100644 --- a/ppdet/ext_op/cornerpool_lib.py +++ b/ppdet/ext_op/cornerpool_lib.py @@ -1,8 +1,16 @@ import os import paddle.fluid as fluid +use_cpp = False + file_dir = os.path.dirname(os.path.abspath(__file__)) -fluid.load_op_library(os.path.join(file_dir, 'src/cornerpool_lib.so')) +try: + fluid.load_op_library(os.path.join(file_dir, 'src/cornerpool_lib.so')) + use_cpp = True +except: + print( + 'Warning: cornerpool_lib.so not found, use python version instead which may drop the inference speed. Compile in ppdet/ext_op at first if you need cpp version.' + ) from paddle.fluid.layer_helper import LayerHelper @@ -14,6 +22,19 @@ __all__ = [ ] +def cornerpool_op(layer_type, input, name): + helper = LayerHelper(layer_type, input=input, name=name) + dtype = helper.input_dtype() + output = helper.create_variable_for_type_inference(dtype) + max_map = helper.create_variable_for_type_inference(dtype) + helper.append_op( + type=layer_type, + inputs={"X": input}, + outputs={"Output": output, + "MaxMap": max_map}) + return output + + def bottom_pool(input, is_test=False, name=None): """ This layer calculates the bottom pooling output based on the input. @@ -34,16 +55,28 @@ def bottom_pool(input, is_test=False, name=None): output = corner_pool.bottom_pool(input) """ if is_test: - helper = LayerHelper('bottom_pool', **locals()) - dtype = helper.input_dtype() - output = helper.create_variable_for_type_inference(dtype) - max_map = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type="bottom_pool", - inputs={"X": input}, - outputs={"Output": output, - "MaxMap": max_map}) - return output + if use_cpp: + output = cornerpool_op("bottom_pool", input, name) + return output + + def cond(i, output): + return i < H + + def body(i, output): + cur = fluid.layers.slice(output, [2], [i], [H]) + next = fluid.layers.slice(output, [2], [0], [H - i]) + max_v = fluid.layers.elementwise_max(cur, next) + orig = fluid.layers.slice(output, [2], [0], [i]) + output = fluid.layers.concat([orig, max_v], axis=2) + i = i * 2 + return [i, output] + + H = fluid.layers.shape(input)[2] + i = fluid.layers.fill_constant(shape=[1], dtype='int32', value=1) + output = input + output = fluid.layers.while_loop(cond, body, [i, output]) + return output[-1] + H = input.shape[2] i = 1 output = input @@ -77,16 +110,27 @@ def top_pool(input, is_test=False, name=None): output = corner_pool.top_pool(input) """ if is_test: - helper = LayerHelper('top_pool', **locals()) - dtype = helper.input_dtype() - output = helper.create_variable_for_type_inference(dtype) - max_map = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type="top_pool", - inputs={"X": input}, - outputs={"Output": output, - "MaxMap": max_map}) - return output + if use_cpp: + output = cornerpool_op("top_pool", input, name) + return output + + def cond(i, output): + return i < H + + def body(i, output): + cur = fluid.layers.slice(output, [2], [0], [H - i]) + next = fluid.layers.slice(output, [2], [i], [H]) + max_v = fluid.layers.elementwise_max(cur, next) + orig = fluid.layers.slice(output, [2], [H - i], [H]) + output = fluid.layers.concat([max_v, orig], axis=2) + i = i * 2 + return [i, output] + + H = fluid.layers.shape(input)[2] + i = fluid.layers.fill_constant(shape=[1], dtype='int32', value=1) + output = input + output = fluid.layers.while_loop(cond, body, [i, output]) + return output[-1] H = input.shape[2] i = 1 @@ -121,16 +165,27 @@ def right_pool(input, is_test=False, name=None): output = corner_pool.right_pool(input) """ if is_test: - helper = LayerHelper('right_pool', **locals()) - dtype = helper.input_dtype() - output = helper.create_variable_for_type_inference(dtype) - max_map = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type="right_pool", - inputs={"X": input}, - outputs={"Output": output, - "MaxMap": max_map}) - return output + if use_cpp: + output = cornerpool_op("right_pool", input, name) + return output + + def cond(i, output): + return i < W + + def body(i, output): + cur = fluid.layers.slice(output, [3], [i], [W]) + next = fluid.layers.slice(output, [3], [0], [W - i]) + max_v = fluid.layers.elementwise_max(cur, next) + orig = fluid.layers.slice(output, [3], [0], [i]) + output = fluid.layers.concat([orig, max_v], axis=-1) + i = i * 2 + return [i, output] + + W = fluid.layers.shape(input)[3] + i = fluid.layers.fill_constant(shape=[1], dtype='int32', value=1) + output = input + output = fluid.layers.while_loop(cond, body, [i, output]) + return output[-1] W = input.shape[3] i = 1 @@ -165,16 +220,27 @@ def left_pool(input, is_test=False, name=None): output = corner_pool.left_pool(input) """ if is_test: - helper = LayerHelper('left_pool', **locals()) - dtype = helper.input_dtype() - output = helper.create_variable_for_type_inference(dtype) - max_map = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type="left_pool", - inputs={"X": input}, - outputs={"Output": output, - "MaxMap": max_map}) - return output + if use_cpp: + output = cornerpool_op("left_pool", input, name) + return output + + def cond(i, output): + return i < W + + def body(i, output): + cur = fluid.layers.slice(output, [3], [0], [W - i]) + next = fluid.layers.slice(output, [3], [i], [W]) + max_v = fluid.layers.elementwise_max(cur, next) + orig = fluid.layers.slice(output, [3], [W - i], [W]) + output = fluid.layers.concat([max_v, orig], axis=-1) + i = i * 2 + return [i, output] + + W = fluid.layers.shape(input)[3] + i = fluid.layers.fill_constant(shape=[1], dtype='int32', value=1) + output = input + output = fluid.layers.while_loop(cond, body, [i, output]) + return output[-1] W = input.shape[3] i = 1 diff --git a/ppdet/modeling/architectures/cornernet_squeeze.py b/ppdet/modeling/architectures/cornernet_squeeze.py index fb2ce42a1..6e3a48f7c 100644 --- a/ppdet/modeling/architectures/cornernet_squeeze.py +++ b/ppdet/modeling/architectures/cornernet_squeeze.py @@ -22,6 +22,7 @@ from paddle import fluid from ppdet.core.workspace import register import numpy as np +from ppdet.utils.check import check_version __all__ = ['CornerNetSqueeze'] @@ -48,6 +49,7 @@ class CornerNetSqueeze(object): corner_head='CornerHead', num_classes=80, fpn=None): + check_version('1.8.0') super(CornerNetSqueeze, self).__init__() self.backbone = backbone self.corner_head = corner_head diff --git a/ppdet/utils/check.py b/ppdet/utils/check.py index 2b3283585..10c4478ac 100644 --- a/ppdet/utils/check.py +++ b/ppdet/utils/check.py @@ -45,17 +45,17 @@ def check_gpu(use_gpu): pass -def check_version(): +def check_version(version='1.7.0'): """ Log error and exit when the installed version of paddlepaddle is not satisfied. """ - err = "PaddlePaddle version 1.6 or higher is required, " \ + err = "PaddlePaddle version {} or higher is required, " \ "or a suitable develop version is satisfied as well. \n" \ - "Please make sure the version is good with your code." \ + "Please make sure the version is good with your code.".format(version) try: - fluid.require_version('1.7.0') + fluid.require_version(version) except Exception as e: logger.error(err) sys.exit(1) -- GitLab