Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 1 | /* |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 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 "helpers_asymm.h" |
| 25 | |
| 26 | #if defined(DATA_TYPE) && defined(DATA_TYPE_DELTAS) && defined(WEIGHT_X) && defined(WEIGHT_Y) && defined(WEIGHT_W) && defined(WEIGHT_H) && defined(IMG_WIDTH) && defined(IMG_HEIGHT) && defined(BOX_FIELDS) && defined(SCALE_BEFORE) && defined(OFFSET_BOXES) && defined(SCALE_BOXES) && defined(OFFSET_DELTAS) && defined(SCALE_DELTAS) && defined(OFFSET_PRED_BOXES) && defined(SCALE_PRED_BOXES) // Check for compile time constants |
| 27 | |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 28 | /** Transform proposal bounding boxes to target bounding box using bounding box deltas for quantized data types. |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 29 | * |
| 30 | * @attention The following variables must be passed at compile time: |
| 31 | * -# -DDATA_TYPE= Tensor data type. Supported data types: QASYMM16 for boxes and pred_boxes, QASYMM8 for for deltas |
| 32 | * -# -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 |
| 35 | * -# -DBOX_FIELDS= Number of fields that are used to represent a box in boxes |
| 36 | * |
| 37 | * @param[in] boxes_ptr Pointer to the boxes tensor. Supported data types: QASYMM16 |
| 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: QASYMM8 |
| 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_quantized( |
| 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 float one = 1.f; |
| 74 | const float halfone = 0.5f; |
| 75 | |
| 76 | const int py = get_global_id(1); // box |
| 77 | float4 scale_before = (float4)SCALE_BEFORE; |
| 78 | float4 delta = DEQUANTIZE(vload4(0, (__global DATA_TYPE_DELTAS *)deltas.ptr), OFFSET_DELTAS, SCALE_DELTAS, DATA_TYPE_DELTAS, 4); |
| 79 | float4 box = DEQUANTIZE(vload4(0, (__global DATA_TYPE *)vector_offset(&boxes, BOX_FIELDS * py)), OFFSET_BOXES, SCALE_BOXES, DATA_TYPE, 4) / scale_before; |
| 80 | |
| 81 | // Calculate width and centers of the old boxes |
| 82 | float2 dims = box.s23 - box.s01 + one; |
| 83 | float2 ctr = box.s01 + halfone * dims; |
| 84 | float4 weights = (float4)(WEIGHT_X, WEIGHT_Y, WEIGHT_W, WEIGHT_H); |
| 85 | delta /= weights; |
| 86 | delta.s23 = min(delta.s23, (float)BBOX_XFORM_CLIP); |
| 87 | |
| 88 | // Calculate widths and centers of the new boxes (translation + aspect ratio transformation) |
| 89 | float2 pred_ctr = delta.s01 * dims + ctr; |
| 90 | float2 pred_dims = exp(delta.s23) * dims; |
| 91 | |
| 92 | // Useful vector constant definitions |
| 93 | float4 max_values = (float4)(IMG_WIDTH - 1, IMG_HEIGHT - 1, IMG_WIDTH - 1, IMG_HEIGHT - 1); |
| 94 | float4 sign = (float4)(-1, -1, 1, 1); |
| 95 | float4 min_values = 0; |
| 96 | |
| 97 | // Calculate the coordinates of the new boxes |
| 98 | float4 pred_box = pred_ctr.s0101 + sign * halfone * pred_dims.s0101; |
| 99 | #ifdef OFFSET // Possibly adjust the predicted boxes |
| 100 | pred_box.s23 -= one; |
| 101 | #endif // Possibly adjust the predicted boxes |
| 102 | pred_box = CLAMP(pred_box, min_values, max_values); |
| 103 | #ifdef SCALE_AFTER // Possibly scale the predicted boxes |
| 104 | pred_box *= (float4)SCALE_AFTER; |
| 105 | #endif // Possibly scale the predicted boxes |
| 106 | |
| 107 | // Store them into the output |
| 108 | vstore4(QUANTIZE(pred_box, OFFSET_PRED_BOXES, SCALE_PRED_BOXES, DATA_TYPE, 4), 0, (__global DATA_TYPE *)pred_boxes.ptr); |
| 109 | } |
| 110 | #endif // Check for compile time constants |