blob: 9cb7d618cfc276ee0a3734116f78fcdb619a5322 [file] [log] [blame]
Manuel Bottini5209be52019-02-13 16:34:56 +00001/*
Sheri Zhang7e20e292021-02-02 11:49:34 +00002 * Copyright (c) 2019-2021 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"
Manuel Bottini9e73c932021-03-02 17:40:42 +000028#include "arm_compute/runtime/CL/functions/CLDequantizationLayer.h"
Manuel Bottini5a1bf622021-03-01 17:39:36 +000029#include "arm_compute/runtime/CL/functions/CLQuantizationLayer.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010030#include "src/core/CL/kernels/CLBoundingBoxTransformKernel.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010031#include "src/core/CL/kernels/CLGenerateProposalsLayerKernel.h"
32#include "src/core/CL/kernels/CLPadLayerKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000034
ramelg016d891572021-09-29 10:05:09 +010035#include "src/common/utils/Log.h"
36
Manuel Bottini5209be52019-02-13 16:34:56 +000037namespace arm_compute
38{
39CLGenerateProposalsLayer::CLGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010040 : _memory_group(memory_manager),
Sheri Zhang7e20e292021-02-02 11:49:34 +000041 _permute_deltas(),
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010042 _flatten_deltas(),
Sheri Zhang7e20e292021-02-02 11:49:34 +000043 _permute_scores(),
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010044 _flatten_scores(),
Georgios Pinitas40f51a62020-11-21 03:04:18 +000045 _compute_anchors_kernel(std::make_unique<CLComputeAllAnchorsKernel>()),
46 _bounding_box_kernel(std::make_unique<CLBoundingBoxTransformKernel>()),
47 _pad_kernel(std::make_unique<CLPadLayerKernel>()),
Manuel Bottini9e73c932021-03-02 17:40:42 +000048 _dequantize_anchors(std::make_unique<CLDequantizationLayer>()),
49 _dequantize_deltas(std::make_unique<CLDequantizationLayer>()),
Manuel Bottini5a1bf622021-03-01 17:39:36 +000050 _quantize_all_proposals(std::make_unique<CLQuantizationLayer>()),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010051 _cpp_nms(memory_manager),
Manuel Bottini5209be52019-02-13 16:34:56 +000052 _is_nhwc(false),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010053 _is_qasymm8(false),
Manuel Bottini5209be52019-02-13 16:34:56 +000054 _deltas_permuted(),
55 _deltas_flattened(),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010056 _deltas_flattened_f32(),
Manuel Bottini5209be52019-02-13 16:34:56 +000057 _scores_permuted(),
58 _scores_flattened(),
59 _all_anchors(),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010060 _all_anchors_f32(),
Manuel Bottini5209be52019-02-13 16:34:56 +000061 _all_proposals(),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010062 _all_proposals_quantized(),
Manuel Bottini5209be52019-02-13 16:34:56 +000063 _keeps_nms_unused(),
64 _classes_nms_unused(),
65 _proposals_4_roi_values(),
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010066 _all_proposals_to_use(nullptr),
Manuel Bottini5209be52019-02-13 16:34:56 +000067 _num_valid_proposals(nullptr),
68 _scores_out(nullptr)
69{
70}
71
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010072CLGenerateProposalsLayer::~CLGenerateProposalsLayer() = default;
73
Manuel Bottini5209be52019-02-13 16:34:56 +000074void CLGenerateProposalsLayer::configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals,
75 const GenerateProposalsInfo &info)
76{
Manuel Bottini2b84be52020-04-08 10:15:51 +010077 configure(CLKernelLibrary::get().get_compile_context(), scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info);
78}
79
80void CLGenerateProposalsLayer::configure(const CLCompileContext &compile_context, const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals,
81 ICLTensor *scores_out,
82 ICLTensor *num_valid_proposals, const GenerateProposalsInfo &info)
83{
Manuel Bottini5209be52019-02-13 16:34:56 +000084 ARM_COMPUTE_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
85 ARM_COMPUTE_ERROR_THROW_ON(CLGenerateProposalsLayer::validate(scores->info(), deltas->info(), anchors->info(), proposals->info(), scores_out->info(), num_valid_proposals->info(), info));
ramelg016d891572021-09-29 10:05:09 +010086 ARM_COMPUTE_LOG_PARAMS(scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info);
Manuel Bottini5209be52019-02-13 16:34:56 +000087
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010088 _is_nhwc = scores->info()->data_layout() == DataLayout::NHWC;
89 const DataType scores_data_type = scores->info()->data_type();
90 _is_qasymm8 = scores_data_type == DataType::QASYMM8;
91 const int num_anchors = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::CHANNEL));
92 const int feat_width = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::WIDTH));
93 const int feat_height = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::HEIGHT));
94 const int total_num_anchors = num_anchors * feat_width * feat_height;
95 const int pre_nms_topN = info.pre_nms_topN();
96 const int post_nms_topN = info.post_nms_topN();
97 const size_t values_per_roi = info.values_per_roi();
98
99 const QuantizationInfo scores_qinfo = scores->info()->quantization_info();
100 const DataType rois_data_type = (_is_qasymm8) ? DataType::QASYMM16 : scores_data_type;
101 const QuantizationInfo rois_qinfo = (_is_qasymm8) ? QuantizationInfo(0.125f, 0) : scores->info()->quantization_info();
Manuel Bottini5209be52019-02-13 16:34:56 +0000102
103 // Compute all the anchors
104 _memory_group.manage(&_all_anchors);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100105 _compute_anchors_kernel->configure(compile_context, anchors, &_all_anchors, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale()));
Manuel Bottini5209be52019-02-13 16:34:56 +0000106
107 const TensorShape flatten_shape_deltas(values_per_roi, total_num_anchors);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100108 _deltas_flattened.allocator()->init(TensorInfo(flatten_shape_deltas, 1, scores_data_type, deltas->info()->quantization_info()));
Manuel Bottini5209be52019-02-13 16:34:56 +0000109
110 // Permute and reshape deltas
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100111 _memory_group.manage(&_deltas_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000112 if(!_is_nhwc)
113 {
114 _memory_group.manage(&_deltas_permuted);
Sheri Zhang7e20e292021-02-02 11:49:34 +0000115 _permute_deltas.configure(compile_context, deltas, &_deltas_permuted, PermutationVector{ 2, 0, 1 });
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100116 _flatten_deltas.configure(compile_context, &_deltas_permuted, &_deltas_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000117 _deltas_permuted.allocator()->allocate();
118 }
119 else
120 {
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100121 _flatten_deltas.configure(compile_context, deltas, &_deltas_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000122 }
123
124 const TensorShape flatten_shape_scores(1, total_num_anchors);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100125 _scores_flattened.allocator()->init(TensorInfo(flatten_shape_scores, 1, scores_data_type, scores_qinfo));
Manuel Bottini5209be52019-02-13 16:34:56 +0000126
127 // Permute and reshape scores
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100128 _memory_group.manage(&_scores_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000129 if(!_is_nhwc)
130 {
131 _memory_group.manage(&_scores_permuted);
Sheri Zhang7e20e292021-02-02 11:49:34 +0000132 _permute_scores.configure(compile_context, scores, &_scores_permuted, PermutationVector{ 2, 0, 1 });
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100133 _flatten_scores.configure(compile_context, &_scores_permuted, &_scores_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000134 _scores_permuted.allocator()->allocate();
135 }
136 else
137 {
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100138 _flatten_scores.configure(compile_context, scores, &_scores_flattened);
Manuel Bottini5209be52019-02-13 16:34:56 +0000139 }
140
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100141 CLTensor *anchors_to_use = &_all_anchors;
142 CLTensor *deltas_to_use = &_deltas_flattened;
143 if(_is_qasymm8)
144 {
145 _all_anchors_f32.allocator()->init(TensorInfo(_all_anchors.info()->tensor_shape(), 1, DataType::F32));
146 _deltas_flattened_f32.allocator()->init(TensorInfo(_deltas_flattened.info()->tensor_shape(), 1, DataType::F32));
147 _memory_group.manage(&_all_anchors_f32);
148 _memory_group.manage(&_deltas_flattened_f32);
149 // Dequantize anchors to float
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100150 _dequantize_anchors->configure(compile_context, &_all_anchors, &_all_anchors_f32);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100151 _all_anchors.allocator()->allocate();
152 anchors_to_use = &_all_anchors_f32;
153 // Dequantize deltas to float
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100154 _dequantize_deltas->configure(compile_context, &_deltas_flattened, &_deltas_flattened_f32);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100155 _deltas_flattened.allocator()->allocate();
156 deltas_to_use = &_deltas_flattened_f32;
157 }
Manuel Bottini5209be52019-02-13 16:34:56 +0000158 // Bounding box transform
159 _memory_group.manage(&_all_proposals);
160 BoundingBoxTransformInfo bbox_info(info.im_width(), info.im_height(), 1.f);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100161 _bounding_box_kernel->configure(compile_context, anchors_to_use, &_all_proposals, deltas_to_use, bbox_info);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100162 deltas_to_use->allocator()->allocate();
163 anchors_to_use->allocator()->allocate();
Manuel Bottini5209be52019-02-13 16:34:56 +0000164
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100165 _all_proposals_to_use = &_all_proposals;
166 if(_is_qasymm8)
167 {
168 _memory_group.manage(&_all_proposals_quantized);
169 // Requantize all_proposals to QASYMM16 with 0.125 scale and 0 offset
170 _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 +0100171 _quantize_all_proposals->configure(compile_context, &_all_proposals, &_all_proposals_quantized);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100172 _all_proposals.allocator()->allocate();
173 _all_proposals_to_use = &_all_proposals_quantized;
174 }
Manuel Bottini5209be52019-02-13 16:34:56 +0000175 // The original layer implementation first selects the best pre_nms_topN anchors (thus having a lightweight sort)
176 // that are then transformed by bbox_transform. The boxes generated are then fed into a non-sorting NMS operation.
177 // 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)
178 // and the filtering
179 const int scores_nms_size = std::min<int>(std::min<int>(post_nms_topN, pre_nms_topN), total_num_anchors);
180 const float min_size_scaled = info.min_size() * info.im_scale();
181 _memory_group.manage(&_classes_nms_unused);
182 _memory_group.manage(&_keeps_nms_unused);
183
184 // Note that NMS needs outputs preinitialized.
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100185 auto_init_if_empty(*scores_out->info(), TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo);
186 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 +0000187 auto_init_if_empty(*num_valid_proposals->info(), TensorShape(1), 1, DataType::U32);
188
189 // Initialize temporaries (unused) outputs
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100190 _classes_nms_unused.allocator()->init(TensorInfo(TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo));
Manuel Bottini5209be52019-02-13 16:34:56 +0000191 _keeps_nms_unused.allocator()->init(*scores_out->info());
192
193 // Save the output (to map and unmap them at run)
194 _scores_out = scores_out;
195 _num_valid_proposals = num_valid_proposals;
196
197 _memory_group.manage(&_proposals_4_roi_values);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100198 _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,
199 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 +0000200 _keeps_nms_unused.allocator()->allocate();
201 _classes_nms_unused.allocator()->allocate();
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100202 _all_proposals_to_use->allocator()->allocate();
Manuel Bottini5209be52019-02-13 16:34:56 +0000203 _scores_flattened.allocator()->allocate();
204
205 // 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 +0100206 _pad_kernel->configure(compile_context, &_proposals_4_roi_values, proposals, PaddingList{ { 1, 0 } });
Manuel Bottini5209be52019-02-13 16:34:56 +0000207 _proposals_4_roi_values.allocator()->allocate();
Manuel Bottini5209be52019-02-13 16:34:56 +0000208}
209
210Status CLGenerateProposalsLayer::validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out,
211 const ITensorInfo *num_valid_proposals, const GenerateProposalsInfo &info)
212{
213 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100214 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 +0000215 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(scores, DataLayout::NCHW, DataLayout::NHWC);
216 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(scores, deltas);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100217 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores, deltas);
Manuel Bottini5209be52019-02-13 16:34:56 +0000218
219 const int num_anchors = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::CHANNEL));
220 const int feat_width = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::WIDTH));
221 const int feat_height = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::HEIGHT));
222 const int num_images = scores->dimension(3);
223 const int total_num_anchors = num_anchors * feat_width * feat_height;
224 const int values_per_roi = info.values_per_roi();
225
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100226 const bool is_qasymm8 = scores->data_type() == DataType::QASYMM8;
227
Manuel Bottini5209be52019-02-13 16:34:56 +0000228 ARM_COMPUTE_RETURN_ERROR_ON(num_images > 1);
229
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100230 if(is_qasymm8)
231 {
232 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(anchors, 1, DataType::QSYMM16);
233 const UniformQuantizationInfo anchors_qinfo = anchors->quantization_info().uniform();
234 ARM_COMPUTE_RETURN_ERROR_ON(anchors_qinfo.scale != 0.125f);
235 }
236
Manuel Bottini5209be52019-02-13 16:34:56 +0000237 TensorInfo all_anchors_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
238 ARM_COMPUTE_RETURN_ON_ERROR(CLComputeAllAnchorsKernel::validate(anchors, &all_anchors_info, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale())));
239
240 TensorInfo deltas_permuted_info = deltas->clone()->set_tensor_shape(TensorShape(values_per_roi * num_anchors, feat_width, feat_height)).set_is_resizable(true);
241 TensorInfo scores_permuted_info = scores->clone()->set_tensor_shape(TensorShape(num_anchors, feat_width, feat_height)).set_is_resizable(true);
242 if(scores->data_layout() == DataLayout::NHWC)
243 {
244 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(deltas, &deltas_permuted_info);
245 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(scores, &scores_permuted_info);
246 }
247 else
248 {
Sheri Zhang7e20e292021-02-02 11:49:34 +0000249 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(deltas, &deltas_permuted_info, PermutationVector{ 2, 0, 1 }));
250 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(scores, &scores_permuted_info, PermutationVector{ 2, 0, 1 }));
Manuel Bottini5209be52019-02-13 16:34:56 +0000251 }
252
253 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 +0100254 ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(&deltas_permuted_info, &deltas_flattened_info));
Manuel Bottini5209be52019-02-13 16:34:56 +0000255
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100256 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 +0000257 TensorInfo proposals_4_roi_values(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
258
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100259 ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(&scores_permuted_info, &scores_flattened_info));
Manuel Bottini5209be52019-02-13 16:34:56 +0000260
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100261 TensorInfo *proposals_4_roi_values_to_use = &proposals_4_roi_values;
262 TensorInfo proposals_4_roi_values_quantized(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
263 proposals_4_roi_values_quantized.set_data_type(DataType::QASYMM16).set_quantization_info(QuantizationInfo(0.125f, 0));
264 if(is_qasymm8)
265 {
266 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));
Manuel Bottini9e73c932021-03-02 17:40:42 +0000267 ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayer::validate(&all_anchors_info, &all_anchors_f32_info));
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100268
269 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));
Manuel Bottini9e73c932021-03-02 17:40:42 +0000270 ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayer::validate(&deltas_flattened_info, &deltas_flattened_f32_info));
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100271
272 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));
273 ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_f32_info, &proposals_4_roi_values_f32, &deltas_flattened_f32_info,
274 BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f)));
275
Manuel Bottini5a1bf622021-03-01 17:39:36 +0000276 ARM_COMPUTE_RETURN_ON_ERROR(CLQuantizationLayer::validate(&proposals_4_roi_values_f32, &proposals_4_roi_values_quantized));
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100277 proposals_4_roi_values_to_use = &proposals_4_roi_values_quantized;
278 }
279 else
280 {
281 ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_info, &proposals_4_roi_values, &deltas_flattened_info,
282 BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f)));
283 }
284
285 ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(proposals_4_roi_values_to_use, proposals, PaddingList{ { 1, 0 } }));
Manuel Bottini5209be52019-02-13 16:34:56 +0000286
287 if(num_valid_proposals->total_size() > 0)
288 {
289 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->num_dimensions() > 1);
290 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->dimension(0) > 1);
291 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(num_valid_proposals, 1, DataType::U32);
292 }
293
294 if(proposals->total_size() > 0)
295 {
296 ARM_COMPUTE_RETURN_ERROR_ON(proposals->num_dimensions() > 2);
297 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(0) != size_t(values_per_roi) + 1);
298 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(1) != size_t(total_num_anchors));
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100299 if(is_qasymm8)
300 {
301 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(proposals, 1, DataType::QASYMM16);
302 const UniformQuantizationInfo proposals_qinfo = proposals->quantization_info().uniform();
303 ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.scale != 0.125f);
304 ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.offset != 0);
305 }
306 else
307 {
308 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(proposals, scores);
309 }
Manuel Bottini5209be52019-02-13 16:34:56 +0000310 }
311
312 if(scores_out->total_size() > 0)
313 {
314 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->num_dimensions() > 1);
315 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->dimension(0) != size_t(total_num_anchors));
316 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores_out, scores);
317 }
318
319 return Status{};
320}
321
322void CLGenerateProposalsLayer::run_cpp_nms_kernel()
323{
324 // Map inputs
325 _scores_flattened.map(true);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100326 _all_proposals_to_use->map(true);
Manuel Bottini5209be52019-02-13 16:34:56 +0000327
328 // Map outputs
329 _scores_out->map(CLScheduler::get().queue(), true);
330 _proposals_4_roi_values.map(CLScheduler::get().queue(), true);
331 _num_valid_proposals->map(CLScheduler::get().queue(), true);
332 _keeps_nms_unused.map(true);
333 _classes_nms_unused.map(true);
334
335 // Run nms
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100336 _cpp_nms.run();
Manuel Bottini5209be52019-02-13 16:34:56 +0000337
338 // Unmap outputs
339 _keeps_nms_unused.unmap();
340 _classes_nms_unused.unmap();
341 _scores_out->unmap(CLScheduler::get().queue());
342 _proposals_4_roi_values.unmap(CLScheduler::get().queue());
343 _num_valid_proposals->unmap(CLScheduler::get().queue());
344
345 // Unmap inputs
346 _scores_flattened.unmap();
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100347 _all_proposals_to_use->unmap();
Manuel Bottini5209be52019-02-13 16:34:56 +0000348}
349
350void CLGenerateProposalsLayer::run()
351{
352 // Acquire all the temporaries
Georgios Pinitasda953f22019-04-02 17:27:03 +0100353 MemoryGroupResourceScope scope_mg(_memory_group);
Manuel Bottini5209be52019-02-13 16:34:56 +0000354
355 // Compute all the anchors
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100356 CLScheduler::get().enqueue(*_compute_anchors_kernel, false);
Manuel Bottini5209be52019-02-13 16:34:56 +0000357
358 // Transpose and reshape the inputs
359 if(!_is_nhwc)
360 {
Sheri Zhang7e20e292021-02-02 11:49:34 +0000361 _permute_deltas.run();
362 _permute_scores.run();
Manuel Bottini5209be52019-02-13 16:34:56 +0000363 }
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100364 _flatten_deltas.run();
365 _flatten_scores.run();
Manuel Bottini5209be52019-02-13 16:34:56 +0000366
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100367 if(_is_qasymm8)
368 {
Manuel Bottini9e73c932021-03-02 17:40:42 +0000369 _dequantize_anchors->run();
370 _dequantize_deltas->run();
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100371 }
372
Manuel Bottini5209be52019-02-13 16:34:56 +0000373 // Build the boxes
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100374 CLScheduler::get().enqueue(*_bounding_box_kernel, false);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100375
376 if(_is_qasymm8)
377 {
Manuel Bottini5a1bf622021-03-01 17:39:36 +0000378 _quantize_all_proposals->run();
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100379 }
380
Manuel Bottini5209be52019-02-13 16:34:56 +0000381 // Non maxima suppression
382 run_cpp_nms_kernel();
383 // Add dummy batch indexes
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100384 CLScheduler::get().enqueue(*_pad_kernel, true);
Manuel Bottini5209be52019-02-13 16:34:56 +0000385}
386} // namespace arm_compute