blob: f937832c0ea8e53053314da111a7b1ee9674667b [file] [log] [blame]
Pablo Telloc9564cb2019-09-13 10:20:25 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Pablo Telloc9564cb2019-09-13 10:20:25 +01003 *
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_NEGENERATEPROPOSALSLAYER_H
25#define ARM_COMPUTE_NEGENERATEPROPOSALSLAYER_H
Michalis Spyroubcd23522020-05-21 15:02:36 +010026
Pablo Telloc9564cb2019-09-13 10:20:25 +010027#include "arm_compute/core/NEON/kernels/NEBoundingBoxTransformKernel.h"
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010028#include "arm_compute/core/NEON/kernels/NEDequantizationLayerKernel.h"
Pablo Telloc9564cb2019-09-13 10:20:25 +010029#include "arm_compute/core/NEON/kernels/NEGenerateProposalsLayerKernel.h"
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010030#include "arm_compute/core/NEON/kernels/NEPadLayerKernel.h"
Pablo Telloc9564cb2019-09-13 10:20:25 +010031#include "arm_compute/core/NEON/kernels/NEPermuteKernel.h"
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010032#include "arm_compute/core/NEON/kernels/NEQuantizationLayerKernel.h"
Pablo Telloc9564cb2019-09-13 10:20:25 +010033#include "arm_compute/core/Types.h"
34#include "arm_compute/runtime/CPP/CPPScheduler.h"
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010035#include "arm_compute/runtime/CPP/functions/CPPBoxWithNonMaximaSuppressionLimit.h"
Pablo Telloc9564cb2019-09-13 10:20:25 +010036#include "arm_compute/runtime/IFunction.h"
37#include "arm_compute/runtime/MemoryGroup.h"
Michalis Spyroubcd23522020-05-21 15:02:36 +010038#include "arm_compute/runtime/NEON/functions/NEReshapeLayer.h"
Pablo Telloc9564cb2019-09-13 10:20:25 +010039#include "arm_compute/runtime/Tensor.h"
40
41namespace arm_compute
42{
43class ITensor;
44
45/** Basic function to generate proposals for a RPN (Region Proposal Network)
46 *
47 * This function calls the following Neon kernels:
48 * -# @ref NEComputeAllAnchors
49 * -# @ref NEPermute x 2
50 * -# @ref NEReshapeLayer x 2
Pablo Telloc9564cb2019-09-13 10:20:25 +010051 * -# @ref NEBoundingBoxTransform
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010052 * -# @ref NEPadLayerKernel
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010053 * -# @ref NEDequantizationLayerKernel x 2
54 * -# @ref NEQuantizationLayerKernel
Pablo Telloc9564cb2019-09-13 10:20:25 +010055 * And the following CPP kernels:
56 * -# @ref CPPBoxWithNonMaximaSuppressionLimit
57 */
58class NEGenerateProposalsLayer : public IFunction
59{
60public:
61 /** Default constructor
62 *
63 * @param[in] memory_manager (Optional) Memory manager.
64 */
65 NEGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
66 /** Prevent instances of this class from being copied (As this class contains pointers) */
67 NEGenerateProposalsLayer(const NEGenerateProposalsLayer &) = delete;
Pablo Telloc9564cb2019-09-13 10:20:25 +010068 /** Prevent instances of this class from being copied (As this class contains pointers) */
69 NEGenerateProposalsLayer &operator=(const NEGenerateProposalsLayer &) = delete;
Pablo Telloc9564cb2019-09-13 10:20:25 +010070
71 /** Set the input and output tensors.
72 *
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +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
Pablo Telloc9564cb2019-09-13 10:20:25 +010075 * @param[in] deltas Bounding box deltas from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +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
Pablo Telloc9564cb2019-09-13 10:20:25 +010079 * @param[out] scores_out Box scores output tensor of size (W*H*A). Data types supported: Same as @p input
80 * @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 ITensor *scores, const ITensor *deltas, const ITensor *anchors, ITensor *proposals, ITensor *scores_out, ITensor *num_valid_proposals,
87 const GenerateProposalsInfo &info);
88
89 /** Static function to check if given info will lead to a valid configuration of @ref NEGenerateProposalsLayer
90 *
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010091 * @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.
92 * Data types supported: QASYMM8/F16/F32
Pablo Telloc9564cb2019-09-13 10:20:25 +010093 * @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 Giorgio58c71ef2019-09-30 15:03:21 +010094 * @param[in] anchors Anchors tensor info 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[in] proposals Box proposals info 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
Pablo Telloc9564cb2019-09-13 10:20:25 +010097 * @param[in] scores_out Box scores output tensor info of size (W*H*A). Data types supported: Same as @p input
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010098 * @param[in] num_valid_proposals Scalar output tensor info which says which of the first proposals are valid. Data types supported: U32
Pablo Telloc9564cb2019-09-13 10:20:25 +010099 * @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
112 MemoryGroup _memory_group;
113
114 // Neon kernels
115 NEPermuteKernel _permute_deltas_kernel;
Michalis Spyroubcd23522020-05-21 15:02:36 +0100116 NEReshapeLayer _flatten_deltas;
Pablo Telloc9564cb2019-09-13 10:20:25 +0100117 NEPermuteKernel _permute_scores_kernel;
Michalis Spyroubcd23522020-05-21 15:02:36 +0100118 NEReshapeLayer _flatten_scores;
Pablo Telloc9564cb2019-09-13 10:20:25 +0100119 NEComputeAllAnchorsKernel _compute_anchors_kernel;
120 NEBoundingBoxTransformKernel _bounding_box_kernel;
Michele Di Giorgio4c268b92019-09-20 14:01:48 +0100121 NEPadLayerKernel _pad_kernel;
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100122 NEDequantizationLayerKernel _dequantize_anchors;
123 NEDequantizationLayerKernel _dequantize_deltas;
124 NEQuantizationLayerKernel _quantize_all_proposals;
Pablo Telloc9564cb2019-09-13 10:20:25 +0100125
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100126 // CPP functions
127 CPPBoxWithNonMaximaSuppressionLimit _cpp_nms;
Pablo Telloc9564cb2019-09-13 10:20:25 +0100128
129 bool _is_nhwc;
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100130 bool _is_qasymm8;
Pablo Telloc9564cb2019-09-13 10:20:25 +0100131
132 // Temporary tensors
133 Tensor _deltas_permuted;
134 Tensor _deltas_flattened;
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100135 Tensor _deltas_flattened_f32;
Pablo Telloc9564cb2019-09-13 10:20:25 +0100136 Tensor _scores_permuted;
137 Tensor _scores_flattened;
138 Tensor _all_anchors;
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100139 Tensor _all_anchors_f32;
Pablo Telloc9564cb2019-09-13 10:20:25 +0100140 Tensor _all_proposals;
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100141 Tensor _all_proposals_quantized;
Pablo Telloc9564cb2019-09-13 10:20:25 +0100142 Tensor _keeps_nms_unused;
143 Tensor _classes_nms_unused;
144 Tensor _proposals_4_roi_values;
145
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100146 // Temporary tensor pointers
147 Tensor *_all_proposals_to_use;
148
Pablo Telloc9564cb2019-09-13 10:20:25 +0100149 // Output tensor pointers
150 ITensor *_num_valid_proposals;
151 ITensor *_scores_out;
Pablo Telloc9564cb2019-09-13 10:20:25 +0100152};
153} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000154#endif /* ARM_COMPUTE_NEGENERATEPROPOSALSLAYER_H */