blob: a9b0496a6eb73df9cbf47ac98d35c1a9f896c4b7 [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2019 Arm Limited.
giuros01c04a0e82018-10-03 12:44:35 +01003 *
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 "helpers.h"
25
giuros01d696cb62018-11-16 10:39:59 +000026#if defined(DATA_TYPE) && defined(WEIGHT_X) && defined(WEIGHT_Y) && defined(WEIGHT_W) && defined(WEIGHT_H) && defined(IMG_WIDTH) && defined(IMG_HEIGHT) && defined(BOX_FIELDS) && defined(SCALE_BEFORE) // Check for compile time constants
giuros01c04a0e82018-10-03 12:44:35 +010027
28/** Perform a padded copy of input tensor to the output tensor. Padding values are defined at compile time
29 *
30 * @attention The following variables must be passed at compile time:
Manuel Bottini5209be52019-02-13 16:34:56 +000031 * -# -DDATA_TYPE= Tensor data type. Supported data types: F16/F32
giuros01c04a0e82018-10-03 12:44:35 +010032 * -# -DWEIGHT{X,Y,W,H}= Weights [wx, wy, ww, wh] for the deltas
33 * -# -DIMG_WIDTH= Original image width
34 * -# -DIMG_HEIGHT= Original image height
Manuel Bottini5209be52019-02-13 16:34:56 +000035 * -# -DBOX_FIELDS= Number of fields that are used to represent a box in boxes
giuros01c04a0e82018-10-03 12:44:35 +010036 *
37 * @param[in] boxes_ptr Pointer to the boxes tensor. Supported data types: F16/F32
38 * @param[in] boxes_stride_x Stride of the boxes tensor in X dimension (in bytes)
39 * @param[in] boxes_step_x boxes_stride_x * number of elements along X processed per workitem(in bytes)
40 * @param[in] boxes_stride_y Stride of the boxes tensor in Y dimension (in bytes)
41 * @param[in] boxes_step_y boxes_stride_y * number of elements along Y processed per workitem(in bytes)
42 * @param[in] boxes_stride_z Stride of the source tensor in Z dimension (in bytes)
43 * @param[in] boxes_step_z boxes_stride_z * number of elements along Z processed per workitem(in bytes)
44 * @param[in] boxes_offset_first_element_in_bytes The offset of the first element in the boxes tensor
45 * @param[out] pred_boxes_ptr Pointer to the predicted boxes. Supported data types: same as @p in_ptr
46 * @param[in] pred_boxes_stride_x Stride of the predicted boxes in X dimension (in bytes)
47 * @param[in] pred_boxes_step_x pred_boxes_stride_x * number of elements along X processed per workitem(in bytes)
48 * @param[in] pred_boxes_stride_y Stride of the predicted boxes in Y dimension (in bytes)
49 * @param[in] pred_boxes_step_y pred_boxes_stride_y * number of elements along Y processed per workitem(in bytes)
50 * @param[in] pred_boxes_stride_z Stride of the predicted boxes in Z dimension (in bytes)
51 * @param[in] pred_boxes_step_z pred_boxes_stride_z * number of elements along Z processed per workitem(in bytes)
52 * @param[in] pred_boxes_offset_first_element_in_bytes The offset of the first element in the predicted boxes
53 * @param[in] deltas_ptr Pointer to the deltas tensor. Supported data types: same as @p in_ptr
54 * @param[in] deltas_stride_x Stride of the deltas tensor in X dimension (in bytes)
55 * @param[in] deltas_step_x deltas_stride_x * number of elements along X processed per workitem(in bytes)
56 * @param[in] deltas_stride_y Stride of the deltas tensor in Y dimension (in bytes)
57 * @param[in] deltas_step_y deltas_stride_y * number of elements along Y processed per workitem(in bytes)
58 * @param[in] deltas_stride_z Stride of the source tensor in Z dimension (in bytes)
59 * @param[in] deltas_step_z deltas_stride_z * number of elements along Z processed per workitem(in bytes)
60 * @param[in] deltas_offset_first_element_in_bytes The offset of the first element in the deltas tensor
61 */
62__kernel void bounding_box_transform(
63 VECTOR_DECLARATION(boxes),
64 IMAGE_DECLARATION(pred_boxes),
65 IMAGE_DECLARATION(deltas))
66{
67 // Get pixels pointer
68 Vector boxes = CONVERT_TO_VECTOR_STRUCT_NO_STEP(boxes);
69 Image pred_boxes = CONVERT_TO_IMAGE_STRUCT(pred_boxes);
70 Image deltas = CONVERT_TO_IMAGE_STRUCT(deltas);
71
72 // Load delta and box values into registers
73 const DATA_TYPE one = (DATA_TYPE)1.f;
74 const DATA_TYPE halfone = (DATA_TYPE)0.5f;
75
76 const int py = get_global_id(1); // box
giuros01d696cb62018-11-16 10:39:59 +000077 const VEC_DATA_TYPE(DATA_TYPE, 4)
78 scale_before = (VEC_DATA_TYPE(DATA_TYPE, 4))SCALE_BEFORE;
giuros01c04a0e82018-10-03 12:44:35 +010079 VEC_DATA_TYPE(DATA_TYPE, 4)
80 delta = vload4(0, (__global DATA_TYPE *)deltas.ptr);
81 const VEC_DATA_TYPE(DATA_TYPE, 4)
giuros01d696cb62018-11-16 10:39:59 +000082 box = vload4(0, (__global DATA_TYPE *)vector_offset(&boxes, BOX_FIELDS * py)) / scale_before;
giuros01c04a0e82018-10-03 12:44:35 +010083
84 // Calculate width and centers of the old boxes
85 const VEC_DATA_TYPE(DATA_TYPE, 2)
86 dims = box.s23 - box.s01 + one;
87 const VEC_DATA_TYPE(DATA_TYPE, 2)
88 ctr = box.s01 + halfone * dims;
89 const VEC_DATA_TYPE(DATA_TYPE, 4)
90 weights = (VEC_DATA_TYPE(DATA_TYPE, 4))(WEIGHT_X, WEIGHT_Y, WEIGHT_W, WEIGHT_H);
91 delta /= weights;
92 delta.s23 = min(delta.s23, (DATA_TYPE)BBOX_XFORM_CLIP);
93
94 // Calculate widths and centers of the new boxes (translation + aspect ratio transformation)
95 const VEC_DATA_TYPE(DATA_TYPE, 2)
96 pred_ctr = delta.s01 * dims + ctr;
97 const VEC_DATA_TYPE(DATA_TYPE, 2)
98 pred_dims = exp(delta.s23) * dims;
99
100 // Useful vector constant definitions
101 const VEC_DATA_TYPE(DATA_TYPE, 4)
giuros01cd96a262018-10-03 12:44:35 +0100102 max_values = (VEC_DATA_TYPE(DATA_TYPE, 4))(IMG_WIDTH - 1, IMG_HEIGHT - 1, IMG_WIDTH - 1, IMG_HEIGHT - 1);
giuros01c04a0e82018-10-03 12:44:35 +0100103 const VEC_DATA_TYPE(DATA_TYPE, 4)
104 sign = (VEC_DATA_TYPE(DATA_TYPE, 4))(-1, -1, 1, 1);
105 const VEC_DATA_TYPE(DATA_TYPE, 4)
106 min_values = 0;
107
108 // Calculate the coordinates of the new boxes
109 VEC_DATA_TYPE(DATA_TYPE, 4)
110 pred_box = pred_ctr.s0101 + sign * halfone * pred_dims.s0101;
giuros01d696cb62018-11-16 10:39:59 +0000111#ifdef OFFSET // Possibly adjust the predicted boxes
112 pred_box.s23 -= one;
113#endif // Possibly adjust the predicted boxes
giuros01c04a0e82018-10-03 12:44:35 +0100114 pred_box = CLAMP(pred_box, min_values, max_values);
giuros01d696cb62018-11-16 10:39:59 +0000115#ifdef SCALE_AFTER // Possibly scale the predicted boxes
116 pred_box *= (VEC_DATA_TYPE(DATA_TYPE, 4))SCALE_AFTER;
giuros01c04a0e82018-10-03 12:44:35 +0100117#endif // Possibly scale the predicted boxes
118
119 // Store them into the output
120 vstore4(pred_box, 0, (__global DATA_TYPE *)pred_boxes.ptr);
121}
122
giuros01d696cb62018-11-16 10:39:59 +0000123#endif // defined(DATA_TYPE) && defined(WEIGHT_X) && defined(WEIGHT_Y) && defined(WEIGHT_W) && defined(WEIGHT_H) && defined(IMG_WIDTH) && defined(IMG_HEIGHT) && defined(BOX_FIELDS) && defined(SCALE_BEFORE)