blob: 2d08c879cc3ed5ac3262288a637392408652d6b2 [file] [log] [blame]
Dana Zlotnik3475ffe2022-01-03 14:37:10 +02001/*
2 * Copyright (c) 2019-2022 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 "src/cpu/kernels/boundingboxtransform/generic/neon/impl.h"
25namespace arm_compute
26{
27namespace cpu
28{
29void bounding_box_transform_qsymm16(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, BoundingBoxTransformInfo bbinfo, const Window &window)
30
31{
32 const size_t num_classes = deltas->info()->tensor_shape()[0] >> 2;
33 const size_t deltas_width = deltas->info()->tensor_shape()[0];
34 const int img_h = std::floor(bbinfo.img_height() / bbinfo.scale() + 0.5f);
35 const int img_w = std::floor(bbinfo.img_width() / bbinfo.scale() + 0.5f);
36
37 const auto scale_after = (bbinfo.apply_scale() ? bbinfo.scale() : 1.f);
38 const auto scale_before = bbinfo.scale();
39 const auto offset = (bbinfo.correct_transform_coords() ? 1.f : 0.f);
40
41 auto pred_ptr = reinterpret_cast<uint16_t *>(pred_boxes->buffer() + pred_boxes->info()->offset_first_element_in_bytes());
42 auto delta_ptr = reinterpret_cast<uint8_t *>(deltas->buffer() + deltas->info()->offset_first_element_in_bytes());
43
44 const auto boxes_qinfo = boxes->info()->quantization_info().uniform();
45 const auto deltas_qinfo = deltas->info()->quantization_info().uniform();
46 const auto pred_qinfo = pred_boxes->info()->quantization_info().uniform();
47
48 Iterator box_it(boxes, window);
49 execute_window_loop(window, [&](const Coordinates & id)
50 {
51 const auto ptr = reinterpret_cast<uint16_t *>(box_it.ptr());
52 const auto b0 = dequantize_qasymm16(*ptr, boxes_qinfo);
53 const auto b1 = dequantize_qasymm16(*(ptr + 1), boxes_qinfo);
54 const auto b2 = dequantize_qasymm16(*(ptr + 2), boxes_qinfo);
55 const auto b3 = dequantize_qasymm16(*(ptr + 3), boxes_qinfo);
56 const float width = (b2 / scale_before) - (b0 / scale_before) + 1.f;
57 const float height = (b3 / scale_before) - (b1 / scale_before) + 1.f;
58 const float ctr_x = (b0 / scale_before) + 0.5f * width;
59 const float ctr_y = (b1 / scale_before) + 0.5f * height;
60 for(size_t j = 0; j < num_classes; ++j)
61 {
62 // Extract deltas
63 const size_t delta_id = id.y() * deltas_width + 4u * j;
64 const float dx = dequantize_qasymm8(delta_ptr[delta_id], deltas_qinfo) / bbinfo.weights()[0];
65 const float dy = dequantize_qasymm8(delta_ptr[delta_id + 1], deltas_qinfo) / bbinfo.weights()[1];
66 float dw = dequantize_qasymm8(delta_ptr[delta_id + 2], deltas_qinfo) / bbinfo.weights()[2];
67 float dh = dequantize_qasymm8(delta_ptr[delta_id + 3], deltas_qinfo) / bbinfo.weights()[3];
68 // Clip dw and dh
69 dw = std::min(dw, bbinfo.bbox_xform_clip());
70 dh = std::min(dh, bbinfo.bbox_xform_clip());
71 // Determine the predictions
72 const float pred_ctr_x = dx * width + ctr_x;
73 const float pred_ctr_y = dy * height + ctr_y;
74 const float pred_w = std::exp(dw) * width;
75 const float pred_h = std::exp(dh) * height;
76 // Store the prediction into the output tensor
77 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);
78 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);
79 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);
80 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);
81 }
82 },
83 box_it);
84}
85
86template <typename T>
87void bounding_box_transform(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, BoundingBoxTransformInfo bbinfo, const Window &window)
88{
89 const size_t num_classes = deltas->info()->tensor_shape()[0] >> 2;
90 const size_t deltas_width = deltas->info()->tensor_shape()[0];
91 const int img_h = std::floor(bbinfo.img_height() / bbinfo.scale() + 0.5f);
92 const int img_w = std::floor(bbinfo.img_width() / bbinfo.scale() + 0.5f);
93
94 const auto scale_after = (bbinfo.apply_scale() ? T(bbinfo.scale()) : T(1));
95 const auto scale_before = T(bbinfo.scale());
96 ARM_COMPUTE_ERROR_ON(scale_before <= 0);
97 const auto offset = (bbinfo.correct_transform_coords() ? T(1.f) : T(0.f));
98
99 auto pred_ptr = reinterpret_cast<T *>(pred_boxes->buffer() + pred_boxes->info()->offset_first_element_in_bytes());
100 auto delta_ptr = reinterpret_cast<T *>(deltas->buffer() + deltas->info()->offset_first_element_in_bytes());
101
102 Iterator box_it(boxes, window);
103 execute_window_loop(window, [&](const Coordinates & id)
104 {
105 const auto ptr = reinterpret_cast<T *>(box_it.ptr());
106 const auto b0 = *ptr;
107 const auto b1 = *(ptr + 1);
108 const auto b2 = *(ptr + 2);
109 const auto b3 = *(ptr + 3);
110 const T width = (b2 / scale_before) - (b0 / scale_before) + T(1.f);
111 const T height = (b3 / scale_before) - (b1 / scale_before) + T(1.f);
112 const T ctr_x = (b0 / scale_before) + T(0.5f) * width;
113 const T ctr_y = (b1 / scale_before) + T(0.5f) * height;
114 for(size_t j = 0; j < num_classes; ++j)
115 {
116 // Extract deltas
117 const size_t delta_id = id.y() * deltas_width + 4u * j;
118 const T dx = delta_ptr[delta_id] / T(bbinfo.weights()[0]);
119 const T dy = delta_ptr[delta_id + 1] / T(bbinfo.weights()[1]);
120 T dw = delta_ptr[delta_id + 2] / T(bbinfo.weights()[2]);
121 T dh = delta_ptr[delta_id + 3] / T(bbinfo.weights()[3]);
122 // Clip dw and dh
123 dw = std::min(dw, T(bbinfo.bbox_xform_clip()));
124 dh = std::min(dh, T(bbinfo.bbox_xform_clip()));
125 // Determine the predictions
126 const T pred_ctr_x = dx * width + ctr_x;
127 const T pred_ctr_y = dy * height + ctr_y;
128 const T pred_w = std::exp(dw) * width;
129 const T pred_h = std::exp(dh) * height;
130 // Store the prediction into the output tensor
131 pred_ptr[delta_id] = scale_after * utility::clamp<T>(pred_ctr_x - T(0.5f) * pred_w, T(0), T(img_w - 1));
132 pred_ptr[delta_id + 1] = scale_after * utility::clamp<T>(pred_ctr_y - T(0.5f) * pred_h, T(0), T(img_h - 1));
133 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));
134 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));
135 }
136 },
137 box_it);
138}
139
140template void bounding_box_transform<float>(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, BoundingBoxTransformInfo bbinfo, const Window &window);
141
142#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
143template void bounding_box_transform<float16_t>(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, BoundingBoxTransformInfo bbinfo, const Window &window);
144#endif //defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
145} // namespace cpu
146} // namespace arm_compute