blob: bc7b5501a048bc2e145ebb2ec86253eb0edb4526 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyroubcfd09a2019-05-01 13:03:59 +01002 * Copyright (c) 2016-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/runtime/Pyramid.h"
25
26#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/PyramidInfo.h"
28#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/TensorShape.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010030#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
32#include <cmath>
33
34using namespace arm_compute;
35
36void Pyramid::init(const PyramidInfo &info)
37{
38 internal_init(info, false);
39}
40
41void Pyramid::init_auto_padding(const PyramidInfo &info)
42{
43 internal_init(info, true);
44}
45
46void Pyramid::internal_init(const PyramidInfo &info, bool auto_padding)
47{
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010048 _info = info;
49 _pyramid.resize(_info.num_levels());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050
51 size_t w = _info.width();
52 size_t h = _info.height();
53 size_t ref_w = w;
54 size_t ref_h = h;
55 bool is_orb_scale = (SCALE_PYRAMID_ORB == _info.scale());
56 TensorShape tensor_shape = _info.tensor_shape();
57
58 // Note: Look-up table used by the OpenVX sample implementation
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010059 const std::array<float, 4> c_orbscale = { 0.5f,
60 SCALE_PYRAMID_ORB,
61 SCALE_PYRAMID_ORB * SCALE_PYRAMID_ORB,
62 SCALE_PYRAMID_ORB *SCALE_PYRAMID_ORB * SCALE_PYRAMID_ORB
63 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064
65 for(size_t i = 0; i < _info.num_levels(); ++i)
66 {
67 TensorInfo tensor_info(tensor_shape, _info.format());
68
69 if(auto_padding)
70 {
71 tensor_info.auto_padding();
72 }
73
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010074 _pyramid[i].allocator()->init(tensor_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075
76 if(is_orb_scale)
77 {
78 float orb_scale = c_orbscale[(i + 1) % 4];
79 w = static_cast<int>(std::ceil(static_cast<float>(ref_w) * orb_scale));
80 h = static_cast<int>(std::ceil(static_cast<float>(ref_h) * orb_scale));
81
82 if(0 == ((i + 1) % 4))
83 {
84 ref_w = w;
85 ref_h = h;
86 }
87 }
88 else
89 {
90 w = (w + 1) * _info.scale();
91 h = (h + 1) * _info.scale();
92 }
93
94 // Update tensor_shape
95 tensor_shape.set(0, w);
96 tensor_shape.set(1, h);
97 }
98}
99
100void Pyramid::allocate()
101{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 for(size_t i = 0; i < _info.num_levels(); ++i)
103 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100104 _pyramid[i].allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 }
106}
107
108const PyramidInfo *Pyramid::info() const
109{
110 return &_info;
111}
112
113Tensor *Pyramid::get_pyramid_level(size_t index) const
114{
115 ARM_COMPUTE_ERROR_ON(index >= _info.num_levels());
116
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100117 return &_pyramid[index];
118}