blob: b2a6ca8c35e79de8e67f464d9e0185df8a7dd21e [file] [log] [blame]
Pablo Telloc9564cb2019-09-13 10:20:25 +01001/*
2 * Copyright (c) 2019 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 "support/ToolchainSupport.h"
29
30namespace arm_compute
31{
32NEGenerateProposalsLayer::NEGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager)
33 : _memory_group(std::move(memory_manager)),
34 _permute_deltas_kernel(),
35 _flatten_deltas_kernel(),
36 _permute_scores_kernel(),
37 _flatten_scores_kernel(),
38 _compute_anchors_kernel(),
39 _bounding_box_kernel(),
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010040 _pad_kernel(),
Pablo Telloc9564cb2019-09-13 10:20:25 +010041 _cpp_nms_kernel(),
42 _is_nhwc(false),
43 _deltas_permuted(),
44 _deltas_flattened(),
45 _scores_permuted(),
46 _scores_flattened(),
47 _all_anchors(),
48 _all_proposals(),
49 _keeps_nms_unused(),
50 _classes_nms_unused(),
51 _proposals_4_roi_values(),
52 _num_valid_proposals(nullptr),
53 _scores_out(nullptr)
54{
55}
56
57void NEGenerateProposalsLayer::configure(const ITensor *scores, const ITensor *deltas, const ITensor *anchors, ITensor *proposals, ITensor *scores_out, ITensor *num_valid_proposals,
58 const GenerateProposalsInfo &info)
59{
60 ARM_COMPUTE_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
61 ARM_COMPUTE_ERROR_THROW_ON(NEGenerateProposalsLayer::validate(scores->info(), deltas->info(), anchors->info(), proposals->info(), scores_out->info(), num_valid_proposals->info(), info));
62
63 _is_nhwc = scores->info()->data_layout() == DataLayout::NHWC;
64 const DataType data_type = deltas->info()->data_type();
65 const int num_anchors = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::CHANNEL));
66 const int feat_width = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::WIDTH));
67 const int feat_height = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::HEIGHT));
68 const int total_num_anchors = num_anchors * feat_width * feat_height;
69 const int pre_nms_topN = info.pre_nms_topN();
70 const int post_nms_topN = info.post_nms_topN();
71 const size_t values_per_roi = info.values_per_roi();
72
73 // Compute all the anchors
74 _memory_group.manage(&_all_anchors);
75 _compute_anchors_kernel.configure(anchors, &_all_anchors, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale()));
76
77 const TensorShape flatten_shape_deltas(values_per_roi, total_num_anchors);
78 _deltas_flattened.allocator()->init(TensorInfo(flatten_shape_deltas, 1, data_type));
79 _memory_group.manage(&_deltas_flattened);
80
81 // Permute and reshape deltas
82 if(!_is_nhwc)
83 {
84 _memory_group.manage(&_deltas_permuted);
85 _permute_deltas_kernel.configure(deltas, &_deltas_permuted, PermutationVector{ 2, 0, 1 });
86 _flatten_deltas_kernel.configure(&_deltas_permuted, &_deltas_flattened);
87 _deltas_permuted.allocator()->allocate();
88 }
89 else
90 {
91 _flatten_deltas_kernel.configure(deltas, &_deltas_flattened);
92 }
93
94 const TensorShape flatten_shape_scores(1, total_num_anchors);
95 _scores_flattened.allocator()->init(TensorInfo(flatten_shape_scores, 1, data_type));
96 _memory_group.manage(&_scores_flattened);
97 // Permute and reshape scores
98 if(!_is_nhwc)
99 {
100 _memory_group.manage(&_scores_permuted);
101 _permute_scores_kernel.configure(scores, &_scores_permuted, PermutationVector{ 2, 0, 1 });
102 _flatten_scores_kernel.configure(&_scores_permuted, &_scores_flattened);
103 _scores_permuted.allocator()->allocate();
104 }
105 else
106 {
107 _flatten_scores_kernel.configure(scores, &_scores_flattened);
108 }
109
110 // Bounding box transform
111 _memory_group.manage(&_all_proposals);
112 BoundingBoxTransformInfo bbox_info(info.im_width(), info.im_height(), 1.f);
113 _bounding_box_kernel.configure(&_all_anchors, &_all_proposals, &_deltas_flattened, bbox_info);
114 _deltas_flattened.allocator()->allocate();
115 _all_anchors.allocator()->allocate();
116
117 // The original layer implementation first selects the best pre_nms_topN anchors (thus having a lightweight sort)
118 // that are then transformed by bbox_transform. The boxes generated are then fed into a non-sorting NMS operation.
119 // 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)
120 // and the filtering
121 const int scores_nms_size = std::min<int>(std::min<int>(post_nms_topN, pre_nms_topN), total_num_anchors);
122 const float min_size_scaled = info.min_size() * info.im_scale();
123 _memory_group.manage(&_classes_nms_unused);
124 _memory_group.manage(&_keeps_nms_unused);
125
126 // Note that NMS needs outputs preinitialized.
127 auto_init_if_empty(*scores_out->info(), TensorShape(scores_nms_size), 1, data_type);
128 auto_init_if_empty(*_proposals_4_roi_values.info(), TensorShape(values_per_roi, scores_nms_size), 1, data_type);
129 auto_init_if_empty(*num_valid_proposals->info(), TensorShape(scores_nms_size), 1, DataType::U32);
130
131 // Initialize temporaries (unused) outputs
Michele Di Giorgio4c268b92019-09-20 14:01:48 +0100132 _classes_nms_unused.allocator()->init(TensorInfo(TensorShape(scores_nms_size), 1, data_type));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100133 _keeps_nms_unused.allocator()->init(*scores_out->info());
134
135 // Save the output (to map and unmap them at run)
136 _scores_out = scores_out;
137 _num_valid_proposals = num_valid_proposals;
138
139 _memory_group.manage(&_proposals_4_roi_values);
140
141 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());
142 _cpp_nms_kernel.configure(&_scores_flattened /*scores_in*/,
143 &_all_proposals /*boxes_in,*/,
144 nullptr /* batch_splits_in*/,
145 scores_out /* scores_out*/,
146 &_proposals_4_roi_values /*boxes_out*/,
147 &_classes_nms_unused /*classes*/,
148 nullptr /*batch_splits_out*/,
149 &_keeps_nms_unused /*keeps*/,
150 num_valid_proposals /* keeps_size*/,
151 box_nms_info);
152
153 _keeps_nms_unused.allocator()->allocate();
154 _classes_nms_unused.allocator()->allocate();
155 _all_proposals.allocator()->allocate();
156 _scores_flattened.allocator()->allocate();
157
158 // Add the first column that represents the batch id. This will be all zeros, as we don't support multiple images
Michele Di Giorgio4c268b92019-09-20 14:01:48 +0100159 _pad_kernel.configure(&_proposals_4_roi_values, proposals, PaddingList{ { 1, 0 } });
Pablo Telloc9564cb2019-09-13 10:20:25 +0100160 _proposals_4_roi_values.allocator()->allocate();
Pablo Telloc9564cb2019-09-13 10:20:25 +0100161}
162
163Status NEGenerateProposalsLayer::validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out,
164 const ITensorInfo *num_valid_proposals, const GenerateProposalsInfo &info)
165{
166 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals);
167 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(scores, DataLayout::NCHW, DataLayout::NHWC);
168 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(scores, deltas);
169
170 const int num_anchors = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::CHANNEL));
171 const int feat_width = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::WIDTH));
172 const int feat_height = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::HEIGHT));
173 const int num_images = scores->dimension(3);
174 const int total_num_anchors = num_anchors * feat_width * feat_height;
175 const int values_per_roi = info.values_per_roi();
176
177 ARM_COMPUTE_RETURN_ERROR_ON(num_images > 1);
178
179 TensorInfo all_anchors_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
180 ARM_COMPUTE_RETURN_ON_ERROR(NEComputeAllAnchorsKernel::validate(anchors, &all_anchors_info, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale())));
181
182 TensorInfo deltas_permuted_info = deltas->clone()->set_tensor_shape(TensorShape(values_per_roi * num_anchors, feat_width, feat_height)).set_is_resizable(true);
183 TensorInfo scores_permuted_info = scores->clone()->set_tensor_shape(TensorShape(num_anchors, feat_width, feat_height)).set_is_resizable(true);
184 if(scores->data_layout() == DataLayout::NHWC)
185 {
186 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(deltas, &deltas_permuted_info);
187 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(scores, &scores_permuted_info);
188 }
189 else
190 {
191 ARM_COMPUTE_RETURN_ON_ERROR(NEPermuteKernel::validate(deltas, &deltas_permuted_info, PermutationVector{ 2, 0, 1 }));
192 ARM_COMPUTE_RETURN_ON_ERROR(NEPermuteKernel::validate(scores, &scores_permuted_info, PermutationVector{ 2, 0, 1 }));
193 }
194
195 TensorInfo deltas_flattened_info(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
196 ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayerKernel::validate(&deltas_permuted_info, &deltas_flattened_info));
197
198 TensorInfo scores_flattened_info(scores->clone()->set_tensor_shape(TensorShape(1, total_num_anchors)).set_is_resizable(true));
199 TensorInfo proposals_4_roi_values(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
200
201 ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayerKernel::validate(&scores_permuted_info, &scores_flattened_info));
202 ARM_COMPUTE_RETURN_ON_ERROR(NEBoundingBoxTransformKernel::validate(&all_anchors_info, &proposals_4_roi_values, &deltas_flattened_info, BoundingBoxTransformInfo(info.im_width(), info.im_height(),
203 1.f)));
204
Michele Di Giorgio4c268b92019-09-20 14:01:48 +0100205 ARM_COMPUTE_RETURN_ON_ERROR(NEPadLayerKernel::validate(&proposals_4_roi_values, proposals, PaddingList{ { 1, 0 } }));
Pablo Telloc9564cb2019-09-13 10:20:25 +0100206
207 if(num_valid_proposals->total_size() > 0)
208 {
209 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->num_dimensions() > 1);
210 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->dimension(0) > 1);
211 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(num_valid_proposals, 1, DataType::U32);
212 }
213
214 if(proposals->total_size() > 0)
215 {
216 ARM_COMPUTE_RETURN_ERROR_ON(proposals->num_dimensions() > 2);
217 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(0) != size_t(values_per_roi) + 1);
218 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(1) != size_t(total_num_anchors));
219 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(proposals, deltas);
220 }
221
222 if(scores_out->total_size() > 0)
223 {
224 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->num_dimensions() > 1);
225 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->dimension(0) != size_t(total_num_anchors));
226 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores_out, scores);
227 }
228
229 return Status{};
230}
231
232void NEGenerateProposalsLayer::run()
233{
234 // Acquire all the temporaries
235 MemoryGroupResourceScope scope_mg(_memory_group);
236
237 // Compute all the anchors
238 NEScheduler::get().schedule(&_compute_anchors_kernel, Window::DimY);
239
240 // Transpose and reshape the inputs
241 if(!_is_nhwc)
242 {
243 NEScheduler::get().schedule(&_permute_deltas_kernel, Window::DimY);
244 NEScheduler::get().schedule(&_permute_scores_kernel, Window::DimY);
245 }
246
247 NEScheduler::get().schedule(&_flatten_deltas_kernel, Window::DimY);
248 NEScheduler::get().schedule(&_flatten_scores_kernel, Window::DimY);
249
250 // Build the boxes
251 NEScheduler::get().schedule(&_bounding_box_kernel, Window::DimY);
252
253 // Non maxima suppression
254 CPPScheduler::get().schedule(&_cpp_nms_kernel, Window::DimX);
255
256 // Add dummy batch indexes
Michele Di Giorgio4c268b92019-09-20 14:01:48 +0100257 NEScheduler::get().schedule(&_pad_kernel, Window::DimY);
Pablo Telloc9564cb2019-09-13 10:20:25 +0100258}
259} // namespace arm_compute