Compute Library
 21.11
NEBoundingBoxTransformKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 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/Utils.h"
30 #include "src/core/CPP/Validate.h"
33 
34 #include <arm_neon.h>
35 
36 namespace arm_compute
37 {
38 namespace
39 {
40 Status validate_arguments(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
41 {
42  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
46  ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[1] != boxes->tensor_shape()[1]);
47  ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[0] % 4 != 0);
48  ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
49  ARM_COMPUTE_RETURN_ERROR_ON(deltas->num_dimensions() > 2);
50  ARM_COMPUTE_RETURN_ERROR_ON(boxes->num_dimensions() > 2);
51  ARM_COMPUTE_RETURN_ERROR_ON(info.scale() <= 0);
52 
53  if(boxes->data_type() == DataType::QASYMM16)
54  {
56  const UniformQuantizationInfo deltas_qinfo = deltas->quantization_info().uniform();
57  ARM_COMPUTE_RETURN_ERROR_ON(deltas_qinfo.scale != 0.125f);
58  ARM_COMPUTE_RETURN_ERROR_ON(deltas_qinfo.offset != 0);
59  }
60  else
61  {
63  }
64 
65  if(pred_boxes->total_size() > 0)
66  {
67  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(pred_boxes->tensor_shape(), deltas->tensor_shape());
69  ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes->num_dimensions() > 2);
70  if(pred_boxes->data_type() == DataType::QASYMM16)
71  {
72  const UniformQuantizationInfo pred_qinfo = pred_boxes->quantization_info().uniform();
73  ARM_COMPUTE_RETURN_ERROR_ON(pred_qinfo.scale != 0.125f);
74  ARM_COMPUTE_RETURN_ERROR_ON(pred_qinfo.offset != 0);
75  }
76  }
77 
78  return Status{};
79 }
80 } // namespace
81 
83  : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr), _bbinfo(0, 0, 0)
84 {
85 }
86 
87 void NEBoundingBoxTransformKernel::configure(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, const BoundingBoxTransformInfo &info)
88 {
89  ARM_COMPUTE_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
90  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(boxes->info(), pred_boxes->info(), deltas->info(), info));
91 
92  // Configure kernel window
93  auto_init_if_empty(*pred_boxes->info(), deltas->info()->clone()->set_data_type(boxes->info()->data_type()).set_quantization_info(boxes->info()->quantization_info()));
94 
95  // Set instance variables
96  _boxes = boxes;
97  _pred_boxes = pred_boxes;
98  _deltas = deltas;
99  _bbinfo = info;
100 
101  const unsigned int num_boxes = boxes->info()->dimension(1);
102  Window win = calculate_max_window(*pred_boxes->info(), Steps());
103  win.set(Window::DimX, Window::Dimension(0, 1u));
104  win.set(Window::DimY, Window::Dimension(0, num_boxes));
105 
106  INEKernel::configure(win);
107 }
108 
110 {
111  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(boxes, pred_boxes, deltas, info));
112  return Status{};
113 }
114 
115 template <>
116 void NEBoundingBoxTransformKernel::internal_run<uint16_t>(const Window &window)
117 {
118  const size_t num_classes = _deltas->info()->tensor_shape()[0] >> 2;
119  const size_t deltas_width = _deltas->info()->tensor_shape()[0];
120  const int img_h = std::floor(_bbinfo.img_height() / _bbinfo.scale() + 0.5f);
121  const int img_w = std::floor(_bbinfo.img_width() / _bbinfo.scale() + 0.5f);
122 
123  const auto scale_after = (_bbinfo.apply_scale() ? _bbinfo.scale() : 1.f);
124  const auto scale_before = _bbinfo.scale();
125  const auto offset = (_bbinfo.correct_transform_coords() ? 1.f : 0.f);
126 
127  auto pred_ptr = reinterpret_cast<uint16_t *>(_pred_boxes->buffer() + _pred_boxes->info()->offset_first_element_in_bytes());
128  auto delta_ptr = reinterpret_cast<uint8_t *>(_deltas->buffer() + _deltas->info()->offset_first_element_in_bytes());
129 
130  const auto boxes_qinfo = _boxes->info()->quantization_info().uniform();
131  const auto deltas_qinfo = _deltas->info()->quantization_info().uniform();
132  const auto pred_qinfo = _pred_boxes->info()->quantization_info().uniform();
133 
134  Iterator box_it(_boxes, window);
135  execute_window_loop(window, [&](const Coordinates & id)
136  {
137  const auto ptr = reinterpret_cast<uint16_t *>(box_it.ptr());
138  const auto b0 = dequantize_qasymm16(*ptr, boxes_qinfo);
139  const auto b1 = dequantize_qasymm16(*(ptr + 1), boxes_qinfo);
140  const auto b2 = dequantize_qasymm16(*(ptr + 2), boxes_qinfo);
141  const auto b3 = dequantize_qasymm16(*(ptr + 3), boxes_qinfo);
142  const float width = (b2 / scale_before) - (b0 / scale_before) + 1.f;
143  const float height = (b3 / scale_before) - (b1 / scale_before) + 1.f;
144  const float ctr_x = (b0 / scale_before) + 0.5f * width;
145  const float ctr_y = (b1 / scale_before) + 0.5f * height;
146  for(size_t j = 0; j < num_classes; ++j)
147  {
148  // Extract deltas
149  const size_t delta_id = id.y() * deltas_width + 4u * j;
150  const float dx = dequantize_qasymm8(delta_ptr[delta_id], deltas_qinfo) / _bbinfo.weights()[0];
151  const float dy = dequantize_qasymm8(delta_ptr[delta_id + 1], deltas_qinfo) / _bbinfo.weights()[1];
152  float dw = dequantize_qasymm8(delta_ptr[delta_id + 2], deltas_qinfo) / _bbinfo.weights()[2];
153  float dh = dequantize_qasymm8(delta_ptr[delta_id + 3], deltas_qinfo) / _bbinfo.weights()[3];
154  // Clip dw and dh
155  dw = std::min(dw, _bbinfo.bbox_xform_clip());
156  dh = std::min(dh, _bbinfo.bbox_xform_clip());
157  // Determine the predictions
158  const float pred_ctr_x = dx * width + ctr_x;
159  const float pred_ctr_y = dy * height + ctr_y;
160  const float pred_w = std::exp(dw) * width;
161  const float pred_h = std::exp(dh) * height;
162  // Store the prediction into the output tensor
163  pred_ptr[delta_id] = quantize_qasymm16(scale_after * utility::clamp<float>(pred_ctr_x - 0.5f * pred_w, 0.f, img_w - 1.f), pred_qinfo);
164  pred_ptr[delta_id + 1] = quantize_qasymm16(scale_after * utility::clamp<float>(pred_ctr_y - 0.5f * pred_h, 0.f, img_h - 1.f), pred_qinfo);
165  pred_ptr[delta_id + 2] = quantize_qasymm16(scale_after * utility::clamp<float>(pred_ctr_x + 0.5f * pred_w - offset, 0.f, img_w - 1.f), pred_qinfo);
166  pred_ptr[delta_id + 3] = quantize_qasymm16(scale_after * utility::clamp<float>(pred_ctr_y + 0.5f * pred_h - offset, 0.f, img_h - 1.f), pred_qinfo);
167  }
168  },
169  box_it);
170 }
171 
172 template <typename T>
173 void NEBoundingBoxTransformKernel::internal_run(const Window &window)
174 {
175  const size_t num_classes = _deltas->info()->tensor_shape()[0] >> 2;
176  const size_t deltas_width = _deltas->info()->tensor_shape()[0];
177  const int img_h = std::floor(_bbinfo.img_height() / _bbinfo.scale() + 0.5f);
178  const int img_w = std::floor(_bbinfo.img_width() / _bbinfo.scale() + 0.5f);
179 
180  const auto scale_after = (_bbinfo.apply_scale() ? T(_bbinfo.scale()) : T(1));
181  const auto scale_before = T(_bbinfo.scale());
182  ARM_COMPUTE_ERROR_ON(scale_before <= 0);
183  const auto offset = (_bbinfo.correct_transform_coords() ? T(1.f) : T(0.f));
184 
185  auto pred_ptr = reinterpret_cast<T *>(_pred_boxes->buffer() + _pred_boxes->info()->offset_first_element_in_bytes());
186  auto delta_ptr = reinterpret_cast<T *>(_deltas->buffer() + _deltas->info()->offset_first_element_in_bytes());
187 
188  Iterator box_it(_boxes, window);
189  execute_window_loop(window, [&](const Coordinates & id)
190  {
191  const auto ptr = reinterpret_cast<T *>(box_it.ptr());
192  const auto b0 = *ptr;
193  const auto b1 = *(ptr + 1);
194  const auto b2 = *(ptr + 2);
195  const auto b3 = *(ptr + 3);
196  const T width = (b2 / scale_before) - (b0 / scale_before) + T(1.f);
197  const T height = (b3 / scale_before) - (b1 / scale_before) + T(1.f);
198  const T ctr_x = (b0 / scale_before) + T(0.5f) * width;
199  const T ctr_y = (b1 / scale_before) + T(0.5f) * height;
200  for(size_t j = 0; j < num_classes; ++j)
201  {
202  // Extract deltas
203  const size_t delta_id = id.y() * deltas_width + 4u * j;
204  const T dx = delta_ptr[delta_id] / T(_bbinfo.weights()[0]);
205  const T dy = delta_ptr[delta_id + 1] / T(_bbinfo.weights()[1]);
206  T dw = delta_ptr[delta_id + 2] / T(_bbinfo.weights()[2]);
207  T dh = delta_ptr[delta_id + 3] / T(_bbinfo.weights()[3]);
208  // Clip dw and dh
209  dw = std::min(dw, T(_bbinfo.bbox_xform_clip()));
210  dh = std::min(dh, T(_bbinfo.bbox_xform_clip()));
211  // Determine the predictions
212  const T pred_ctr_x = dx * width + ctr_x;
213  const T pred_ctr_y = dy * height + ctr_y;
214  const T pred_w = std::exp(dw) * width;
215  const T pred_h = std::exp(dh) * height;
216  // Store the prediction into the output tensor
217  pred_ptr[delta_id] = scale_after * utility::clamp<T>(pred_ctr_x - T(0.5f) * pred_w, T(0), T(img_w - 1));
218  pred_ptr[delta_id + 1] = scale_after * utility::clamp<T>(pred_ctr_y - T(0.5f) * pred_h, T(0), T(img_h - 1));
219  pred_ptr[delta_id + 2] = scale_after * utility::clamp<T>(pred_ctr_x + T(0.5f) * pred_w - offset, T(0), T(img_w - 1));
220  pred_ptr[delta_id + 3] = scale_after * utility::clamp<T>(pred_ctr_y + T(0.5f) * pred_h - offset, T(0), T(img_h - 1));
221  }
222  },
223  box_it);
224 }
225 
226 void NEBoundingBoxTransformKernel::run(const Window &window, const ThreadInfo &info)
227 {
228  ARM_COMPUTE_UNUSED(info);
231  switch(_boxes->info()->data_type())
232  {
233  case DataType::F32:
234  {
235  internal_run<float>(window);
236  break;
237  }
238  case DataType::QASYMM16:
239  {
240  internal_run<uint16_t>(window);
241  break;
242  }
243 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
244  case DataType::F16:
245  {
246  internal_run<float16_t>(window);
247  break;
248  }
249 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
250  default:
251  {
252  ARM_COMPUTE_ERROR("Data type not supported");
253  }
254  }
255 }
256 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1069
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
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
float dequantize_qasymm8(uint8_t value, const INFO_TYPE &qinfo)
Dequantize a value given an unsigned 8-bit asymmetric quantization scheme.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
void configure(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, const BoundingBoxTransformInfo &info)
Set the input and output tensors.
#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
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
quantized, asymmetric fixed-point 16-bit number
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 CPU tensor.
Definition: ITensor.h:36
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
float dequantize_qasymm16(uint16_t value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 16-bit asymmetric quantization scheme.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory. ...
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
UniformQuantizationInfo uniform() const
Return per layer quantization info.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
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
Bounding Box Transform information class.
Definition: Types.h:1442
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
static Status validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
Static function to check if given info will lead to a valid configuration of CLBoundingBoxTransform.
virtual size_t offset_first_element_in_bytes() const =0
The offset from the beginning of the memory allocation to the first element of the tensor...
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:158
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
std::array< float, 4 > weights() const
Definition: Types.h:1463
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
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
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:690
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39
uint16_t quantize_qasymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a 16-bit asymmetric quantization scheme.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201