blob: 865f389f7f95c85cf58fd34a75dea0839d2ef39a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
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/CLPyramid.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 <array>
33#include <cmath>
34
35using namespace arm_compute;
36
37CLPyramid::CLPyramid()
38 : _info(), _pyramid(nullptr)
39{
40}
41
42void CLPyramid::init(const PyramidInfo &info)
43{
44 internal_init(info, false);
45}
46
47void CLPyramid::init_auto_padding(const PyramidInfo &info)
48{
49 internal_init(info, true);
50}
51
52void CLPyramid::internal_init(const PyramidInfo &info, bool auto_padding)
53{
54 _info = info;
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010055 _pyramid = arm_compute::support::cpp14::make_unique<CLTensor[]>(_info.num_levels());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056
57 size_t w = _info.width();
58 size_t h = _info.height();
59 size_t ref_w = w;
60 size_t ref_h = h;
61 const bool is_orb_scale = (SCALE_PYRAMID_ORB == _info.scale());
62 TensorShape tensor_shape = _info.tensor_shape();
63
64 // Note: Look-up table used by the OpenVX sample implementation
65 const std::array<float, 4> c_orbscale =
66 {
67 {
68 0.5f,
69 SCALE_PYRAMID_ORB,
70 SCALE_PYRAMID_ORB * SCALE_PYRAMID_ORB,
71 SCALE_PYRAMID_ORB *SCALE_PYRAMID_ORB * SCALE_PYRAMID_ORB
72 }
73 };
74
75 for(size_t i = 0; i < _info.num_levels(); ++i)
76 {
77 TensorInfo tensor_info(tensor_shape, _info.format());
78
79 if(auto_padding)
80 {
81 tensor_info.auto_padding();
82 }
83
84 _pyramid[i].allocator()->init(tensor_info);
85
86 if(is_orb_scale)
87 {
88 const float orb_scale = c_orbscale[(i + 1) % 4];
89 w = std::ceil(ref_w * orb_scale);
90 h = std::ceil(ref_h * orb_scale);
91
92 if(0 == ((i + 1) % 4))
93 {
94 ref_w = w;
95 ref_h = h;
96 }
97 }
98 else
99 {
100 w = (w + 1) * _info.scale();
101 h = (h + 1) * _info.scale();
102 }
103
104 // Update tensor_shape
105 tensor_shape.set(0, w);
106 tensor_shape.set(1, h);
107 }
108}
109
110void CLPyramid::allocate()
111{
112 ARM_COMPUTE_ERROR_ON(_pyramid == nullptr);
113
114 for(size_t i = 0; i < _info.num_levels(); ++i)
115 {
116 (_pyramid.get() + i)->allocator()->allocate();
117 }
118}
119
120const PyramidInfo *CLPyramid::info() const
121{
122 return &_info;
123}
124
125CLTensor *CLPyramid::get_pyramid_level(size_t index) const
126{
127 ARM_COMPUTE_ERROR_ON(index >= _info.num_levels());
128
129 return (_pyramid.get() + index);
130}