Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 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/NEON/functions/NEHOGDescriptor.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/NEON/NEScheduler.h" |
| 31 | |
| 32 | using namespace arm_compute; |
| 33 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 34 | NEHOGDescriptor::NEHOGDescriptor(std::shared_ptr<IMemoryManager> memory_manager) |
| 35 | : _memory_group(std::move(memory_manager)), _gradient(), _orient_bin(), _block_norm(), _mag(), _phase(), _hog_space() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | void NEHOGDescriptor::configure(ITensor *input, ITensor *output, const IHOG *hog, BorderMode border_mode, uint8_t constant_border_value) |
| 40 | { |
| 41 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 42 | ARM_COMPUTE_ERROR_ON(nullptr == output); |
| 43 | ARM_COMPUTE_ERROR_ON(nullptr == hog); |
| 44 | |
| 45 | const HOGInfo *hog_info = hog->info(); |
| 46 | const size_t width = input->info()->dimension(Window::DimX); |
| 47 | const size_t height = input->info()->dimension(Window::DimY); |
| 48 | const size_t num_bins = hog_info->num_bins(); |
| 49 | |
| 50 | Size2D cell_size = hog_info->cell_size(); |
| 51 | |
| 52 | // Calculate number of cells along the x and y directions for the hog_space |
| 53 | const size_t num_cells_x = width / cell_size.width; |
| 54 | const size_t num_cells_y = height / cell_size.height; |
| 55 | |
| 56 | // TensorShape of the input image |
| 57 | const TensorShape &shape_img = input->info()->tensor_shape(); |
| 58 | |
| 59 | // TensorShape of the hog space |
| 60 | TensorShape shape_hog_space = input->info()->tensor_shape(); |
| 61 | shape_hog_space.set(Window::DimX, num_cells_x); |
| 62 | shape_hog_space.set(Window::DimY, num_cells_y); |
| 63 | |
| 64 | // Allocate memory for magnitude, phase and hog space |
| 65 | TensorInfo info_mag(shape_img, Format::S16); |
| 66 | _mag.allocator()->init(info_mag); |
| 67 | |
| 68 | TensorInfo info_phase(shape_img, Format::U8); |
| 69 | _phase.allocator()->init(info_phase); |
| 70 | |
| 71 | TensorInfo info_space(shape_hog_space, num_bins, DataType::F32); |
| 72 | _hog_space.allocator()->init(info_space); |
| 73 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 74 | // Manage intermediate buffers |
| 75 | _memory_group.manage(&_mag); |
| 76 | _memory_group.manage(&_phase); |
| 77 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | // Initialise gradient kernel |
| 79 | _gradient.configure(input, &_mag, &_phase, hog_info->phase_type(), border_mode, constant_border_value); |
| 80 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 81 | // Manage intermediate buffers |
| 82 | _memory_group.manage(&_hog_space); |
| 83 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | // Initialise orientation binning kernel |
| 85 | _orient_bin.configure(&_mag, &_phase, &_hog_space, hog->info()); |
| 86 | |
| 87 | // Initialize HOG norm kernel |
| 88 | _block_norm.configure(&_hog_space, output, hog->info()); |
| 89 | |
| 90 | // Allocate intermediate tensors |
| 91 | _mag.allocator()->allocate(); |
| 92 | _phase.allocator()->allocate(); |
| 93 | _hog_space.allocator()->allocate(); |
| 94 | } |
| 95 | |
| 96 | void NEHOGDescriptor::run() |
| 97 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 98 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 99 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | // Run gradient |
| 101 | _gradient.run(); |
| 102 | |
| 103 | // Run orientation binning kernel |
| 104 | NEScheduler::get().schedule(&_orient_bin, Window::DimY); |
| 105 | |
| 106 | // Run block normalization kernel |
| 107 | NEScheduler::get().schedule(&_block_norm, Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | } |