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