Compute Library
 21.02
NEHOGDescriptor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-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 
26 #include "arm_compute/core/Error.h"
34 
35 namespace arm_compute
36 {
38 
39 NEHOGDescriptor::NEHOGDescriptor(std::shared_ptr<IMemoryManager> memory_manager)
40  : _memory_group(std::move(memory_manager)), _gradient(), _orient_bin(), _block_norm(), _mag(), _phase(), _hog_space()
41 {
42 }
43 
44 void NEHOGDescriptor::configure(ITensor *input, ITensor *output, const IHOG *hog, BorderMode border_mode, uint8_t constant_border_value)
45 {
47  ARM_COMPUTE_ERROR_ON(nullptr == output);
48  ARM_COMPUTE_ERROR_ON(nullptr == hog);
49 
50  const HOGInfo *hog_info = hog->info();
51  const size_t width = input->info()->dimension(Window::DimX);
52  const size_t height = input->info()->dimension(Window::DimY);
53  const size_t num_bins = hog_info->num_bins();
54 
55  Size2D cell_size = hog_info->cell_size();
56 
57  // Calculate number of cells along the x and y directions for the hog_space
58  const size_t num_cells_x = width / cell_size.width;
59  const size_t num_cells_y = height / cell_size.height;
60 
61  // TensorShape of the input image
62  const TensorShape &shape_img = input->info()->tensor_shape();
63 
64  // TensorShape of the hog space
65  TensorShape shape_hog_space = input->info()->tensor_shape();
66  shape_hog_space.set(Window::DimX, num_cells_x);
67  shape_hog_space.set(Window::DimY, num_cells_y);
68 
69  // Allocate memory for magnitude, phase and hog space
70  TensorInfo info_mag(shape_img, Format::S16);
71  _mag.allocator()->init(info_mag);
72 
73  TensorInfo info_phase(shape_img, Format::U8);
74  _phase.allocator()->init(info_phase);
75 
76  TensorInfo info_space(shape_hog_space, num_bins, DataType::F32);
77  _hog_space.allocator()->init(info_space);
78 
79  // Manage intermediate buffers
80  _memory_group.manage(&_mag);
81  _memory_group.manage(&_phase);
82 
83  // Initialise gradient kernel
84  _gradient.configure(input, &_mag, &_phase, hog_info->phase_type(), border_mode, constant_border_value);
85 
86  // Manage intermediate buffers
87  _memory_group.manage(&_hog_space);
88 
89  // Initialise orientation binning kernel
90  _orient_bin = std::make_unique<NEHOGOrientationBinningKernel>();
91  _orient_bin->configure(&_mag, &_phase, &_hog_space, hog->info());
92 
93  // Initialize HOG norm kernel
94  _block_norm = std::make_unique<NEHOGBlockNormalizationKernel>();
95  _block_norm->configure(&_hog_space, output, hog->info());
96 
97  // Allocate intermediate tensors
98  _mag.allocator()->allocate();
99  _phase.allocator()->allocate();
100  _hog_space.allocator()->allocate();
101 }
102 
104 {
105  MemoryGroupResourceScope scope_mg(_memory_group);
106 
107  // Run gradient
108  _gradient.run();
109 
110  // Run orientation binning kernel
111  NEScheduler::get().schedule(_orient_bin.get(), Window::DimY);
112 
113  // Run block normalization kernel
114  NEScheduler::get().schedule(_block_norm.get(), Window::DimY);
115 }
116 } // namespace arm_compute
BorderMode
Methods available to handle borders.
Definition: Types.h:265
size_t num_bins() const
The number of histogram bins for each cell.
Definition: HOGInfo.cpp:111
Shape of a tensor.
Definition: TensorShape.h:39
Interface for HOG data-object.
Definition: IHOG.h:35
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
const Size2D & cell_size() const
The cell size in pixels.
Definition: HOGInfo.cpp:91
NEHOGDescriptor(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Default constructor.
1 channel, 1 U8 per channel
Store the HOG&#39;s metadata.
Definition: HOGInfo.h:35
1 channel, 1 F32 per channel
#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
PhaseType phase_type() const
The type of PhaseType.
Definition: HOGInfo.cpp:126
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:90
void run() override
Run the kernels contained in the function.
TensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: Tensor.cpp:48
void configure(ITensor *input, ITensor *output_magnitude, ITensor *output_phase, PhaseType phase_type, BorderMode border_mode, uint8_t constant_border_value=0)
Initialise the function&#39;s source, destinations, phase type and border mode.
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
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
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:89
~NEHOGDescriptor()
Default destructor.
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
void run() override
Run the kernels contained in the function.
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
virtual const HOGInfo * info() const =0
Interface to be implemented by the child class to return the HOG&#39;s metadata.
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79
void configure(ITensor *input, ITensor *output, const IHOG *hog, BorderMode border_mode, uint8_t constant_border_value=0)
Initialise the function&#39;s source, destination, HOG data-object and border mode.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94