blob: 0e6601e4f9d29bd26319413774a446d106446243 [file] [log] [blame]
Pablo Telloc9564cb2019-09-13 10:20:25 +01001/*
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_NEGENERATEPROPOSALSLAYER_H__
25#define __ARM_COMPUTE_NEGENERATEPROPOSALSLAYER_H__
26#include "arm_compute/core/CPP/kernels/CPPBoxWithNonMaximaSuppressionLimitKernel.h"
27#include "arm_compute/core/NEON/kernels/NEBoundingBoxTransformKernel.h"
28#include "arm_compute/core/NEON/kernels/NECopyKernel.h"
29#include "arm_compute/core/NEON/kernels/NEGenerateProposalsLayerKernel.h"
30#include "arm_compute/core/NEON/kernels/NEMemsetKernel.h"
31#include "arm_compute/core/NEON/kernels/NEPermuteKernel.h"
32#include "arm_compute/core/NEON/kernels/NEReshapeLayerKernel.h"
33#include "arm_compute/core/NEON/kernels/NEStridedSliceKernel.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/runtime/CPP/CPPScheduler.h"
36#include "arm_compute/runtime/IFunction.h"
37#include "arm_compute/runtime/MemoryGroup.h"
38#include "arm_compute/runtime/Tensor.h"
39
40namespace arm_compute
41{
42class ITensor;
43
44/** Basic function to generate proposals for a RPN (Region Proposal Network)
45 *
46 * This function calls the following Neon kernels:
47 * -# @ref NEComputeAllAnchors
48 * -# @ref NEPermute x 2
49 * -# @ref NEReshapeLayer x 2
50 * -# @ref NEStridedSlice x 3
51 * -# @ref NEBoundingBoxTransform
52 * -# @ref NECopyKernel
53 * -# @ref NEMemsetKernel
54 * And the following CPP kernels:
55 * -# @ref CPPBoxWithNonMaximaSuppressionLimit
56 */
57class NEGenerateProposalsLayer : public IFunction
58{
59public:
60 /** Default constructor
61 *
62 * @param[in] memory_manager (Optional) Memory manager.
63 */
64 NEGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
65 /** Prevent instances of this class from being copied (As this class contains pointers) */
66 NEGenerateProposalsLayer(const NEGenerateProposalsLayer &) = delete;
67 /** Default move constructor */
68 NEGenerateProposalsLayer(NEGenerateProposalsLayer &&) = default;
69 /** Prevent instances of this class from being copied (As this class contains pointers) */
70 NEGenerateProposalsLayer &operator=(const NEGenerateProposalsLayer &) = delete;
71 /** Default move assignment operator */
72 NEGenerateProposalsLayer &operator=(NEGenerateProposalsLayer &&) = default;
73
74 /** Set the input and output tensors.
75 *
76 * @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
77 * @param[in] deltas Bounding box deltas from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores
78 * @param[in] anchors Anchors tensor of size (4, A). Data types supported: Same as @p input
79 * @param[out] proposals Box proposals output tensor of size (5, W*H*A). Data types supported: Same as @p input
80 * @param[out] scores_out Box scores output tensor of size (W*H*A). Data types supported: Same as @p input
81 * @param[out] num_valid_proposals Scalar output tensor which says which of the first proposals are valid. Data types supported: U32
82 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
83 *
84 * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the @ref GenerateProposalsInfo struct.
85 * @note Proposals contains all the proposals. Of those, only the first num_valid_proposals are valid.
86 */
87 void configure(const ITensor *scores, const ITensor *deltas, const ITensor *anchors, ITensor *proposals, ITensor *scores_out, ITensor *num_valid_proposals,
88 const GenerateProposalsInfo &info);
89
90 /** Static function to check if given info will lead to a valid configuration of @ref NEGenerateProposalsLayer
91 *
92 * @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
93 * @param[in] deltas Bounding box deltas info from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores
94 * @param[in] anchors Anchors tensor info of size (4, A). Data types supported: Same as @p input
95 * @param[in] proposals Box proposals info output tensor of size (5, W*H*A). Data types supported: Data types supported: U32
96 * @param[in] scores_out Box scores output tensor info of size (W*H*A). Data types supported: Same as @p input
97 * @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
98 * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo
99 *
100 * @return a Status
101 */
102 static Status validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out,
103 const ITensorInfo *num_valid_proposals,
104 const GenerateProposalsInfo &info);
105
106 // Inherited methods overridden:
107 void run() override;
108
109private:
110 // Memory group manager
111 MemoryGroup _memory_group;
112
113 // Neon kernels
114 NEPermuteKernel _permute_deltas_kernel;
115 NEReshapeLayerKernel _flatten_deltas_kernel;
116 NEPermuteKernel _permute_scores_kernel;
117 NEReshapeLayerKernel _flatten_scores_kernel;
118 NEComputeAllAnchorsKernel _compute_anchors_kernel;
119 NEBoundingBoxTransformKernel _bounding_box_kernel;
120 NEMemsetKernel _memset_kernel;
121 NECopyKernel _padded_copy_kernel;
122
123 // CPP kernels
124 CPPBoxWithNonMaximaSuppressionLimitKernel _cpp_nms_kernel;
125
126 bool _is_nhwc;
127
128 // Temporary tensors
129 Tensor _deltas_permuted;
130 Tensor _deltas_flattened;
131 Tensor _scores_permuted;
132 Tensor _scores_flattened;
133 Tensor _all_anchors;
134 Tensor _all_proposals;
135 Tensor _keeps_nms_unused;
136 Tensor _classes_nms_unused;
137 Tensor _proposals_4_roi_values;
138
139 // Output tensor pointers
140 ITensor *_num_valid_proposals;
141 ITensor *_scores_out;
142
143 /** Internal function to run the CPP BoxWithNMS kernel */
144 void run_cpp_nms_kernel();
145};
146} // namespace arm_compute
147#endif /* __ARM_COMPUTE_NEGENERATEPROPOSALSLAYER_H__ */