blob: 08bfe90892a561adee7e16901e65029e211919db [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * 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"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000032#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34using namespace arm_compute;
35
36CLGradientKernel::CLGradientKernel()
37 : _gx(nullptr), _gy(nullptr), _magnitude(nullptr), _phase(nullptr)
38{
39}
40
41void CLGradientKernel::configure(const ICLTensor *gx, const ICLTensor *gy, ICLTensor *magnitude, ICLTensor *phase, int32_t norm_type)
42{
43 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gx, 1, DataType::S16, DataType::S32);
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gy, 1, DataType::S16, DataType::S32);
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
46 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
47 ARM_COMPUTE_ERROR_ON_MSG(data_size_from_type(gx->info()->data_type()) != data_size_from_type(gy->info()->data_type()),
48 "Gx and Gy must have the same pixel size");
49 ARM_COMPUTE_ERROR_ON_MSG(data_size_from_type(gx->info()->data_type()) != data_size_from_type(magnitude->info()->data_type()),
50 "Mag must have the same pixel size as Gx and Gy");
51
52 _gx = gx;
53 _gy = gy;
54 _magnitude = magnitude;
55 _phase = phase;
56
57 // Create build opts
58 std::set<std::string> built_opts;
59 built_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(gx->info()->data_type()));
60 built_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(gx->info()->data_type()));
61
62 // Create kernel
63 const std::string kernel_name = (norm_type == 1) ? std::string("combine_gradients_L1") : std::string("combine_gradients_L2");
64 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, built_opts));
65
66 // Configure kernel window
67 constexpr unsigned int num_elems_processed_per_iteration = 4;
68
69 Window win = calculate_max_window(*_gx->info(), Steps(num_elems_processed_per_iteration));
70
71 AccessWindowHorizontal gx_access(_gx->info(), 0, num_elems_processed_per_iteration);
72 AccessWindowHorizontal gy_access(_gy->info(), 0, num_elems_processed_per_iteration);
73 AccessWindowHorizontal mag_access(_magnitude->info(), 0, num_elems_processed_per_iteration);
74 AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
75
76 update_window_and_padding(win, gx_access, gy_access, mag_access, phase_access);
77
78 mag_access.set_valid_region(win, _gx->info()->valid_region());
79 phase_access.set_valid_region(win, _gx->info()->valid_region());
80
Anthony Barbierb6eb3532018-08-08 13:20:04 +010081 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010082
83 // Set config_id for enabling LWS tuning
84 _config_id = kernel_name;
85 _config_id += "_";
86 _config_id += lower_string(string_from_data_type(gx->info()->data_type()));
87 _config_id += "_";
88 _config_id += support::cpp11::to_string(gx->info()->dimension(0));
89 _config_id += "_";
90 _config_id += support::cpp11::to_string(gx->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091}
92
93void CLGradientKernel::run(const Window &window, cl::CommandQueue &queue)
94{
95 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
96 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
97
98 Window slice = window.first_slice_window_2D();
99 do
100 {
101 unsigned int idx = 0;
102 add_2D_tensor_argument(idx, _gx, slice);
103 add_2D_tensor_argument(idx, _gy, slice);
104 add_2D_tensor_argument(idx, _magnitude, slice);
105 add_2D_tensor_argument(idx, _phase, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100106 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 }
108 while(window.slide_window_slice_2D(slice));
109}
110
111CLEdgeNonMaxSuppressionKernel::CLEdgeNonMaxSuppressionKernel()
112 : _magnitude(nullptr), _phase(nullptr), _output(nullptr)
113{
114}
115
116BorderSize CLEdgeNonMaxSuppressionKernel::border_size() const
117{
118 return BorderSize(1);
119}
120
121void CLEdgeNonMaxSuppressionKernel::configure(const ICLTensor *magnitude, const ICLTensor *phase, ICLTensor *output, int32_t lower_thr, bool border_undefined)
122{
123 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
124 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
125 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U16, DataType::U32);
126
127 _magnitude = magnitude;
128 _phase = phase;
129 _output = output;
130
131 // Create build opts
132 std::set<std::string> built_opts;
133 built_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(magnitude->info()->data_type()));
134 built_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
135
136 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100137 const std::string kernel_name = std::string("suppress_non_maximum");
138 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, built_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139
140 // Set minimum threshold argument
141 unsigned int idx = 3 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
142 _kernel.setArg(idx++, lower_thr);
143
144 // Configure kernel window
145 constexpr unsigned int num_elems_processed_per_iteration = 1;
146 constexpr unsigned int num_elems_read_written_per_iteration = 3;
147
148 Window win = calculate_max_window(*_magnitude->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
149
150 AccessWindowRectangle mag_access(_magnitude->info(), -border_size().left, -border_size().top,
151 num_elems_read_written_per_iteration, num_elems_read_written_per_iteration);
152 AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
153 AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);
154
155 update_window_and_padding(win, mag_access, phase_access, output_access);
156
157 output_access.set_valid_region(win, _magnitude->info()->valid_region(), border_undefined, border_size());
158
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100159 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100160
161 // Set config_id for enabling LWS tuning
162 _config_id = kernel_name;
163 _config_id += "_";
164 _config_id += lower_string(string_from_data_type(output->info()->data_type()));
165 _config_id += "_";
166 _config_id += support::cpp11::to_string(output->info()->dimension(0));
167 _config_id += "_";
168 _config_id += support::cpp11::to_string(output->info()->dimension(1));
169 _config_id += "_";
170 _config_id += support::cpp11::to_string(border_undefined);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171}
172
173void CLEdgeNonMaxSuppressionKernel::run(const Window &window, cl::CommandQueue &queue)
174{
175 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
176 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
177
178 Window slice = window.first_slice_window_2D();
179 do
180 {
181 unsigned int idx = 0;
182 add_2D_tensor_argument(idx, _magnitude, slice);
183 add_2D_tensor_argument(idx, _phase, slice);
184 add_2D_tensor_argument(idx, _output, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100185 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186 }
187 while(window.slide_window_slice_2D(slice));
188}
189
190CLEdgeTraceKernel::CLEdgeTraceKernel()
191 : _input(nullptr), _output(nullptr), _lower_thr(0), _upper_thr(0), _visited(nullptr), _recorded(nullptr), _l1_stack(nullptr), _l1_stack_counter(nullptr)
192{
193}
194
195void CLEdgeTraceKernel::configure(const ICLTensor *input, ICLTensor *output, int32_t upper_thr, int32_t lower_thr,
196 ICLTensor *visited, ICLTensor *recorded, ICLTensor *l1_stack, ICLTensor *l1_stack_counter)
197{
198 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::U32);
199 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
200 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(visited, 1, DataType::U32);
201 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(recorded, 1, DataType::U32);
202 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(l1_stack, 1, DataType::S32);
203 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(l1_stack_counter, 1, DataType::U8);
204
205 _input = input;
206 _output = output;
207 _lower_thr = lower_thr;
208 _upper_thr = upper_thr;
209 _visited = visited;
210 _recorded = recorded;
211 _l1_stack = l1_stack;
212 _l1_stack_counter = l1_stack_counter;
213
214 // Create build opts
215 std::set<std::string> built_opts;
216 built_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(input->info()->data_type()));
217 built_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
218
219 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100220 const std::string kernel_name = std::string("hysteresis");
221 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, built_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222
223 // Set constant kernel args
224 unsigned int width = _input->info()->dimension(0);
225 unsigned int height = _input->info()->dimension(1);
226 unsigned int idx = 6 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
227 _kernel.setArg(idx++, static_cast<cl_uint>(_lower_thr));
228 _kernel.setArg(idx++, static_cast<cl_uint>(_upper_thr));
229 _kernel.setArg(idx++, static_cast<cl_uint>(width));
230 _kernel.setArg(idx++, static_cast<cl_uint>(height));
231
232 // Configure kernel window
233 constexpr unsigned int num_elems_processed_per_iteration = 1;
234 Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration));
235
236 AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);
237 AccessWindowHorizontal visited_access(_visited->info(), 0, num_elems_processed_per_iteration);
238 AccessWindowHorizontal recorded_access(_recorded->info(), 0, num_elems_processed_per_iteration);
239 AccessWindowHorizontal l1_stack_access(_l1_stack->info(), 0, num_elems_processed_per_iteration);
240 AccessWindowHorizontal l1_stack_counter_access(_l1_stack_counter->info(), 0, num_elems_processed_per_iteration);
241
242 update_window_and_padding(win,
243 AccessWindowHorizontal(_input->info(), 0, num_elems_processed_per_iteration),
244 output_access,
245 visited_access,
246 recorded_access,
247 l1_stack_access,
248 l1_stack_counter_access);
249
250 output_access.set_valid_region(win, _input->info()->valid_region());
251 visited_access.set_valid_region(win, _input->info()->valid_region());
252 recorded_access.set_valid_region(win, _input->info()->valid_region());
253 l1_stack_access.set_valid_region(win, _input->info()->valid_region());
254 l1_stack_counter_access.set_valid_region(win, _input->info()->valid_region());
255
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100256 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100257
258 // Set config_id for enabling LWS tuning
259 _config_id = kernel_name;
260 _config_id += "_";
261 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
262 _config_id += "_";
263 _config_id += support::cpp11::to_string(input->info()->dimension(0));
264 _config_id += "_";
265 _config_id += support::cpp11::to_string(input->info()->dimension(1));
266 _config_id += "_";
267 _config_id += lower_string(string_from_format(output->info()->format()));
268 _config_id += "_";
269 _config_id += support::cpp11::to_string(output->info()->dimension(0));
270 _config_id += "_";
271 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272}
273
274void CLEdgeTraceKernel::run(const Window &window, cl::CommandQueue &queue)
275{
276 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
277 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
278
279 Window slice = window.first_slice_window_2D();
280 do
281 {
282 unsigned int idx = 0;
283 add_2D_tensor_argument(idx, _input, slice);
284 add_2D_tensor_argument(idx, _output, slice);
285 add_2D_tensor_argument(idx, _visited, slice);
286 add_2D_tensor_argument(idx, _recorded, slice);
287 add_2D_tensor_argument(idx, _l1_stack, slice);
288 add_2D_tensor_argument(idx, _l1_stack_counter, slice);
289
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100290 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100291 }
292 while(window.slide_window_slice_2D(slice));
293}