1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2019-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/runtime/CPP/functions/CPPDetectionPostProcessLayer.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/Error.h"
27*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
28*c217d954SCole Faust #include "arm_compute/core/Validate.h"
29*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
30*c217d954SCole Faust
31*c217d954SCole Faust #include "src/common/utils/Log.h"
32*c217d954SCole Faust
33*c217d954SCole Faust #include <cstddef>
34*c217d954SCole Faust #include <ios>
35*c217d954SCole Faust #include <list>
36*c217d954SCole Faust
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace
40*c217d954SCole Faust {
validate_arguments(const ITensorInfo * input_box_encoding,const ITensorInfo * input_class_score,const ITensorInfo * input_anchors,ITensorInfo * output_boxes,ITensorInfo * output_classes,ITensorInfo * output_scores,ITensorInfo * num_detection,DetectionPostProcessLayerInfo info,const unsigned int kBatchSize,const unsigned int kNumCoordBox)41*c217d954SCole Faust Status validate_arguments(const ITensorInfo *input_box_encoding, const ITensorInfo *input_class_score, const ITensorInfo *input_anchors,
42*c217d954SCole Faust ITensorInfo *output_boxes, ITensorInfo *output_classes, ITensorInfo *output_scores, ITensorInfo *num_detection,
43*c217d954SCole Faust DetectionPostProcessLayerInfo info, const unsigned int kBatchSize, const unsigned int kNumCoordBox)
44*c217d954SCole Faust {
45*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input_box_encoding, input_class_score, input_anchors);
46*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_box_encoding, 1, DataType::F32, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
47*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_box_encoding, input_anchors);
48*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(input_box_encoding->num_dimensions() > 3, "The location input tensor shape should be [4, N, kBatchSize].");
49*c217d954SCole Faust if(input_box_encoding->num_dimensions() > 2)
50*c217d954SCole Faust {
51*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR(input_box_encoding->dimension(2) != kBatchSize, "The third dimension of the input box_encoding tensor should be equal to %d.", kBatchSize);
52*c217d954SCole Faust }
53*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR(input_box_encoding->dimension(0) != kNumCoordBox, "The first dimension of the input box_encoding tensor should be equal to %d.", kNumCoordBox);
54*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(input_class_score->dimension(0) != (info.num_classes() + 1),
55*c217d954SCole Faust "The first dimension of the input class_prediction should be equal to the number of classes plus one.");
56*c217d954SCole Faust
57*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(input_anchors->num_dimensions() > 3, "The anchors input tensor shape should be [4, N, kBatchSize].");
58*c217d954SCole Faust if(input_anchors->num_dimensions() > 2)
59*c217d954SCole Faust {
60*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR(input_anchors->dimension(0) != kNumCoordBox, "The first dimension of the input anchors tensor should be equal to %d.", kNumCoordBox);
61*c217d954SCole Faust }
62*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((input_box_encoding->dimension(1) != input_class_score->dimension(1))
63*c217d954SCole Faust || (input_box_encoding->dimension(1) != input_anchors->dimension(1)),
64*c217d954SCole Faust "The second dimension of the inputs should be the same.");
65*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_detection->num_dimensions() > 1, "The num_detection output tensor shape should be [M].");
66*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((info.iou_threshold() <= 0.0f) || (info.iou_threshold() > 1.0f), "The intersection over union should be positive and less than 1.");
67*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_classes_per_detection() <= 0, "The number of max classes per detection should be positive.");
68*c217d954SCole Faust
69*c217d954SCole Faust const unsigned int num_detected_boxes = info.max_detections() * info.max_classes_per_detection();
70*c217d954SCole Faust
71*c217d954SCole Faust // Validate configured outputs
72*c217d954SCole Faust if(output_boxes->total_size() != 0)
73*c217d954SCole Faust {
74*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output_boxes->tensor_shape(), TensorShape(4U, num_detected_boxes, 1U));
75*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_boxes, 1, DataType::F32);
76*c217d954SCole Faust }
77*c217d954SCole Faust if(output_classes->total_size() != 0)
78*c217d954SCole Faust {
79*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output_classes->tensor_shape(), TensorShape(num_detected_boxes, 1U));
80*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_classes, 1, DataType::F32);
81*c217d954SCole Faust }
82*c217d954SCole Faust if(output_scores->total_size() != 0)
83*c217d954SCole Faust {
84*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output_scores->tensor_shape(), TensorShape(num_detected_boxes, 1U));
85*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_scores, 1, DataType::F32);
86*c217d954SCole Faust }
87*c217d954SCole Faust if(num_detection->total_size() != 0)
88*c217d954SCole Faust {
89*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(num_detection->tensor_shape(), TensorShape(1U));
90*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(num_detection, 1, DataType::F32);
91*c217d954SCole Faust }
92*c217d954SCole Faust
93*c217d954SCole Faust return Status{};
94*c217d954SCole Faust }
95*c217d954SCole Faust
DecodeBoxCorner(BBox & box_centersize,BBox & anchor,Iterator & decoded_it,DetectionPostProcessLayerInfo info)96*c217d954SCole Faust inline void DecodeBoxCorner(BBox &box_centersize, BBox &anchor, Iterator &decoded_it, DetectionPostProcessLayerInfo info)
97*c217d954SCole Faust {
98*c217d954SCole Faust const float half_factor = 0.5f;
99*c217d954SCole Faust
100*c217d954SCole Faust // BBox is equavalent to CenterSizeEncoding [y,x,h,w]
101*c217d954SCole Faust const float y_center = box_centersize[0] / info.scale_value_y() * anchor[2] + anchor[0];
102*c217d954SCole Faust const float x_center = box_centersize[1] / info.scale_value_x() * anchor[3] + anchor[1];
103*c217d954SCole Faust const float half_h = half_factor * static_cast<float>(std::exp(box_centersize[2] / info.scale_value_h())) * anchor[2];
104*c217d954SCole Faust const float half_w = half_factor * static_cast<float>(std::exp(box_centersize[3] / info.scale_value_w())) * anchor[3];
105*c217d954SCole Faust
106*c217d954SCole Faust // Box Corner encoding boxes are saved as [xmin, ymin, xmax, ymax]
107*c217d954SCole Faust auto decoded_ptr = reinterpret_cast<float *>(decoded_it.ptr());
108*c217d954SCole Faust *(decoded_ptr) = x_center - half_w; // xmin
109*c217d954SCole Faust *(1 + decoded_ptr) = y_center - half_h; // ymin
110*c217d954SCole Faust *(2 + decoded_ptr) = x_center + half_w; // xmax
111*c217d954SCole Faust *(3 + decoded_ptr) = y_center + half_h; // ymax
112*c217d954SCole Faust }
113*c217d954SCole Faust
114*c217d954SCole Faust /** Decode a bbox according to a anchors and scale info.
115*c217d954SCole Faust *
116*c217d954SCole Faust * @param[in] input_box_encoding The input prior bounding boxes.
117*c217d954SCole Faust * @param[in] input_anchors The corresponding input variance.
118*c217d954SCole Faust * @param[in] info The detection informations
119*c217d954SCole Faust * @param[out] decoded_boxes The decoded bboxes.
120*c217d954SCole Faust */
DecodeCenterSizeBoxes(const ITensor * input_box_encoding,const ITensor * input_anchors,DetectionPostProcessLayerInfo info,Tensor * decoded_boxes)121*c217d954SCole Faust void DecodeCenterSizeBoxes(const ITensor *input_box_encoding, const ITensor *input_anchors, DetectionPostProcessLayerInfo info, Tensor *decoded_boxes)
122*c217d954SCole Faust {
123*c217d954SCole Faust const QuantizationInfo &qi_box = input_box_encoding->info()->quantization_info();
124*c217d954SCole Faust const QuantizationInfo &qi_anchors = input_anchors->info()->quantization_info();
125*c217d954SCole Faust BBox box_centersize{ {} };
126*c217d954SCole Faust BBox anchor{ {} };
127*c217d954SCole Faust
128*c217d954SCole Faust Window win;
129*c217d954SCole Faust win.use_tensor_dimensions(input_box_encoding->info()->tensor_shape());
130*c217d954SCole Faust win.set_dimension_step(0U, 4U);
131*c217d954SCole Faust win.set_dimension_step(1U, 1U);
132*c217d954SCole Faust Iterator box_it(input_box_encoding, win);
133*c217d954SCole Faust Iterator anchor_it(input_anchors, win);
134*c217d954SCole Faust Iterator decoded_it(decoded_boxes, win);
135*c217d954SCole Faust
136*c217d954SCole Faust if(input_box_encoding->info()->data_type() == DataType::QASYMM8)
137*c217d954SCole Faust {
138*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates &)
139*c217d954SCole Faust {
140*c217d954SCole Faust const auto box_ptr = reinterpret_cast<const qasymm8_t *>(box_it.ptr());
141*c217d954SCole Faust const auto anchor_ptr = reinterpret_cast<const qasymm8_t *>(anchor_it.ptr());
142*c217d954SCole Faust box_centersize = BBox({ dequantize_qasymm8(*box_ptr, qi_box), dequantize_qasymm8(*(box_ptr + 1), qi_box),
143*c217d954SCole Faust dequantize_qasymm8(*(2 + box_ptr), qi_box), dequantize_qasymm8(*(3 + box_ptr), qi_box)
144*c217d954SCole Faust });
145*c217d954SCole Faust anchor = BBox({ dequantize_qasymm8(*anchor_ptr, qi_anchors), dequantize_qasymm8(*(anchor_ptr + 1), qi_anchors),
146*c217d954SCole Faust dequantize_qasymm8(*(2 + anchor_ptr), qi_anchors), dequantize_qasymm8(*(3 + anchor_ptr), qi_anchors)
147*c217d954SCole Faust });
148*c217d954SCole Faust DecodeBoxCorner(box_centersize, anchor, decoded_it, info);
149*c217d954SCole Faust },
150*c217d954SCole Faust box_it, anchor_it, decoded_it);
151*c217d954SCole Faust }
152*c217d954SCole Faust else if(input_box_encoding->info()->data_type() == DataType::QASYMM8_SIGNED)
153*c217d954SCole Faust {
154*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates &)
155*c217d954SCole Faust {
156*c217d954SCole Faust const auto box_ptr = reinterpret_cast<const qasymm8_signed_t *>(box_it.ptr());
157*c217d954SCole Faust const auto anchor_ptr = reinterpret_cast<const qasymm8_signed_t *>(anchor_it.ptr());
158*c217d954SCole Faust box_centersize = BBox({ dequantize_qasymm8_signed(*box_ptr, qi_box), dequantize_qasymm8_signed(*(box_ptr + 1), qi_box),
159*c217d954SCole Faust dequantize_qasymm8_signed(*(2 + box_ptr), qi_box), dequantize_qasymm8_signed(*(3 + box_ptr), qi_box)
160*c217d954SCole Faust });
161*c217d954SCole Faust anchor = BBox({ dequantize_qasymm8_signed(*anchor_ptr, qi_anchors), dequantize_qasymm8_signed(*(anchor_ptr + 1), qi_anchors),
162*c217d954SCole Faust dequantize_qasymm8_signed(*(2 + anchor_ptr), qi_anchors), dequantize_qasymm8_signed(*(3 + anchor_ptr), qi_anchors)
163*c217d954SCole Faust });
164*c217d954SCole Faust DecodeBoxCorner(box_centersize, anchor, decoded_it, info);
165*c217d954SCole Faust },
166*c217d954SCole Faust box_it, anchor_it, decoded_it);
167*c217d954SCole Faust }
168*c217d954SCole Faust else
169*c217d954SCole Faust {
170*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates &)
171*c217d954SCole Faust {
172*c217d954SCole Faust const auto box_ptr = reinterpret_cast<const float *>(box_it.ptr());
173*c217d954SCole Faust const auto anchor_ptr = reinterpret_cast<const float *>(anchor_it.ptr());
174*c217d954SCole Faust box_centersize = BBox({ *box_ptr, *(box_ptr + 1), *(2 + box_ptr), *(3 + box_ptr) });
175*c217d954SCole Faust anchor = BBox({ *anchor_ptr, *(anchor_ptr + 1), *(2 + anchor_ptr), *(3 + anchor_ptr) });
176*c217d954SCole Faust DecodeBoxCorner(box_centersize, anchor, decoded_it, info);
177*c217d954SCole Faust },
178*c217d954SCole Faust box_it, anchor_it, decoded_it);
179*c217d954SCole Faust }
180*c217d954SCole Faust }
181*c217d954SCole Faust
SaveOutputs(const Tensor * decoded_boxes,const std::vector<int> & result_idx_boxes_after_nms,const std::vector<float> & result_scores_after_nms,const std::vector<int> & result_classes_after_nms,std::vector<unsigned int> & sorted_indices,const unsigned int num_output,const unsigned int max_detections,ITensor * output_boxes,ITensor * output_classes,ITensor * output_scores,ITensor * num_detection)182*c217d954SCole Faust void SaveOutputs(const Tensor *decoded_boxes, const std::vector<int> &result_idx_boxes_after_nms, const std::vector<float> &result_scores_after_nms, const std::vector<int> &result_classes_after_nms,
183*c217d954SCole Faust std::vector<unsigned int> &sorted_indices, const unsigned int num_output, const unsigned int max_detections, ITensor *output_boxes, ITensor *output_classes, ITensor *output_scores,
184*c217d954SCole Faust ITensor *num_detection)
185*c217d954SCole Faust {
186*c217d954SCole Faust // xmin,ymin,xmax,ymax -> ymin,xmin,ymax,xmax
187*c217d954SCole Faust unsigned int i = 0;
188*c217d954SCole Faust for(; i < num_output; ++i)
189*c217d954SCole Faust {
190*c217d954SCole Faust const unsigned int box_in_idx = result_idx_boxes_after_nms[sorted_indices[i]];
191*c217d954SCole Faust *(reinterpret_cast<float *>(output_boxes->ptr_to_element(Coordinates(0, i)))) = *(reinterpret_cast<float *>(decoded_boxes->ptr_to_element(Coordinates(1, box_in_idx))));
192*c217d954SCole Faust *(reinterpret_cast<float *>(output_boxes->ptr_to_element(Coordinates(1, i)))) = *(reinterpret_cast<float *>(decoded_boxes->ptr_to_element(Coordinates(0, box_in_idx))));
193*c217d954SCole Faust *(reinterpret_cast<float *>(output_boxes->ptr_to_element(Coordinates(2, i)))) = *(reinterpret_cast<float *>(decoded_boxes->ptr_to_element(Coordinates(3, box_in_idx))));
194*c217d954SCole Faust *(reinterpret_cast<float *>(output_boxes->ptr_to_element(Coordinates(3, i)))) = *(reinterpret_cast<float *>(decoded_boxes->ptr_to_element(Coordinates(2, box_in_idx))));
195*c217d954SCole Faust *(reinterpret_cast<float *>(output_classes->ptr_to_element(Coordinates(i)))) = static_cast<float>(result_classes_after_nms[sorted_indices[i]]);
196*c217d954SCole Faust *(reinterpret_cast<float *>(output_scores->ptr_to_element(Coordinates(i)))) = result_scores_after_nms[sorted_indices[i]];
197*c217d954SCole Faust }
198*c217d954SCole Faust for(; i < max_detections; ++i)
199*c217d954SCole Faust {
200*c217d954SCole Faust *(reinterpret_cast<float *>(output_boxes->ptr_to_element(Coordinates(1, i)))) = 0.0f;
201*c217d954SCole Faust *(reinterpret_cast<float *>(output_boxes->ptr_to_element(Coordinates(0, i)))) = 0.0f;
202*c217d954SCole Faust *(reinterpret_cast<float *>(output_boxes->ptr_to_element(Coordinates(3, i)))) = 0.0f;
203*c217d954SCole Faust *(reinterpret_cast<float *>(output_boxes->ptr_to_element(Coordinates(2, i)))) = 0.0f;
204*c217d954SCole Faust *(reinterpret_cast<float *>(output_classes->ptr_to_element(Coordinates(i)))) = 0.0f;
205*c217d954SCole Faust *(reinterpret_cast<float *>(output_scores->ptr_to_element(Coordinates(i)))) = 0.0f;
206*c217d954SCole Faust }
207*c217d954SCole Faust *(reinterpret_cast<float *>(num_detection->ptr_to_element(Coordinates(0)))) = num_output;
208*c217d954SCole Faust }
209*c217d954SCole Faust } // namespace
210*c217d954SCole Faust
CPPDetectionPostProcessLayer(std::shared_ptr<IMemoryManager> memory_manager)211*c217d954SCole Faust CPPDetectionPostProcessLayer::CPPDetectionPostProcessLayer(std::shared_ptr<IMemoryManager> memory_manager)
212*c217d954SCole Faust : _memory_group(std::move(memory_manager)), _nms(), _input_box_encoding(nullptr), _input_scores(nullptr), _input_anchors(nullptr), _output_boxes(nullptr), _output_classes(nullptr),
213*c217d954SCole Faust _output_scores(nullptr), _num_detection(nullptr), _info(), _num_boxes(), _num_classes_with_background(), _num_max_detected_boxes(), _dequantize_scores(false), _decoded_boxes(), _decoded_scores(),
214*c217d954SCole Faust _selected_indices(), _class_scores(), _input_scores_to_use(nullptr)
215*c217d954SCole Faust {
216*c217d954SCole Faust }
217*c217d954SCole Faust
configure(const ITensor * input_box_encoding,const ITensor * input_scores,const ITensor * input_anchors,ITensor * output_boxes,ITensor * output_classes,ITensor * output_scores,ITensor * num_detection,DetectionPostProcessLayerInfo info)218*c217d954SCole Faust void CPPDetectionPostProcessLayer::configure(const ITensor *input_box_encoding, const ITensor *input_scores,
219*c217d954SCole Faust const ITensor *input_anchors, ITensor *output_boxes, ITensor *output_classes,
220*c217d954SCole Faust ITensor *output_scores, ITensor *num_detection, DetectionPostProcessLayerInfo info)
221*c217d954SCole Faust {
222*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(input_box_encoding, input_scores, input_anchors, output_boxes, output_classes, output_scores);
223*c217d954SCole Faust ARM_COMPUTE_LOG_PARAMS(input_box_encoding, input_scores, input_anchors, output_boxes, output_classes, output_scores,
224*c217d954SCole Faust num_detection, info);
225*c217d954SCole Faust
226*c217d954SCole Faust _num_max_detected_boxes = info.max_detections() * info.max_classes_per_detection();
227*c217d954SCole Faust
228*c217d954SCole Faust auto_init_if_empty(*output_boxes->info(), TensorInfo(TensorShape(_kNumCoordBox, _num_max_detected_boxes, _kBatchSize), 1, DataType::F32));
229*c217d954SCole Faust auto_init_if_empty(*output_classes->info(), TensorInfo(TensorShape(_num_max_detected_boxes, _kBatchSize), 1, DataType::F32));
230*c217d954SCole Faust auto_init_if_empty(*output_scores->info(), TensorInfo(TensorShape(_num_max_detected_boxes, _kBatchSize), 1, DataType::F32));
231*c217d954SCole Faust auto_init_if_empty(*num_detection->info(), TensorInfo(TensorShape(1U), 1, DataType::F32));
232*c217d954SCole Faust
233*c217d954SCole Faust // Perform validation step
234*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_box_encoding->info(), input_scores->info(), input_anchors->info(), output_boxes->info(), output_classes->info(), output_scores->info(),
235*c217d954SCole Faust num_detection->info(),
236*c217d954SCole Faust info, _kBatchSize, _kNumCoordBox));
237*c217d954SCole Faust
238*c217d954SCole Faust _input_box_encoding = input_box_encoding;
239*c217d954SCole Faust _input_scores = input_scores;
240*c217d954SCole Faust _input_anchors = input_anchors;
241*c217d954SCole Faust _output_boxes = output_boxes;
242*c217d954SCole Faust _output_classes = output_classes;
243*c217d954SCole Faust _output_scores = output_scores;
244*c217d954SCole Faust _num_detection = num_detection;
245*c217d954SCole Faust _info = info;
246*c217d954SCole Faust _num_boxes = input_box_encoding->info()->dimension(1);
247*c217d954SCole Faust _num_classes_with_background = _input_scores->info()->dimension(0);
248*c217d954SCole Faust _dequantize_scores = (info.dequantize_scores() && is_data_type_quantized(input_box_encoding->info()->data_type()));
249*c217d954SCole Faust
250*c217d954SCole Faust auto_init_if_empty(*_decoded_boxes.info(), TensorInfo(TensorShape(_kNumCoordBox, _input_box_encoding->info()->dimension(1), _kBatchSize), 1, DataType::F32));
251*c217d954SCole Faust auto_init_if_empty(*_decoded_scores.info(), TensorInfo(TensorShape(_input_scores->info()->dimension(0), _input_scores->info()->dimension(1), _kBatchSize), 1, DataType::F32));
252*c217d954SCole Faust auto_init_if_empty(*_selected_indices.info(), TensorInfo(TensorShape(info.use_regular_nms() ? info.detection_per_class() : info.max_detections()), 1, DataType::S32));
253*c217d954SCole Faust const unsigned int num_classes_per_box = std::min(info.max_classes_per_detection(), info.num_classes());
254*c217d954SCole Faust auto_init_if_empty(*_class_scores.info(), TensorInfo(info.use_regular_nms() ? TensorShape(_num_boxes) : TensorShape(_num_boxes * num_classes_per_box), 1, DataType::F32));
255*c217d954SCole Faust
256*c217d954SCole Faust _input_scores_to_use = _dequantize_scores ? &_decoded_scores : _input_scores;
257*c217d954SCole Faust
258*c217d954SCole Faust // Manage intermediate buffers
259*c217d954SCole Faust _memory_group.manage(&_decoded_boxes);
260*c217d954SCole Faust _memory_group.manage(&_decoded_scores);
261*c217d954SCole Faust _memory_group.manage(&_selected_indices);
262*c217d954SCole Faust _memory_group.manage(&_class_scores);
263*c217d954SCole Faust _nms.configure(&_decoded_boxes, &_class_scores, &_selected_indices, info.use_regular_nms() ? info.detection_per_class() : info.max_detections(), info.nms_score_threshold(), info.iou_threshold());
264*c217d954SCole Faust
265*c217d954SCole Faust // Allocate and reserve intermediate tensors and vectors
266*c217d954SCole Faust _decoded_boxes.allocator()->allocate();
267*c217d954SCole Faust _decoded_scores.allocator()->allocate();
268*c217d954SCole Faust _selected_indices.allocator()->allocate();
269*c217d954SCole Faust _class_scores.allocator()->allocate();
270*c217d954SCole Faust }
271*c217d954SCole Faust
validate(const ITensorInfo * input_box_encoding,const ITensorInfo * input_class_score,const ITensorInfo * input_anchors,ITensorInfo * output_boxes,ITensorInfo * output_classes,ITensorInfo * output_scores,ITensorInfo * num_detection,DetectionPostProcessLayerInfo info)272*c217d954SCole Faust Status CPPDetectionPostProcessLayer::validate(const ITensorInfo *input_box_encoding, const ITensorInfo *input_class_score, const ITensorInfo *input_anchors,
273*c217d954SCole Faust ITensorInfo *output_boxes, ITensorInfo *output_classes, ITensorInfo *output_scores, ITensorInfo *num_detection, DetectionPostProcessLayerInfo info)
274*c217d954SCole Faust {
275*c217d954SCole Faust constexpr unsigned int kBatchSize = 1;
276*c217d954SCole Faust constexpr unsigned int kNumCoordBox = 4;
277*c217d954SCole Faust const TensorInfo _decoded_boxes_info = TensorInfo(TensorShape(kNumCoordBox, input_box_encoding->dimension(1)), 1, DataType::F32);
278*c217d954SCole Faust const TensorInfo _decoded_scores_info = TensorInfo(TensorShape(input_box_encoding->dimension(1)), 1, DataType::F32);
279*c217d954SCole Faust const TensorInfo _selected_indices_info = TensorInfo(TensorShape(info.max_detections()), 1, DataType::S32);
280*c217d954SCole Faust
281*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CPPNonMaximumSuppression::validate(&_decoded_boxes_info, &_decoded_scores_info, &_selected_indices_info, info.max_detections(), info.nms_score_threshold(),
282*c217d954SCole Faust info.iou_threshold()));
283*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input_box_encoding, input_class_score, input_anchors, output_boxes, output_classes, output_scores, num_detection, info, kBatchSize, kNumCoordBox));
284*c217d954SCole Faust
285*c217d954SCole Faust return Status{};
286*c217d954SCole Faust }
287*c217d954SCole Faust
run()288*c217d954SCole Faust void CPPDetectionPostProcessLayer::run()
289*c217d954SCole Faust {
290*c217d954SCole Faust const unsigned int num_classes = _info.num_classes();
291*c217d954SCole Faust const unsigned int max_detections = _info.max_detections();
292*c217d954SCole Faust
293*c217d954SCole Faust DecodeCenterSizeBoxes(_input_box_encoding, _input_anchors, _info, &_decoded_boxes);
294*c217d954SCole Faust
295*c217d954SCole Faust // Decode scores if necessary
296*c217d954SCole Faust if(_dequantize_scores)
297*c217d954SCole Faust {
298*c217d954SCole Faust if(_input_box_encoding->info()->data_type() == DataType::QASYMM8)
299*c217d954SCole Faust {
300*c217d954SCole Faust for(unsigned int idx_c = 0; idx_c < _num_classes_with_background; ++idx_c)
301*c217d954SCole Faust {
302*c217d954SCole Faust for(unsigned int idx_b = 0; idx_b < _num_boxes; ++idx_b)
303*c217d954SCole Faust {
304*c217d954SCole Faust *(reinterpret_cast<float *>(_decoded_scores.ptr_to_element(Coordinates(idx_c, idx_b)))) =
305*c217d954SCole Faust dequantize_qasymm8(*(reinterpret_cast<qasymm8_t *>(_input_scores->ptr_to_element(Coordinates(idx_c, idx_b)))), _input_scores->info()->quantization_info());
306*c217d954SCole Faust }
307*c217d954SCole Faust }
308*c217d954SCole Faust }
309*c217d954SCole Faust else if(_input_box_encoding->info()->data_type() == DataType::QASYMM8_SIGNED)
310*c217d954SCole Faust {
311*c217d954SCole Faust for(unsigned int idx_c = 0; idx_c < _num_classes_with_background; ++idx_c)
312*c217d954SCole Faust {
313*c217d954SCole Faust for(unsigned int idx_b = 0; idx_b < _num_boxes; ++idx_b)
314*c217d954SCole Faust {
315*c217d954SCole Faust *(reinterpret_cast<float *>(_decoded_scores.ptr_to_element(Coordinates(idx_c, idx_b)))) =
316*c217d954SCole Faust dequantize_qasymm8_signed(*(reinterpret_cast<qasymm8_signed_t *>(_input_scores->ptr_to_element(Coordinates(idx_c, idx_b)))), _input_scores->info()->quantization_info());
317*c217d954SCole Faust }
318*c217d954SCole Faust }
319*c217d954SCole Faust }
320*c217d954SCole Faust }
321*c217d954SCole Faust
322*c217d954SCole Faust // Regular NMS
323*c217d954SCole Faust if(_info.use_regular_nms())
324*c217d954SCole Faust {
325*c217d954SCole Faust std::vector<int> result_idx_boxes_after_nms;
326*c217d954SCole Faust std::vector<int> result_classes_after_nms;
327*c217d954SCole Faust std::vector<float> result_scores_after_nms;
328*c217d954SCole Faust std::vector<unsigned int> sorted_indices;
329*c217d954SCole Faust
330*c217d954SCole Faust for(unsigned int c = 0; c < num_classes; ++c)
331*c217d954SCole Faust {
332*c217d954SCole Faust // For each boxes get scores of the boxes for the class c
333*c217d954SCole Faust for(unsigned int i = 0; i < _num_boxes; ++i)
334*c217d954SCole Faust {
335*c217d954SCole Faust *(reinterpret_cast<float *>(_class_scores.ptr_to_element(Coordinates(i)))) =
336*c217d954SCole Faust *(reinterpret_cast<float *>(_input_scores_to_use->ptr_to_element(Coordinates(c + 1, i)))); // i * _num_classes_with_background + c + 1
337*c217d954SCole Faust }
338*c217d954SCole Faust
339*c217d954SCole Faust // Run Non-maxima Suppression
340*c217d954SCole Faust _nms.run();
341*c217d954SCole Faust
342*c217d954SCole Faust for(unsigned int i = 0; i < _info.detection_per_class(); ++i)
343*c217d954SCole Faust {
344*c217d954SCole Faust const auto selected_index = *(reinterpret_cast<int *>(_selected_indices.ptr_to_element(Coordinates(i))));
345*c217d954SCole Faust if(selected_index == -1)
346*c217d954SCole Faust {
347*c217d954SCole Faust // Nms will return -1 for all the last M-elements not valid
348*c217d954SCole Faust break;
349*c217d954SCole Faust }
350*c217d954SCole Faust result_idx_boxes_after_nms.emplace_back(selected_index);
351*c217d954SCole Faust result_scores_after_nms.emplace_back((reinterpret_cast<float *>(_class_scores.buffer()))[selected_index]);
352*c217d954SCole Faust result_classes_after_nms.emplace_back(c);
353*c217d954SCole Faust }
354*c217d954SCole Faust }
355*c217d954SCole Faust
356*c217d954SCole Faust // We select the max detection numbers of the highest score of all classes
357*c217d954SCole Faust const auto num_selected = result_scores_after_nms.size();
358*c217d954SCole Faust const auto num_output = std::min<unsigned int>(max_detections, num_selected);
359*c217d954SCole Faust
360*c217d954SCole Faust // Sort selected indices based on result scores
361*c217d954SCole Faust sorted_indices.resize(num_selected);
362*c217d954SCole Faust std::iota(sorted_indices.begin(), sorted_indices.end(), 0);
363*c217d954SCole Faust std::partial_sort(sorted_indices.data(),
364*c217d954SCole Faust sorted_indices.data() + num_output,
365*c217d954SCole Faust sorted_indices.data() + num_selected,
366*c217d954SCole Faust [&](unsigned int first, unsigned int second)
367*c217d954SCole Faust {
368*c217d954SCole Faust
369*c217d954SCole Faust return result_scores_after_nms[first] > result_scores_after_nms[second];
370*c217d954SCole Faust });
371*c217d954SCole Faust
372*c217d954SCole Faust SaveOutputs(&_decoded_boxes, result_idx_boxes_after_nms, result_scores_after_nms, result_classes_after_nms, sorted_indices,
373*c217d954SCole Faust num_output, max_detections, _output_boxes, _output_classes, _output_scores, _num_detection);
374*c217d954SCole Faust }
375*c217d954SCole Faust // Fast NMS
376*c217d954SCole Faust else
377*c217d954SCole Faust {
378*c217d954SCole Faust const unsigned int num_classes_per_box = std::min<unsigned int>(_info.max_classes_per_detection(), _info.num_classes());
379*c217d954SCole Faust std::vector<float> max_scores;
380*c217d954SCole Faust std::vector<int> box_indices;
381*c217d954SCole Faust std::vector<int> max_score_classes;
382*c217d954SCole Faust
383*c217d954SCole Faust for(unsigned int b = 0; b < _num_boxes; ++b)
384*c217d954SCole Faust {
385*c217d954SCole Faust std::vector<float> box_scores;
386*c217d954SCole Faust for(unsigned int c = 0; c < num_classes; ++c)
387*c217d954SCole Faust {
388*c217d954SCole Faust box_scores.emplace_back(*(reinterpret_cast<float *>(_input_scores_to_use->ptr_to_element(Coordinates(c + 1, b)))));
389*c217d954SCole Faust }
390*c217d954SCole Faust
391*c217d954SCole Faust std::vector<unsigned int> max_score_indices;
392*c217d954SCole Faust max_score_indices.resize(_info.num_classes());
393*c217d954SCole Faust std::iota(max_score_indices.data(), max_score_indices.data() + _info.num_classes(), 0);
394*c217d954SCole Faust std::partial_sort(max_score_indices.data(),
395*c217d954SCole Faust max_score_indices.data() + num_classes_per_box,
396*c217d954SCole Faust max_score_indices.data() + num_classes,
397*c217d954SCole Faust [&](unsigned int first, unsigned int second)
398*c217d954SCole Faust {
399*c217d954SCole Faust return box_scores[first] > box_scores[second];
400*c217d954SCole Faust });
401*c217d954SCole Faust
402*c217d954SCole Faust for(unsigned int i = 0; i < num_classes_per_box; ++i)
403*c217d954SCole Faust {
404*c217d954SCole Faust const float score_to_add = box_scores[max_score_indices[i]];
405*c217d954SCole Faust *(reinterpret_cast<float *>(_class_scores.ptr_to_element(Coordinates(b * num_classes_per_box + i)))) = score_to_add;
406*c217d954SCole Faust max_scores.emplace_back(score_to_add);
407*c217d954SCole Faust box_indices.emplace_back(b);
408*c217d954SCole Faust max_score_classes.emplace_back(max_score_indices[i]);
409*c217d954SCole Faust }
410*c217d954SCole Faust }
411*c217d954SCole Faust
412*c217d954SCole Faust // Run Non-maxima Suppression
413*c217d954SCole Faust _nms.run();
414*c217d954SCole Faust std::vector<unsigned int> selected_indices;
415*c217d954SCole Faust for(unsigned int i = 0; i < max_detections; ++i)
416*c217d954SCole Faust {
417*c217d954SCole Faust // NMS returns M valid indices, the not valid tail is filled with -1
418*c217d954SCole Faust if(*(reinterpret_cast<int *>(_selected_indices.ptr_to_element(Coordinates(i)))) == -1)
419*c217d954SCole Faust {
420*c217d954SCole Faust // Nms will return -1 for all the last M-elements not valid
421*c217d954SCole Faust break;
422*c217d954SCole Faust }
423*c217d954SCole Faust selected_indices.emplace_back(*(reinterpret_cast<int *>(_selected_indices.ptr_to_element(Coordinates(i)))));
424*c217d954SCole Faust }
425*c217d954SCole Faust // We select the max detection numbers of the highest score of all classes
426*c217d954SCole Faust const auto num_output = std::min<unsigned int>(_info.max_detections(), selected_indices.size());
427*c217d954SCole Faust
428*c217d954SCole Faust SaveOutputs(&_decoded_boxes, box_indices, max_scores, max_score_classes, selected_indices,
429*c217d954SCole Faust num_output, max_detections, _output_boxes, _output_classes, _output_scores, _num_detection);
430*c217d954SCole Faust }
431*c217d954SCole Faust }
432*c217d954SCole Faust } // namespace arm_compute
433