Compute Library
 21.02
CLLaplacianReconstruct.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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  */
25 
27 #include "arm_compute/core/Error.h"
33 
34 #include <cstddef>
35 
36 using namespace arm_compute;
37 
39  : _tmp_pyr(),
40  _addf(),
41  _scalef(),
42  _depthf()
43 {
44 }
45 
46 void CLLaplacianReconstruct::configure(const CLPyramid *pyramid, ICLTensor *input, ICLTensor *output, BorderMode border_mode, uint8_t constant_border_value)
47 {
48  configure(CLKernelLibrary::get().get_compile_context(), pyramid, input, output, border_mode, constant_border_value);
49 }
50 
51 void CLLaplacianReconstruct::configure(const CLCompileContext &compile_context, const CLPyramid *pyramid, ICLTensor *input, ICLTensor *output, BorderMode border_mode, uint8_t constant_border_value)
52 {
53  ARM_COMPUTE_ERROR_ON(nullptr == pyramid);
54  ARM_COMPUTE_ERROR_ON(input == output);
59  ARM_COMPUTE_ERROR_ON(output->info()->dimension(0) != pyramid->get_pyramid_level(0)->info()->dimension(0));
60  ARM_COMPUTE_ERROR_ON(output->info()->dimension(1) != pyramid->get_pyramid_level(0)->info()->dimension(1));
61  ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->get_pyramid_level(pyramid->info()->num_levels() - 1)->info()->dimension(0));
62  ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->get_pyramid_level(pyramid->info()->num_levels() - 1)->info()->dimension(1));
63 
64  const size_t num_levels = pyramid->info()->num_levels();
65 
66  // Create and initialize the tmp pyramid: I(n-2) = upsample( input + Laplace(n-1) )
67  PyramidInfo pyramid_info;
68  pyramid_info.init(num_levels, 0.5f, output->info()->tensor_shape(), arm_compute::Format::S16);
69  _tmp_pyr.init(pyramid_info);
70 
71  // Allocate add and scale functions. Level 0 does not need to be scaled.
72  _addf.resize(num_levels);
73  _scalef.resize(num_levels - 1);
74 
75  const size_t last_level = num_levels - 1;
76 
77  _addf[last_level].configure(compile_context, input, pyramid->get_pyramid_level(last_level), _tmp_pyr.get_pyramid_level(last_level), ConvertPolicy::SATURATE);
78 
79  // Scale levels n-1 to 1, and add levels n-2 to 0
80  for(size_t l = 0; l < last_level; ++l)
81  {
82  _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 });
83  _addf[l].configure(compile_context, _tmp_pyr.get_pyramid_level(l), pyramid->get_pyramid_level(l), _tmp_pyr.get_pyramid_level(l), ConvertPolicy::SATURATE);
84  }
85 
86  // Convert level 0 from S16 to U8
87  _depthf.configure(compile_context, _tmp_pyr.get_pyramid_level(0), output, ConvertPolicy::SATURATE, 0);
88 
89  _tmp_pyr.allocate();
90 }
91 
93 {
94  ARM_COMPUTE_ERROR_ON_MSG(_addf.empty(), "Unconfigured function");
95 
96  const size_t last_level = _tmp_pyr.info()->num_levels() - 1;
97 
98  _addf[last_level].run();
99 
100  // Run l = [last_level - 1, 0]
101  for(size_t l = last_level; l-- > 0;)
102  {
103  _scalef[l].run();
104  _addf[l].run();
105  }
106 
107  _depthf.run();
108 }
BorderMode
Methods available to handle borders.
Definition: Types.h:265
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Definition: CLTensor.cpp:41
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
1 channel, 1 U8 per channel
void run() override
Run the kernels contained in the function.
size_t dimension(size_t index) const override
Return the size of the requested dimension.
Definition: TensorInfo.h:233
const PyramidInfo * info() const override
Interface to be implemented by the child class to return the Pyramid&#39;s metadata.
Definition: CLPyramid.cpp:117
void init(size_t num_levels, float scale, size_t width, size_t height, Format format)
Initialize pyramid&#39;s metadata for 2D tensors.
Definition: PyramidInfo.cpp:50
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Output values are defined to match the source pixel whose center is nearest to the sample position...
Copyright (c) 2017-2021 Arm Limited.
size_t num_dimensions() const override
The number of dimensions of the tensor (rank)
Definition: TensorInfo.h:254
Basic implementation of the OpenCL pyramid interface.
Definition: CLPyramid.h:40
void run() override final
Run the kernels contained in the function.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
void init(const PyramidInfo &info)
Initialize pyramid data-object using the given Pyramid&#39;s metadata.
Definition: CLPyramid.cpp:41
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void allocate()
Allocate the planes in the pyramid.
Definition: CLPyramid.cpp:109
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
CLCompileContext class.
void configure(const ICLTensor *input, ICLTensor *output, ConvertPolicy policy, uint32_t shift)
Initialize the function&#39;s source, destination.
Store the Pyramid&#39;s metadata.
Definition: PyramidInfo.h:35
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
CLTensor * get_pyramid_level(size_t index) const override
Retrieves a level of the pyramid as a ITensor pointer.
Definition: CLPyramid.cpp:122
size_t num_levels() const
Return the number of the pyramid levels.
Definition: PyramidInfo.cpp:77
void configure(const CLPyramid *pyramid, ICLTensor *input, ICLTensor *output, BorderMode border_mode, uint8_t constant_border_value)
Initialise the function&#39;s source, destinations and border mode.