blob: 80ed0e55a409241f71338b8ea6fbae5c1c18e648 [file] [log] [blame]
giuros01cd96a262018-10-03 12:44:35 +01001/*
2 * Copyright (c) 2018 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/CL/functions/CLGenerateProposalsLayer.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/Types.h"
28#include "support/ToolchainSupport.h"
29
30namespace arm_compute
31{
32CLGenerateProposalsLayer::CLGenerateProposalsLayer(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(),
40 _memset_kernel(),
41 _padded_copy_kernel(),
42 _cpp_nms_kernel(),
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 CLGenerateProposalsLayer::configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals,
58 const GenerateProposalsInfo &info)
59{
60 const DataType data_type = deltas->info()->data_type();
61 const int num_anchors = scores->info()->dimension(2);
62 const int feat_width = scores->info()->dimension(0);
63 const int feat_height = scores->info()->dimension(1);
64 const int total_num_anchors = num_anchors * feat_width * feat_height;
65 const int pre_nms_topN = info.pre_nms_topN();
66 const int post_nms_topN = info.post_nms_topN();
67 const size_t values_per_roi = info.values_per_roi();
68
69 // Compute all the anchors
70 _memory_group.manage(&_all_anchors);
71 _compute_anchors_kernel.configure(anchors, &_all_anchors, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale()));
72
73 const TensorShape flatten_shape_deltas(values_per_roi, total_num_anchors);
74 _deltas_flattened.allocator()->init(TensorInfo(flatten_shape_deltas, 1, data_type));
75
76 // Permute and reshape deltas
77 _memory_group.manage(&_deltas_permuted);
78 _memory_group.manage(&_deltas_flattened);
79 _permute_deltas_kernel.configure(deltas, &_deltas_permuted, PermutationVector{ 2, 0, 1 });
80 _flatten_deltas_kernel.configure(&_deltas_permuted, &_deltas_flattened);
81 _deltas_permuted.allocator()->allocate();
82
83 const TensorShape flatten_shape_scores(1, total_num_anchors);
84 _scores_flattened.allocator()->init(TensorInfo(flatten_shape_scores, 1, data_type));
85
86 // Permute and reshape scores
87 _memory_group.manage(&_scores_permuted);
88 _memory_group.manage(&_scores_flattened);
89 _permute_scores_kernel.configure(scores, &_scores_permuted, PermutationVector{ 2, 0, 1 });
90 _flatten_scores_kernel.configure(&_scores_permuted, &_scores_flattened);
91 _scores_permuted.allocator()->allocate();
92
93 // Bounding box transform
94 _memory_group.manage(&_all_proposals);
95 BoundingBoxTransformInfo bbox_info(info.im_width(), info.im_height(), 1.f);
96 _bounding_box_kernel.configure(&_all_anchors, &_all_proposals, &_deltas_flattened, bbox_info);
97 _deltas_flattened.allocator()->allocate();
98 _all_anchors.allocator()->allocate();
99
100 // The original layer implementation first selects the best pre_nms_topN anchors (thus having a lightweight sort)
101 // that are then transformed by bbox_transform. The boxes generated are then fed into a non-sorting NMS operation.
102 // 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)
103 // and the filtering
104 const int scores_nms_size = std::min<int>(std::min<int>(post_nms_topN, pre_nms_topN), total_num_anchors);
105 const float min_size_scaled = info.min_size() * info.im_scale();
106 _memory_group.manage(&_classes_nms_unused);
107 _memory_group.manage(&_keeps_nms_unused);
108
109 // Note that NMS needs outputs preinitialized.
110 auto_init_if_empty(*scores_out->info(), TensorShape(scores_nms_size), 1, data_type);
111 auto_init_if_empty(*_proposals_4_roi_values.info(), TensorShape(values_per_roi, scores_nms_size), 1, data_type);
112 auto_init_if_empty(*num_valid_proposals->info(), TensorShape(values_per_roi, scores_nms_size), 1, data_type);
113
114 // Initialize temporaries (unused) outputs
115 _classes_nms_unused.allocator()->init(TensorInfo(TensorShape(1, 1), 1, data_type));
116 _keeps_nms_unused.allocator()->init(*scores_out->info());
117
118 // Save the output (to map and unmap them at run)
119 _scores_out = scores_out;
120 _num_valid_proposals = num_valid_proposals;
121
122 _memory_group.manage(&_proposals_4_roi_values);
123 _cpp_nms_kernel.configure(&_scores_flattened, &_all_proposals, nullptr, scores_out, &_proposals_4_roi_values, &_classes_nms_unused, nullptr, &_keeps_nms_unused, num_valid_proposals,
124 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()));
125 _keeps_nms_unused.allocator()->allocate();
126 _classes_nms_unused.allocator()->allocate();
127 _all_proposals.allocator()->allocate();
128 _scores_flattened.allocator()->allocate();
129
130 // Add the first column that represents the batch id. This will be all zeros, as we don't support multiple images
131 _padded_copy_kernel.configure(&_proposals_4_roi_values, proposals, PaddingList{ { 1, 0 } });
132 _proposals_4_roi_values.allocator()->allocate();
133
134 _memset_kernel.configure(proposals, PixelValue());
135}
136
137Status CLGenerateProposalsLayer::validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out,
138 const ITensorInfo *num_valid_proposals, const GenerateProposalsInfo &info)
139{
140 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(proposals, scores_out, num_valid_proposals);
141
142 const int num_anchors = scores->dimension(2);
143 const int feat_width = scores->dimension(0);
144 const int feat_height = scores->dimension(1);
145 const int num_images = scores->dimension(3);
146 const int total_num_anchors = num_anchors * feat_width * feat_height;
147 const int values_per_roi = info.values_per_roi();
148
149 ARM_COMPUTE_RETURN_ERROR_ON(num_images > 1);
150
151 TensorInfo all_anchors_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
152 ARM_COMPUTE_RETURN_ON_ERROR(CLComputeAllAnchorsKernel::validate(anchors, &all_anchors_info, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale())));
153
154 TensorInfo deltas_permuted_info = deltas->clone()->set_tensor_shape(TensorShape(values_per_roi * num_anchors, feat_width, feat_height)).set_is_resizable(true);
155 ARM_COMPUTE_RETURN_ON_ERROR(CLPermuteKernel::validate(deltas, &deltas_permuted_info, PermutationVector{ 2, 0, 1 }));
156
157 TensorInfo deltas_flattened_info(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
158 ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayerKernel::validate(&deltas_permuted_info, &deltas_flattened_info));
159
160 TensorInfo scores_permuted_info = scores->clone()->set_tensor_shape(TensorShape(num_anchors, feat_width, feat_height)).set_is_resizable(true);
161 ARM_COMPUTE_RETURN_ON_ERROR(CLPermuteKernel::validate(scores, &scores_permuted_info, PermutationVector{ 2, 0, 1 }));
162
163 TensorInfo scores_flattened_info(deltas->clone()->set_tensor_shape(TensorShape(1, total_num_anchors)).set_is_resizable(true));
164 TensorInfo proposals_4_roi_values(proposals->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true));
165
166 ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayerKernel::validate(&scores_permuted_info, &scores_flattened_info));
167 ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_info, &proposals_4_roi_values, &deltas_flattened_info, BoundingBoxTransformInfo(info.im_width(), info.im_height(),
168 1.f)));
169
170 ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(&proposals_4_roi_values, proposals, PaddingList{ { 0, 1 } }));
171 ARM_COMPUTE_RETURN_ON_ERROR(CLMemsetKernel::validate(proposals, PixelValue()));
172
173 if(num_valid_proposals->total_size() > 0)
174 {
175 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->num_dimensions() > 1);
176 ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->dimension(0) > 1);
177 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(num_valid_proposals, DataType::U32);
178 }
179
180 if(proposals->total_size() > 0)
181 {
182 ARM_COMPUTE_RETURN_ERROR_ON(proposals->num_dimensions() > 2);
183 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(0) != size_t(values_per_roi) + 1);
184 ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(1) != size_t(total_num_anchors));
185 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(proposals, deltas);
186 }
187
188 if(scores_out->total_size() > 0)
189 {
190 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->num_dimensions() > 1);
191 ARM_COMPUTE_RETURN_ERROR_ON(scores_out->dimension(0) != size_t(total_num_anchors));
192 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores_out, scores);
193 }
194
195 return Status{};
196}
197
198void CLGenerateProposalsLayer::run_cpp_nms_kernel()
199{
200 // Map inputs
201 _scores_flattened.map(true);
202 _all_proposals.map(true);
203
204 // Map outputs
205 _scores_out->map(CLScheduler::get().queue(), true);
206 _proposals_4_roi_values.map(CLScheduler::get().queue(), true);
207 _num_valid_proposals->map(CLScheduler::get().queue(), true);
208 _keeps_nms_unused.map(true);
209 _classes_nms_unused.map(true);
210
211 // Run nms
212 CPPScheduler::get().schedule(&_cpp_nms_kernel, Window::DimX);
213
214 // Unmap outputs
215 _keeps_nms_unused.unmap();
216 _classes_nms_unused.unmap();
217 _scores_out->unmap(CLScheduler::get().queue());
218 _proposals_4_roi_values.unmap(CLScheduler::get().queue());
219 _num_valid_proposals->unmap(CLScheduler::get().queue());
220
221 // Unmap inputs
222 _scores_flattened.unmap();
223 _all_proposals.unmap();
224}
225
226void CLGenerateProposalsLayer::run()
227{
228 // Acquire all the temporaries
229 _memory_group.acquire();
230
231 // Compute all the anchors
232 CLScheduler::get().enqueue(_compute_anchors_kernel, false);
233
234 // Transpose and reshape the inputs
235 CLScheduler::get().enqueue(_permute_deltas_kernel, false);
236 CLScheduler::get().enqueue(_flatten_deltas_kernel, false);
237 CLScheduler::get().enqueue(_permute_scores_kernel, false);
238 CLScheduler::get().enqueue(_flatten_scores_kernel, false);
239
240 // Build the boxes
241 CLScheduler::get().enqueue(_bounding_box_kernel, false);
242 // Non maxima suppression
243 run_cpp_nms_kernel();
244 // Add dummy batch indexes
245 CLScheduler::get().enqueue(_memset_kernel, true);
246 CLScheduler::get().enqueue(_padded_copy_kernel, true);
247
248 // Release all the temporaries
249 _memory_group.release();
250}
251} // namespace arm_compute