blob: c76ec6769ece82787ff8b45a72941ac781180b21 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/core/CL/kernels/CLCannyEdgeKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000033#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35using namespace arm_compute;
36
37CLGradientKernel::CLGradientKernel()
38 : _gx(nullptr), _gy(nullptr), _magnitude(nullptr), _phase(nullptr)
39{
40}
41
42void CLGradientKernel::configure(const ICLTensor *gx, const ICLTensor *gy, ICLTensor *magnitude, ICLTensor *phase, int32_t norm_type)
43{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010044 configure(CLKernelLibrary::get().get_compile_context(), gx, gy, magnitude, phase, norm_type);
45}
46
Manuel Bottini256c0b92020-04-21 13:29:30 +010047void CLGradientKernel::configure(const CLCompileContext &compile_context, const ICLTensor *gx, const ICLTensor *gy, ICLTensor *magnitude, ICLTensor *phase, int32_t norm_type)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010048{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gx, 1, DataType::S16, DataType::S32);
50 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gy, 1, DataType::S16, DataType::S32);
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
53 ARM_COMPUTE_ERROR_ON_MSG(data_size_from_type(gx->info()->data_type()) != data_size_from_type(gy->info()->data_type()),
54 "Gx and Gy must have the same pixel size");
55 ARM_COMPUTE_ERROR_ON_MSG(data_size_from_type(gx->info()->data_type()) != data_size_from_type(magnitude->info()->data_type()),
56 "Mag must have the same pixel size as Gx and Gy");
57
58 _gx = gx;
59 _gy = gy;
60 _magnitude = magnitude;
61 _phase = phase;
62
63 // Create build opts
64 std::set<std::string> built_opts;
65 built_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(gx->info()->data_type()));
66 built_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(gx->info()->data_type()));
67
68 // Create kernel
69 const std::string kernel_name = (norm_type == 1) ? std::string("combine_gradients_L1") : std::string("combine_gradients_L2");
Manuel Bottini4c6bd512020-04-08 10:15:51 +010070 _kernel = create_kernel(compile_context, kernel_name, built_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
72 // Configure kernel window
73 constexpr unsigned int num_elems_processed_per_iteration = 4;
74
75 Window win = calculate_max_window(*_gx->info(), Steps(num_elems_processed_per_iteration));
76
77 AccessWindowHorizontal gx_access(_gx->info(), 0, num_elems_processed_per_iteration);
78 AccessWindowHorizontal gy_access(_gy->info(), 0, num_elems_processed_per_iteration);
79 AccessWindowHorizontal mag_access(_magnitude->info(), 0, num_elems_processed_per_iteration);
80 AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
81
82 update_window_and_padding(win, gx_access, gy_access, mag_access, phase_access);
83
84 mag_access.set_valid_region(win, _gx->info()->valid_region());
85 phase_access.set_valid_region(win, _gx->info()->valid_region());
86
Anthony Barbierb6eb3532018-08-08 13:20:04 +010087 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010088
89 // Set config_id for enabling LWS tuning
90 _config_id = kernel_name;
91 _config_id += "_";
92 _config_id += lower_string(string_from_data_type(gx->info()->data_type()));
93 _config_id += "_";
94 _config_id += support::cpp11::to_string(gx->info()->dimension(0));
95 _config_id += "_";
96 _config_id += support::cpp11::to_string(gx->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097}
98
99void CLGradientKernel::run(const Window &window, cl::CommandQueue &queue)
100{
101 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
102 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
103
104 Window slice = window.first_slice_window_2D();
105 do
106 {
107 unsigned int idx = 0;
108 add_2D_tensor_argument(idx, _gx, slice);
109 add_2D_tensor_argument(idx, _gy, slice);
110 add_2D_tensor_argument(idx, _magnitude, slice);
111 add_2D_tensor_argument(idx, _phase, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100112 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 }
114 while(window.slide_window_slice_2D(slice));
115}
116
117CLEdgeNonMaxSuppressionKernel::CLEdgeNonMaxSuppressionKernel()
118 : _magnitude(nullptr), _phase(nullptr), _output(nullptr)
119{
120}
121
122BorderSize CLEdgeNonMaxSuppressionKernel::border_size() const
123{
124 return BorderSize(1);
125}
126
127void CLEdgeNonMaxSuppressionKernel::configure(const ICLTensor *magnitude, const ICLTensor *phase, ICLTensor *output, int32_t lower_thr, bool border_undefined)
128{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100129 configure(CLKernelLibrary::get().get_compile_context(), magnitude, phase, output, lower_thr, border_undefined);
130}
131
Manuel Bottini256c0b92020-04-21 13:29:30 +0100132void CLEdgeNonMaxSuppressionKernel::configure(const CLCompileContext &compile_context, const ICLTensor *magnitude, const ICLTensor *phase, ICLTensor *output, int32_t lower_thr, bool border_undefined)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100133{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
135 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
136 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U16, DataType::U32);
137
138 _magnitude = magnitude;
139 _phase = phase;
140 _output = output;
141
142 // Create build opts
143 std::set<std::string> built_opts;
144 built_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(magnitude->info()->data_type()));
145 built_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
146
147 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100148 const std::string kernel_name = std::string("suppress_non_maximum");
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100149 _kernel = create_kernel(compile_context, kernel_name, built_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150
151 // Set minimum threshold argument
152 unsigned int idx = 3 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
153 _kernel.setArg(idx++, lower_thr);
154
155 // Configure kernel window
156 constexpr unsigned int num_elems_processed_per_iteration = 1;
157 constexpr unsigned int num_elems_read_written_per_iteration = 3;
158
159 Window win = calculate_max_window(*_magnitude->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
160
161 AccessWindowRectangle mag_access(_magnitude->info(), -border_size().left, -border_size().top,
162 num_elems_read_written_per_iteration, num_elems_read_written_per_iteration);
163 AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
164 AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);
165
166 update_window_and_padding(win, mag_access, phase_access, output_access);
167
168 output_access.set_valid_region(win, _magnitude->info()->valid_region(), border_undefined, border_size());
169
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100170 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100171
172 // Set config_id for enabling LWS tuning
173 _config_id = kernel_name;
174 _config_id += "_";
175 _config_id += lower_string(string_from_data_type(output->info()->data_type()));
176 _config_id += "_";
177 _config_id += support::cpp11::to_string(output->info()->dimension(0));
178 _config_id += "_";
179 _config_id += support::cpp11::to_string(output->info()->dimension(1));
180 _config_id += "_";
181 _config_id += support::cpp11::to_string(border_undefined);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182}
183
184void CLEdgeNonMaxSuppressionKernel::run(const Window &window, cl::CommandQueue &queue)
185{
186 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
187 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
188
189 Window slice = window.first_slice_window_2D();
190 do
191 {
192 unsigned int idx = 0;
193 add_2D_tensor_argument(idx, _magnitude, slice);
194 add_2D_tensor_argument(idx, _phase, slice);
195 add_2D_tensor_argument(idx, _output, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100196 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197 }
198 while(window.slide_window_slice_2D(slice));
199}
200
201CLEdgeTraceKernel::CLEdgeTraceKernel()
202 : _input(nullptr), _output(nullptr), _lower_thr(0), _upper_thr(0), _visited(nullptr), _recorded(nullptr), _l1_stack(nullptr), _l1_stack_counter(nullptr)
203{
204}
205
206void CLEdgeTraceKernel::configure(const ICLTensor *input, ICLTensor *output, int32_t upper_thr, int32_t lower_thr,
207 ICLTensor *visited, ICLTensor *recorded, ICLTensor *l1_stack, ICLTensor *l1_stack_counter)
208{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100209 configure(CLKernelLibrary::get().get_compile_context(), input, output, upper_thr, lower_thr, visited, recorded, l1_stack, l1_stack_counter);
210}
211
Manuel Bottini256c0b92020-04-21 13:29:30 +0100212void CLEdgeTraceKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, int32_t upper_thr, int32_t lower_thr,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100213 ICLTensor *visited, ICLTensor *recorded, ICLTensor *l1_stack, ICLTensor *l1_stack_counter)
214{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::U32);
216 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
217 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(visited, 1, DataType::U32);
218 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(recorded, 1, DataType::U32);
219 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(l1_stack, 1, DataType::S32);
220 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(l1_stack_counter, 1, DataType::U8);
221
222 _input = input;
223 _output = output;
224 _lower_thr = lower_thr;
225 _upper_thr = upper_thr;
226 _visited = visited;
227 _recorded = recorded;
228 _l1_stack = l1_stack;
229 _l1_stack_counter = l1_stack_counter;
230
231 // Create build opts
232 std::set<std::string> built_opts;
233 built_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(input->info()->data_type()));
234 built_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
235
236 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100237 const std::string kernel_name = std::string("hysteresis");
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100238 _kernel = create_kernel(compile_context, kernel_name, built_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239
240 // Set constant kernel args
241 unsigned int width = _input->info()->dimension(0);
242 unsigned int height = _input->info()->dimension(1);
243 unsigned int idx = 6 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
244 _kernel.setArg(idx++, static_cast<cl_uint>(_lower_thr));
245 _kernel.setArg(idx++, static_cast<cl_uint>(_upper_thr));
246 _kernel.setArg(idx++, static_cast<cl_uint>(width));
247 _kernel.setArg(idx++, static_cast<cl_uint>(height));
248
249 // Configure kernel window
250 constexpr unsigned int num_elems_processed_per_iteration = 1;
251 Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration));
252
253 AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);
254 AccessWindowHorizontal visited_access(_visited->info(), 0, num_elems_processed_per_iteration);
255 AccessWindowHorizontal recorded_access(_recorded->info(), 0, num_elems_processed_per_iteration);
256 AccessWindowHorizontal l1_stack_access(_l1_stack->info(), 0, num_elems_processed_per_iteration);
257 AccessWindowHorizontal l1_stack_counter_access(_l1_stack_counter->info(), 0, num_elems_processed_per_iteration);
258
259 update_window_and_padding(win,
260 AccessWindowHorizontal(_input->info(), 0, num_elems_processed_per_iteration),
261 output_access,
262 visited_access,
263 recorded_access,
264 l1_stack_access,
265 l1_stack_counter_access);
266
267 output_access.set_valid_region(win, _input->info()->valid_region());
268 visited_access.set_valid_region(win, _input->info()->valid_region());
269 recorded_access.set_valid_region(win, _input->info()->valid_region());
270 l1_stack_access.set_valid_region(win, _input->info()->valid_region());
271 l1_stack_counter_access.set_valid_region(win, _input->info()->valid_region());
272
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100273 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100274
275 // Set config_id for enabling LWS tuning
276 _config_id = kernel_name;
277 _config_id += "_";
278 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
279 _config_id += "_";
280 _config_id += support::cpp11::to_string(input->info()->dimension(0));
281 _config_id += "_";
282 _config_id += support::cpp11::to_string(input->info()->dimension(1));
283 _config_id += "_";
284 _config_id += lower_string(string_from_format(output->info()->format()));
285 _config_id += "_";
286 _config_id += support::cpp11::to_string(output->info()->dimension(0));
287 _config_id += "_";
288 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289}
290
291void CLEdgeTraceKernel::run(const Window &window, cl::CommandQueue &queue)
292{
293 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
294 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
295
296 Window slice = window.first_slice_window_2D();
297 do
298 {
299 unsigned int idx = 0;
300 add_2D_tensor_argument(idx, _input, slice);
301 add_2D_tensor_argument(idx, _output, slice);
302 add_2D_tensor_argument(idx, _visited, slice);
303 add_2D_tensor_argument(idx, _recorded, slice);
304 add_2D_tensor_argument(idx, _l1_stack, slice);
305 add_2D_tensor_argument(idx, _l1_stack_counter, slice);
306
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100307 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308 }
309 while(window.slide_window_slice_2D(slice));
310}