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