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