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