blob: 1022b4153eb4fbf4a78c7c56c4c195890c687eb7 [file] [log] [blame]
Pablo Telloc9564cb2019-09-13 10:20:25 +01001/*
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +00002 * Copyright (c) 2019-2021 Arm Limited.
Pablo Telloc9564cb2019-09-13 10:20:25 +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/NEGenerateProposalsLayer.h"
25
26#include "arm_compute/core/Types.h"
27#include "arm_compute/runtime/NEON/NEScheduler.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
ramelg01cbbb0382021-09-17 17:36:57 +010029#include "src/common/utils/Log.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "src/core/helpers/AutoConfiguration.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010031#include "src/core/NEON/kernels/NEFillBorderKernel.h"
Georgios Pinitas8c3c0e72020-12-03 20:11:53 +000032#include "src/core/NEON/kernels/NEGenerateProposalsLayerKernel.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010033#include "src/core/NEON/kernels/NEPadLayerKernel.h"
Pablo Telloc9564cb2019-09-13 10:20:25 +010034
35namespace arm_compute
36{
37NEGenerateProposalsLayer::NEGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010038 : _memory_group(memory_manager),
Michalis Spyrouebcebf12020-10-21 00:04:14 +010039 _permute_deltas(),
Michalis Spyroubcd23522020-05-21 15:02:36 +010040 _flatten_deltas(),
Michalis Spyrouebcebf12020-10-21 00:04:14 +010041 _permute_scores(),
Michalis Spyroubcd23522020-05-21 15:02:36 +010042 _flatten_scores(),
Georgios Pinitas8c3c0e72020-12-03 20:11:53 +000043 _compute_anchors(nullptr),
Michalis Spyrouebcebf12020-10-21 00:04:14 +010044 _bounding_box(),
45 _pad(),
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010046 _dequantize_anchors(),
47 _dequantize_deltas(),
48 _quantize_all_proposals(),
49 _cpp_nms(memory_manager),
Pablo Telloc9564cb2019-09-13 10:20:25 +010050 _is_nhwc(false),
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010051 _is_qasymm8(false),
Pablo Telloc9564cb2019-09-13 10:20:25 +010052 _deltas_permuted(),
53 _deltas_flattened(),
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010054 _deltas_flattened_f32(),
Pablo Telloc9564cb2019-09-13 10:20:25 +010055 _scores_permuted(),
56 _scores_flattened(),
57 _all_anchors(),
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010058 _all_anchors_f32(),
Pablo Telloc9564cb2019-09-13 10:20:25 +010059 _all_proposals(),
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010060 _all_proposals_quantized(),
Pablo Telloc9564cb2019-09-13 10:20:25 +010061 _keeps_nms_unused(),
62 _classes_nms_unused(),
63 _proposals_4_roi_values(),
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010064 _all_proposals_to_use(nullptr),
Pablo Telloc9564cb2019-09-13 10:20:25 +010065 _num_valid_proposals(nullptr),
66 _scores_out(nullptr)
67{
68}
69
Michalis Spyrouebcebf12020-10-21 00:04:14 +010070NEGenerateProposalsLayer::~NEGenerateProposalsLayer() = default;
71
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072void NEGenerateProposalsLayer::configure(const ITensor *scores,
73 const ITensor *deltas,
74 const ITensor *anchors,
75 ITensor *proposals,
76 ITensor *scores_out,
77 ITensor *num_valid_proposals,
Pablo Telloc9564cb2019-09-13 10:20:25 +010078 const GenerateProposalsInfo &info)
79{
80 ARM_COMPUTE_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 ARM_COMPUTE_ERROR_THROW_ON(NEGenerateProposalsLayer::validate(scores->info(), deltas->info(), anchors->info(),
82 proposals->info(), scores_out->info(),
83 num_valid_proposals->info(), info));
ramelg01cbbb0382021-09-17 17:36:57 +010084 ARM_COMPUTE_LOG_PARAMS(scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info);
Pablo Telloc9564cb2019-09-13 10:20:25 +010085
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010086 _is_nhwc = scores->info()->data_layout() == DataLayout::NHWC;
87 const DataType scores_data_type = scores->info()->data_type();
88 _is_qasymm8 = scores_data_type == DataType::QASYMM8;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 const int num_anchors = scores->info()->dimension(
90 get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::CHANNEL));
91 const int feat_width = scores->info()->dimension(
92 get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::WIDTH));
93 const int feat_height = scores->info()->dimension(
94 get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::HEIGHT));
95 const int total_num_anchors = num_anchors * feat_width * feat_height;
96 const int pre_nms_topN = info.pre_nms_topN();
97 const int post_nms_topN = info.post_nms_topN();
98 const size_t values_per_roi = info.values_per_roi();
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +010099
100 const QuantizationInfo scores_qinfo = scores->info()->quantization_info();
101 const DataType rois_data_type = (_is_qasymm8) ? DataType::QASYMM16 : scores_data_type;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 const QuantizationInfo rois_qinfo =
103 (_is_qasymm8) ? QuantizationInfo(0.125f, 0) : scores->info()->quantization_info();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100104
105 // Compute all the anchors
106 _memory_group.manage(&_all_anchors);
Georgios Pinitas8c3c0e72020-12-03 20:11:53 +0000107 _compute_anchors = std::make_unique<NEComputeAllAnchorsKernel>();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 _compute_anchors->configure(anchors, &_all_anchors,
109 ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale()));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100110
111 const TensorShape flatten_shape_deltas(values_per_roi, total_num_anchors);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 _deltas_flattened.allocator()->init(
113 TensorInfo(flatten_shape_deltas, 1, scores_data_type, deltas->info()->quantization_info()));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100114
115 // Permute and reshape deltas
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100116 _memory_group.manage(&_deltas_flattened);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 if (!_is_nhwc)
Pablo Telloc9564cb2019-09-13 10:20:25 +0100118 {
119 _memory_group.manage(&_deltas_permuted);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 _permute_deltas.configure(deltas, &_deltas_permuted, PermutationVector{2, 0, 1});
Michalis Spyroubcd23522020-05-21 15:02:36 +0100121 _flatten_deltas.configure(&_deltas_permuted, &_deltas_flattened);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100122 _deltas_permuted.allocator()->allocate();
123 }
124 else
125 {
Michalis Spyroubcd23522020-05-21 15:02:36 +0100126 _flatten_deltas.configure(deltas, &_deltas_flattened);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100127 }
128
129 const TensorShape flatten_shape_scores(1, total_num_anchors);
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100130 _scores_flattened.allocator()->init(TensorInfo(flatten_shape_scores, 1, scores_data_type, scores_qinfo));
131
Pablo Telloc9564cb2019-09-13 10:20:25 +0100132 // Permute and reshape scores
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100133 _memory_group.manage(&_scores_flattened);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 if (!_is_nhwc)
Pablo Telloc9564cb2019-09-13 10:20:25 +0100135 {
136 _memory_group.manage(&_scores_permuted);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 _permute_scores.configure(scores, &_scores_permuted, PermutationVector{2, 0, 1});
Michalis Spyroubcd23522020-05-21 15:02:36 +0100138 _flatten_scores.configure(&_scores_permuted, &_scores_flattened);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100139 _scores_permuted.allocator()->allocate();
140 }
141 else
142 {
Michalis Spyroubcd23522020-05-21 15:02:36 +0100143 _flatten_scores.configure(scores, &_scores_flattened);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100144 }
145
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100146 Tensor *anchors_to_use = &_all_anchors;
147 Tensor *deltas_to_use = &_deltas_flattened;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 if (_is_qasymm8)
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100149 {
150 _all_anchors_f32.allocator()->init(TensorInfo(_all_anchors.info()->tensor_shape(), 1, DataType::F32));
151 _deltas_flattened_f32.allocator()->init(TensorInfo(_deltas_flattened.info()->tensor_shape(), 1, DataType::F32));
152 _memory_group.manage(&_all_anchors_f32);
153 _memory_group.manage(&_deltas_flattened_f32);
154 // Dequantize anchors to float
155 _dequantize_anchors.configure(&_all_anchors, &_all_anchors_f32);
156 _all_anchors.allocator()->allocate();
157 anchors_to_use = &_all_anchors_f32;
158 // Dequantize deltas to float
159 _dequantize_deltas.configure(&_deltas_flattened, &_deltas_flattened_f32);
160 _deltas_flattened.allocator()->allocate();
161 deltas_to_use = &_deltas_flattened_f32;
162 }
Pablo Telloc9564cb2019-09-13 10:20:25 +0100163 // Bounding box transform
164 _memory_group.manage(&_all_proposals);
165 BoundingBoxTransformInfo bbox_info(info.im_width(), info.im_height(), 1.f);
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100166 _bounding_box.configure(anchors_to_use, &_all_proposals, deltas_to_use, bbox_info);
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100167 deltas_to_use->allocator()->allocate();
168 anchors_to_use->allocator()->allocate();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100169
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100170 _all_proposals_to_use = &_all_proposals;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100171 if (_is_qasymm8)
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100172 {
173 _memory_group.manage(&_all_proposals_quantized);
174 // Requantize all_proposals to QASYMM16 with 0.125 scale and 0 offset
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 _all_proposals_quantized.allocator()->init(
176 TensorInfo(_all_proposals.info()->tensor_shape(), 1, DataType::QASYMM16, QuantizationInfo(0.125f, 0)));
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100177 _quantize_all_proposals.configure(&_all_proposals, &_all_proposals_quantized);
178 _all_proposals.allocator()->allocate();
179 _all_proposals_to_use = &_all_proposals_quantized;
180 }
Pablo Telloc9564cb2019-09-13 10:20:25 +0100181 // The original layer implementation first selects the best pre_nms_topN anchors (thus having a lightweight sort)
182 // that are then transformed by bbox_transform. The boxes generated are then fed into a non-sorting NMS operation.
183 // Since we are reusing the NMS layer and we don't implement any CL/sort, we let NMS do the sorting (of all the input)
184 // and the filtering
185 const int scores_nms_size = std::min<int>(std::min<int>(post_nms_topN, pre_nms_topN), total_num_anchors);
186 const float min_size_scaled = info.min_size() * info.im_scale();
187 _memory_group.manage(&_classes_nms_unused);
188 _memory_group.manage(&_keeps_nms_unused);
189
190 // Note that NMS needs outputs preinitialized.
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100191 auto_init_if_empty(*scores_out->info(), TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 auto_init_if_empty(*_proposals_4_roi_values.info(), TensorShape(values_per_roi, scores_nms_size), 1, rois_data_type,
193 rois_qinfo);
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100194 auto_init_if_empty(*num_valid_proposals->info(), TensorShape(1), 1, DataType::U32);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100195
196 // Initialize temporaries (unused) outputs
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100197 _classes_nms_unused.allocator()->init(TensorInfo(TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100198 _keeps_nms_unused.allocator()->init(*scores_out->info());
199
200 // Save the output (to map and unmap them at run)
201 _scores_out = scores_out;
202 _num_valid_proposals = num_valid_proposals;
203
204 _memory_group.manage(&_proposals_4_roi_values);
205
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 const BoxNMSLimitInfo box_nms_info(0.0f, info.nms_thres(), scores_nms_size, false, NMSType::LINEAR, 0.5f, 0.001f,
207 true, min_size_scaled, info.im_width(), info.im_height());
208 _cpp_nms.configure(&_scores_flattened /*scores_in*/, _all_proposals_to_use /*boxes_in,*/,
209 nullptr /* batch_splits_in*/, scores_out /* scores_out*/, &_proposals_4_roi_values /*boxes_out*/,
210 &_classes_nms_unused /*classes*/, nullptr /*batch_splits_out*/, &_keeps_nms_unused /*keeps*/,
211 num_valid_proposals /* keeps_size*/, box_nms_info);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100212
213 _keeps_nms_unused.allocator()->allocate();
214 _classes_nms_unused.allocator()->allocate();
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100215 _all_proposals_to_use->allocator()->allocate();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100216 _scores_flattened.allocator()->allocate();
217
218 // Add the first column that represents the batch id. This will be all zeros, as we don't support multiple images
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100219 _pad.configure(&_proposals_4_roi_values, proposals, PaddingList{{1, 0}});
Pablo Telloc9564cb2019-09-13 10:20:25 +0100220 _proposals_4_roi_values.allocator()->allocate();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100221}
222
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223Status NEGenerateProposalsLayer::validate(const ITensorInfo *scores,
224 const ITensorInfo *deltas,
225 const ITensorInfo *anchors,
226 const ITensorInfo *proposals,
227 const ITensorInfo *scores_out,
228 const ITensorInfo *num_valid_proposals,
229 const GenerateProposalsInfo &info)
Pablo Telloc9564cb2019-09-13 10:20:25 +0100230{
231 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100232 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(scores, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100233 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(scores, DataLayout::NCHW, DataLayout::NHWC);
234 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(scores, deltas);
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100235 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores, deltas);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100236
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100237 const int num_anchors =
238 scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::CHANNEL));
239 const int feat_width =
240 scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::WIDTH));
241 const int feat_height =
242 scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::HEIGHT));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100243 const int num_images = scores->dimension(3);
244 const int total_num_anchors = num_anchors * feat_width * feat_height;
245 const int values_per_roi = info.values_per_roi();
246
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100247 const bool is_qasymm8 = scores->data_type() == DataType::QASYMM8;
248
Pablo Telloc9564cb2019-09-13 10:20:25 +0100249 ARM_COMPUTE_RETURN_ERROR_ON(num_images > 1);
250
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100251 if (is_qasymm8)
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100252 {
253 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(anchors, 1, DataType::QSYMM16);
254 const UniformQuantizationInfo anchors_qinfo = anchors->quantization_info().uniform();
255 ARM_COMPUTE_RETURN_ERROR_ON(anchors_qinfo.scale != 0.125f);
256 }
257
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100258 TensorInfo all_anchors_info(
259 anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
260 ARM_COMPUTE_RETURN_ON_ERROR(NEComputeAllAnchorsKernel::validate(
261 anchors, &all_anchors_info, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale())));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100262
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100263 TensorInfo deltas_permuted_info =
264 deltas->clone()
265 ->set_tensor_shape(TensorShape(values_per_roi * num_anchors, feat_width, feat_height))
266 .set_is_resizable(true);
267 TensorInfo scores_permuted_info =
268 scores->clone()->set_tensor_shape(TensorShape(num_anchors, feat_width, feat_height)).set_is_resizable(true);
269 if (scores->data_layout() == DataLayout::NHWC)
Pablo Telloc9564cb2019-09-13 10:20:25 +0100270 {
271 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(deltas, &deltas_permuted_info);
272 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(scores, &scores_permuted_info);
273 }
274 else
275 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100276 ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(deltas, &deltas_permuted_info, PermutationVector{2, 0, 1}));
277 ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(scores, &scores_permuted_info, PermutationVector{2, 0, 1}));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100278 }
279
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100280 TensorInfo deltas_flattened_info(
281 deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
Michalis Spyroubcd23522020-05-21 15:02:36 +0100282 ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayer::validate(&deltas_permuted_info, &deltas_flattened_info));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100283
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100284 TensorInfo scores_flattened_info(
285 scores->clone()->set_tensor_shape(TensorShape(1, total_num_anchors)).set_is_resizable(true));
286 TensorInfo proposals_4_roi_values(
287 deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100288
Michalis Spyroubcd23522020-05-21 15:02:36 +0100289 ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayer::validate(&scores_permuted_info, &scores_flattened_info));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100290
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100291 TensorInfo *proposals_4_roi_values_to_use = &proposals_4_roi_values;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292 TensorInfo proposals_4_roi_values_quantized(
293 deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
294 proposals_4_roi_values_quantized.set_data_type(DataType::QASYMM16)
295 .set_quantization_info(QuantizationInfo(0.125f, 0));
296 if (is_qasymm8)
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100297 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100298 TensorInfo all_anchors_f32_info(anchors->clone()
299 ->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors))
300 .set_is_resizable(true)
301 .set_data_type(DataType::F32));
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100302 ARM_COMPUTE_RETURN_ON_ERROR(NEDequantizationLayer::validate(&all_anchors_info, &all_anchors_f32_info));
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100303
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100304 TensorInfo deltas_flattened_f32_info(deltas->clone()
305 ->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors))
306 .set_is_resizable(true)
307 .set_data_type(DataType::F32));
308 ARM_COMPUTE_RETURN_ON_ERROR(
309 NEDequantizationLayer::validate(&deltas_flattened_info, &deltas_flattened_f32_info));
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100310
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100311 TensorInfo proposals_4_roi_values_f32(deltas->clone()
312 ->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors))
313 .set_is_resizable(true)
314 .set_data_type(DataType::F32));
315 ARM_COMPUTE_RETURN_ON_ERROR(NEBoundingBoxTransform::validate(
316 &all_anchors_f32_info, &proposals_4_roi_values_f32, &deltas_flattened_f32_info,
317 BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f)));
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100318
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100319 ARM_COMPUTE_RETURN_ON_ERROR(
320 NEQuantizationLayer::validate(&proposals_4_roi_values_f32, &proposals_4_roi_values_quantized));
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100321 proposals_4_roi_values_to_use = &proposals_4_roi_values_quantized;
322 }
323 else
324 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100325 ARM_COMPUTE_RETURN_ON_ERROR(
326 NEBoundingBoxTransform::validate(&all_anchors_info, &proposals_4_roi_values, &deltas_flattened_info,
327 BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f)));
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100328 }
329
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100330 ARM_COMPUTE_RETURN_ON_ERROR(NEPadLayer::validate(proposals_4_roi_values_to_use, proposals, PaddingList{{1, 0}}));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100331
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100332 if (num_valid_proposals->total_size() > 0)
Pablo Telloc9564cb2019-09-13 10:20:25 +0100333 {
334 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->num_dimensions() > 1);
335 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->dimension(0) > 1);
336 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(num_valid_proposals, 1, DataType::U32);
337 }
338
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100339 if (proposals->total_size() > 0)
Pablo Telloc9564cb2019-09-13 10:20:25 +0100340 {
341 ARM_COMPUTE_RETURN_ERROR_ON(proposals->num_dimensions() > 2);
342 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(0) != size_t(values_per_roi) + 1);
343 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(1) != size_t(total_num_anchors));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100344 if (is_qasymm8)
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100345 {
346 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(proposals, 1, DataType::QASYMM16);
347 const UniformQuantizationInfo proposals_qinfo = proposals->quantization_info().uniform();
348 ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.scale != 0.125f);
349 ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.offset != 0);
350 }
351 else
352 {
353 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(proposals, scores);
354 }
Pablo Telloc9564cb2019-09-13 10:20:25 +0100355 }
356
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100357 if (scores_out->total_size() > 0)
Pablo Telloc9564cb2019-09-13 10:20:25 +0100358 {
359 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->num_dimensions() > 1);
360 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->dimension(0) != size_t(total_num_anchors));
361 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores_out, scores);
362 }
363
364 return Status{};
365}
366
367void NEGenerateProposalsLayer::run()
368{
369 // Acquire all the temporaries
370 MemoryGroupResourceScope scope_mg(_memory_group);
371
372 // Compute all the anchors
Georgios Pinitas8c3c0e72020-12-03 20:11:53 +0000373 NEScheduler::get().schedule(_compute_anchors.get(), Window::DimY);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100374
375 // Transpose and reshape the inputs
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100376 if (!_is_nhwc)
Pablo Telloc9564cb2019-09-13 10:20:25 +0100377 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100378 _permute_deltas.run();
379 _permute_scores.run();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100380 }
381
Michalis Spyroubcd23522020-05-21 15:02:36 +0100382 _flatten_deltas.run();
383 _flatten_scores.run();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100384
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100385 if (_is_qasymm8)
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100386 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100387 _dequantize_anchors.run();
388 _dequantize_deltas.run();
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100389 }
390
Pablo Telloc9564cb2019-09-13 10:20:25 +0100391 // Build the boxes
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100392 _bounding_box.run();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100393
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100394 if (_is_qasymm8)
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100395 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100396 _quantize_all_proposals.run();
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100397 }
398
Pablo Telloc9564cb2019-09-13 10:20:25 +0100399 // Non maxima suppression
Michele Di Giorgio58c71ef2019-09-30 15:03:21 +0100400 _cpp_nms.run();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100401
402 // Add dummy batch indexes
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100403 _pad.run();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100404}
405} // namespace arm_compute