Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [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 "ComputeAllAnchors.h" |
| 25 | |
| 26 | #include "arm_compute/core/Types.h" |
| 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 28 | #include "arm_compute/core/utils/misc/Utility.h" |
| 29 | #include "tests/validation/Helpers.h" |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace test |
| 34 | { |
| 35 | namespace validation |
| 36 | { |
| 37 | namespace reference |
| 38 | { |
| 39 | template <typename T> |
| 40 | SimpleTensor<T> compute_all_anchors(const SimpleTensor<T> &anchors, const ComputeAnchorsInfo &info) |
| 41 | { |
| 42 | const int num_anchors = anchors.shape()[1]; |
| 43 | const auto width = int(info.feat_width()); |
| 44 | const auto height = int(info.feat_height()); |
| 45 | const float stride = 1. / info.spatial_scale(); |
| 46 | |
| 47 | SimpleTensor<T> all_anchors(TensorShape(4, width * height * num_anchors), anchors.data_type()); |
| 48 | const T *anchors_ptr = anchors.data(); |
| 49 | T *all_anchors_ptr = all_anchors.data(); |
| 50 | |
| 51 | // Iterate over the input grid and anchors |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 52 | #if defined(_OPENMP) |
| 53 | #pragma omp parallel for schedule(dynamic, 1) collapse(3) |
| 54 | #endif /* _OPENMP */ |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 55 | for(int y = 0; y < height; y++) |
| 56 | { |
| 57 | for(int x = 0; x < width; x++) |
| 58 | { |
| 59 | for(int a = 0; a < num_anchors; a++) |
| 60 | { |
| 61 | const T shift_x = T(x) * T(stride); |
| 62 | const T shift_y = T(y) * T(stride); |
| 63 | const size_t anchor_id = a + x * num_anchors + y * width * num_anchors; |
| 64 | // x1 |
| 65 | all_anchors_ptr[anchor_id * 4] = anchors_ptr[4 * a] + shift_x; |
| 66 | // y1 |
| 67 | all_anchors_ptr[anchor_id * 4 + 1] = anchors_ptr[4 * a + 1] + shift_y; |
| 68 | // x2 |
| 69 | all_anchors_ptr[anchor_id * 4 + 2] = anchors_ptr[4 * a + 2] + shift_x; |
| 70 | // y2 |
| 71 | all_anchors_ptr[anchor_id * 4 + 3] = anchors_ptr[4 * a + 3] + shift_y; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | return all_anchors; |
| 76 | } |
| 77 | template SimpleTensor<float> compute_all_anchors(const SimpleTensor<float> &anchors, const ComputeAnchorsInfo &info); |
| 78 | template SimpleTensor<half> compute_all_anchors(const SimpleTensor<half> &anchors, const ComputeAnchorsInfo &info); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 79 | |
| 80 | template <> |
| 81 | SimpleTensor<int16_t> compute_all_anchors(const SimpleTensor<int16_t> &anchors, const ComputeAnchorsInfo &info) |
| 82 | { |
| 83 | SimpleTensor<float> anchors_tmp = convert_from_symmetric(anchors); |
| 84 | SimpleTensor<float> all_anchors_tmp = compute_all_anchors(anchors_tmp, info); |
| 85 | SimpleTensor<int16_t> all_anchors = convert_to_symmetric<int16_t>(all_anchors_tmp, anchors.quantization_info()); |
| 86 | return all_anchors; |
| 87 | } |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 88 | } // namespace reference |
| 89 | } // namespace validation |
| 90 | } // namespace test |
| 91 | } // namespace arm_compute |