Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "helpers.h" |
| 25 | |
| 26 | #if defined(DATA_TYPE) && defined(WIDTH) && defined(HEIGHT) && defined(LAYER_WIDTH) && defined(LAYER_HEIGHT) && defined(OFFSET) && defined(STEP_X) && defined(STEP_Y) && defined(NUM_PRIORS) && defined(VARIANCE_0) && defined(VARIANCE_1) && defined(VARIANCE_2) && defined(VARIANCE_3) |
| 27 | |
| 28 | /** Compute prior boxes and clip (NCHW) |
| 29 | * |
| 30 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F32 |
| 31 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 32 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 33 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 34 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 35 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 36 | * @param[in] idx Index to write to |
| 37 | * @param[in] center_x Center value of the x axis |
| 38 | * @param[in] center_y Center value of the y axis |
| 39 | * @param[in] box_width Prior box width |
| 40 | * @param[in] box_height Prior box height |
| 41 | * |
| 42 | */ |
| 43 | inline void calculate_xy_min_max_nchw(Image *out, int idx, float center_x, float center_y, float box_width, float box_height) |
| 44 | { |
| 45 | float xmin = (center_x - box_width / 2.f) / WIDTH; |
| 46 | float ymin = (center_y - box_height / 2.f) / HEIGHT; |
| 47 | float xmax = (center_x + box_width / 2.f) / WIDTH; |
| 48 | float ymax = (center_y + box_height / 2.f) / HEIGHT; |
| 49 | |
| 50 | #if defined(CLIP) |
| 51 | xmin = clamp(xmin, 0.f, 1.f); |
| 52 | ymin = clamp(ymin, 0.f, 1.f); |
| 53 | xmax = clamp(xmax, 0.f, 1.f); |
| 54 | ymax = clamp(ymax, 0.f, 1.f); |
| 55 | #endif // defined(CLIP) |
| 56 | |
| 57 | // Store result |
| 58 | vstore4((VEC_DATA_TYPE(DATA_TYPE, 4))(xmin, ymin, xmax, ymax), 0, ((__global DATA_TYPE *)offset(out, idx + 0, 0))); |
| 59 | } |
| 60 | |
| 61 | /** Compute prior boxes (NCHW) |
| 62 | * |
| 63 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F32 |
| 64 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 65 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 66 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 67 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 68 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 69 | * @param[in] min_size Prior box min size |
| 70 | * @param[in] min_idx Index of the min vector |
| 71 | * @param[in] idx Index to write to |
| 72 | * |
| 73 | * @return The updated index |
| 74 | */ |
| 75 | inline int calculate_min_nchw(Image *out, float *max, float *aspect_ratios, int max_size, int aspect_ratios_size, float min_size, int min_idx, int idx) |
| 76 | { |
| 77 | const float center_x = ((float)(get_global_id(0) % LAYER_WIDTH) + OFFSET) * STEP_X; |
| 78 | const float center_y = ((float)(get_global_id(0) / LAYER_WIDTH) + OFFSET) * STEP_Y; |
| 79 | |
| 80 | float box_width = min_size; |
| 81 | float box_height = min_size; |
| 82 | calculate_xy_min_max_nchw(out, idx, center_x, center_y, box_width, box_height); |
| 83 | idx += 4; |
| 84 | |
| 85 | if(max_size > 0) |
| 86 | { |
| 87 | box_width = sqrt(min_size * max[min_idx]); |
| 88 | box_height = box_width; |
| 89 | calculate_xy_min_max_nchw(out, idx, center_x, center_y, box_width, box_height); |
| 90 | idx += 4; |
| 91 | } |
| 92 | for(unsigned int i = 0; i < aspect_ratios_size; ++i) |
| 93 | { |
| 94 | if(fabs(aspect_ratios[i] - 1.f) < 1e-6f) |
| 95 | { |
| 96 | continue; |
| 97 | } |
| 98 | box_width = min_size * sqrt(aspect_ratios[i]); |
| 99 | box_height = min_size * rsqrt(aspect_ratios[i]); |
| 100 | |
| 101 | calculate_xy_min_max_nchw(out, idx, center_x, center_y, box_width, box_height); |
| 102 | idx += 4; |
| 103 | } |
| 104 | |
| 105 | return idx; |
| 106 | } |
| 107 | |
| 108 | /** Compute prior boxes and clip (NHWC) |
| 109 | * |
| 110 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F32 |
| 111 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 112 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 113 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 114 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 115 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 116 | * @param[in] idx Index to write to |
| 117 | * @param[in] center_x Center value of the x axis |
| 118 | * @param[in] center_y Center value of the y axis |
| 119 | * @param[in] box_width Prior box width |
| 120 | * @param[in] box_height Prior box height |
| 121 | * |
| 122 | */ |
| 123 | inline void calculate_xy_min_max_nhwc(Tensor3D *out, int idx, float center_x, float center_y, float box_width, float box_height) |
| 124 | { |
| 125 | float xmin = (center_x - box_width / 2.f) / WIDTH; |
| 126 | float ymin = (center_y - box_height / 2.f) / HEIGHT; |
| 127 | float xmax = (center_x + box_width / 2.f) / WIDTH; |
| 128 | float ymax = (center_y + box_height / 2.f) / HEIGHT; |
| 129 | |
| 130 | #if defined(CLIP) |
| 131 | xmin = clamp(xmin, 0.f, 1.f); |
| 132 | ymin = clamp(ymin, 0.f, 1.f); |
| 133 | xmax = clamp(xmax, 0.f, 1.f); |
| 134 | ymax = clamp(ymax, 0.f, 1.f); |
| 135 | #endif // defined(CLIP) |
| 136 | |
| 137 | *((__global DATA_TYPE *)tensor3D_offset(out, 0, idx + 0, 0)) = xmin; |
| 138 | *((__global DATA_TYPE *)tensor3D_offset(out, 0, idx + 1, 0)) = ymin; |
| 139 | *((__global DATA_TYPE *)tensor3D_offset(out, 0, idx + 2, 0)) = xmax; |
| 140 | *((__global DATA_TYPE *)tensor3D_offset(out, 0, idx + 3, 0)) = ymax; |
| 141 | } |
| 142 | |
| 143 | /** Compute prior boxes (NHWC) |
| 144 | * |
| 145 | * @param[in,out] out Tensor output |
| 146 | * @param[in] max The maximum values |
| 147 | * @param[in] aspect_ratios The aspect ratio values |
| 148 | * @param[in] max_size The maximum values values size |
| 149 | * @param[in] aspect_ratios_size The aspect ratio values size |
| 150 | * @param[in] min_size The minimum values size |
| 151 | * @param[in] min_idx Index of the min vector |
| 152 | * @param[in] idx Index to write to |
| 153 | * |
| 154 | * @return The updated index |
| 155 | */ |
| 156 | inline int calculate_min_nhwc(Image *out, float *max, float *aspect_ratios, int max_size, int aspect_ratios_size, float min_size, int min_idx, int idx) |
| 157 | { |
| 158 | const float center_x = ((float)(get_global_id(1) % LAYER_WIDTH) + OFFSET) * STEP_X; |
| 159 | const float center_y = ((float)(get_global_id(1) / LAYER_WIDTH) + OFFSET) * STEP_Y; |
| 160 | |
| 161 | float box_width = min_size; |
| 162 | float box_height = min_size; |
| 163 | |
| 164 | calculate_xy_min_max_nhwc(out, idx, center_x, center_y, box_width, box_height); |
| 165 | idx += 4; |
| 166 | if(max_size > 0) |
| 167 | { |
| 168 | box_width = sqrt(min_size * max[min_idx]); |
| 169 | box_height = box_width; |
| 170 | calculate_xy_min_max_nhwc(out, idx, center_x, center_y, box_width, box_height); |
| 171 | idx += 4; |
| 172 | } |
| 173 | for(unsigned int i = 0; i < aspect_ratios_size; ++i) |
| 174 | { |
| 175 | if(fabs(aspect_ratios[i] - 1.f) < 1e-6f) |
| 176 | { |
| 177 | continue; |
| 178 | } |
| 179 | box_width = min_size * sqrt(aspect_ratios[i]); |
| 180 | box_height = min_size * rsqrt(aspect_ratios[i]); |
| 181 | |
| 182 | calculate_xy_min_max_nhwc(out, idx, center_x, center_y, box_width, box_height); |
| 183 | idx += 4; |
| 184 | } |
| 185 | |
| 186 | return idx; |
| 187 | } |
| 188 | |
| 189 | /** Calculate prior boxes with NCHW format. |
| 190 | * |
| 191 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F32 |
| 192 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 193 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 194 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 195 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 196 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 197 | * @param[in] min The minimum values |
| 198 | * @param[in] max The maximum_values |
| 199 | * @param[in] aspect_ratios The aspect ratio values |
| 200 | * @param[in] min_size The minimum values size |
| 201 | * @param[in] max_size The maximum_values values size |
| 202 | * @param[in] aspect_ratios_size The aspect ratio values size |
| 203 | */ |
| 204 | __kernel void prior_box_layer_nchw(IMAGE_DECLARATION(output), __global float *min, __global float *max, __global float *aspect_ratios, unsigned int min_size, unsigned int max_size, |
| 205 | unsigned int aspect_ratios_size) |
| 206 | { |
| 207 | Image out = CONVERT_TO_IMAGE_STRUCT(output); |
| 208 | |
| 209 | int idx = 0; |
| 210 | for(unsigned int i = 0; i < min_size; ++i) |
| 211 | { |
| 212 | idx = calculate_min_nchw(&out, max, aspect_ratios, max_size, aspect_ratios_size, min[i], i, idx); |
| 213 | } |
| 214 | |
| 215 | // Store variances |
| 216 | for(int i = 0; i < (NUM_PRIORS * 4); i += 4) |
| 217 | { |
| 218 | vstore4((VEC_DATA_TYPE(DATA_TYPE, 4))(VARIANCE_0, VARIANCE_1, VARIANCE_2, VARIANCE_3), 0, ((__global DATA_TYPE *)offset(&out, i, 1))); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** Calculate prior boxes with NHWC format. |
| 223 | * |
| 224 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F32 |
| 225 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 226 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 227 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 228 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 229 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 230 | * @param[in] min The minimum values |
| 231 | * @param[in] max The maximum_values |
| 232 | * @param[in] aspect_ratios The aspect ratio values |
| 233 | * @param[in] min_size The minimum values size |
| 234 | * @param[in] max_size The maximum_values values size |
| 235 | * @param[in] aspect_ratios_size The aspect ratio values size |
| 236 | */ |
| 237 | __kernel void prior_box_layer_nhwc(TENSOR3D_DECLARATION(output), __global float *min, __global float *max, __global float *aspect_ratios, unsigned int min_size, unsigned int max_size, |
| 238 | unsigned int aspect_ratios_size) |
| 239 | { |
| 240 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 241 | |
| 242 | int idx = 0; |
| 243 | for(unsigned int i = 0; i < min_size; ++i) |
| 244 | { |
| 245 | idx = calculate_min_nhwc(&out, max, aspect_ratios, max_size, aspect_ratios_size, min[i], i, idx); |
| 246 | } |
| 247 | |
| 248 | for(int i = 0; i < (NUM_PRIORS * 4); i += 4) |
| 249 | { |
| 250 | *((__global DATA_TYPE *)tensor3D_offset(&out, 0, i + 0, 1)) = VARIANCE_0; |
| 251 | *((__global DATA_TYPE *)tensor3D_offset(&out, 0, i + 1, 1)) = VARIANCE_1; |
| 252 | *((__global DATA_TYPE *)tensor3D_offset(&out, 0, i + 2, 1)) = VARIANCE_2; |
| 253 | *((__global DATA_TYPE *)tensor3D_offset(&out, 0, i + 3, 1)) = VARIANCE_3; |
| 254 | } |
| 255 | } |
| 256 | #endif /* defined(DATA_TYPE) && defined(WIDTH) && defined(HEIGHT) && defined(LAYER_WIDTH) && defined(LAYER_HEIGHT) && defined(OFFSET) && defined(STEP_X) && defined(STEP_Y) && defined(NUM_PRIORS) && defined(VARIANCE_0) && defined(VARIANCE_1) && defined(VARIANCE_2) && defined(VARIANCE_3) */ |