From 0a6618f48656d8f744250e19dd45cd2ad2dfde0b Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Tue, 21 Jun 2022 13:06:47 +0000 Subject: [PATCH 1/4] add distribute_fpn_proposals --- .../test_distribute_fpn_proposals_op.py | 74 ++++++++++- python/paddle/vision/ops.py | 115 ++++++++++++++++++ 2 files changed, 188 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_distribute_fpn_proposals_op.py b/python/paddle/fluid/tests/unittests/test_distribute_fpn_proposals_op.py index 06cdaed1988cc..6c74c0c04fc44 100644 --- a/python/paddle/fluid/tests/unittests/test_distribute_fpn_proposals_op.py +++ b/python/paddle/fluid/tests/unittests/test_distribute_fpn_proposals_op.py @@ -1,4 +1,17 @@ -# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -18,6 +31,8 @@ import numpy as np import math import sys +import paddle + from op_test import OpTest @@ -164,5 +179,62 @@ def init_test_case(self): self.pixel_offset = False +class TestDistributeFpnProposalsAPI(unittest.TestCase): + + def setUp(self): + np.random.seed(678) + self.rois_np = np.random.rand(10, 4).astype('float32') + self.rois_num_np = np.array([4, 6]).astype('int32') + + def test_dygraph_with_static(self): + paddle.enable_static() + rois = paddle.static.data(name='rois', shape=[10, 4], dtype='float32') + rois_num = paddle.static.data(name='rois_num', + shape=[None], + dtype='int32') + multi_rois, restore_ind, rois_num_per_level = paddle.vision.ops.distribute_fpn_proposals( + fpn_rois=rois, + min_level=2, + max_level=5, + refer_level=4, + refer_scale=224, + rois_num=rois_num) + fetch_list = multi_rois + [restore_ind] + rois_num_per_level + + exe = paddle.static.Executor() + output_stat = exe.run(paddle.static.default_main_program(), + feed={ + 'rois': self.rois_np, + 'rois_num': self.rois_num_np + }, + fetch_list=fetch_list, + return_numpy=False) + output_stat_np = [] + for output in output_stat: + output_np = np.array(output) + if len(output_np) > 0: + output_stat_np.append(output_np) + + paddle.disable_static() + rois_dy = paddle.to_tensor(self.rois_np) + rois_num_dy = paddle.to_tensor(self.rois_num_np) + multi_rois_dy, restore_ind_dy, rois_num_per_level_dy = paddle.vision.ops.distribute_fpn_proposals( + fpn_rois=rois_dy, + min_level=2, + max_level=5, + refer_level=4, + refer_scale=224, + rois_num=rois_num_dy) + output_dy = multi_rois_dy + [restore_ind_dy] + rois_num_per_level_dy + output_dy_np = [] + for output in output_dy: + output_np = output.numpy() + if len(output_np) > 0: + output_dy_np.append(output_np) + + for res_stat, res_dy in zip(output_stat_np, output_dy_np): + self.assertTrue(np.allclose(res_stat, res_dy)) + + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/vision/ops.py b/python/paddle/vision/ops.py index 876b80c6abc73..05290cb2a698d 100644 --- a/python/paddle/vision/ops.py +++ b/python/paddle/vision/ops.py @@ -28,6 +28,7 @@ 'yolo_box', 'deform_conv2d', 'DeformConv2D', + 'distribute_fpn_proposals', 'read_file', 'decode_jpeg', 'roi_pool', @@ -835,6 +836,120 @@ def forward(self, x, offset, mask=None): return out +def distribute_fpn_proposals(fpn_rois, + min_level, + max_level, + refer_level, + refer_scale, + pixel_offset=False, + rois_num=None, + name=None): + r""" + In Feature Pyramid Networks (FPN) models, it is needed to distribute + all proposals into different FPN level, with respect to scale of the proposals, + the referring scale and the referring level. Besides, to restore the order of + proposals, we return an array which indicates the original index of rois + in current proposals. To compute FPN level for each roi, the formula is given as follows: + + .. math:: + roi\_scale &= \sqrt{BBoxArea(fpn\_roi)} + level = floor(&\log(\\frac{roi\_scale}{refer\_scale}) + refer\_level) + where BBoxArea is a function to compute the area of each roi. + + Args: + fpn_rois(Tensor): 2-D Tensor with shape [N, 4] and data type is + float32 or float64. The input fpn_rois. + min_level(int): The lowest level of FPN layer where the proposals come + from. + max_level(int): The highest level of FPN layer where the proposals + come from. + refer_level(int): The referring level of FPN layer with specified scale. + refer_scale(int): The referring scale of FPN layer with specified level. + rois_num(Tensor): 1-D Tensor contains the number of RoIs in each image. + The shape is [B] and data type is int32. B is the number of images. + If it is not None then return a list of 1-D Tensor. Each element + is the output RoIs' number of each image on the corresponding level + and the shape is [B]. None by default. + name(str, optional): For detailed information, please refer + to :ref:`api_guide_Name`. Usually name is no need to set and + None by default. + + Returns: + multi_rois(List) : A list of 2-D Tensor with shape [M, 4] + and data type of float32 and float64. The length is + max_level-min_level+1. The proposals in each FPN level. + restore_ind(Tensor): A 2-D Tensor with shape [N, 1], N is + the number of total rois. The data type is int32. It is + used to restore the order of fpn_rois. + rois_num_per_level(List): A list of 1-D Tensor and each Tensor is + the RoIs' number in each image on the corresponding level. The shape + is [B] and data type of int32. B is the number of images + Examples: + .. code-block:: python + import paddle + from ppdet.modeling import ops + paddle.enable_static() + fpn_rois = paddle.static.data( + name='data', shape=[None, 4], dtype='float32', lod_level=1) + multi_rois, restore_ind = ops.distribute_fpn_proposals( + fpn_rois=fpn_rois, + min_level=2, + max_level=5, + refer_level=4, + refer_scale=224) + """ + num_lvl = max_level - min_level + 1 + + if _non_static_mode(): + assert rois_num is not None, "rois_num should not be None in dygraph mode." + attrs = ('min_level', min_level, 'max_level', max_level, 'refer_level', + refer_level, 'refer_scale', refer_scale, 'pixel_offset', + pixel_offset) + multi_rois, restore_ind, rois_num_per_level = _C_ops.distribute_fpn_proposals( + fpn_rois, rois_num, num_lvl, num_lvl, *attrs) + return multi_rois, restore_ind, rois_num_per_level + + else: + check_variable_and_dtype(fpn_rois, 'fpn_rois', ['float32', 'float64'], + 'distribute_fpn_proposals') + helper = LayerHelper('distribute_fpn_proposals', **locals()) + dtype = helper.input_dtype('fpn_rois') + multi_rois = [ + helper.create_variable_for_type_inference(dtype) + for i in range(num_lvl) + ] + + restore_ind = helper.create_variable_for_type_inference(dtype='int32') + + inputs = {'FpnRois': fpn_rois} + outputs = { + 'MultiFpnRois': multi_rois, + 'RestoreIndex': restore_ind, + } + + if rois_num is not None: + inputs['RoisNum'] = rois_num + rois_num_per_level = [ + helper.create_variable_for_type_inference(dtype='int32') + for i in range(num_lvl) + ] + outputs['MultiLevelRoIsNum'] = rois_num_per_level + else: + rois_num_per_level = None + + helper.append_op(type='distribute_fpn_proposals', + inputs=inputs, + outputs=outputs, + attrs={ + 'min_level': min_level, + 'max_level': max_level, + 'refer_level': refer_level, + 'refer_scale': refer_scale, + 'pixel_offset': pixel_offset + }) + return multi_rois, restore_ind, rois_num_per_level + + def read_file(filename, name=None): """ Reads and outputs the bytes contents of a file as a uint8 Tensor From ceeb9076d8a522888a6d4c4f5b714bed1a43e7d4 Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Thu, 30 Jun 2022 09:31:43 +0000 Subject: [PATCH 2/4] change to new dygraph --- python/paddle/vision/ops.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/paddle/vision/ops.py b/python/paddle/vision/ops.py index 05290cb2a698d..dd6d690795738 100644 --- a/python/paddle/vision/ops.py +++ b/python/paddle/vision/ops.py @@ -884,14 +884,14 @@ def distribute_fpn_proposals(fpn_rois, rois_num_per_level(List): A list of 1-D Tensor and each Tensor is the RoIs' number in each image on the corresponding level. The shape is [B] and data type of int32. B is the number of images + Examples: .. code-block:: python + import paddle - from ppdet.modeling import ops - paddle.enable_static() - fpn_rois = paddle.static.data( - name='data', shape=[None, 4], dtype='float32', lod_level=1) - multi_rois, restore_ind = ops.distribute_fpn_proposals( + + fpn_rois = paddle.rand((10, 4)) + multi_rois, restore_ind = paddle.vision.ops.distribute_fpn_proposals( fpn_rois=fpn_rois, min_level=2, max_level=5, @@ -900,7 +900,7 @@ def distribute_fpn_proposals(fpn_rois, """ num_lvl = max_level - min_level + 1 - if _non_static_mode(): + if in_dygraph_mode(): assert rois_num is not None, "rois_num should not be None in dygraph mode." attrs = ('min_level', min_level, 'max_level', max_level, 'refer_level', refer_level, 'refer_scale', refer_scale, 'pixel_offset', From be91e5816856f2a97530bc0b99181c5e6a252cf8 Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Thu, 7 Jul 2022 10:17:08 +0000 Subject: [PATCH 3/4] fix doc and example code --- python/paddle/vision/ops.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/python/paddle/vision/ops.py b/python/paddle/vision/ops.py index b6610770ea0ae..545ba25f5b420 100644 --- a/python/paddle/vision/ops.py +++ b/python/paddle/vision/ops.py @@ -845,7 +845,7 @@ def distribute_fpn_proposals(fpn_rois, rois_num=None, name=None): r""" - In Feature Pyramid Networks (FPN) models, it is needed to distribute + In Feature Pyramid Networks (FPN) models, it is needed to distribute all proposals into different FPN level, with respect to scale of the proposals, the referring scale and the referring level. Besides, to restore the order of proposals, we return an array which indicates the original index of rois @@ -866,10 +866,10 @@ def distribute_fpn_proposals(fpn_rois, refer_level (int): The referring level of FPN layer with specified scale. refer_scale (int): The referring scale of FPN layer with specified level. pixel_offset (bool, optional): Whether there is pixel offset. If True, the offset of - `img_size` will be 1. 'False' by default. + image shape will be 1. 'False' by default. rois_num (Tensor, optional): 1-D Tensor contains the number of RoIs in each image. The shape is [B] and data type is int32. B is the number of images. - If rois_num not None, it will return a list of 1-D Tensor. Each element + If rois_num not None, it will return a list of 1-D Tensor. Each element is the output RoIs' number of each image on the corresponding level and the shape is [B]. None by default. name (str, optional): For detailed information, please refer @@ -891,12 +891,15 @@ def distribute_fpn_proposals(fpn_rois, import paddle fpn_rois = paddle.rand((10, 4)) - multi_rois, restore_ind = paddle.vision.ops.distribute_fpn_proposals( + rois_num = paddle.to_tensor([3, 1, 4, 2], dtype=paddle.int32) + + multi_rois, restore_ind, rois_num_per_level = paddle.vision.ops.distribute_fpn_proposals( fpn_rois=fpn_rois, min_level=2, max_level=5, refer_level=4, - refer_scale=224) + refer_scale=224, + rois_num=rois_num) """ num_lvl = max_level - min_level + 1 From becaf7bec4ed8822c25af405ea024a6e29634ae3 Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Wed, 13 Jul 2022 04:02:25 +0000 Subject: [PATCH 4/4] change fluid impl to current version --- python/paddle/fluid/layers/detection.py | 55 ++++--------------------- 1 file changed, 9 insertions(+), 46 deletions(-) diff --git a/python/paddle/fluid/layers/detection.py b/python/paddle/fluid/layers/detection.py index f89c95b93a1d3..aa6df245480cb 100644 --- a/python/paddle/fluid/layers/detection.py +++ b/python/paddle/fluid/layers/detection.py @@ -17,6 +17,8 @@ from __future__ import print_function +import paddle + from .layer_function_generator import generate_layer_fn from .layer_function_generator import autodoc, templatedoc from ..layer_helper import LayerHelper @@ -3774,52 +3776,13 @@ def distribute_fpn_proposals(fpn_rois, refer_level=4, refer_scale=224) """ - num_lvl = max_level - min_level + 1 - - if _non_static_mode(): - assert rois_num is not None, "rois_num should not be None in dygraph mode." - attrs = ('min_level', min_level, 'max_level', max_level, 'refer_level', - refer_level, 'refer_scale', refer_scale) - multi_rois, restore_ind, rois_num_per_level = _C_ops.distribute_fpn_proposals( - fpn_rois, rois_num, num_lvl, num_lvl, *attrs) - return multi_rois, restore_ind, rois_num_per_level - - check_variable_and_dtype(fpn_rois, 'fpn_rois', ['float32', 'float64'], - 'distribute_fpn_proposals') - helper = LayerHelper('distribute_fpn_proposals', **locals()) - dtype = helper.input_dtype('fpn_rois') - multi_rois = [ - helper.create_variable_for_type_inference(dtype) for i in range(num_lvl) - ] - - restore_ind = helper.create_variable_for_type_inference(dtype='int32') - - inputs = {'FpnRois': fpn_rois} - outputs = { - 'MultiFpnRois': multi_rois, - 'RestoreIndex': restore_ind, - } - - if rois_num is not None: - inputs['RoisNum'] = rois_num - rois_num_per_level = [ - helper.create_variable_for_type_inference(dtype='int32') - for i in range(num_lvl) - ] - outputs['MultiLevelRoIsNum'] = rois_num_per_level - - helper.append_op(type='distribute_fpn_proposals', - inputs=inputs, - outputs=outputs, - attrs={ - 'min_level': min_level, - 'max_level': max_level, - 'refer_level': refer_level, - 'refer_scale': refer_scale - }) - if rois_num is not None: - return multi_rois, restore_ind, rois_num_per_level - return multi_rois, restore_ind + return paddle.vision.ops.distribute_fpn_proposals(fpn_rois=fpn_rois, + min_level=min_level, + max_level=max_level, + refer_level=refer_level, + refer_scale=refer_scale, + rois_num=rois_num, + name=name) @templatedoc()