blob: efbbe44c1b49dec1d0a59199ab7766de30cb012f [file] [log] [blame]
Manuel Bottini5209be52019-02-13 16:34:56 +00001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
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 */
24#ifndef __ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H__
25#define __ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H__
26#include "arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h"
27#include "arm_compute/core/CL/kernels/CLCopyKernel.h"
28#include "arm_compute/core/CL/kernels/CLGenerateProposalsLayerKernel.h"
29#include "arm_compute/core/CL/kernels/CLMemsetKernel.h"
30#include "arm_compute/core/CL/kernels/CLPermuteKernel.h"
31#include "arm_compute/core/CL/kernels/CLReshapeLayerKernel.h"
32#include "arm_compute/core/CL/kernels/CLStridedSliceKernel.h"
33#include "arm_compute/core/CPP/kernels/CPPBoxWithNonMaximaSuppressionLimitKernel.h"
34#include "arm_compute/core/Types.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000035#include "arm_compute/runtime/CL/CLScheduler.h"
36#include "arm_compute/runtime/CL/CLTensor.h"
37#include "arm_compute/runtime/CPP/CPPScheduler.h"
38#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
51 * -# @ref CLStridedSlice x 3
52 * -# @ref CLBoundingBoxTransform
53 * -# @ref CLCopyKernel
54 * -# @ref CLMemsetKernel
55 * And the following CPP kernels:
56 * -# @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;
68 /** Default move constructor */
69 CLGenerateProposalsLayer(CLGenerateProposalsLayer &&) = default;
70 /** Prevent instances of this class from being copied (As this class contains pointers) */
71 CLGenerateProposalsLayer &operator=(const CLGenerateProposalsLayer &) = delete;
72 /** Default move assignment operator */
73 CLGenerateProposalsLayer &operator=(CLGenerateProposalsLayer &&) = default;
74
75 /** Set the input and output tensors.
76 *
77 * @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. Data types supported: F16/F32
78 * @param[in] deltas Bounding box deltas from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores
79 * @param[in] anchors Anchors tensor of size (4, A). Data types supported: Same as @p input
80 * @param[out] proposals Box proposals output tensor of size (5, W*H*A). Data types supported: Same as @p input
81 * @param[out] scores_out Box scores output tensor of size (W*H*A). Data types supported: Same as @p input
82 * @param[out] num_valid_proposals Scalar output tensor which says which of the first proposals are valid. Data types supported: U32
83 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
84 *
85 * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the @ref GenerateProposalsInfo struct.
86 * @note Proposals contains all the proposals. Of those, only the first num_valid_proposals are valid.
87 */
88 void configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals,
89 const GenerateProposalsInfo &info);
90
91 /** Static function to check if given info will lead to a valid configuration of @ref CLGenerateProposalsLayer
92 *
93 * @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. Data types supported: F16/F32
94 * @param[in] deltas Bounding box deltas info from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores
95 * @param[in] anchors Anchors tensor info of size (4, A). Data types supported: Same as @p input
96 * @param[in] proposals Box proposals info output tensor of size (5, W*H*A). Data types supported: Data types supported: U32
97 * @param[in] scores_out Box scores output tensor info of size (W*H*A). Data types supported: Same as @p input
98 * @param[in] num_valid_proposals Scalar output tensor info which says which of the first proposals are valid. Data types supported: Same as @p input
99 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
100 *
101 * @return a Status
102 */
103 static Status validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out,
104 const ITensorInfo *num_valid_proposals,
105 const GenerateProposalsInfo &info);
106
107 // Inherited methods overridden:
108 void run() override;
109
110private:
111 // Memory group manager
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100112 MemoryGroup _memory_group;
Manuel Bottini5209be52019-02-13 16:34:56 +0000113
114 // OpenCL kernels
115 CLPermuteKernel _permute_deltas_kernel;
116 CLReshapeLayerKernel _flatten_deltas_kernel;
117 CLPermuteKernel _permute_scores_kernel;
118 CLReshapeLayerKernel _flatten_scores_kernel;
119 CLComputeAllAnchorsKernel _compute_anchors_kernel;
120 CLBoundingBoxTransformKernel _bounding_box_kernel;
121 CLMemsetKernel _memset_kernel;
122 CLCopyKernel _padded_copy_kernel;
123
124 // CPP kernels
125 CPPBoxWithNonMaximaSuppressionLimitKernel _cpp_nms_kernel;
126
127 bool _is_nhwc;
128
129 // Temporary tensors
130 CLTensor _deltas_permuted;
131 CLTensor _deltas_flattened;
132 CLTensor _scores_permuted;
133 CLTensor _scores_flattened;
134 CLTensor _all_anchors;
135 CLTensor _all_proposals;
136 CLTensor _keeps_nms_unused;
137 CLTensor _classes_nms_unused;
138 CLTensor _proposals_4_roi_values;
139
140 // Output tensor pointers
141 ICLTensor *_num_valid_proposals;
142 ICLTensor *_scores_out;
143
144 /** Internal function to run the CPP BoxWithNMS kernel */
145 void run_cpp_nms_kernel();
146};
147} // namespace arm_compute
148#endif /* __ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H__ */