blob: cbb952c3f69017143a1ba17d2d4f94951b4f307e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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/CL/functions/CLLaplacianReconstruct.h"
25
26#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/IPyramid.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Validate.h"
31
32#include <cstddef>
33
34using namespace arm_compute;
35
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010036CLLaplacianReconstruct::CLLaplacianReconstruct() // NOLINT
37 : _tmp_pyr(),
38 _addf(),
39 _scalef(),
40 _depthf()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
42}
43
Diego Lopez Recas0021d752017-12-18 14:42:56 +000044void CLLaplacianReconstruct::configure(const CLPyramid *pyramid, ICLTensor *input, ICLTensor *output, BorderMode border_mode, uint8_t constant_border_value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045{
Manuel Bottini2b84be52020-04-08 10:15:51 +010046 configure(CLKernelLibrary::get().get_compile_context(), pyramid, input, output, border_mode, constant_border_value);
47}
48
49void CLLaplacianReconstruct::configure(const CLCompileContext &compile_context, const CLPyramid *pyramid, ICLTensor *input, ICLTensor *output, BorderMode border_mode, uint8_t constant_border_value)
50{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 ARM_COMPUTE_ERROR_ON(nullptr == pyramid);
52 ARM_COMPUTE_ERROR_ON(input == output);
53 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S16);
54 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
55 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
56 ARM_COMPUTE_ERROR_ON(output->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
57 ARM_COMPUTE_ERROR_ON(output->info()->dimension(0) != pyramid->get_pyramid_level(0)->info()->dimension(0));
58 ARM_COMPUTE_ERROR_ON(output->info()->dimension(1) != pyramid->get_pyramid_level(0)->info()->dimension(1));
59 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->get_pyramid_level(pyramid->info()->num_levels() - 1)->info()->dimension(0));
60 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->get_pyramid_level(pyramid->info()->num_levels() - 1)->info()->dimension(1));
61
62 const size_t num_levels = pyramid->info()->num_levels();
63
64 // Create and initialize the tmp pyramid: I(n-2) = upsample( input + Laplace(n-1) )
65 PyramidInfo pyramid_info;
66 pyramid_info.init(num_levels, 0.5f, output->info()->tensor_shape(), arm_compute::Format::S16);
67 _tmp_pyr.init(pyramid_info);
68
69 // Allocate add and scale functions. Level 0 does not need to be scaled.
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010070 _addf.resize(num_levels);
71 _scalef.resize(num_levels - 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072
73 const size_t last_level = num_levels - 1;
74
Manuel Bottini2b84be52020-04-08 10:15:51 +010075 _addf[last_level].configure(compile_context, input, pyramid->get_pyramid_level(last_level), _tmp_pyr.get_pyramid_level(last_level), ConvertPolicy::SATURATE);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
77 // Scale levels n-1 to 1, and add levels n-2 to 0
78 for(size_t l = 0; l < last_level; ++l)
79 {
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010080 _scalef[l].configure(compile_context, _tmp_pyr.get_pyramid_level(l + 1), _tmp_pyr.get_pyramid_level(l), ScaleKernelInfo{ arm_compute::InterpolationPolicy::NEAREST_NEIGHBOR, border_mode, constant_border_value });
Manuel Bottini2b84be52020-04-08 10:15:51 +010081 _addf[l].configure(compile_context, _tmp_pyr.get_pyramid_level(l), pyramid->get_pyramid_level(l), _tmp_pyr.get_pyramid_level(l), ConvertPolicy::SATURATE);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 }
83
84 // Convert level 0 from S16 to U8
Manuel Bottini2b84be52020-04-08 10:15:51 +010085 _depthf.configure(compile_context, _tmp_pyr.get_pyramid_level(0), output, ConvertPolicy::SATURATE, 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
87 _tmp_pyr.allocate();
88}
89
90void CLLaplacianReconstruct::run()
91{
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010092 ARM_COMPUTE_ERROR_ON_MSG(_addf.empty(), "Unconfigured function");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
94 const size_t last_level = _tmp_pyr.info()->num_levels() - 1;
95
96 _addf[last_level].run();
97
98 // Run l = [last_level - 1, 0]
99 for(size_t l = last_level; l-- > 0;)
100 {
101 _scalef[l].run();
102 _addf[l].run();
103 }
104
105 _depthf.run();
106}