blob: e195c9672230db988232e047be52563fc4b420d1 [file] [log] [blame]
Pablo Telloed0e35b2019-08-30 14:44:52 +01001/*
2 * Copyright (c) 2019 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 */
24#include "arm_compute/core/NEON/kernels/NEBoundingBoxTransformKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CPP/Validate.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Window.h"
32
33#include <arm_neon.h>
34
35namespace arm_compute
36{
37namespace
38{
39Status validate_arguments(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
40{
41 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
42 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(boxes);
Pablo Telloffd31de2019-09-04 13:38:14 +010043 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(boxes, DataType::QASYMM16, DataType::F32, DataType::F16);
44 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(deltas, DataType::QASYMM8, DataType::F32, DataType::F16);
Pablo Telloed0e35b2019-08-30 14:44:52 +010045 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[1] != boxes->tensor_shape()[1]);
46 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[0] % 4 != 0);
47 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
48 ARM_COMPUTE_RETURN_ERROR_ON(deltas->num_dimensions() > 2);
49 ARM_COMPUTE_RETURN_ERROR_ON(boxes->num_dimensions() > 2);
Pablo Telloffd31de2019-09-04 13:38:14 +010050 ARM_COMPUTE_RETURN_ERROR_ON(info.scale() <= 0);
51
52 if(boxes->data_type() == DataType::QASYMM16)
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(deltas, 1, DataType::QASYMM8);
55 const UniformQuantizationInfo deltas_qinfo = deltas->quantization_info().uniform();
56 ARM_COMPUTE_RETURN_ERROR_ON(deltas_qinfo.scale != 0.125f);
57 ARM_COMPUTE_RETURN_ERROR_ON(deltas_qinfo.offset != 0);
58 }
59 else
60 {
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(boxes, deltas);
62 }
Pablo Telloed0e35b2019-08-30 14:44:52 +010063
64 if(pred_boxes->total_size() > 0)
65 {
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(pred_boxes->tensor_shape(), deltas->tensor_shape());
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(pred_boxes, deltas);
68 ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes->num_dimensions() > 2);
Pablo Telloffd31de2019-09-04 13:38:14 +010069 if(pred_boxes->data_type() == DataType::QASYMM16)
70 {
71 const UniformQuantizationInfo pred_qinfo = pred_boxes->quantization_info().uniform();
72 ARM_COMPUTE_RETURN_ERROR_ON(pred_qinfo.scale != 0.125f);
73 ARM_COMPUTE_RETURN_ERROR_ON(pred_qinfo.offset != 0);
74 }
Pablo Telloed0e35b2019-08-30 14:44:52 +010075 }
Pablo Telloffd31de2019-09-04 13:38:14 +010076
Pablo Telloed0e35b2019-08-30 14:44:52 +010077 return Status{};
78}
79
80std::pair<Status, Window> validate_and_configure_window(ITensorInfo *boxes, ITensorInfo *pred_boxes, ITensorInfo *deltas, const BoundingBoxTransformInfo &bb_info)
81{
Michalis Spyrou6bff1952019-10-02 17:22:11 +010082 ARM_COMPUTE_UNUSED(bb_info);
Pablo Telloed0e35b2019-08-30 14:44:52 +010083 ARM_COMPUTE_ERROR_ON_NULLPTR(boxes, pred_boxes);
84
Pablo Telloffd31de2019-09-04 13:38:14 +010085 auto_init_if_empty(*pred_boxes, deltas->clone()->set_data_type(boxes->data_type()).set_quantization_info(boxes->quantization_info()));
Pablo Telloed0e35b2019-08-30 14:44:52 +010086
87 const unsigned int num_boxes = boxes->dimension(1);
88
89 Window window;
90 window.set(Window::DimX, Window::Dimension(0, 1u));
91 window.set(Window::DimY, Window::Dimension(0, num_boxes));
92
93 AccessWindowHorizontal input_access(boxes, 0, 4u);
94 AccessWindowHorizontal output_access(pred_boxes, 0, 4u);
95
96 const bool window_changed = update_window_and_padding(window, input_access, output_access);
97 output_access.set_valid_region(window, ValidRegion(Coordinates(), pred_boxes->tensor_shape()));
98
99 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
100 return std::make_pair(err, window);
101}
102
103} // namespace
104
105NEBoundingBoxTransformKernel::NEBoundingBoxTransformKernel()
106 : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr), _bbinfo(0, 0, 0)
107{
108}
109
110void NEBoundingBoxTransformKernel::configure(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, const BoundingBoxTransformInfo &info)
111{
112 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(boxes->info(), pred_boxes->info(), deltas->info(), info));
113
114 // Configure kernel window
115 auto win_config = validate_and_configure_window(boxes->info(), pred_boxes->info(), deltas->info(), info);
116 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
117
118 // Set instance variables
119 _boxes = boxes;
120 _pred_boxes = pred_boxes;
121 _deltas = deltas;
122 _bbinfo = info;
123
124 INEKernel::configure(win_config.second);
125}
126
127Status NEBoundingBoxTransformKernel::validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
128{
129 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(boxes, pred_boxes, deltas, info));
130 return Status{};
131}
132
Pablo Telloffd31de2019-09-04 13:38:14 +0100133template <>
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100134void NEBoundingBoxTransformKernel::internal_run<uint16_t>(const Window &window)
Pablo Telloffd31de2019-09-04 13:38:14 +0100135{
136 const size_t num_classes = _deltas->info()->tensor_shape()[0] >> 2;
137 const size_t deltas_width = _deltas->info()->tensor_shape()[0];
138 const int img_h = std::floor(_bbinfo.img_height() / _bbinfo.scale() + 0.5f);
139 const int img_w = std::floor(_bbinfo.img_width() / _bbinfo.scale() + 0.5f);
140
141 const auto scale_after = (_bbinfo.apply_scale() ? _bbinfo.scale() : 1.f);
142 const auto scale_before = _bbinfo.scale();
143 const auto offset = (_bbinfo.correct_transform_coords() ? 1.f : 0.f);
144
145 auto pred_ptr = reinterpret_cast<uint16_t *>(_pred_boxes->buffer() + _pred_boxes->info()->offset_first_element_in_bytes());
146 auto delta_ptr = reinterpret_cast<uint8_t *>(_deltas->buffer() + _deltas->info()->offset_first_element_in_bytes());
147
148 const auto boxes_qinfo = _boxes->info()->quantization_info().uniform();
149 const auto deltas_qinfo = _deltas->info()->quantization_info().uniform();
150 const auto pred_qinfo = _pred_boxes->info()->quantization_info().uniform();
151
152 Iterator box_it(_boxes, window);
153 execute_window_loop(window, [&](const Coordinates & id)
154 {
155 const auto ptr = reinterpret_cast<uint16_t *>(box_it.ptr());
156 const auto b0 = dequantize_qasymm16(*ptr, boxes_qinfo);
157 const auto b1 = dequantize_qasymm16(*(ptr + 1), boxes_qinfo);
158 const auto b2 = dequantize_qasymm16(*(ptr + 2), boxes_qinfo);
159 const auto b3 = dequantize_qasymm16(*(ptr + 3), boxes_qinfo);
160 const float width = (b2 / scale_before) - (b0 / scale_before) + 1.f;
161 const float height = (b3 / scale_before) - (b1 / scale_before) + 1.f;
162 const float ctr_x = (b0 / scale_before) + 0.5f * width;
163 const float ctr_y = (b1 / scale_before) + 0.5f * height;
164 for(size_t j = 0; j < num_classes; ++j)
165 {
166 // Extract deltas
167 const size_t delta_id = id.y() * deltas_width + 4u * j;
168 const float dx = dequantize_qasymm8(delta_ptr[delta_id], deltas_qinfo) / _bbinfo.weights()[0];
169 const float dy = dequantize_qasymm8(delta_ptr[delta_id + 1], deltas_qinfo) / _bbinfo.weights()[1];
170 float dw = dequantize_qasymm8(delta_ptr[delta_id + 2], deltas_qinfo) / _bbinfo.weights()[2];
171 float dh = dequantize_qasymm8(delta_ptr[delta_id + 3], deltas_qinfo) / _bbinfo.weights()[3];
172 // Clip dw and dh
173 dw = std::min(dw, _bbinfo.bbox_xform_clip());
174 dh = std::min(dh, _bbinfo.bbox_xform_clip());
175 // Determine the predictions
176 const float pred_ctr_x = dx * width + ctr_x;
177 const float pred_ctr_y = dy * height + ctr_y;
178 const float pred_w = std::exp(dw) * width;
179 const float pred_h = std::exp(dh) * height;
180 // Store the prediction into the output tensor
181 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);
182 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);
183 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);
184 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);
185 }
186 },
187 box_it);
188}
189
Pablo Telloed0e35b2019-08-30 14:44:52 +0100190template <typename T>
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100191void NEBoundingBoxTransformKernel::internal_run(const Window &window)
Pablo Telloed0e35b2019-08-30 14:44:52 +0100192{
193 const size_t num_classes = _deltas->info()->tensor_shape()[0] >> 2;
194 const size_t deltas_width = _deltas->info()->tensor_shape()[0];
Pablo Telloffd31de2019-09-04 13:38:14 +0100195 const int img_h = std::floor(_bbinfo.img_height() / _bbinfo.scale() + 0.5f);
196 const int img_w = std::floor(_bbinfo.img_width() / _bbinfo.scale() + 0.5f);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100197
198 const auto scale_after = (_bbinfo.apply_scale() ? T(_bbinfo.scale()) : T(1));
199 const auto scale_before = T(_bbinfo.scale());
200 ARM_COMPUTE_ERROR_ON(scale_before <= 0);
201 const auto offset = (_bbinfo.correct_transform_coords() ? T(1.f) : T(0.f));
202
203 auto pred_ptr = reinterpret_cast<T *>(_pred_boxes->buffer() + _pred_boxes->info()->offset_first_element_in_bytes());
204 auto delta_ptr = reinterpret_cast<T *>(_deltas->buffer() + _deltas->info()->offset_first_element_in_bytes());
205
206 Iterator box_it(_boxes, window);
207 execute_window_loop(window, [&](const Coordinates & id)
208 {
209 const auto ptr = reinterpret_cast<T *>(box_it.ptr());
210 const auto b0 = *ptr;
211 const auto b1 = *(ptr + 1);
212 const auto b2 = *(ptr + 2);
213 const auto b3 = *(ptr + 3);
214 const T width = (b2 / scale_before) - (b0 / scale_before) + T(1.f);
215 const T height = (b3 / scale_before) - (b1 / scale_before) + T(1.f);
216 const T ctr_x = (b0 / scale_before) + T(0.5f) * width;
217 const T ctr_y = (b1 / scale_before) + T(0.5f) * height;
218 for(size_t j = 0; j < num_classes; ++j)
219 {
220 // Extract deltas
221 const size_t delta_id = id.y() * deltas_width + 4u * j;
222 const T dx = delta_ptr[delta_id] / T(_bbinfo.weights()[0]);
223 const T dy = delta_ptr[delta_id + 1] / T(_bbinfo.weights()[1]);
224 T dw = delta_ptr[delta_id + 2] / T(_bbinfo.weights()[2]);
225 T dh = delta_ptr[delta_id + 3] / T(_bbinfo.weights()[3]);
226 // Clip dw and dh
227 dw = std::min(dw, T(_bbinfo.bbox_xform_clip()));
228 dh = std::min(dh, T(_bbinfo.bbox_xform_clip()));
229 // Determine the predictions
230 const T pred_ctr_x = dx * width + ctr_x;
231 const T pred_ctr_y = dy * height + ctr_y;
Pablo Telloffd31de2019-09-04 13:38:14 +0100232 const T pred_w = std::exp(dw) * width;
233 const T pred_h = std::exp(dh) * height;
Pablo Telloed0e35b2019-08-30 14:44:52 +0100234 // Store the prediction into the output tensor
235 pred_ptr[delta_id] = scale_after * utility::clamp<T>(pred_ctr_x - T(0.5f) * pred_w, T(0), T(img_w - 1));
236 pred_ptr[delta_id + 1] = scale_after * utility::clamp<T>(pred_ctr_y - T(0.5f) * pred_h, T(0), T(img_h - 1));
237 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));
238 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));
239 }
240 },
241 box_it);
242}
243
244void NEBoundingBoxTransformKernel::run(const Window &window, const ThreadInfo &info)
245{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100246 ARM_COMPUTE_UNUSED(info);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100247 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
248 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
249 switch(_boxes->info()->data_type())
250 {
251 case DataType::F32:
252 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100253 internal_run<float>(window);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100254 break;
255 }
Pablo Telloffd31de2019-09-04 13:38:14 +0100256 case DataType::QASYMM16:
257 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100258 internal_run<uint16_t>(window);
Pablo Telloffd31de2019-09-04 13:38:14 +0100259 break;
260 }
Pablo Telloed0e35b2019-08-30 14:44:52 +0100261#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
262 case DataType::F16:
263 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100264 internal_run<float16_t>(window);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100265 break;
266 }
267#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
268 default:
269 {
270 ARM_COMPUTE_ERROR("Data type not supported");
271 }
272 }
273}
274} // namespace arm_compute