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) 2016-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/Pyramid.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/PyramidInfo.h" |
| 28 | #include "arm_compute/core/TensorInfo.h" |
| 29 | #include "arm_compute/core/TensorShape.h" |
| 30 | |
| 31 | #include <cmath> |
| 32 | |
| 33 | using namespace arm_compute; |
| 34 | |
| 35 | void Pyramid::init(const PyramidInfo &info) |
| 36 | { |
| 37 | internal_init(info, false); |
| 38 | } |
| 39 | |
| 40 | void Pyramid::init_auto_padding(const PyramidInfo &info) |
| 41 | { |
| 42 | internal_init(info, true); |
| 43 | } |
| 44 | |
| 45 | void Pyramid::internal_init(const PyramidInfo &info, bool auto_padding) |
| 46 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 47 | _info = info; |
| 48 | _pyramid.resize(_info.num_levels()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | |
| 50 | size_t w = _info.width(); |
| 51 | size_t h = _info.height(); |
| 52 | size_t ref_w = w; |
| 53 | size_t ref_h = h; |
| 54 | bool is_orb_scale = (SCALE_PYRAMID_ORB == _info.scale()); |
| 55 | TensorShape tensor_shape = _info.tensor_shape(); |
| 56 | |
| 57 | // Note: Look-up table used by the OpenVX sample implementation |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 58 | const std::array<float, 4> c_orbscale = { 0.5f, |
| 59 | SCALE_PYRAMID_ORB, |
| 60 | SCALE_PYRAMID_ORB * SCALE_PYRAMID_ORB, |
| 61 | SCALE_PYRAMID_ORB *SCALE_PYRAMID_ORB * SCALE_PYRAMID_ORB |
| 62 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | |
| 64 | for(size_t i = 0; i < _info.num_levels(); ++i) |
| 65 | { |
| 66 | TensorInfo tensor_info(tensor_shape, _info.format()); |
| 67 | |
| 68 | if(auto_padding) |
| 69 | { |
| 70 | tensor_info.auto_padding(); |
| 71 | } |
| 72 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 73 | _pyramid[i].allocator()->init(tensor_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | |
| 75 | if(is_orb_scale) |
| 76 | { |
| 77 | float orb_scale = c_orbscale[(i + 1) % 4]; |
| 78 | w = static_cast<int>(std::ceil(static_cast<float>(ref_w) * orb_scale)); |
| 79 | h = static_cast<int>(std::ceil(static_cast<float>(ref_h) * orb_scale)); |
| 80 | |
| 81 | if(0 == ((i + 1) % 4)) |
| 82 | { |
| 83 | ref_w = w; |
| 84 | ref_h = h; |
| 85 | } |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | w = (w + 1) * _info.scale(); |
| 90 | h = (h + 1) * _info.scale(); |
| 91 | } |
| 92 | |
| 93 | // Update tensor_shape |
| 94 | tensor_shape.set(0, w); |
| 95 | tensor_shape.set(1, h); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void Pyramid::allocate() |
| 100 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | for(size_t i = 0; i < _info.num_levels(); ++i) |
| 102 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 103 | _pyramid[i].allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | const PyramidInfo *Pyramid::info() const |
| 108 | { |
| 109 | return &_info; |
| 110 | } |
| 111 | |
| 112 | Tensor *Pyramid::get_pyramid_level(size_t index) const |
| 113 | { |
| 114 | ARM_COMPUTE_ERROR_ON(index >= _info.num_levels()); |
| 115 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 116 | return &_pyramid[index]; |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 117 | } |