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/runtime/CL/functions/CLHOGDescriptor.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/HOGInfo.h" |
| 28 | #include "arm_compute/core/Size2D.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 31 | #include "src/core/CL/kernels/CLFillBorderKernel.h" |
| 32 | #include "src/core/CL/kernels/CLHOGDescriptorKernel.h" |
| 33 | #include "src/core/CL/kernels/CLMagnitudePhaseKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 37 | CLHOGDescriptor::CLHOGDescriptor(std::shared_ptr<IMemoryManager> memory_manager) |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 38 | : _memory_group(std::move(memory_manager)), |
| 39 | _gradient(), |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 40 | _orient_bin(std::make_unique<CLHOGOrientationBinningKernel>()), |
| 41 | _block_norm(std::make_unique<CLHOGBlockNormalizationKernel>()), |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 42 | _mag(), |
| 43 | _phase(), |
| 44 | _hog_space() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 48 | CLHOGDescriptor::~CLHOGDescriptor() = default; |
| 49 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | void CLHOGDescriptor::configure(ICLTensor *input, ICLTensor *output, const IHOG *hog, BorderMode border_mode, uint8_t constant_border_value) |
| 51 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 52 | configure(CLKernelLibrary::get().get_compile_context(), input, output, hog, border_mode, constant_border_value); |
| 53 | } |
| 54 | |
| 55 | void CLHOGDescriptor::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const IHOG *hog, BorderMode border_mode, uint8_t constant_border_value) |
| 56 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 58 | ARM_COMPUTE_ERROR_ON(nullptr == output); |
| 59 | ARM_COMPUTE_ERROR_ON(nullptr == hog); |
| 60 | |
| 61 | const HOGInfo *hog_info = hog->info(); |
| 62 | const size_t width = input->info()->dimension(Window::DimX); |
| 63 | const size_t height = input->info()->dimension(Window::DimY); |
| 64 | const size_t num_bins = hog_info->num_bins(); |
| 65 | |
| 66 | Size2D cell_size = hog_info->cell_size(); |
| 67 | |
| 68 | // Calculate number of cells along the x and y directions for the hog_space |
| 69 | const size_t num_cells_x = width / cell_size.width; |
| 70 | const size_t num_cells_y = height / cell_size.height; |
| 71 | |
| 72 | // TensorShape of the input image |
| 73 | const TensorShape &shape_img = input->info()->tensor_shape(); |
| 74 | |
| 75 | // TensorShape of the hog space |
| 76 | TensorShape shape_hog_space = input->info()->tensor_shape(); |
| 77 | shape_hog_space.set(Window::DimX, num_cells_x); |
| 78 | shape_hog_space.set(Window::DimY, num_cells_y); |
| 79 | |
| 80 | // Intitialize tensors for magnitude, phase and hog space |
| 81 | TensorInfo info_mag(shape_img, Format::S16); |
| 82 | _mag.allocator()->init(info_mag); |
| 83 | |
| 84 | TensorInfo info_phase(shape_img, Format::U8); |
| 85 | _phase.allocator()->init(info_phase); |
| 86 | |
| 87 | TensorInfo info_space(shape_hog_space, num_bins, DataType::F32); |
| 88 | _hog_space.allocator()->init(info_space); |
| 89 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 90 | // Manage intermediate buffers |
| 91 | _memory_group.manage(&_mag); |
| 92 | _memory_group.manage(&_phase); |
| 93 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | // Initialise gradient kernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 95 | _gradient.configure(compile_context, input, &_mag, &_phase, hog_info->phase_type(), border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 97 | // Manage intermediate buffers |
| 98 | _memory_group.manage(&_hog_space); |
| 99 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | // Initialise orientation binning kernel |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 101 | _orient_bin->configure(compile_context, &_mag, &_phase, &_hog_space, hog->info()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | |
| 103 | // Initialize HOG norm kernel |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 104 | _block_norm->configure(compile_context, &_hog_space, output, hog->info()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | |
| 106 | // Allocate intermediate tensors |
| 107 | _mag.allocator()->allocate(); |
| 108 | _phase.allocator()->allocate(); |
| 109 | _hog_space.allocator()->allocate(); |
| 110 | } |
| 111 | |
| 112 | void CLHOGDescriptor::run() |
| 113 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 114 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 115 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | // Run gradient |
| 117 | _gradient.run(); |
| 118 | |
| 119 | // Run orientation binning |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 120 | CLScheduler::get().enqueue(*_orient_bin, false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | |
| 122 | // Run block normalization |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 123 | CLScheduler::get().enqueue(*_block_norm); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | } |