blob: b347390162d4947d6111835e2f6c9a2a73eda57f [file] [log] [blame]
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001/*
ramelg01cbbb0382021-09-17 17:36:57 +01002 * Copyright (c) 2019-2021 Arm Limited.
Giuseppe Rossinid9853782019-10-25 11:11:44 +01003 *
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
ramelg01cbbb0382021-09-17 17:36:57 +010030#include "src/common/utils/Log.h"
Giuseppe Rossinid9853782019-10-25 11:11:44 +010031
32#include <cstddef>
33#include <ios>
34#include <list>
35
36namespace arm_compute
37{
38NEDetectionPostProcessLayer::NEDetectionPostProcessLayer(std::shared_ptr<IMemoryManager> memory_manager)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 : _memory_group(std::move(memory_manager)),
40 _dequantize(),
41 _detection_post_process(),
42 _decoded_scores(),
43 _run_dequantize(false)
Giuseppe Rossinid9853782019-10-25 11:11:44 +010044{
45}
46
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047void 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 Rossinid9853782019-10-25 11:11:44 +010055{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 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 Rossinid9853782019-10-25 11:11:44 +010063
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010068 if (_run_dequantize)
Giuseppe Rossinid9853782019-10-25 11:11:44 +010069 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 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 Rossinid9853782019-10-25 11:11:44 +010082 info_to_use = info_quantized;
83 }
84
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 _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 Rossinid9853782019-10-25 11:11:44 +010087 _decoded_scores.allocator()->allocate();
88}
89
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090Status 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 Rossinid9853782019-10-25 11:11:44 +010098{
99 bool run_dequantize = is_data_type_quantized(input_box_encoding->data_type());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 if (run_dequantize)
Giuseppe Rossinid9853782019-10-25 11:11:44 +0100101 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100105 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 Rossinid9853782019-10-25 11:11:44 +0100108
109 return Status{};
110}
111
112void NEDetectionPostProcessLayer::run()
113{
114 MemoryGroupResourceScope scope_mg(_memory_group);
115
116 // Decode scores if necessary
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 if (_run_dequantize)
Giuseppe Rossinid9853782019-10-25 11:11:44 +0100118 {
119 _dequantize.run();
120 }
121 _detection_post_process.run();
122}
123} // namespace arm_compute