Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 1 | /* |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +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 | */ |
| 24 | #include "arm_compute/runtime/NEON/functions/NEDetectionPostProcessLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/Validate.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 30 | #include "src/common/utils/Log.h" |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 31 | |
| 32 | #include <cstddef> |
| 33 | #include <ios> |
| 34 | #include <list> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | NEDetectionPostProcessLayer::NEDetectionPostProcessLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | : _memory_group(std::move(memory_manager)), |
| 40 | _dequantize(), |
| 41 | _detection_post_process(), |
| 42 | _decoded_scores(), |
| 43 | _run_dequantize(false) |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 47 | void NEDetectionPostProcessLayer::configure(const ITensor *input_box_encoding, |
| 48 | const ITensor *input_scores, |
| 49 | const ITensor *input_anchors, |
| 50 | ITensor *output_boxes, |
| 51 | ITensor *output_classes, |
| 52 | ITensor *output_scores, |
| 53 | ITensor *num_detection, |
| 54 | DetectionPostProcessLayerInfo info) |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 55 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_ERROR_ON_NULLPTR(input_box_encoding, input_scores, input_anchors, output_boxes, output_classes, |
| 57 | output_scores); |
| 58 | ARM_COMPUTE_ERROR_THROW_ON(NEDetectionPostProcessLayer::validate( |
| 59 | input_box_encoding->info(), input_scores->info(), input_anchors->info(), output_boxes->info(), |
| 60 | output_classes->info(), output_scores->info(), num_detection->info(), info)); |
| 61 | ARM_COMPUTE_LOG_PARAMS(input_box_encoding, input_scores, input_anchors, output_boxes, output_classes, output_scores, |
| 62 | num_detection, info); |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 63 | |
| 64 | const ITensor *input_scores_to_use = input_scores; |
| 65 | DetectionPostProcessLayerInfo info_to_use = info; |
| 66 | _run_dequantize = is_data_type_quantized(input_box_encoding->info()->data_type()); |
| 67 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | if (_run_dequantize) |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 69 | { |
| 70 | _memory_group.manage(&_decoded_scores); |
| 71 | |
| 72 | _dequantize.configure(input_scores, &_decoded_scores); |
| 73 | |
| 74 | input_scores_to_use = &_decoded_scores; |
| 75 | |
| 76 | // Create a new info struct to avoid dequantizing in the CPP layer |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 77 | std::array<float, 4> scales_values{info.scale_value_y(), info.scale_value_x(), info.scale_value_h(), |
| 78 | info.scale_value_w()}; |
| 79 | DetectionPostProcessLayerInfo info_quantized( |
| 80 | info.max_detections(), info.max_classes_per_detection(), info.nms_score_threshold(), info.iou_threshold(), |
| 81 | info.num_classes(), scales_values, info.use_regular_nms(), info.detection_per_class(), false); |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 82 | info_to_use = info_quantized; |
| 83 | } |
| 84 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | _detection_post_process.configure(input_box_encoding, input_scores_to_use, input_anchors, output_boxes, |
| 86 | output_classes, output_scores, num_detection, info_to_use); |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 87 | _decoded_scores.allocator()->allocate(); |
| 88 | } |
| 89 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | Status NEDetectionPostProcessLayer::validate(const ITensorInfo *input_box_encoding, |
| 91 | const ITensorInfo *input_scores, |
| 92 | const ITensorInfo *input_anchors, |
| 93 | ITensorInfo *output_boxes, |
| 94 | ITensorInfo *output_classes, |
| 95 | ITensorInfo *output_scores, |
| 96 | ITensorInfo *num_detection, |
| 97 | DetectionPostProcessLayerInfo info) |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 98 | { |
| 99 | bool run_dequantize = is_data_type_quantized(input_box_encoding->data_type()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 100 | if (run_dequantize) |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 101 | { |
| 102 | TensorInfo decoded_classes_info = input_scores->clone()->set_is_resizable(true).set_data_type(DataType::F32); |
| 103 | ARM_COMPUTE_RETURN_ON_ERROR(NEDequantizationLayer::validate(input_scores, &decoded_classes_info)); |
| 104 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 105 | ARM_COMPUTE_RETURN_ON_ERROR(CPPDetectionPostProcessLayer::validate(input_box_encoding, input_scores, input_anchors, |
| 106 | output_boxes, output_classes, output_scores, |
| 107 | num_detection, info)); |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 108 | |
| 109 | return Status{}; |
| 110 | } |
| 111 | |
| 112 | void NEDetectionPostProcessLayer::run() |
| 113 | { |
| 114 | MemoryGroupResourceScope scope_mg(_memory_group); |
| 115 | |
| 116 | // Decode scores if necessary |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 117 | if (_run_dequantize) |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 118 | { |
| 119 | _dequantize.run(); |
| 120 | } |
| 121 | _detection_post_process.run(); |
| 122 | } |
| 123 | } // namespace arm_compute |