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