blob: e536816f97f1590ada1407e3a0949ece4091d0bc [file] [log] [blame]
Manuel Bottini5209be52019-02-13 16:34:56 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Manuel Bottini5209be52019-02-13 16:34:56 +00003 *
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/CL/functions/CLGenerateProposalsLayer.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/Types.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010028#include "src/core/CL/kernels/CLBoundingBoxTransformKernel.h"
29#include "src/core/CL/kernels/CLDequantizationLayerKernel.h"
30#include "src/core/CL/kernels/CLGenerateProposalsLayerKernel.h"
31#include "src/core/CL/kernels/CLPadLayerKernel.h"
32#include "src/core/CL/kernels/CLPermuteKernel.h"
33#include "src/core/CL/kernels/CLQuantizationLayerKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000035
36namespace arm_compute
37{
38CLGenerateProposalsLayer::CLGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010039 : _memory_group(memory_manager),
Georgios Pinitas40f51a62020-11-21 03:04:18 +000040 _permute_deltas_kernel(std::make_unique<CLPermuteKernel>()),
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010041 _flatten_deltas(),
Georgios Pinitas40f51a62020-11-21 03:04:18 +000042 _permute_scores_kernel(std::make_unique<CLPermuteKernel>()),
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010043 _flatten_scores(),
Georgios Pinitas40f51a62020-11-21 03:04:18 +000044 _compute_anchors_kernel(std::make_unique<CLComputeAllAnchorsKernel>()),
45 _bounding_box_kernel(std::make_unique<CLBoundingBoxTransformKernel>()),
46 _pad_kernel(std::make_unique<CLPadLayerKernel>()),
47 _dequantize_anchors(std::make_unique<CLDequantizationLayerKernel>()),
48 _dequantize_deltas(std::make_unique<CLDequantizationLayerKernel>()),
49 _quantize_all_proposals(std::make_unique<CLQuantizationLayerKernel>()),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010050 _cpp_nms(memory_manager),
Manuel Bottini5209be52019-02-13 16:34:56 +000051 _is_nhwc(false),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010052 _is_qasymm8(false),
Manuel Bottini5209be52019-02-13 16:34:56 +000053 _deltas_permuted(),
54 _deltas_flattened(),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010055 _deltas_flattened_f32(),
Manuel Bottini5209be52019-02-13 16:34:56 +000056 _scores_permuted(),
57 _scores_flattened(),
58 _all_anchors(),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010059 _all_anchors_f32(),
Manuel Bottini5209be52019-02-13 16:34:56 +000060 _all_proposals(),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010061 _all_proposals_quantized(),
Manuel Bottini5209be52019-02-13 16:34:56 +000062 _keeps_nms_unused(),
63 _classes_nms_unused(),
64 _proposals_4_roi_values(),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010065 _all_proposals_to_use(nullptr),
Manuel Bottini5209be52019-02-13 16:34:56 +000066 _num_valid_proposals(nullptr),
67 _scores_out(nullptr)
68{
69}
70
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010071CLGenerateProposalsLayer::~CLGenerateProposalsLayer() = default;
72
Manuel Bottini5209be52019-02-13 16:34:56 +000073void CLGenerateProposalsLayer::configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals,
74 const GenerateProposalsInfo &info)
75{
Manuel Bottini2b84be52020-04-08 10:15:51 +010076 configure(CLKernelLibrary::get().get_compile_context(), scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info);
77}
78
79void CLGenerateProposalsLayer::configure(const CLCompileContext &compile_context, const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals,
80 ICLTensor *scores_out,
81 ICLTensor *num_valid_proposals, const GenerateProposalsInfo &info)
82{
Manuel Bottini5209be52019-02-13 16:34:56 +000083 ARM_COMPUTE_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
84 ARM_COMPUTE_ERROR_THROW_ON(CLGenerateProposalsLayer::validate(scores->info(), deltas->info(), anchors->info(), proposals->info(), scores_out->info(), num_valid_proposals->info(), info));
85
Michele Di Giorgio6b612f52019-09-05 12:30:22 +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;
89 const int num_anchors = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::CHANNEL));
90 const int feat_width = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::WIDTH));
91 const int feat_height = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::HEIGHT));
92 const int total_num_anchors = num_anchors * feat_width * feat_height;
93 const int pre_nms_topN = info.pre_nms_topN();
94 const int post_nms_topN = info.post_nms_topN();
95 const size_t values_per_roi = info.values_per_roi();
96
97 const QuantizationInfo scores_qinfo = scores->info()->quantization_info();
98 const DataType rois_data_type = (_is_qasymm8) ? DataType::QASYMM16 : scores_data_type;
99 const QuantizationInfo rois_qinfo = (_is_qasymm8) ? QuantizationInfo(0.125f, 0) : scores->info()->quantization_info();
Manuel Bottini5209be52019-02-13 16:34:56 +0000100
101 // Compute all the anchors
102 _memory_group.manage(&_all_anchors);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100103 _compute_anchors_kernel->configure(compile_context, anchors, &_all_anchors, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale()));
Manuel Bottini5209be52019-02-13 16:34:56 +0000104
105 const TensorShape flatten_shape_deltas(values_per_roi, total_num_anchors);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100106 _deltas_flattened.allocator()->init(TensorInfo(flatten_shape_deltas, 1, scores_data_type, deltas->info()->quantization_info()));
Manuel Bottini5209be52019-02-13 16:34:56 +0000107
108 // Permute and reshape deltas
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100109 _memory_group.manage(&_deltas_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000110 if(!_is_nhwc)
111 {
112 _memory_group.manage(&_deltas_permuted);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100113 _permute_deltas_kernel->configure(compile_context, deltas, &_deltas_permuted, PermutationVector{ 2, 0, 1 });
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100114 _flatten_deltas.configure(compile_context, &_deltas_permuted, &_deltas_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000115 _deltas_permuted.allocator()->allocate();
116 }
117 else
118 {
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100119 _flatten_deltas.configure(compile_context, deltas, &_deltas_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000120 }
121
122 const TensorShape flatten_shape_scores(1, total_num_anchors);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100123 _scores_flattened.allocator()->init(TensorInfo(flatten_shape_scores, 1, scores_data_type, scores_qinfo));
Manuel Bottini5209be52019-02-13 16:34:56 +0000124
125 // Permute and reshape scores
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100126 _memory_group.manage(&_scores_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000127 if(!_is_nhwc)
128 {
129 _memory_group.manage(&_scores_permuted);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100130 _permute_scores_kernel->configure(compile_context, scores, &_scores_permuted, PermutationVector{ 2, 0, 1 });
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100131 _flatten_scores.configure(compile_context, &_scores_permuted, &_scores_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000132 _scores_permuted.allocator()->allocate();
133 }
134 else
135 {
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100136 _flatten_scores.configure(compile_context, scores, &_scores_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000137 }
138
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100139 CLTensor *anchors_to_use = &_all_anchors;
140 CLTensor *deltas_to_use = &_deltas_flattened;
141 if(_is_qasymm8)
142 {
143 _all_anchors_f32.allocator()->init(TensorInfo(_all_anchors.info()->tensor_shape(), 1, DataType::F32));
144 _deltas_flattened_f32.allocator()->init(TensorInfo(_deltas_flattened.info()->tensor_shape(), 1, DataType::F32));
145 _memory_group.manage(&_all_anchors_f32);
146 _memory_group.manage(&_deltas_flattened_f32);
147 // Dequantize anchors to float
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100148 _dequantize_anchors->configure(compile_context, &_all_anchors, &_all_anchors_f32);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100149 _all_anchors.allocator()->allocate();
150 anchors_to_use = &_all_anchors_f32;
151 // Dequantize deltas to float
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100152 _dequantize_deltas->configure(compile_context, &_deltas_flattened, &_deltas_flattened_f32);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100153 _deltas_flattened.allocator()->allocate();
154 deltas_to_use = &_deltas_flattened_f32;
155 }
Manuel Bottini5209be52019-02-13 16:34:56 +0000156 // Bounding box transform
157 _memory_group.manage(&_all_proposals);
158 BoundingBoxTransformInfo bbox_info(info.im_width(), info.im_height(), 1.f);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100159 _bounding_box_kernel->configure(compile_context, anchors_to_use, &_all_proposals, deltas_to_use, bbox_info);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100160 deltas_to_use->allocator()->allocate();
161 anchors_to_use->allocator()->allocate();
Manuel Bottini5209be52019-02-13 16:34:56 +0000162
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100163 _all_proposals_to_use = &_all_proposals;
164 if(_is_qasymm8)
165 {
166 _memory_group.manage(&_all_proposals_quantized);
167 // Requantize all_proposals to QASYMM16 with 0.125 scale and 0 offset
168 _all_proposals_quantized.allocator()->init(TensorInfo(_all_proposals.info()->tensor_shape(), 1, DataType::QASYMM16, QuantizationInfo(0.125f, 0)));
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100169 _quantize_all_proposals->configure(compile_context, &_all_proposals, &_all_proposals_quantized);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100170 _all_proposals.allocator()->allocate();
171 _all_proposals_to_use = &_all_proposals_quantized;
172 }
Manuel Bottini5209be52019-02-13 16:34:56 +0000173 // The original layer implementation first selects the best pre_nms_topN anchors (thus having a lightweight sort)
174 // that are then transformed by bbox_transform. The boxes generated are then fed into a non-sorting NMS operation.
175 // 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)
176 // and the filtering
177 const int scores_nms_size = std::min<int>(std::min<int>(post_nms_topN, pre_nms_topN), total_num_anchors);
178 const float min_size_scaled = info.min_size() * info.im_scale();
179 _memory_group.manage(&_classes_nms_unused);
180 _memory_group.manage(&_keeps_nms_unused);
181
182 // Note that NMS needs outputs preinitialized.
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100183 auto_init_if_empty(*scores_out->info(), TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo);
184 auto_init_if_empty(*_proposals_4_roi_values.info(), TensorShape(values_per_roi, scores_nms_size), 1, rois_data_type, rois_qinfo);
Manuel Bottini5209be52019-02-13 16:34:56 +0000185 auto_init_if_empty(*num_valid_proposals->info(), TensorShape(1), 1, DataType::U32);
186
187 // Initialize temporaries (unused) outputs
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100188 _classes_nms_unused.allocator()->init(TensorInfo(TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo));
Manuel Bottini5209be52019-02-13 16:34:56 +0000189 _keeps_nms_unused.allocator()->init(*scores_out->info());
190
191 // Save the output (to map and unmap them at run)
192 _scores_out = scores_out;
193 _num_valid_proposals = num_valid_proposals;
194
195 _memory_group.manage(&_proposals_4_roi_values);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100196 _cpp_nms.configure(&_scores_flattened, _all_proposals_to_use, nullptr, scores_out, &_proposals_4_roi_values, &_classes_nms_unused, nullptr, &_keeps_nms_unused, num_valid_proposals,
197 BoxNMSLimitInfo(0.0f, info.nms_thres(), scores_nms_size, false, NMSType::LINEAR, 0.5f, 0.001f, true, min_size_scaled, info.im_width(), info.im_height()));
Manuel Bottini5209be52019-02-13 16:34:56 +0000198 _keeps_nms_unused.allocator()->allocate();
199 _classes_nms_unused.allocator()->allocate();
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100200 _all_proposals_to_use->allocator()->allocate();
Manuel Bottini5209be52019-02-13 16:34:56 +0000201 _scores_flattened.allocator()->allocate();
202
203 // Add the first column that represents the batch id. This will be all zeros, as we don't support multiple images
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100204 _pad_kernel->configure(compile_context, &_proposals_4_roi_values, proposals, PaddingList{ { 1, 0 } });
Manuel Bottini5209be52019-02-13 16:34:56 +0000205 _proposals_4_roi_values.allocator()->allocate();
Manuel Bottini5209be52019-02-13 16:34:56 +0000206}
207
208Status CLGenerateProposalsLayer::validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out,
209 const ITensorInfo *num_valid_proposals, const GenerateProposalsInfo &info)
210{
211 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100212 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(scores, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Manuel Bottini5209be52019-02-13 16:34:56 +0000213 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(scores, DataLayout::NCHW, DataLayout::NHWC);
214 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(scores, deltas);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100215 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores, deltas);
Manuel Bottini5209be52019-02-13 16:34:56 +0000216
217 const int num_anchors = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::CHANNEL));
218 const int feat_width = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::WIDTH));
219 const int feat_height = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::HEIGHT));
220 const int num_images = scores->dimension(3);
221 const int total_num_anchors = num_anchors * feat_width * feat_height;
222 const int values_per_roi = info.values_per_roi();
223
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100224 const bool is_qasymm8 = scores->data_type() == DataType::QASYMM8;
225
Manuel Bottini5209be52019-02-13 16:34:56 +0000226 ARM_COMPUTE_RETURN_ERROR_ON(num_images > 1);
227
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100228 if(is_qasymm8)
229 {
230 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(anchors, 1, DataType::QSYMM16);
231 const UniformQuantizationInfo anchors_qinfo = anchors->quantization_info().uniform();
232 ARM_COMPUTE_RETURN_ERROR_ON(anchors_qinfo.scale != 0.125f);
233 }
234
Manuel Bottini5209be52019-02-13 16:34:56 +0000235 TensorInfo all_anchors_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
236 ARM_COMPUTE_RETURN_ON_ERROR(CLComputeAllAnchorsKernel::validate(anchors, &all_anchors_info, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale())));
237
238 TensorInfo deltas_permuted_info = deltas->clone()->set_tensor_shape(TensorShape(values_per_roi * num_anchors, feat_width, feat_height)).set_is_resizable(true);
239 TensorInfo scores_permuted_info = scores->clone()->set_tensor_shape(TensorShape(num_anchors, feat_width, feat_height)).set_is_resizable(true);
240 if(scores->data_layout() == DataLayout::NHWC)
241 {
242 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(deltas, &deltas_permuted_info);
243 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(scores, &scores_permuted_info);
244 }
245 else
246 {
247 ARM_COMPUTE_RETURN_ON_ERROR(CLPermuteKernel::validate(deltas, &deltas_permuted_info, PermutationVector{ 2, 0, 1 }));
248 ARM_COMPUTE_RETURN_ON_ERROR(CLPermuteKernel::validate(scores, &scores_permuted_info, PermutationVector{ 2, 0, 1 }));
249 }
250
251 TensorInfo deltas_flattened_info(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100252 ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(&deltas_permuted_info, &deltas_flattened_info));
Manuel Bottini5209be52019-02-13 16:34:56 +0000253
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100254 TensorInfo scores_flattened_info(scores->clone()->set_tensor_shape(TensorShape(1, total_num_anchors)).set_is_resizable(true));
Manuel Bottini5209be52019-02-13 16:34:56 +0000255 TensorInfo proposals_4_roi_values(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
256
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100257 ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(&scores_permuted_info, &scores_flattened_info));
Manuel Bottini5209be52019-02-13 16:34:56 +0000258
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100259 TensorInfo *proposals_4_roi_values_to_use = &proposals_4_roi_values;
260 TensorInfo proposals_4_roi_values_quantized(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
261 proposals_4_roi_values_quantized.set_data_type(DataType::QASYMM16).set_quantization_info(QuantizationInfo(0.125f, 0));
262 if(is_qasymm8)
263 {
264 TensorInfo all_anchors_f32_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true).set_data_type(DataType::F32));
265 ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayerKernel::validate(&all_anchors_info, &all_anchors_f32_info));
266
267 TensorInfo deltas_flattened_f32_info(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true).set_data_type(DataType::F32));
268 ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayerKernel::validate(&deltas_flattened_info, &deltas_flattened_f32_info));
269
270 TensorInfo proposals_4_roi_values_f32(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true).set_data_type(DataType::F32));
271 ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_f32_info, &proposals_4_roi_values_f32, &deltas_flattened_f32_info,
272 BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f)));
273
274 ARM_COMPUTE_RETURN_ON_ERROR(CLQuantizationLayerKernel::validate(&proposals_4_roi_values_f32, &proposals_4_roi_values_quantized));
275 proposals_4_roi_values_to_use = &proposals_4_roi_values_quantized;
276 }
277 else
278 {
279 ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_info, &proposals_4_roi_values, &deltas_flattened_info,
280 BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f)));
281 }
282
283 ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(proposals_4_roi_values_to_use, proposals, PaddingList{ { 1, 0 } }));
Manuel Bottini5209be52019-02-13 16:34:56 +0000284
285 if(num_valid_proposals->total_size() > 0)
286 {
287 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->num_dimensions() > 1);
288 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->dimension(0) > 1);
289 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(num_valid_proposals, 1, DataType::U32);
290 }
291
292 if(proposals->total_size() > 0)
293 {
294 ARM_COMPUTE_RETURN_ERROR_ON(proposals->num_dimensions() > 2);
295 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(0) != size_t(values_per_roi) + 1);
296 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(1) != size_t(total_num_anchors));
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100297 if(is_qasymm8)
298 {
299 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(proposals, 1, DataType::QASYMM16);
300 const UniformQuantizationInfo proposals_qinfo = proposals->quantization_info().uniform();
301 ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.scale != 0.125f);
302 ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.offset != 0);
303 }
304 else
305 {
306 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(proposals, scores);
307 }
Manuel Bottini5209be52019-02-13 16:34:56 +0000308 }
309
310 if(scores_out->total_size() > 0)
311 {
312 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->num_dimensions() > 1);
313 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->dimension(0) != size_t(total_num_anchors));
314 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores_out, scores);
315 }
316
317 return Status{};
318}
319
320void CLGenerateProposalsLayer::run_cpp_nms_kernel()
321{
322 // Map inputs
323 _scores_flattened.map(true);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100324 _all_proposals_to_use->map(true);
Manuel Bottini5209be52019-02-13 16:34:56 +0000325
326 // Map outputs
327 _scores_out->map(CLScheduler::get().queue(), true);
328 _proposals_4_roi_values.map(CLScheduler::get().queue(), true);
329 _num_valid_proposals->map(CLScheduler::get().queue(), true);
330 _keeps_nms_unused.map(true);
331 _classes_nms_unused.map(true);
332
333 // Run nms
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100334 _cpp_nms.run();
Manuel Bottini5209be52019-02-13 16:34:56 +0000335
336 // Unmap outputs
337 _keeps_nms_unused.unmap();
338 _classes_nms_unused.unmap();
339 _scores_out->unmap(CLScheduler::get().queue());
340 _proposals_4_roi_values.unmap(CLScheduler::get().queue());
341 _num_valid_proposals->unmap(CLScheduler::get().queue());
342
343 // Unmap inputs
344 _scores_flattened.unmap();
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100345 _all_proposals_to_use->unmap();
Manuel Bottini5209be52019-02-13 16:34:56 +0000346}
347
348void CLGenerateProposalsLayer::run()
349{
350 // Acquire all the temporaries
Georgios Pinitasda953f22019-04-02 17:27:03 +0100351 MemoryGroupResourceScope scope_mg(_memory_group);
Manuel Bottini5209be52019-02-13 16:34:56 +0000352
353 // Compute all the anchors
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100354 CLScheduler::get().enqueue(*_compute_anchors_kernel, false);
Manuel Bottini5209be52019-02-13 16:34:56 +0000355
356 // Transpose and reshape the inputs
357 if(!_is_nhwc)
358 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100359 CLScheduler::get().enqueue(*_permute_deltas_kernel, false);
360 CLScheduler::get().enqueue(*_permute_scores_kernel, false);
Manuel Bottini5209be52019-02-13 16:34:56 +0000361 }
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100362 _flatten_deltas.run();
363 _flatten_scores.run();
Manuel Bottini5209be52019-02-13 16:34:56 +0000364
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100365 if(_is_qasymm8)
366 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100367 CLScheduler::get().enqueue(*_dequantize_anchors, false);
368 CLScheduler::get().enqueue(*_dequantize_deltas, false);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100369 }
370
Manuel Bottini5209be52019-02-13 16:34:56 +0000371 // Build the boxes
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100372 CLScheduler::get().enqueue(*_bounding_box_kernel, false);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100373
374 if(_is_qasymm8)
375 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100376 CLScheduler::get().enqueue(*_quantize_all_proposals, false);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100377 }
378
Manuel Bottini5209be52019-02-13 16:34:56 +0000379 // Non maxima suppression
380 run_cpp_nms_kernel();
381 // Add dummy batch indexes
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100382 CLScheduler::get().enqueue(*_pad_kernel, true);
Manuel Bottini5209be52019-02-13 16:34:56 +0000383}
384} // namespace arm_compute