blob: e6b0eed6d8fa058d46ea372df5bcd8e0a22098b8 [file] [log] [blame]
Manuel Bottini5209be52019-02-13 16:34:56 +00001/*
Michele Di Giorgioee82d342021-01-05 16:14:28 +00002 * Copyright (c) 2019-2021 Arm Limited.
Manuel Bottini5209be52019-02-13 16:34:56 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H
25#define ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010026
Manuel Bottini5209be52019-02-13 16:34:56 +000027#include "arm_compute/core/Types.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000028#include "arm_compute/runtime/CL/CLScheduler.h"
29#include "arm_compute/runtime/CL/CLTensor.h"
Sheri Zhang7e20e292021-02-02 11:49:34 +000030#include "arm_compute/runtime/CL/functions/CLPermute.h"
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010031#include "arm_compute/runtime/CL/functions/CLReshapeLayer.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000032#include "arm_compute/runtime/CPP/CPPScheduler.h"
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010033#include "arm_compute/runtime/CPP/functions/CPPBoxWithNonMaximaSuppressionLimit.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000034#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010035#include "arm_compute/runtime/MemoryGroup.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000036
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010037#include <memory>
38
Manuel Bottini5209be52019-02-13 16:34:56 +000039namespace arm_compute
40{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010041class CLCompileContext;
42class CLBoundingBoxTransformKernel;
43class CLDequantizationLayerKernel;
44class CLComputeAllAnchorsKernel;
45class CLPadLayerKernel;
Manuel Bottini5a1bf622021-03-01 17:39:36 +000046class CLQuantizationLayer;
Manuel Bottini5209be52019-02-13 16:34:56 +000047class ICLTensor;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010048class ITensorInfo;
Manuel Bottini5209be52019-02-13 16:34:56 +000049
50/** Basic function to generate proposals for a RPN (Region Proposal Network)
51 *
52 * This function calls the following OpenCL kernels:
Michele Di Giorgioee82d342021-01-05 16:14:28 +000053 * -# @ref CLComputeAllAnchorsKernel
Manuel Bottini5209be52019-02-13 16:34:56 +000054 * -# @ref CLPermute x 2
55 * -# @ref CLReshapeLayer x 2
Manuel Bottini5209be52019-02-13 16:34:56 +000056 * -# @ref CLBoundingBoxTransform
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010057 * -# @ref CLPadLayerKernel
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010058 * -# @ref CLDequantizationLayerKernel x 2
Manuel Bottini5a1bf622021-03-01 17:39:36 +000059 * -# @ref CLQuantizationLayer
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010060 * And the following CPP functions:
Manuel Bottini5209be52019-02-13 16:34:56 +000061 * -# @ref CPPBoxWithNonMaximaSuppressionLimit
62 */
63class CLGenerateProposalsLayer : public IFunction
64{
65public:
66 /** Default constructor
67 *
68 * @param[in] memory_manager (Optional) Memory manager.
69 */
70 CLGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
71 /** Prevent instances of this class from being copied (As this class contains pointers) */
72 CLGenerateProposalsLayer(const CLGenerateProposalsLayer &) = delete;
Manuel Bottini5209be52019-02-13 16:34:56 +000073 /** Prevent instances of this class from being copied (As this class contains pointers) */
74 CLGenerateProposalsLayer &operator=(const CLGenerateProposalsLayer &) = delete;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010075 /** Default destructor */
76 ~CLGenerateProposalsLayer();
Manuel Bottini5209be52019-02-13 16:34:56 +000077
78 /** Set the input and output tensors.
79 *
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010080 * @param[in] scores Scores from convolution layer of size (W, H, A), where H and W are the height and width of the feature map, and A is the number of anchors.
81 * Data types supported: QASYMM8/F16/F32
Manuel Bottini5209be52019-02-13 16:34:56 +000082 * @param[in] deltas Bounding box deltas from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010083 * @param[in] anchors Anchors tensor of size (4, A). Data types supported: QSYMM16 with scale of 0.125 if @p scores is QASYMM8, otherwise same as @p scores
84 * @param[out] proposals Box proposals output tensor of size (5, W*H*A).
85 * Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores
86 * @param[out] scores_out Box scores output tensor of size (W*H*A). Data types supported: Same as @p scores
Manuel Bottini5209be52019-02-13 16:34:56 +000087 * @param[out] num_valid_proposals Scalar output tensor which says which of the first proposals are valid. Data types supported: U32
88 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
89 *
90 * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the @ref GenerateProposalsInfo struct.
91 * @note Proposals contains all the proposals. Of those, only the first num_valid_proposals are valid.
92 */
93 void configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals,
94 const GenerateProposalsInfo &info);
Manuel Bottini2b84be52020-04-08 10:15:51 +010095 /** Set the input and output tensors.
96 *
97 * @param[in] compile_context The compile context to be used.
98 * @param[in] scores Scores from convolution layer of size (W, H, A), where H and W are the height and width of the feature map, and A is the number of anchors.
99 * Data types supported: QASYMM8/F16/F32
100 * @param[in] deltas Bounding box deltas from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores
101 * @param[in] anchors Anchors tensor of size (4, A). Data types supported: QSYMM16 with scale of 0.125 if @p scores is QASYMM8, otherwise same as @p scores
102 * @param[out] proposals Box proposals output tensor of size (5, W*H*A).
103 * Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores
104 * @param[out] scores_out Box scores output tensor of size (W*H*A). Data types supported: Same as @p scores
105 * @param[out] num_valid_proposals Scalar output tensor which says which of the first proposals are valid. Data types supported: U32
106 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
107 *
108 * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the @ref GenerateProposalsInfo struct.
109 * @note Proposals contains all the proposals. Of those, only the first num_valid_proposals are valid.
110 */
111 void configure(const CLCompileContext &compile_context, const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out,
112 ICLTensor *num_valid_proposals, const GenerateProposalsInfo &info);
Manuel Bottini5209be52019-02-13 16:34:56 +0000113
114 /** Static function to check if given info will lead to a valid configuration of @ref CLGenerateProposalsLayer
115 *
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100116 * @param[in] scores Scores info from convolution layer of size (W, H, A), where H and W are the height and width of the feature map, and A is the number of anchors.
117 * Data types supported: QASYMM8/F16/F32
Manuel Bottini5209be52019-02-13 16:34:56 +0000118 * @param[in] deltas Bounding box deltas info from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100119 * @param[in] anchors Anchors tensor of size (4, A). Data types supported: QSYMM16 with scale of 0.125 if @p scores is QASYMM8, otherwise same as @p scores
120 * @param[in] proposals Box proposals info output tensor of size (5, W*H*A).
121 * Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores
122 * @param[in] scores_out Box scores output tensor info of size (W*H*A). Data types supported: Same as @p scores
123 * @param[in] num_valid_proposals Scalar output tensor info which says which of the first proposals are valid. Data types supported: U32
Manuel Bottini5209be52019-02-13 16:34:56 +0000124 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
125 *
126 * @return a Status
127 */
128 static Status validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out,
129 const ITensorInfo *num_valid_proposals,
130 const GenerateProposalsInfo &info);
131
132 // Inherited methods overridden:
133 void run() override;
134
135private:
136 // Memory group manager
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100137 MemoryGroup _memory_group;
Manuel Bottini5209be52019-02-13 16:34:56 +0000138
139 // OpenCL kernels
Sheri Zhang7e20e292021-02-02 11:49:34 +0000140 CLPermute _permute_deltas;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100141 CLReshapeLayer _flatten_deltas;
Sheri Zhang7e20e292021-02-02 11:49:34 +0000142 CLPermute _permute_scores;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100143 CLReshapeLayer _flatten_scores;
144 std::unique_ptr<CLComputeAllAnchorsKernel> _compute_anchors_kernel;
145 std::unique_ptr<CLBoundingBoxTransformKernel> _bounding_box_kernel;
146 std::unique_ptr<CLPadLayerKernel> _pad_kernel;
147 std::unique_ptr<CLDequantizationLayerKernel> _dequantize_anchors;
148 std::unique_ptr<CLDequantizationLayerKernel> _dequantize_deltas;
Manuel Bottini5a1bf622021-03-01 17:39:36 +0000149 std::unique_ptr<CLQuantizationLayer> _quantize_all_proposals;
Manuel Bottini5209be52019-02-13 16:34:56 +0000150
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100151 // CPP functions
152 CPPBoxWithNonMaximaSuppressionLimit _cpp_nms;
Manuel Bottini5209be52019-02-13 16:34:56 +0000153
154 bool _is_nhwc;
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100155 bool _is_qasymm8;
Manuel Bottini5209be52019-02-13 16:34:56 +0000156
157 // Temporary tensors
158 CLTensor _deltas_permuted;
159 CLTensor _deltas_flattened;
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100160 CLTensor _deltas_flattened_f32;
Manuel Bottini5209be52019-02-13 16:34:56 +0000161 CLTensor _scores_permuted;
162 CLTensor _scores_flattened;
163 CLTensor _all_anchors;
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100164 CLTensor _all_anchors_f32;
Manuel Bottini5209be52019-02-13 16:34:56 +0000165 CLTensor _all_proposals;
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100166 CLTensor _all_proposals_quantized;
Manuel Bottini5209be52019-02-13 16:34:56 +0000167 CLTensor _keeps_nms_unused;
168 CLTensor _classes_nms_unused;
169 CLTensor _proposals_4_roi_values;
170
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100171 // Temporary tensor pointers
172 CLTensor *_all_proposals_to_use;
173
Manuel Bottini5209be52019-02-13 16:34:56 +0000174 // Output tensor pointers
175 ICLTensor *_num_valid_proposals;
176 ICLTensor *_scores_out;
177
178 /** Internal function to run the CPP BoxWithNMS kernel */
179 void run_cpp_nms_kernel();
180};
181} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000182#endif /* ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H */