Isabella Gottardi | 05e5644 | 2018-11-16 11:26:52 +0000 | [diff] [blame] | 1 | /* |
Pablo Tello | e96e4f0 | 2018-12-21 16:47:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Isabella Gottardi | 05e5644 | 2018-11-16 11:26:52 +0000 | [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 | */ |
| 24 | #ifndef __ARM_COMPUTE_CPP_DETECTION_OUTPUT_LAYER_H__ |
| 25 | #define __ARM_COMPUTE_CPP_DETECTION_OUTPUT_LAYER_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/CPP/ICPPSimpleFunction.h" |
| 28 | |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | |
| 31 | #include <map> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | class ITensor; |
| 36 | |
| 37 | // Normalized Bounding Box [xmin, ymin, xmax, ymax] |
| 38 | using NormalizedBBox = std::array<float, 4>; |
| 39 | // LabelBBox used for map label and bounding box |
| 40 | using LabelBBox = std::map<int, std::vector<NormalizedBBox>>; |
| 41 | |
Pablo Tello | e96e4f0 | 2018-12-21 16:47:23 +0000 | [diff] [blame] | 42 | /** CPP Function to perform non maximum suppression on the bounding boxes and scores |
| 43 | * |
| 44 | */ |
| 45 | class CPPNonMaximumSuppression : public IFunction |
| 46 | { |
| 47 | public: |
| 48 | /** Default constructor */ |
| 49 | CPPNonMaximumSuppression(); |
| 50 | /** Configure the function to perform non maximal suppression |
| 51 | * |
| 52 | * @param[in] bboxes The input bounding boxes. Data types supported: F32. |
| 53 | * @param[in] scores The corresponding input confidence. Same as @p scores. |
| 54 | * @param[out] indices The kept indices of bboxes after nms. Data types supported: S32. |
| 55 | * @param[in] max_output_size An integer tensor representing the maximum number of boxes to be selected by non max suppression. |
| 56 | * @param[in] score_threshold The threshold used to filter detection results. |
| 57 | * @param[in] nms_threshold The threshold used in non maximum suppression. |
| 58 | * |
| 59 | */ |
| 60 | void configure(const ITensor *bboxes, const ITensor *scores, ITensor *indices, unsigned int max_output_size, const float score_threshold, const float nms_threshold); |
| 61 | |
| 62 | /** Static function to check if given arguments will lead to a valid configuration of @ref CPPNonMaximumSuppression |
| 63 | * |
| 64 | * @param[in] bboxes The input bounding boxes. Data types supported: F32. |
| 65 | * @param[in] scores The corresponding input confidence. Same as @p scores. |
| 66 | * @param[out] indices The kept indices of bboxes after nms. Data types supported: S32. |
| 67 | * @param[in] max_output_size An integer tensor representing the maximum number of boxes to be selected by non max suppression. |
| 68 | * @param[in] score_threshold The threshold used to filter detection results. |
| 69 | * @param[in] nms_threshold The threshold used in non maximum suppression. |
| 70 | * |
| 71 | */ |
| 72 | static Status validate(const ITensorInfo *bboxes, const ITensorInfo *scores, const ITensorInfo *indices, unsigned int max_output_size, |
| 73 | const float score_threshold, const float nms_threshold); |
| 74 | |
| 75 | // Inherited methods overridden: |
| 76 | void run() override; |
| 77 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 78 | CPPNonMaximumSuppression(const CPPNonMaximumSuppression &) = delete; |
| 79 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 80 | CPPNonMaximumSuppression &operator=(const CPPNonMaximumSuppression &) = delete; |
| 81 | |
| 82 | private: |
| 83 | const ITensor *_bboxes; |
| 84 | const ITensor *_scores; |
| 85 | ITensor *_indices; |
| 86 | unsigned int _max_output_size; |
| 87 | |
| 88 | float _score_threshold; |
| 89 | float _nms_threshold; |
| 90 | }; |
| 91 | |
Isabella Gottardi | 05e5644 | 2018-11-16 11:26:52 +0000 | [diff] [blame] | 92 | /** CPP Function to generate the detection output based on location and confidence |
| 93 | * predictions by doing non maximum suppression. |
| 94 | * |
| 95 | * @note Intended for use with MultiBox detection method. |
| 96 | */ |
| 97 | class CPPDetectionOutputLayer : public IFunction |
| 98 | { |
| 99 | public: |
| 100 | /** Default constructor */ |
| 101 | CPPDetectionOutputLayer(); |
| 102 | /** Configure the detection output layer CPP kernel |
| 103 | * |
| 104 | * @param[in] input_loc The mbox location input tensor of size [C1, N]. Data types supported: F32. |
| 105 | * @param[in] input_conf The mbox confidence input tensor of size [C2, N]. Data types supported: F32. |
| 106 | * @param[in] input_priorbox The mbox prior box input tensor of size [C3, 2, N]. Data types supported: F32. |
| 107 | * @param[out] output The output tensor of size [7, M]. Data types supported: Same as @p input |
| 108 | * @param[in] info (Optional) DetectionOutputLayerInfo information. |
| 109 | * |
| 110 | * @note Output contains all the detections. Of those, only the ones selected by the valid region are valid. |
| 111 | */ |
| 112 | void configure(const ITensor *input_loc, const ITensor *input_conf, const ITensor *input_priorbox, ITensor *output, DetectionOutputLayerInfo info = DetectionOutputLayerInfo()); |
| 113 | /** Static function to check if given info will lead to a valid configuration of @ref CPPDetectionOutputLayer |
| 114 | * |
| 115 | * @param[in] input_loc The mbox location input tensor info. Data types supported: F32. |
| 116 | * @param[in] input_conf The mbox confidence input tensor info. Data types supported: F32. |
| 117 | * @param[in] input_priorbox The mbox prior box input tensor info. Data types supported: F32. |
| 118 | * @param[in] output The output tensor info. Data types supported: Same as @p input |
| 119 | * @param[in] info (Optional) DetectionOutputLayerInfo information. |
| 120 | * |
| 121 | * @return a status |
| 122 | */ |
| 123 | static Status validate(const ITensorInfo *input_loc, const ITensorInfo *input_conf, const ITensorInfo *input_priorbox, const ITensorInfo *output, |
| 124 | DetectionOutputLayerInfo info = DetectionOutputLayerInfo()); |
| 125 | // Inherited methods overridden: |
| 126 | void run() override; |
| 127 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 128 | CPPDetectionOutputLayer(const CPPDetectionOutputLayer &) = delete; |
| 129 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 130 | CPPDetectionOutputLayer &operator=(const CPPDetectionOutputLayer &) = delete; |
| 131 | |
| 132 | private: |
| 133 | const ITensor *_input_loc; |
| 134 | const ITensor *_input_conf; |
| 135 | const ITensor *_input_priorbox; |
| 136 | ITensor *_output; |
| 137 | DetectionOutputLayerInfo _info; |
| 138 | |
| 139 | int _num_priors; |
| 140 | int _num; |
| 141 | |
| 142 | std::vector<LabelBBox> _all_location_predictions; |
| 143 | std::vector<std::map<int, std::vector<float>>> _all_confidence_scores; |
| 144 | std::vector<NormalizedBBox> _all_prior_bboxes; |
| 145 | std::vector<std::array<float, 4>> _all_prior_variances; |
| 146 | std::vector<LabelBBox> _all_decode_bboxes; |
| 147 | std::vector<std::map<int, std::vector<int>>> _all_indices; |
| 148 | }; |
| 149 | } // namespace arm_compute |
| 150 | #endif /* __ARM_COMPUTE_CPP_DETECTION_OUTPUT_LAYER_H__ */ |