Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H |
| 25 | #define ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h" |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/kernels/CLDequantizationLayerKernel.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/kernels/CLGenerateProposalsLayerKernel.h" |
Michele Di Giorgio | 4c268b9 | 2019-09-20 14:01:48 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/kernels/CLPadLayerKernel.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/CL/kernels/CLPermuteKernel.h" |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/CL/kernels/CLReshapeLayerKernel.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/Types.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 34 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 35 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 36 | #include "arm_compute/runtime/CPP/CPPScheduler.h" |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 37 | #include "arm_compute/runtime/CPP/functions/CPPBoxWithNonMaximaSuppressionLimit.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 38 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 39 | #include "arm_compute/runtime/MemoryGroup.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | class ICLTensor; |
| 44 | |
| 45 | /** Basic function to generate proposals for a RPN (Region Proposal Network) |
| 46 | * |
| 47 | * This function calls the following OpenCL kernels: |
| 48 | * -# @ref CLComputeAllAnchors |
| 49 | * -# @ref CLPermute x 2 |
| 50 | * -# @ref CLReshapeLayer x 2 |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 51 | * -# @ref CLBoundingBoxTransform |
Michele Di Giorgio | 4c268b9 | 2019-09-20 14:01:48 +0100 | [diff] [blame] | 52 | * -# @ref CLPadLayerKernel |
Michele Di Giorgio | 58c71ef | 2019-09-30 15:03:21 +0100 | [diff] [blame] | 53 | * -# @ref CLDequantizationLayerKernel x 2 |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 54 | * -# @ref CLQuantizationLayerKernel |
| 55 | * And the following CPP functions: |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 56 | * -# @ref CPPBoxWithNonMaximaSuppressionLimit |
| 57 | */ |
| 58 | class CLGenerateProposalsLayer : public IFunction |
| 59 | { |
| 60 | public: |
| 61 | /** Default constructor |
| 62 | * |
| 63 | * @param[in] memory_manager (Optional) Memory manager. |
| 64 | */ |
| 65 | CLGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 66 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 67 | CLGenerateProposalsLayer(const CLGenerateProposalsLayer &) = delete; |
| 68 | /** Default move constructor */ |
| 69 | CLGenerateProposalsLayer(CLGenerateProposalsLayer &&) = default; |
| 70 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 71 | CLGenerateProposalsLayer &operator=(const CLGenerateProposalsLayer &) = delete; |
| 72 | /** Default move assignment operator */ |
| 73 | CLGenerateProposalsLayer &operator=(CLGenerateProposalsLayer &&) = default; |
| 74 | |
| 75 | /** Set the input and output tensors. |
| 76 | * |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 77 | * @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. |
| 78 | * Data types supported: QASYMM8/F16/F32 |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 79 | * @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 | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 80 | * @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 |
| 81 | * @param[out] proposals Box proposals output tensor of size (5, W*H*A). |
| 82 | * Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores |
| 83 | * @param[out] scores_out Box scores output tensor of size (W*H*A). Data types supported: Same as @p scores |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 84 | * @param[out] num_valid_proposals Scalar output tensor which says which of the first proposals are valid. Data types supported: U32 |
| 85 | * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo |
| 86 | * |
| 87 | * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the @ref GenerateProposalsInfo struct. |
| 88 | * @note Proposals contains all the proposals. Of those, only the first num_valid_proposals are valid. |
| 89 | */ |
| 90 | void configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals, |
| 91 | const GenerateProposalsInfo &info); |
| 92 | |
| 93 | /** Static function to check if given info will lead to a valid configuration of @ref CLGenerateProposalsLayer |
| 94 | * |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 95 | * @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. |
| 96 | * Data types supported: QASYMM8/F16/F32 |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 97 | * @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 | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 98 | * @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 |
| 99 | * @param[in] proposals Box proposals info output tensor of size (5, W*H*A). |
| 100 | * Data types supported: QASYMM16 with scale of 0.125 and 0 offset if @p scores is QASYMM8, otherwise same as @p scores |
| 101 | * @param[in] scores_out Box scores output tensor info of size (W*H*A). Data types supported: Same as @p scores |
| 102 | * @param[in] num_valid_proposals Scalar output tensor info which says which of the first proposals are valid. Data types supported: U32 |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 103 | * @param[in] info Contains GenerateProposals operation information described in @ref GenerateProposalsInfo |
| 104 | * |
| 105 | * @return a Status |
| 106 | */ |
| 107 | static Status validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out, |
| 108 | const ITensorInfo *num_valid_proposals, |
| 109 | const GenerateProposalsInfo &info); |
| 110 | |
| 111 | // Inherited methods overridden: |
| 112 | void run() override; |
| 113 | |
| 114 | private: |
| 115 | // Memory group manager |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 116 | MemoryGroup _memory_group; |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 117 | |
| 118 | // OpenCL kernels |
| 119 | CLPermuteKernel _permute_deltas_kernel; |
| 120 | CLReshapeLayerKernel _flatten_deltas_kernel; |
| 121 | CLPermuteKernel _permute_scores_kernel; |
| 122 | CLReshapeLayerKernel _flatten_scores_kernel; |
| 123 | CLComputeAllAnchorsKernel _compute_anchors_kernel; |
| 124 | CLBoundingBoxTransformKernel _bounding_box_kernel; |
Michele Di Giorgio | 4c268b9 | 2019-09-20 14:01:48 +0100 | [diff] [blame] | 125 | CLPadLayerKernel _pad_kernel; |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 126 | CLDequantizationLayerKernel _dequantize_anchors; |
| 127 | CLDequantizationLayerKernel _dequantize_deltas; |
| 128 | CLQuantizationLayerKernel _quantize_all_proposals; |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 129 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 130 | // CPP functions |
| 131 | CPPBoxWithNonMaximaSuppressionLimit _cpp_nms; |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 132 | |
| 133 | bool _is_nhwc; |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 134 | bool _is_qasymm8; |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 135 | |
| 136 | // Temporary tensors |
| 137 | CLTensor _deltas_permuted; |
| 138 | CLTensor _deltas_flattened; |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 139 | CLTensor _deltas_flattened_f32; |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 140 | CLTensor _scores_permuted; |
| 141 | CLTensor _scores_flattened; |
| 142 | CLTensor _all_anchors; |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 143 | CLTensor _all_anchors_f32; |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 144 | CLTensor _all_proposals; |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 145 | CLTensor _all_proposals_quantized; |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 146 | CLTensor _keeps_nms_unused; |
| 147 | CLTensor _classes_nms_unused; |
| 148 | CLTensor _proposals_4_roi_values; |
| 149 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 150 | // Temporary tensor pointers |
| 151 | CLTensor *_all_proposals_to_use; |
| 152 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 153 | // Output tensor pointers |
| 154 | ICLTensor *_num_valid_proposals; |
| 155 | ICLTensor *_scores_out; |
| 156 | |
| 157 | /** Internal function to run the CPP BoxWithNMS kernel */ |
| 158 | void run_cpp_nms_kernel(); |
| 159 | }; |
| 160 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 161 | #endif /* ARM_COMPUTE_CLGENERATEPROPOSALSLAYER_H */ |