Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +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 | */ |
| 24 | #ifndef __ARM_COMPUTE_CPP_DETECTION_POSTPROCESS_H__ |
| 25 | #define __ARM_COMPUTE_CPP_DETECTION_POSTPROCESS_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/CPP/ICPPSimpleFunction.h" |
| 28 | |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/runtime/CPP/functions/CPPNonMaximumSuppression.h" |
| 31 | #include "arm_compute/runtime/IMemoryManager.h" |
| 32 | #include "arm_compute/runtime/MemoryGroup.h" |
| 33 | #include "arm_compute/runtime/Tensor.h" |
| 34 | |
| 35 | #include <map> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | class ITensor; |
| 40 | |
| 41 | /** CPP Function to generate the detection output based on center size encoded boxes, class prediction and anchors |
| 42 | * by doing non maximum suppression. |
| 43 | * |
| 44 | * @note Intended for use with MultiBox detection method. |
| 45 | */ |
| 46 | class CPPDetectionPostProcessLayer : public IFunction |
| 47 | { |
| 48 | public: |
| 49 | /** Constructor */ |
| 50 | CPPDetectionPostProcessLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 51 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 52 | CPPDetectionPostProcessLayer(const CPPDetectionPostProcessLayer &) = delete; |
| 53 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 54 | CPPDetectionPostProcessLayer &operator=(const CPPDetectionPostProcessLayer &) = delete; |
| 55 | /** Configure the detection output layer CPP function |
| 56 | * |
| 57 | * @param[in] input_box_encoding The bounding box input tensor. Data types supported: F32, QASYMM8. |
| 58 | * @param[in] input_score The class prediction input tensor. Data types supported: Same as @p input_box_encoding. |
| 59 | * @param[in] input_anchors The anchors input tensor. Data types supported: Same as @p input_box_encoding. |
| 60 | * @param[out] output_boxes The boxes output tensor. Data types supported: F32. |
| 61 | * @param[out] output_classes The classes output tensor. Data types supported: Same as @p output_boxes. |
| 62 | * @param[out] output_scores The scores output tensor. Data types supported: Same as @p output_boxes. |
| 63 | * @param[out] num_detection The number of output detection. Data types supported: Same as @p output_boxes. |
| 64 | * @param[in] info (Optional) DetectionPostProcessLayerInfo information. |
| 65 | * |
| 66 | * @note Output contains all the detections. Of those, only the ones selected by the valid region are valid. |
| 67 | */ |
| 68 | void configure(const ITensor *input_box_encoding, const ITensor *input_score, const ITensor *input_anchors, |
| 69 | ITensor *output_boxes, ITensor *output_classes, ITensor *output_scores, ITensor *num_detection, DetectionPostProcessLayerInfo info = DetectionPostProcessLayerInfo()); |
| 70 | /** Static function to check if given info will lead to a valid configuration of @ref CPPDetectionPostProcessLayer |
| 71 | * |
| 72 | * @param[in] input_box_encoding The bounding box input tensor info. Data types supported: F32, QASYMM8. |
| 73 | * @param[in] input_class_score The class prediction input tensor info. Data types supported: F32, QASYMM8. |
| 74 | * @param[in] input_anchors The anchors input tensor. Data types supported: F32, QASYMM8. |
| 75 | * @param[out] output_boxes The output tensor. Data types supported: F32. |
| 76 | * @param[out] output_classes The output tensor. Data types supported: Same as @p output_boxes. |
| 77 | * @param[out] output_scores The output tensor. Data types supported: Same as @p output_boxes. |
| 78 | * @param[out] num_detection The number of output detection. Data types supported: Same as @p output_boxes. |
| 79 | * @param[in] info (Optional) DetectionPostProcessLayerInfo information. |
| 80 | * |
| 81 | * @return a status |
| 82 | */ |
| 83 | static Status validate(const ITensorInfo *input_box_encoding, const ITensorInfo *input_class_score, const ITensorInfo *input_anchors, |
| 84 | ITensorInfo *output_boxes, ITensorInfo *output_classes, ITensorInfo *output_scores, ITensorInfo *num_detection, |
| 85 | DetectionPostProcessLayerInfo info = DetectionPostProcessLayerInfo()); |
| 86 | // Inherited methods overridden: |
| 87 | void run() override; |
| 88 | |
| 89 | private: |
| 90 | MemoryGroup _memory_group; |
| 91 | CPPNonMaximumSuppression _nms; |
| 92 | const ITensor *_input_box_encoding; |
| 93 | const ITensor *_input_scores; |
| 94 | const ITensor *_input_anchors; |
| 95 | ITensor *_output_boxes; |
| 96 | ITensor *_output_classes; |
| 97 | ITensor *_output_scores; |
| 98 | ITensor *_num_detection; |
| 99 | DetectionPostProcessLayerInfo _info; |
| 100 | |
| 101 | const unsigned int _kBatchSize = 1; |
| 102 | const unsigned int _kNumCoordBox = 4; |
| 103 | unsigned int _num_boxes; |
| 104 | unsigned int _num_classes_with_background; |
| 105 | unsigned int _num_max_detected_boxes; |
| 106 | |
| 107 | Tensor _decoded_boxes; |
| 108 | Tensor _decoded_scores; |
| 109 | Tensor _selected_indices; |
| 110 | Tensor _class_scores; |
| 111 | const ITensor *_input_scores_to_use; |
| 112 | |
| 113 | // Intermediate results |
| 114 | std::vector<int> _result_idx_boxes_after_nms; |
| 115 | std::vector<int> _result_classes_after_nms; |
| 116 | std::vector<float> _result_scores_after_nms; |
| 117 | std::vector<unsigned int> _sorted_indices; |
| 118 | |
| 119 | // Temporary values |
| 120 | std::vector<float> _box_scores; |
| 121 | }; |
| 122 | } // namespace arm_compute |
| 123 | #endif /* __ARM_COMPUTE_CPP_DETECTION_POSTPROCESS_H__ */ |