Compute Library
 21.02
NEPriorBoxLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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 
28 #include "arm_compute/core/Types.h"
32 
33 #include <arm_neon.h>
34 
35 namespace arm_compute
36 {
37 namespace
38 {
39 Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info)
40 {
41  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
45 
46  // Check variances
47  const int var_size = info.variances().size();
48  if(var_size > 1)
49  {
50  ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size != 4, "Must provide 4 variance values");
51  for(int i = 0; i < var_size; ++i)
52  {
53  ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size <= 0, "Must be greater than 0");
54  }
55  }
56  ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[0] < 0.f, "Step x should be greater or equal to 0");
57  ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[1] < 0.f, "Step y should be greater or equal to 0");
58 
59  if(!info.max_sizes().empty())
60  {
61  ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes().size() != info.min_sizes().size(), "Max and min sizes dimensions should match");
62  }
63 
64  for(unsigned int i = 0; i < info.max_sizes().size(); ++i)
65  {
66  ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes()[i] < info.min_sizes()[i], "Max size should be greater than min size");
67  }
68 
69  if(output != nullptr && output->total_size() != 0)
70  {
71  ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) != 2);
73  }
74 
75  return Status{};
76 }
77 } // namespace
78 
80  : _input1(nullptr), _input2(nullptr), _output(nullptr), _info()
81 {
82 }
83 
84 void NEPriorBoxLayerKernel::store_coordinates(float *out, const int offset, const float center_x, const float center_y, const float box_width, const float box_height, const int width,
85  const int height)
86 {
87  float xmin = (center_x - box_width / 2.f) / width;
88  float ymin = (center_y - box_height / 2.f) / height;
89  float xmax = (center_x + box_width / 2.f) / width;
90  float ymax = (center_y + box_height / 2.f) / height;
91 
92  float32x4_t vec_elements = { xmin, ymin, xmax, ymax };
93  if(_info.clip())
94  {
95  static const float32x4_t CONST_0 = vdupq_n_f32(0.f);
96  static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
97  vec_elements = vmaxq_f32(vminq_f32(vec_elements, CONST_1), CONST_0);
98  }
99  vst1q_f32(out + offset, vec_elements);
100 }
101 
102 void NEPriorBoxLayerKernel::calculate_prior_boxes(const Window &window)
103 {
104  const int num_priors = _info.aspect_ratios().size() * _info.min_sizes().size() + _info.max_sizes().size();
105 
106  const DataLayout data_layout = _input1->info()->data_layout();
107  const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
108  const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
109 
110  const int layer_width = _input1->info()->dimension(width_idx);
111  const int layer_height = _input1->info()->dimension(height_idx);
112 
113  int img_width = _info.img_size().x;
114  int img_height = _info.img_size().y;
115  if(img_width == 0 || img_height == 0)
116  {
117  img_width = _input2->info()->dimension(width_idx);
118  img_height = _input2->info()->dimension(height_idx);
119  }
120 
121  float step_x = _info.steps()[0];
122  float step_y = _info.steps()[1];
123  if(step_x == 0.f || step_y == 0.f)
124  {
125  step_x = static_cast<float>(img_width) / layer_width;
126  step_y = static_cast<float>(img_height) / layer_height;
127  }
128 
130  slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 2));
131 
132  Iterator output(_output, slice);
133  execute_window_loop(slice, [&](const Coordinates & id)
134  {
135  float center_x = 0;
136  float center_y = 0;
137  int idx = id.x() / (4 * num_priors);
138  center_x = (static_cast<float>(idx % layer_width) + _info.offset()) * step_x;
139  center_y = (static_cast<float>(idx / layer_width) + _info.offset()) * step_y;
140 
141  float box_width;
142  float box_height;
143  int offset = 0;
144 
145  auto out = reinterpret_cast<float *>(output.ptr());
146  for(unsigned int i = 0; i < _info.min_sizes().size(); ++i)
147  {
148  const float min_size = _info.min_sizes().at(i);
149  box_width = min_size;
150  box_height = min_size;
151  store_coordinates(out, offset, center_x, center_y, box_width, box_height, img_width, img_height);
152  offset += 4;
153 
154  if(!_info.max_sizes().empty())
155  {
156  const float max_size = _info.max_sizes().at(i);
157  box_width = std::sqrt(min_size * max_size);
158  box_height = box_width;
159 
160  store_coordinates(out, offset, center_x, center_y, box_width, box_height, img_width, img_height);
161  offset += 4;
162  }
163 
164  // rest of priors
165  for(auto ar : _info.aspect_ratios())
166  {
167  if(fabs(ar - 1.) < 1e-6)
168  {
169  continue;
170  }
171 
172  box_width = min_size * sqrt(ar);
173  box_height = min_size / sqrt(ar);
174 
175  store_coordinates(out, offset, center_x, center_y, box_width, box_height, img_width, img_height);
176  offset += 4;
177  }
178  }
179 
180  // set the variance
181  out = reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(id.x(), 1)));
182  float32x4_t var;
183  if(_info.variances().size() == 1)
184  {
185  var = vdupq_n_f32(_info.variances().at(0));
186  }
187  else
188  {
189  const float32x4_t vars = { _info.variances().at(0), _info.variances().at(1), _info.variances().at(2), _info.variances().at(3) };
190  var = vars;
191  }
192  for(int i = 0; i < num_priors; ++i)
193  {
194  vst1q_f32(out + 4 * i, var);
195  }
196  },
197  output);
198 }
199 
200 void NEPriorBoxLayerKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, const PriorBoxLayerInfo &info)
201 {
202  ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
203 
204  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), info));
205 
206  _input1 = input1;
207  _input2 = input2;
208  _info = info;
209  _output = output;
210 
211  // Configure kernel window
212  const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
213  Window win = calculate_max_window(*output->info(), Steps(num_priors * 4));
214  Coordinates coord;
215  coord.set_num_dimensions(output->info()->num_dimensions());
216  output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
217 
218  INEKernel::configure(win);
219 }
220 
221 Status NEPriorBoxLayerKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info)
222 {
223  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
224  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, info));
225 
226  return Status{};
227 }
228 void NEPriorBoxLayerKernel::run(const Window &window, const ThreadInfo &info)
229 {
230  ARM_COMPUTE_UNUSED(info);
233 
234  // Run function
235  calculate_prior_boxes(window);
236 }
237 } // namespace arm_compute
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:846
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
void configure(const ITensor *input1, const ITensor *input2, ITensor *output, const PriorBoxLayerInfo &info)
Set the input and output tensors.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
std::vector< float > aspect_ratios() const
Get aspect ratios.
Definition: Types.h:944
1 channel, 1 F32 per channel
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
const DataLayout data_layout
Definition: Im2Col.cpp:151
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
int32_t x
X coordinates.
Definition: Types.h:465
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
int32_t y
Y coordinates.
Definition: Types.h:466
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
std::array< float, 2 > steps() const
Get the step coordinates.
Definition: Types.h:914
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
PriorBox layer info.
Definition: Types.h:839
float offset() const
Get the offset.
Definition: Types.h:924
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
Coordinates2D img_size() const
Get the image size coordinates.
Definition: Types.h:919
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info)
Static function to check if given info will lead to a valid configuration of NEPriorBoxLayerKernel.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
std::vector< float > max_sizes() const
Get max sizes.
Definition: Types.h:939
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
std::vector< float > variances() const
Get min variances.
Definition: Types.h:909
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
Container for valid region of a window.
Definition: Types.h:188
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
bool clip() const
Get the clip value.
Definition: Types.h:934
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145
std::vector< float > min_sizes() const
Get min sizes.
Definition: Types.h:904