blob: 490b07bfe46c75a91ccb11c98fff8f64bf1b3298 [file] [log] [blame]
Manuel Bottini5209be52019-02-13 16:34:56 +00001/*
Matthew Benthamf5f23912020-03-05 22:32:16 +00002 * Copyright (c) 2019-2020 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
Manuel Bottini5209be52019-02-13 16:34:56 +000026#include "arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h"
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010027#include "arm_compute/core/CL/kernels/CLDequantizationLayerKernel.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000028#include "arm_compute/core/CL/kernels/CLGenerateProposalsLayerKernel.h"
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010029#include "arm_compute/core/CL/kernels/CLPadLayerKernel.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000030#include "arm_compute/core/CL/kernels/CLPermuteKernel.h"
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010031#include "arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000032#include "arm_compute/core/Types.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000033#include "arm_compute/runtime/CL/CLScheduler.h"
34#include "arm_compute/runtime/CL/CLTensor.h"
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010035#include "arm_compute/runtime/CL/functions/CLReshapeLayer.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000036#include "arm_compute/runtime/CPP/CPPScheduler.h"
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010037#include "arm_compute/runtime/CPP/functions/CPPBoxWithNonMaximaSuppressionLimit.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000038#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010039#include "arm_compute/runtime/MemoryGroup.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000040
41namespace arm_compute
42{
43class ICLTensor;
44
45/** Basic function to generate proposals for a RPN (Region Proposal Network)
46 *
47 * This function calls the following OpenCL kernels:
48 * -# @ref CLComputeAllAnchors
49 * -# @ref CLPermute x 2
50 * -# @ref CLReshapeLayer x 2
Manuel Bottini5209be52019-02-13 16:34:56 +000051 * -# @ref CLBoundingBoxTransform
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010052 * -# @ref CLPadLayerKernel
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010053 * -# @ref CLDequantizationLayerKernel x 2
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010054 * -# @ref CLQuantizationLayerKernel
55 * And the following CPP functions:
Manuel Bottini5209be52019-02-13 16:34:56 +000056 * -# @ref CPPBoxWithNonMaximaSuppressionLimit
57 */
58class CLGenerateProposalsLayer : public IFunction
59{
60public:
61 /** Default constructor
62 *
63 * @param[in] memory_manager (Optional) Memory manager.
64 */
65 CLGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
66 /** Prevent instances of this class from being copied (As this class contains pointers) */
67 CLGenerateProposalsLayer(const CLGenerateProposalsLayer &) = delete;
Manuel Bottini5209be52019-02-13 16:34:56 +000068 /** Prevent instances of this class from being copied (As this class contains pointers) */
69 CLGenerateProposalsLayer &operator=(const CLGenerateProposalsLayer &) = delete;
Manuel Bottini5209be52019-02-13 16:34:56 +000070
71 /** Set the input and output tensors.
72 *
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010073 * @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.
74 * Data types supported: QASYMM8/F16/F32
Manuel Bottini5209be52019-02-13 16:34:56 +000075 * @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 +010076 * @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
77 * @param[out] proposals Box proposals output tensor of size (5, W*H*A).
78 * Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores
79 * @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 +000080 * @param[out] num_valid_proposals Scalar output tensor which says which of the first proposals are valid. Data types supported: U32
81 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
82 *
83 * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the @ref GenerateProposalsInfo struct.
84 * @note Proposals contains all the proposals. Of those, only the first num_valid_proposals are valid.
85 */
86 void configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals,
87 const GenerateProposalsInfo &info);
Manuel Bottini2b84be52020-04-08 10:15:51 +010088 /** Set the input and output tensors.
89 *
90 * @param[in] compile_context The compile context to be used.
91 * @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.
92 * Data types supported: QASYMM8/F16/F32
93 * @param[in] deltas Bounding box deltas from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores
94 * @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
95 * @param[out] proposals Box proposals output tensor of size (5, W*H*A).
96 * Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores
97 * @param[out] scores_out Box scores output tensor of size (W*H*A). Data types supported: Same as @p scores
98 * @param[out] num_valid_proposals Scalar output tensor which says which of the first proposals are valid. Data types supported: U32
99 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
100 *
101 * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the @ref GenerateProposalsInfo struct.
102 * @note Proposals contains all the proposals. Of those, only the first num_valid_proposals are valid.
103 */
104 void configure(const CLCompileContext &compile_context, const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out,
105 ICLTensor *num_valid_proposals, const GenerateProposalsInfo &info);
Manuel Bottini5209be52019-02-13 16:34:56 +0000106
107 /** Static function to check if given info will lead to a valid configuration of @ref CLGenerateProposalsLayer
108 *
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100109 * @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.
110 * Data types supported: QASYMM8/F16/F32
Manuel Bottini5209be52019-02-13 16:34:56 +0000111 * @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 +0100112 * @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
113 * @param[in] proposals Box proposals info output tensor of size (5, W*H*A).
114 * Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores
115 * @param[in] scores_out Box scores output tensor info of size (W*H*A). Data types supported: Same as @p scores
116 * @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 +0000117 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
118 *
119 * @return a Status
120 */
121 static Status validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out,
122 const ITensorInfo *num_valid_proposals,
123 const GenerateProposalsInfo &info);
124
125 // Inherited methods overridden:
126 void run() override;
127
128private:
129 // Memory group manager
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100130 MemoryGroup _memory_group;
Manuel Bottini5209be52019-02-13 16:34:56 +0000131
132 // OpenCL kernels
133 CLPermuteKernel _permute_deltas_kernel;
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100134 CLReshapeLayer _flatten_deltas;
Manuel Bottini5209be52019-02-13 16:34:56 +0000135 CLPermuteKernel _permute_scores_kernel;
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100136 CLReshapeLayer _flatten_scores;
Manuel Bottini5209be52019-02-13 16:34:56 +0000137 CLComputeAllAnchorsKernel _compute_anchors_kernel;
138 CLBoundingBoxTransformKernel _bounding_box_kernel;
Michele Di Giorgio4c268b92019-09-20 14:01:48 +0100139 CLPadLayerKernel _pad_kernel;
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100140 CLDequantizationLayerKernel _dequantize_anchors;
141 CLDequantizationLayerKernel _dequantize_deltas;
142 CLQuantizationLayerKernel _quantize_all_proposals;
Manuel Bottini5209be52019-02-13 16:34:56 +0000143
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100144 // CPP functions
145 CPPBoxWithNonMaximaSuppressionLimit _cpp_nms;
Manuel Bottini5209be52019-02-13 16:34:56 +0000146
147 bool _is_nhwc;
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100148 bool _is_qasymm8;
Manuel Bottini5209be52019-02-13 16:34:56 +0000149
150 // Temporary tensors
151 CLTensor _deltas_permuted;
152 CLTensor _deltas_flattened;
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100153 CLTensor _deltas_flattened_f32;
Manuel Bottini5209be52019-02-13 16:34:56 +0000154 CLTensor _scores_permuted;
155 CLTensor _scores_flattened;
156 CLTensor _all_anchors;
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100157 CLTensor _all_anchors_f32;
Manuel Bottini5209be52019-02-13 16:34:56 +0000158 CLTensor _all_proposals;
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100159 CLTensor _all_proposals_quantized;
Manuel Bottini5209be52019-02-13 16:34:56 +0000160 CLTensor _keeps_nms_unused;
161 CLTensor _classes_nms_unused;
162 CLTensor _proposals_4_roi_values;
163
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100164 // Temporary tensor pointers
165 CLTensor *_all_proposals_to_use;
166
Manuel Bottini5209be52019-02-13 16:34:56 +0000167 // Output tensor pointers
168 ICLTensor *_num_valid_proposals;
169 ICLTensor *_scores_out;
170
171 /** Internal function to run the CPP BoxWithNMS kernel */
172 void run_cpp_nms_kernel();
173};
174} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000175#endif /* ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H */