| 1 |
/* |
| 2 |
* Copyright (c) 2019-2021, 2024 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 ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEGENERATEPROPOSALSLAYER_H |
| 25 |
#define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEGENERATEPROPOSALSLAYER_H |
| 26 |
|
| 27 |
#include "arm_compute/core/Types.h" |
| 28 |
#include "arm_compute/runtime/CPP/CPPScheduler.h" |
| 29 |
#include "arm_compute/runtime/CPP/functions/CPPBoxWithNonMaximaSuppressionLimit.h" |
| 30 |
#include "arm_compute/runtime/IFunction.h" |
| 31 |
#include "arm_compute/runtime/MemoryGroup.h" |
| 32 |
#include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 33 |
#include "arm_compute/runtime/NEON/functions/NEBoundingBoxTransform.h" |
| 34 |
#include "arm_compute/runtime/NEON/functions/NEDequantizationLayer.h" |
| 35 |
#include "arm_compute/runtime/NEON/functions/NEPadLayer.h" |
| 36 |
#include "arm_compute/runtime/NEON/functions/NEPermute.h" |
| 37 |
#include "arm_compute/runtime/NEON/functions/NEQuantizationLayer.h" |
| 38 |
#include "arm_compute/runtime/NEON/functions/NEReshapeLayer.h" |
| 39 |
#include "arm_compute/runtime/Tensor.h" |
| 40 |
|
| 41 |
namespace arm_compute |
| 42 |
{ |
| 43 |
class ITensor; |
| 44 |
class NEComputeAllAnchorsKernel; |
| 45 |
|
| 46 |
/** Basic function to generate proposals for a RPN (Region Proposal Network) |
| 47 |
* |
| 48 |
* This function calls the following Arm(R) Neon(TM) layers/kernels: |
| 49 |
* -# NEComputeAllAnchorsKernel |
| 50 |
* -# @ref NEPermute x 2 |
| 51 |
* -# @ref NEReshapeLayer x 2 |
| 52 |
* -# @ref NEBoundingBoxTransform |
| 53 |
* -# NEPadLayerKernel |
| 54 |
* -# @ref NEDequantizationLayer x 2 |
| 55 |
* -# @ref NEQuantizationLayer |
| 56 |
* And the following CPP kernels: |
| 57 |
* -# @ref CPPBoxWithNonMaximaSuppressionLimit |
| 58 |
*/ |
| 59 |
class NEGenerateProposalsLayer : public IFunction |
| 60 |
{ |
| 61 |
public: |
| 62 |
/** Default constructor |
| 63 |
* |
| 64 |
* @param[in] memory_manager (Optional) Memory manager. |
| 65 |
*/ |
| 66 |
NEGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager); |
| 67 |
NEGenerateProposalsLayer() : NEGenerateProposalsLayer(MemoryManagerOnDemand::make_default()) |
| 68 |
{ |
| 69 |
} |
| 70 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 71 |
NEGenerateProposalsLayer(const NEGenerateProposalsLayer &) = delete; |
| 72 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 73 |
NEGenerateProposalsLayer &operator=(const NEGenerateProposalsLayer &) = delete; |
| 74 |
/** Default destructor */ |
| 75 |
~NEGenerateProposalsLayer(); |
| 76 |
|
| 77 |
/** Set the input and output tensors. |
| 78 |
* |
| 79 |
* Valid data layouts: |
| 80 |
* - All |
| 81 |
* |
| 82 |
* Valid data type configurations: |
| 83 |
* |src0 |src1 |src2 |dst | |
| 84 |
* |:--------------|:------------------|:--------|:--------------| |
| 85 |
* |F16 |F16 |F16 |F16 | |
| 86 |
* |F32 |F32 |F32 |F32 | |
| 87 |
* |QASYMM8 |QSYMM8 |QSYMM16 |QASYMM8 | |
| 88 |
* |
| 89 |
* @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. |
| 90 |
* Data types supported: QASYMM8/F16/F32 |
| 91 |
* @param[in] deltas Bounding box deltas from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores |
| 92 |
* @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 |
| 93 |
* @param[out] proposals Box proposals output tensor of size (5, W*H*A). |
| 94 |
* Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores |
| 95 |
* @param[out] scores_out Box scores output tensor of size (W*H*A). Data types supported: Same as @p input |
| 96 |
* @param[out] num_valid_proposals Scalar output tensor which says which of the first proposals are valid. Data types supported: U32 |
| 97 |
* @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo |
| 98 |
* |
| 99 |
* @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the @ref GenerateProposalsInfo struct. |
| 100 |
* @note Proposals contains all the proposals. Of those, only the first num_valid_proposals are valid. |
| 101 |
*/ |
| 102 |
void configure(const ITensor *scores, |
| 103 |
const ITensor *deltas, |
| 104 |
const ITensor *anchors, |
| 105 |
ITensor *proposals, |
| 106 |
ITensor *scores_out, |
| 107 |
ITensor *num_valid_proposals, |
| 108 |
const GenerateProposalsInfo &info); |
| 109 |
|
| 110 |
/** Static function to check if given info will lead to a valid configuration of @ref NEGenerateProposalsLayer |
| 111 |
* |
| 112 |
* @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. |
| 113 |
* Data types supported: QASYMM8/F16/F32 |
| 114 |
* @param[in] deltas Bounding box deltas info from convolution layer of size (W, H, 4*A). Data types supported: Same as @p scores |
| 115 |
* @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 |
| 116 |
* @param[in] proposals Box proposals info output tensor of size (5, W*H*A). |
| 117 |
* Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores |
| 118 |
* @param[in] scores_out Box scores output tensor info of size (W*H*A). Data types supported: Same as @p input |
| 119 |
* @param[in] num_valid_proposals Scalar output tensor info which says which of the first proposals are valid. Data types supported: U32 |
| 120 |
* @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo |
| 121 |
* |
| 122 |
* @return a Status |
| 123 |
*/ |
| 124 |
static Status validate(const ITensorInfo *scores, |
| 125 |
const ITensorInfo *deltas, |
| 126 |
const ITensorInfo *anchors, |
| 127 |
const ITensorInfo *proposals, |
| 128 |
const ITensorInfo *scores_out, |
| 129 |
const ITensorInfo *num_valid_proposals, |
| 130 |
const GenerateProposalsInfo &info); |
| 131 |
|
| 132 |
// Inherited methods overridden: |
| 133 |
void run() override; |
| 134 |
|
| 135 |
private: |
| 136 |
// Memory group manager |
| 137 |
MemoryGroup _memory_group; |
| 138 |
|
| 139 |
// kernels/layers |
| 140 |
NEPermute _permute_deltas; |
| 141 |
NEReshapeLayer _flatten_deltas; |
| 142 |
NEPermute _permute_scores; |
| 143 |
NEReshapeLayer _flatten_scores; |
| 144 |
std::unique_ptr<NEComputeAllAnchorsKernel> _compute_anchors; |
| 145 |
NEBoundingBoxTransform _bounding_box; |
| 146 |
NEPadLayer _pad; |
| 147 |
NEDequantizationLayer _dequantize_anchors; |
| 148 |
NEDequantizationLayer _dequantize_deltas; |
| 149 |
NEQuantizationLayer _quantize_all_proposals; |
| 150 |
|
| 151 |
// CPP functions |
| 152 |
CPPBoxWithNonMaximaSuppressionLimit _cpp_nms; |
| 153 |
|
| 154 |
bool _is_nhwc; |
| 155 |
bool _is_qasymm8; |
| 156 |
|
| 157 |
// Temporary tensors |
| 158 |
Tensor _deltas_permuted; |
| 159 |
Tensor _deltas_flattened; |
| 160 |
Tensor _deltas_flattened_f32; |
| 161 |
Tensor _scores_permuted; |
| 162 |
Tensor _scores_flattened; |
| 163 |
Tensor _all_anchors; |
| 164 |
Tensor _all_anchors_f32; |
| 165 |
Tensor _all_proposals; |
| 166 |
Tensor _all_proposals_quantized; |
| 167 |
Tensor _keeps_nms_unused; |
| 168 |
Tensor _classes_nms_unused; |
| 169 |
Tensor _proposals_4_roi_values; |
| 170 |
|
| 171 |
// Temporary tensor pointers |
| 172 |
Tensor *_all_proposals_to_use; |
| 173 |
|
| 174 |
// Output tensor pointers |
| 175 |
ITensor *_num_valid_proposals; |
| 176 |
ITensor *_scores_out; |
| 177 |
}; |
| 178 |
} // namespace arm_compute |
| 179 |
#endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEGENERATEPROPOSALSLAYER_H |