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/CL/functions/CLGenerateProposalsLayer.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
27*c217d954SCole Faust #include "arm_compute/core/Types.h"
28*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLDequantizationLayer.h"
29*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLQuantizationLayer.h"
30*c217d954SCole Faust #include "src/core/CL/kernels/CLBoundingBoxTransformKernel.h"
31*c217d954SCole Faust #include "src/core/CL/kernels/CLGenerateProposalsLayerKernel.h"
32*c217d954SCole Faust #include "src/core/CL/kernels/CLPadLayerKernel.h"
33*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
34*c217d954SCole Faust
35*c217d954SCole Faust #include "src/common/utils/Log.h"
36*c217d954SCole Faust
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
CLGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager)39*c217d954SCole Faust CLGenerateProposalsLayer::CLGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager)
40*c217d954SCole Faust : _memory_group(memory_manager),
41*c217d954SCole Faust _permute_deltas(),
42*c217d954SCole Faust _flatten_deltas(),
43*c217d954SCole Faust _permute_scores(),
44*c217d954SCole Faust _flatten_scores(),
45*c217d954SCole Faust _compute_anchors_kernel(std::make_unique<CLComputeAllAnchorsKernel>()),
46*c217d954SCole Faust _bounding_box_kernel(std::make_unique<CLBoundingBoxTransformKernel>()),
47*c217d954SCole Faust _pad_kernel(std::make_unique<CLPadLayerKernel>()),
48*c217d954SCole Faust _dequantize_anchors(std::make_unique<CLDequantizationLayer>()),
49*c217d954SCole Faust _dequantize_deltas(std::make_unique<CLDequantizationLayer>()),
50*c217d954SCole Faust _quantize_all_proposals(std::make_unique<CLQuantizationLayer>()),
51*c217d954SCole Faust _cpp_nms(memory_manager),
52*c217d954SCole Faust _is_nhwc(false),
53*c217d954SCole Faust _is_qasymm8(false),
54*c217d954SCole Faust _deltas_permuted(),
55*c217d954SCole Faust _deltas_flattened(),
56*c217d954SCole Faust _deltas_flattened_f32(),
57*c217d954SCole Faust _scores_permuted(),
58*c217d954SCole Faust _scores_flattened(),
59*c217d954SCole Faust _all_anchors(),
60*c217d954SCole Faust _all_anchors_f32(),
61*c217d954SCole Faust _all_proposals(),
62*c217d954SCole Faust _all_proposals_quantized(),
63*c217d954SCole Faust _keeps_nms_unused(),
64*c217d954SCole Faust _classes_nms_unused(),
65*c217d954SCole Faust _proposals_4_roi_values(),
66*c217d954SCole Faust _all_proposals_to_use(nullptr),
67*c217d954SCole Faust _num_valid_proposals(nullptr),
68*c217d954SCole Faust _scores_out(nullptr)
69*c217d954SCole Faust {
70*c217d954SCole Faust }
71*c217d954SCole Faust
72*c217d954SCole Faust CLGenerateProposalsLayer::~CLGenerateProposalsLayer() = default;
73*c217d954SCole Faust
configure(const ICLTensor * scores,const ICLTensor * deltas,const ICLTensor * anchors,ICLTensor * proposals,ICLTensor * scores_out,ICLTensor * num_valid_proposals,const GenerateProposalsInfo & info)74*c217d954SCole Faust void CLGenerateProposalsLayer::configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals,
75*c217d954SCole Faust const GenerateProposalsInfo &info)
76*c217d954SCole Faust {
77*c217d954SCole Faust configure(CLKernelLibrary::get().get_compile_context(), scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info);
78*c217d954SCole Faust }
79*c217d954SCole Faust
configure(const CLCompileContext & compile_context,const ICLTensor * scores,const ICLTensor * deltas,const ICLTensor * anchors,ICLTensor * proposals,ICLTensor * scores_out,ICLTensor * num_valid_proposals,const GenerateProposalsInfo & info)80*c217d954SCole Faust void CLGenerateProposalsLayer::configure(const CLCompileContext &compile_context, const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals,
81*c217d954SCole Faust ICLTensor *scores_out,
82*c217d954SCole Faust ICLTensor *num_valid_proposals, const GenerateProposalsInfo &info)
83*c217d954SCole Faust {
84*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
85*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(CLGenerateProposalsLayer::validate(scores->info(), deltas->info(), anchors->info(), proposals->info(), scores_out->info(), num_valid_proposals->info(), info));
86*c217d954SCole Faust ARM_COMPUTE_LOG_PARAMS(scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info);
87*c217d954SCole Faust
88*c217d954SCole Faust _is_nhwc = scores->info()->data_layout() == DataLayout::NHWC;
89*c217d954SCole Faust const DataType scores_data_type = scores->info()->data_type();
90*c217d954SCole Faust _is_qasymm8 = scores_data_type == DataType::QASYMM8;
91*c217d954SCole Faust const int num_anchors = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::CHANNEL));
92*c217d954SCole Faust const int feat_width = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::WIDTH));
93*c217d954SCole Faust const int feat_height = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::HEIGHT));
94*c217d954SCole Faust const int total_num_anchors = num_anchors * feat_width * feat_height;
95*c217d954SCole Faust const int pre_nms_topN = info.pre_nms_topN();
96*c217d954SCole Faust const int post_nms_topN = info.post_nms_topN();
97*c217d954SCole Faust const size_t values_per_roi = info.values_per_roi();
98*c217d954SCole Faust
99*c217d954SCole Faust const QuantizationInfo scores_qinfo = scores->info()->quantization_info();
100*c217d954SCole Faust const DataType rois_data_type = (_is_qasymm8) ? DataType::QASYMM16 : scores_data_type;
101*c217d954SCole Faust const QuantizationInfo rois_qinfo = (_is_qasymm8) ? QuantizationInfo(0.125f, 0) : scores->info()->quantization_info();
102*c217d954SCole Faust
103*c217d954SCole Faust // Compute all the anchors
104*c217d954SCole Faust _memory_group.manage(&_all_anchors);
105*c217d954SCole Faust _compute_anchors_kernel->configure(compile_context, anchors, &_all_anchors, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale()));
106*c217d954SCole Faust
107*c217d954SCole Faust const TensorShape flatten_shape_deltas(values_per_roi, total_num_anchors);
108*c217d954SCole Faust _deltas_flattened.allocator()->init(TensorInfo(flatten_shape_deltas, 1, scores_data_type, deltas->info()->quantization_info()));
109*c217d954SCole Faust
110*c217d954SCole Faust // Permute and reshape deltas
111*c217d954SCole Faust _memory_group.manage(&_deltas_flattened);
112*c217d954SCole Faust if(!_is_nhwc)
113*c217d954SCole Faust {
114*c217d954SCole Faust _memory_group.manage(&_deltas_permuted);
115*c217d954SCole Faust _permute_deltas.configure(compile_context, deltas, &_deltas_permuted, PermutationVector{ 2, 0, 1 });
116*c217d954SCole Faust _flatten_deltas.configure(compile_context, &_deltas_permuted, &_deltas_flattened);
117*c217d954SCole Faust _deltas_permuted.allocator()->allocate();
118*c217d954SCole Faust }
119*c217d954SCole Faust else
120*c217d954SCole Faust {
121*c217d954SCole Faust _flatten_deltas.configure(compile_context, deltas, &_deltas_flattened);
122*c217d954SCole Faust }
123*c217d954SCole Faust
124*c217d954SCole Faust const TensorShape flatten_shape_scores(1, total_num_anchors);
125*c217d954SCole Faust _scores_flattened.allocator()->init(TensorInfo(flatten_shape_scores, 1, scores_data_type, scores_qinfo));
126*c217d954SCole Faust
127*c217d954SCole Faust // Permute and reshape scores
128*c217d954SCole Faust _memory_group.manage(&_scores_flattened);
129*c217d954SCole Faust if(!_is_nhwc)
130*c217d954SCole Faust {
131*c217d954SCole Faust _memory_group.manage(&_scores_permuted);
132*c217d954SCole Faust _permute_scores.configure(compile_context, scores, &_scores_permuted, PermutationVector{ 2, 0, 1 });
133*c217d954SCole Faust _flatten_scores.configure(compile_context, &_scores_permuted, &_scores_flattened);
134*c217d954SCole Faust _scores_permuted.allocator()->allocate();
135*c217d954SCole Faust }
136*c217d954SCole Faust else
137*c217d954SCole Faust {
138*c217d954SCole Faust _flatten_scores.configure(compile_context, scores, &_scores_flattened);
139*c217d954SCole Faust }
140*c217d954SCole Faust
141*c217d954SCole Faust CLTensor *anchors_to_use = &_all_anchors;
142*c217d954SCole Faust CLTensor *deltas_to_use = &_deltas_flattened;
143*c217d954SCole Faust if(_is_qasymm8)
144*c217d954SCole Faust {
145*c217d954SCole Faust _all_anchors_f32.allocator()->init(TensorInfo(_all_anchors.info()->tensor_shape(), 1, DataType::F32));
146*c217d954SCole Faust _deltas_flattened_f32.allocator()->init(TensorInfo(_deltas_flattened.info()->tensor_shape(), 1, DataType::F32));
147*c217d954SCole Faust _memory_group.manage(&_all_anchors_f32);
148*c217d954SCole Faust _memory_group.manage(&_deltas_flattened_f32);
149*c217d954SCole Faust // Dequantize anchors to float
150*c217d954SCole Faust _dequantize_anchors->configure(compile_context, &_all_anchors, &_all_anchors_f32);
151*c217d954SCole Faust _all_anchors.allocator()->allocate();
152*c217d954SCole Faust anchors_to_use = &_all_anchors_f32;
153*c217d954SCole Faust // Dequantize deltas to float
154*c217d954SCole Faust _dequantize_deltas->configure(compile_context, &_deltas_flattened, &_deltas_flattened_f32);
155*c217d954SCole Faust _deltas_flattened.allocator()->allocate();
156*c217d954SCole Faust deltas_to_use = &_deltas_flattened_f32;
157*c217d954SCole Faust }
158*c217d954SCole Faust // Bounding box transform
159*c217d954SCole Faust _memory_group.manage(&_all_proposals);
160*c217d954SCole Faust BoundingBoxTransformInfo bbox_info(info.im_width(), info.im_height(), 1.f);
161*c217d954SCole Faust _bounding_box_kernel->configure(compile_context, anchors_to_use, &_all_proposals, deltas_to_use, bbox_info);
162*c217d954SCole Faust deltas_to_use->allocator()->allocate();
163*c217d954SCole Faust anchors_to_use->allocator()->allocate();
164*c217d954SCole Faust
165*c217d954SCole Faust _all_proposals_to_use = &_all_proposals;
166*c217d954SCole Faust if(_is_qasymm8)
167*c217d954SCole Faust {
168*c217d954SCole Faust _memory_group.manage(&_all_proposals_quantized);
169*c217d954SCole Faust // Requantize all_proposals to QASYMM16 with 0.125 scale and 0 offset
170*c217d954SCole Faust _all_proposals_quantized.allocator()->init(TensorInfo(_all_proposals.info()->tensor_shape(), 1, DataType::QASYMM16, QuantizationInfo(0.125f, 0)));
171*c217d954SCole Faust _quantize_all_proposals->configure(compile_context, &_all_proposals, &_all_proposals_quantized);
172*c217d954SCole Faust _all_proposals.allocator()->allocate();
173*c217d954SCole Faust _all_proposals_to_use = &_all_proposals_quantized;
174*c217d954SCole Faust }
175*c217d954SCole Faust // The original layer implementation first selects the best pre_nms_topN anchors (thus having a lightweight sort)
176*c217d954SCole Faust // that are then transformed by bbox_transform. The boxes generated are then fed into a non-sorting NMS operation.
177*c217d954SCole Faust // 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*c217d954SCole Faust // and the filtering
179*c217d954SCole Faust const int scores_nms_size = std::min<int>(std::min<int>(post_nms_topN, pre_nms_topN), total_num_anchors);
180*c217d954SCole Faust const float min_size_scaled = info.min_size() * info.im_scale();
181*c217d954SCole Faust _memory_group.manage(&_classes_nms_unused);
182*c217d954SCole Faust _memory_group.manage(&_keeps_nms_unused);
183*c217d954SCole Faust
184*c217d954SCole Faust // Note that NMS needs outputs preinitialized.
185*c217d954SCole Faust auto_init_if_empty(*scores_out->info(), TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo);
186*c217d954SCole Faust auto_init_if_empty(*_proposals_4_roi_values.info(), TensorShape(values_per_roi, scores_nms_size), 1, rois_data_type, rois_qinfo);
187*c217d954SCole Faust auto_init_if_empty(*num_valid_proposals->info(), TensorShape(1), 1, DataType::U32);
188*c217d954SCole Faust
189*c217d954SCole Faust // Initialize temporaries (unused) outputs
190*c217d954SCole Faust _classes_nms_unused.allocator()->init(TensorInfo(TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo));
191*c217d954SCole Faust _keeps_nms_unused.allocator()->init(*scores_out->info());
192*c217d954SCole Faust
193*c217d954SCole Faust // Save the output (to map and unmap them at run)
194*c217d954SCole Faust _scores_out = scores_out;
195*c217d954SCole Faust _num_valid_proposals = num_valid_proposals;
196*c217d954SCole Faust
197*c217d954SCole Faust _memory_group.manage(&_proposals_4_roi_values);
198*c217d954SCole Faust _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*c217d954SCole Faust 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()));
200*c217d954SCole Faust _keeps_nms_unused.allocator()->allocate();
201*c217d954SCole Faust _classes_nms_unused.allocator()->allocate();
202*c217d954SCole Faust _all_proposals_to_use->allocator()->allocate();
203*c217d954SCole Faust _scores_flattened.allocator()->allocate();
204*c217d954SCole Faust
205*c217d954SCole Faust // Add the first column that represents the batch id. This will be all zeros, as we don't support multiple images
206*c217d954SCole Faust _pad_kernel->configure(compile_context, &_proposals_4_roi_values, proposals, PaddingList{ { 1, 0 } });
207*c217d954SCole Faust _proposals_4_roi_values.allocator()->allocate();
208*c217d954SCole Faust }
209*c217d954SCole Faust
validate(const ITensorInfo * scores,const ITensorInfo * deltas,const ITensorInfo * anchors,const ITensorInfo * proposals,const ITensorInfo * scores_out,const ITensorInfo * num_valid_proposals,const GenerateProposalsInfo & info)210*c217d954SCole Faust Status CLGenerateProposalsLayer::validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out,
211*c217d954SCole Faust const ITensorInfo *num_valid_proposals, const GenerateProposalsInfo &info)
212*c217d954SCole Faust {
213*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
214*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(scores, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
215*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(scores, DataLayout::NCHW, DataLayout::NHWC);
216*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(scores, deltas);
217*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores, deltas);
218*c217d954SCole Faust
219*c217d954SCole Faust const int num_anchors = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::CHANNEL));
220*c217d954SCole Faust const int feat_width = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::WIDTH));
221*c217d954SCole Faust const int feat_height = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::HEIGHT));
222*c217d954SCole Faust const int num_images = scores->dimension(3);
223*c217d954SCole Faust const int total_num_anchors = num_anchors * feat_width * feat_height;
224*c217d954SCole Faust const int values_per_roi = info.values_per_roi();
225*c217d954SCole Faust
226*c217d954SCole Faust const bool is_qasymm8 = scores->data_type() == DataType::QASYMM8;
227*c217d954SCole Faust
228*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(num_images > 1);
229*c217d954SCole Faust
230*c217d954SCole Faust if(is_qasymm8)
231*c217d954SCole Faust {
232*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(anchors, 1, DataType::QSYMM16);
233*c217d954SCole Faust const UniformQuantizationInfo anchors_qinfo = anchors->quantization_info().uniform();
234*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(anchors_qinfo.scale != 0.125f);
235*c217d954SCole Faust }
236*c217d954SCole Faust
237*c217d954SCole Faust TensorInfo all_anchors_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
238*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLComputeAllAnchorsKernel::validate(anchors, &all_anchors_info, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale())));
239*c217d954SCole Faust
240*c217d954SCole Faust TensorInfo deltas_permuted_info = deltas->clone()->set_tensor_shape(TensorShape(values_per_roi * num_anchors, feat_width, feat_height)).set_is_resizable(true);
241*c217d954SCole Faust TensorInfo scores_permuted_info = scores->clone()->set_tensor_shape(TensorShape(num_anchors, feat_width, feat_height)).set_is_resizable(true);
242*c217d954SCole Faust if(scores->data_layout() == DataLayout::NHWC)
243*c217d954SCole Faust {
244*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(deltas, &deltas_permuted_info);
245*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(scores, &scores_permuted_info);
246*c217d954SCole Faust }
247*c217d954SCole Faust else
248*c217d954SCole Faust {
249*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(deltas, &deltas_permuted_info, PermutationVector{ 2, 0, 1 }));
250*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(scores, &scores_permuted_info, PermutationVector{ 2, 0, 1 }));
251*c217d954SCole Faust }
252*c217d954SCole Faust
253*c217d954SCole Faust TensorInfo deltas_flattened_info(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
254*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(&deltas_permuted_info, &deltas_flattened_info));
255*c217d954SCole Faust
256*c217d954SCole Faust TensorInfo scores_flattened_info(scores->clone()->set_tensor_shape(TensorShape(1, total_num_anchors)).set_is_resizable(true));
257*c217d954SCole Faust TensorInfo proposals_4_roi_values(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
258*c217d954SCole Faust
259*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(&scores_permuted_info, &scores_flattened_info));
260*c217d954SCole Faust
261*c217d954SCole Faust TensorInfo *proposals_4_roi_values_to_use = &proposals_4_roi_values;
262*c217d954SCole Faust TensorInfo proposals_4_roi_values_quantized(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
263*c217d954SCole Faust proposals_4_roi_values_quantized.set_data_type(DataType::QASYMM16).set_quantization_info(QuantizationInfo(0.125f, 0));
264*c217d954SCole Faust if(is_qasymm8)
265*c217d954SCole Faust {
266*c217d954SCole Faust 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));
267*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayer::validate(&all_anchors_info, &all_anchors_f32_info));
268*c217d954SCole Faust
269*c217d954SCole Faust 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));
270*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayer::validate(&deltas_flattened_info, &deltas_flattened_f32_info));
271*c217d954SCole Faust
272*c217d954SCole Faust 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*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_f32_info, &proposals_4_roi_values_f32, &deltas_flattened_f32_info,
274*c217d954SCole Faust BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f)));
275*c217d954SCole Faust
276*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLQuantizationLayer::validate(&proposals_4_roi_values_f32, &proposals_4_roi_values_quantized));
277*c217d954SCole Faust proposals_4_roi_values_to_use = &proposals_4_roi_values_quantized;
278*c217d954SCole Faust }
279*c217d954SCole Faust else
280*c217d954SCole Faust {
281*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_info, &proposals_4_roi_values, &deltas_flattened_info,
282*c217d954SCole Faust BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f)));
283*c217d954SCole Faust }
284*c217d954SCole Faust
285*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(proposals_4_roi_values_to_use, proposals, PaddingList{ { 1, 0 } }));
286*c217d954SCole Faust
287*c217d954SCole Faust if(num_valid_proposals->total_size() > 0)
288*c217d954SCole Faust {
289*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->num_dimensions() > 1);
290*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->dimension(0) > 1);
291*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(num_valid_proposals, 1, DataType::U32);
292*c217d954SCole Faust }
293*c217d954SCole Faust
294*c217d954SCole Faust if(proposals->total_size() > 0)
295*c217d954SCole Faust {
296*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(proposals->num_dimensions() > 2);
297*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(0) != size_t(values_per_roi) + 1);
298*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(1) != size_t(total_num_anchors));
299*c217d954SCole Faust if(is_qasymm8)
300*c217d954SCole Faust {
301*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(proposals, 1, DataType::QASYMM16);
302*c217d954SCole Faust const UniformQuantizationInfo proposals_qinfo = proposals->quantization_info().uniform();
303*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.scale != 0.125f);
304*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.offset != 0);
305*c217d954SCole Faust }
306*c217d954SCole Faust else
307*c217d954SCole Faust {
308*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(proposals, scores);
309*c217d954SCole Faust }
310*c217d954SCole Faust }
311*c217d954SCole Faust
312*c217d954SCole Faust if(scores_out->total_size() > 0)
313*c217d954SCole Faust {
314*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(scores_out->num_dimensions() > 1);
315*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(scores_out->dimension(0) != size_t(total_num_anchors));
316*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores_out, scores);
317*c217d954SCole Faust }
318*c217d954SCole Faust
319*c217d954SCole Faust return Status{};
320*c217d954SCole Faust }
321*c217d954SCole Faust
run_cpp_nms_kernel()322*c217d954SCole Faust void CLGenerateProposalsLayer::run_cpp_nms_kernel()
323*c217d954SCole Faust {
324*c217d954SCole Faust // Map inputs
325*c217d954SCole Faust _scores_flattened.map(true);
326*c217d954SCole Faust _all_proposals_to_use->map(true);
327*c217d954SCole Faust
328*c217d954SCole Faust // Map outputs
329*c217d954SCole Faust _scores_out->map(CLScheduler::get().queue(), true);
330*c217d954SCole Faust _proposals_4_roi_values.map(CLScheduler::get().queue(), true);
331*c217d954SCole Faust _num_valid_proposals->map(CLScheduler::get().queue(), true);
332*c217d954SCole Faust _keeps_nms_unused.map(true);
333*c217d954SCole Faust _classes_nms_unused.map(true);
334*c217d954SCole Faust
335*c217d954SCole Faust // Run nms
336*c217d954SCole Faust _cpp_nms.run();
337*c217d954SCole Faust
338*c217d954SCole Faust // Unmap outputs
339*c217d954SCole Faust _keeps_nms_unused.unmap();
340*c217d954SCole Faust _classes_nms_unused.unmap();
341*c217d954SCole Faust _scores_out->unmap(CLScheduler::get().queue());
342*c217d954SCole Faust _proposals_4_roi_values.unmap(CLScheduler::get().queue());
343*c217d954SCole Faust _num_valid_proposals->unmap(CLScheduler::get().queue());
344*c217d954SCole Faust
345*c217d954SCole Faust // Unmap inputs
346*c217d954SCole Faust _scores_flattened.unmap();
347*c217d954SCole Faust _all_proposals_to_use->unmap();
348*c217d954SCole Faust }
349*c217d954SCole Faust
run()350*c217d954SCole Faust void CLGenerateProposalsLayer::run()
351*c217d954SCole Faust {
352*c217d954SCole Faust // Acquire all the temporaries
353*c217d954SCole Faust MemoryGroupResourceScope scope_mg(_memory_group);
354*c217d954SCole Faust
355*c217d954SCole Faust // Compute all the anchors
356*c217d954SCole Faust CLScheduler::get().enqueue(*_compute_anchors_kernel, false);
357*c217d954SCole Faust
358*c217d954SCole Faust // Transpose and reshape the inputs
359*c217d954SCole Faust if(!_is_nhwc)
360*c217d954SCole Faust {
361*c217d954SCole Faust _permute_deltas.run();
362*c217d954SCole Faust _permute_scores.run();
363*c217d954SCole Faust }
364*c217d954SCole Faust _flatten_deltas.run();
365*c217d954SCole Faust _flatten_scores.run();
366*c217d954SCole Faust
367*c217d954SCole Faust if(_is_qasymm8)
368*c217d954SCole Faust {
369*c217d954SCole Faust _dequantize_anchors->run();
370*c217d954SCole Faust _dequantize_deltas->run();
371*c217d954SCole Faust }
372*c217d954SCole Faust
373*c217d954SCole Faust // Build the boxes
374*c217d954SCole Faust CLScheduler::get().enqueue(*_bounding_box_kernel, false);
375*c217d954SCole Faust
376*c217d954SCole Faust if(_is_qasymm8)
377*c217d954SCole Faust {
378*c217d954SCole Faust _quantize_all_proposals->run();
379*c217d954SCole Faust }
380*c217d954SCole Faust
381*c217d954SCole Faust // Non maxima suppression
382*c217d954SCole Faust run_cpp_nms_kernel();
383*c217d954SCole Faust // Add dummy batch indexes
384*c217d954SCole Faust CLScheduler::get().enqueue(*_pad_kernel, true);
385*c217d954SCole Faust }
386*c217d954SCole Faust } // namespace arm_compute
387