Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TEST_UTILS_H |
| 25 | #define ARM_COMPUTE_TEST_UTILS_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Coordinates.h" |
| 28 | #include "arm_compute/core/Error.h" |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/HOGInfo.h" |
John Richardson | 8de9261 | 2018-02-22 14:09:31 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/PyramidInfo.h" |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Size2D.h" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/TensorShape.h" |
| 34 | #include "arm_compute/core/Types.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 35 | #include "support/StringSupport.h" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 36 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 38 | #ifdef ARM_COMPUTE_CL |
| 39 | #include "arm_compute/core/CL/OpenCL.h" |
| 40 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 41 | #endif /* ARM_COMPUTE_CL */ |
| 42 | |
| 43 | #ifdef ARM_COMPUTE_GC |
| 44 | #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" |
| 45 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h" |
| 46 | #endif /* ARM_COMPUTE_GC */ |
| 47 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | #include <cmath> |
| 49 | #include <cstddef> |
| 50 | #include <limits> |
| 51 | #include <memory> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 52 | #include <random> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | #include <sstream> |
| 54 | #include <string> |
| 55 | #include <type_traits> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 56 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 58 | #include "arm_compute/runtime/CPP/CPPScheduler.h" |
| 59 | #include "arm_compute/runtime/RuntimeContext.h" |
| 60 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | namespace arm_compute |
| 62 | { |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 63 | #ifdef ARM_COMPUTE_CL |
| 64 | class CLTensor; |
| 65 | #endif /* ARM_COMPUTE_CL */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | namespace test |
| 67 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | /** Round floating-point value with half value rounding to positive infinity. |
| 69 | * |
| 70 | * @param[in] value floating-point value to be rounded. |
| 71 | * |
| 72 | * @return Floating-point value of rounded @p value. |
| 73 | */ |
| 74 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 75 | inline T round_half_up(T value) |
| 76 | { |
| 77 | return std::floor(value + 0.5f); |
| 78 | } |
| 79 | |
| 80 | /** Round floating-point value with half value rounding to nearest even. |
| 81 | * |
| 82 | * @param[in] value floating-point value to be rounded. |
| 83 | * @param[in] epsilon precision. |
| 84 | * |
| 85 | * @return Floating-point value of rounded @p value. |
| 86 | */ |
| 87 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 88 | inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon()) |
| 89 | { |
| 90 | T positive_value = std::abs(value); |
| 91 | T ipart = 0; |
| 92 | std::modf(positive_value, &ipart); |
| 93 | // If 'value' is exactly halfway between two integers |
| 94 | if(std::abs(positive_value - (ipart + 0.5f)) < epsilon) |
| 95 | { |
| 96 | // If 'ipart' is even then return 'ipart' |
| 97 | if(std::fmod(ipart, 2.f) < epsilon) |
| 98 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 99 | return support::cpp11::copysign(ipart, value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | } |
| 101 | // Else return the nearest even integer |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 102 | return support::cpp11::copysign(std::ceil(ipart + 0.5f), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | } |
| 104 | // Otherwise use the usual round to closest |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 105 | return support::cpp11::copysign(support::cpp11::round(positive_value), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | |
| 108 | namespace traits |
| 109 | { |
| 110 | // *INDENT-OFF* |
| 111 | // clang-format off |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 112 | /** Promote a type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | template <typename T> struct promote { }; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 114 | /** Promote uint8_t to uint16_t */ |
| 115 | template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ }; |
| 116 | /** Promote int8_t to int16_t */ |
| 117 | template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ }; |
| 118 | /** Promote uint16_t to uint32_t */ |
| 119 | template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ }; |
| 120 | /** Promote int16_t to int32_t */ |
| 121 | template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ }; |
| 122 | /** Promote uint32_t to uint64_t */ |
| 123 | template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ }; |
| 124 | /** Promote int32_t to int64_t */ |
| 125 | template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ }; |
| 126 | /** Promote float to float */ |
| 127 | template <> struct promote<float> { using type = float; /**< Promoted type */ }; |
| 128 | /** Promote half to half */ |
| 129 | template <> struct promote<half> { using type = half; /**< Promoted type */ }; |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 130 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 131 | /** Get promoted type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | template <typename T> |
| 133 | using promote_t = typename promote<T>::type; |
| 134 | |
| 135 | template <typename T> |
| 136 | using make_signed_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_signed<T>, std::common_type<T>>::type; |
John Richardson | 3c5f949 | 2017-10-04 15:27:37 +0100 | [diff] [blame] | 137 | |
| 138 | template <typename T> |
| 139 | using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; |
| 140 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | // clang-format on |
| 142 | // *INDENT-ON* |
| 143 | } |
| 144 | |
| 145 | /** Look up the format corresponding to a channel. |
| 146 | * |
| 147 | * @param[in] channel Channel type. |
| 148 | * |
| 149 | * @return Format that contains the given channel. |
| 150 | */ |
| 151 | inline Format get_format_for_channel(Channel channel) |
| 152 | { |
| 153 | switch(channel) |
| 154 | { |
| 155 | case Channel::R: |
| 156 | case Channel::G: |
| 157 | case Channel::B: |
| 158 | return Format::RGB888; |
| 159 | default: |
| 160 | throw std::runtime_error("Unsupported channel"); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** Return the format of a channel. |
| 165 | * |
| 166 | * @param[in] channel Channel type. |
| 167 | * |
| 168 | * @return Format of the given channel. |
| 169 | */ |
| 170 | inline Format get_channel_format(Channel channel) |
| 171 | { |
| 172 | switch(channel) |
| 173 | { |
| 174 | case Channel::R: |
| 175 | case Channel::G: |
| 176 | case Channel::B: |
| 177 | return Format::U8; |
| 178 | default: |
| 179 | throw std::runtime_error("Unsupported channel"); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** Base case of foldl. |
| 184 | * |
| 185 | * @return value. |
| 186 | */ |
| 187 | template <typename F, typename T> |
| 188 | inline T foldl(F &&, const T &value) |
| 189 | { |
| 190 | return value; |
| 191 | } |
| 192 | |
| 193 | /** Base case of foldl. |
| 194 | * |
| 195 | * @return func(value1, value2). |
| 196 | */ |
| 197 | template <typename F, typename T, typename U> |
| 198 | inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2)) |
| 199 | { |
| 200 | return func(value1, value2); |
| 201 | } |
| 202 | |
| 203 | /** Fold left. |
| 204 | * |
| 205 | * @param[in] func Binary function to be called. |
| 206 | * @param[in] initial Initial value. |
| 207 | * @param[in] value Argument passed to the function. |
| 208 | * @param[in] values Remaining arguments. |
| 209 | */ |
| 210 | template <typename F, typename I, typename T, typename... Vs> |
| 211 | inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values) |
| 212 | { |
| 213 | return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...); |
| 214 | } |
| 215 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 216 | /** Create a valid region based on tensor shape, border mode and border size |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 217 | * |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 218 | * @param[in] a_shape Shape used as size of the valid region. |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 219 | * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined. |
| 220 | * @param[in] border_size (Optional) Border size used to specify the region to exclude. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 221 | * |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 222 | * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise |
| 223 | * return A valid region starting at (@p border_size.left, @p border_size.top, ...) with reduced size of @p shape. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 224 | */ |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 225 | inline ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined = false, BorderSize border_size = BorderSize(0)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 226 | { |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 227 | ValidRegion valid_region{ Coordinates(), a_shape }; |
| 228 | |
| 229 | Coordinates &anchor = valid_region.anchor; |
| 230 | TensorShape &shape = valid_region.shape; |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 231 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 232 | if(border_undefined) |
| 233 | { |
| 234 | ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2); |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 235 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 236 | anchor.set(0, border_size.left); |
| 237 | anchor.set(1, border_size.top); |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 238 | |
| 239 | const int valid_shape_x = std::max(0, static_cast<int>(shape.x()) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right)); |
| 240 | const int valid_shape_y = std::max(0, static_cast<int>(shape.y()) - static_cast<int>(border_size.top) - static_cast<int>(border_size.bottom)); |
| 241 | |
| 242 | shape.set(0, valid_shape_x); |
| 243 | shape.set(1, valid_shape_y); |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 244 | } |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 245 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 246 | return valid_region; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 249 | /** Create a valid region for Gaussian Pyramid Half based on tensor shape and valid region at level "i - 1" and border mode |
| 250 | * |
| 251 | * @note The border size is 2 in case of Gaussian Pyramid Half |
| 252 | * |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 253 | * @param[in] a_shape Shape used at level "i - 1" of Gaussian Pyramid Half |
| 254 | * @param[in] a_valid_region Valid region used at level "i - 1" of Gaussian Pyramid Half |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 255 | * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined. |
| 256 | * |
| 257 | * return The valid region for the level "i" of Gaussian Pyramid Half |
| 258 | */ |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 259 | inline ValidRegion shape_to_valid_region_gaussian_pyramid_half(const TensorShape &a_shape, const ValidRegion &a_valid_region, bool border_undefined = false) |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 260 | { |
| 261 | constexpr int border_size = 2; |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 262 | |
| 263 | ValidRegion valid_region{ Coordinates(), a_shape }; |
| 264 | |
| 265 | Coordinates &anchor = valid_region.anchor; |
| 266 | TensorShape &shape = valid_region.shape; |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 267 | |
| 268 | // Compute tensor shape for level "i" of Gaussian Pyramid Half |
| 269 | // dst_width = (src_width + 1) * 0.5f |
| 270 | // dst_height = (src_height + 1) * 0.5f |
Gian Marco Iodice | 2abb216 | 2018-04-11 10:49:04 +0100 | [diff] [blame] | 271 | shape.set(0, (a_shape[0] + 1) * 0.5f); |
| 272 | shape.set(1, (a_shape[1] + 1) * 0.5f); |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 273 | |
| 274 | if(border_undefined) |
| 275 | { |
| 276 | ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2); |
| 277 | |
| 278 | // Compute the left and top invalid borders |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 279 | float invalid_border_left = static_cast<float>(a_valid_region.anchor.x() + border_size) / 2.0f; |
| 280 | float invalid_border_top = static_cast<float>(a_valid_region.anchor.y() + border_size) / 2.0f; |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 281 | |
| 282 | // For the new anchor point we can have 2 cases: |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 283 | // 1) If the width/height of the tensor shape is odd, we have to take the ceil value of (a_valid_region.anchor.x() + border_size) / 2.0f or (a_valid_region.anchor.y() + border_size / 2.0f |
| 284 | // 2) If the width/height of the tensor shape is even, we have to take the floor value of (a_valid_region.anchor.x() + border_size) / 2.0f or (a_valid_region.anchor.y() + border_size) / 2.0f |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 285 | // In this manner we should be able to propagate correctly the valid region along all levels of the pyramid |
Gian Marco Iodice | 2abb216 | 2018-04-11 10:49:04 +0100 | [diff] [blame] | 286 | invalid_border_left = (a_shape[0] % 2) ? std::ceil(invalid_border_left) : std::floor(invalid_border_left); |
| 287 | invalid_border_top = (a_shape[1] % 2) ? std::ceil(invalid_border_top) : std::floor(invalid_border_top); |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 288 | |
| 289 | // Set the anchor point |
| 290 | anchor.set(0, static_cast<int>(invalid_border_left)); |
| 291 | anchor.set(1, static_cast<int>(invalid_border_top)); |
| 292 | |
| 293 | // Compute shape |
| 294 | // Calculate the right and bottom invalid borders at the previous level of the pyramid |
Gian Marco Iodice | 2abb216 | 2018-04-11 10:49:04 +0100 | [diff] [blame] | 295 | const float prev_invalid_border_right = static_cast<float>(a_shape[0] - (a_valid_region.anchor.x() + a_valid_region.shape[0])); |
| 296 | const float prev_invalid_border_bottom = static_cast<float>(a_shape[1] - (a_valid_region.anchor.y() + a_valid_region.shape[1])); |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 297 | |
| 298 | // Calculate the right and bottom invalid borders at the current level of the pyramid |
| 299 | const float invalid_border_right = std::ceil((prev_invalid_border_right + static_cast<float>(border_size)) / 2.0f); |
| 300 | const float invalid_border_bottom = std::ceil((prev_invalid_border_bottom + static_cast<float>(border_size)) / 2.0f); |
| 301 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 302 | const int valid_shape_x = std::max(0, static_cast<int>(shape.x()) - static_cast<int>(invalid_border_left) - static_cast<int>(invalid_border_right)); |
| 303 | const int valid_shape_y = std::max(0, static_cast<int>(shape.y()) - static_cast<int>(invalid_border_top) - static_cast<int>(invalid_border_bottom)); |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 304 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 305 | shape.set(0, valid_shape_x); |
| 306 | shape.set(1, valid_shape_y); |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 309 | return valid_region; |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 310 | } |
| 311 | |
John Richardson | 2d008a4 | 2018-03-22 13:48:41 +0000 | [diff] [blame] | 312 | /** Create a valid region for Laplacian Pyramid based on tensor shape and valid region at level "i - 1" and border mode |
| 313 | * |
| 314 | * @note The border size is 2 in case of Laplacian Pyramid |
| 315 | * |
| 316 | * @param[in] a_shape Shape used at level "i - 1" of Laplacian Pyramid |
| 317 | * @param[in] a_valid_region Valid region used at level "i - 1" of Laplacian Pyramid |
| 318 | * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined. |
| 319 | * |
| 320 | * return The valid region for the level "i" of Laplacian Pyramid |
| 321 | */ |
| 322 | inline ValidRegion shape_to_valid_region_laplacian_pyramid(const TensorShape &a_shape, const ValidRegion &a_valid_region, bool border_undefined = false) |
| 323 | { |
| 324 | ValidRegion valid_region = shape_to_valid_region_gaussian_pyramid_half(a_shape, a_valid_region, border_undefined); |
| 325 | |
| 326 | if(border_undefined) |
| 327 | { |
| 328 | const BorderSize gaussian5x5_border(2); |
| 329 | |
| 330 | auto border_left = static_cast<int>(gaussian5x5_border.left); |
| 331 | auto border_right = static_cast<int>(gaussian5x5_border.right); |
| 332 | auto border_top = static_cast<int>(gaussian5x5_border.top); |
| 333 | auto border_bottom = static_cast<int>(gaussian5x5_border.bottom); |
| 334 | |
| 335 | valid_region.anchor.set(0, valid_region.anchor[0] + border_left); |
| 336 | valid_region.anchor.set(1, valid_region.anchor[1] + border_top); |
| 337 | valid_region.shape.set(0, std::max(0, static_cast<int>(valid_region.shape[0]) - border_right - border_left)); |
| 338 | valid_region.shape.set(1, std::max(0, static_cast<int>(valid_region.shape[1]) - border_top - border_bottom)); |
| 339 | } |
| 340 | |
| 341 | return valid_region; |
| 342 | } |
| 343 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 344 | /** Write the value after casting the pointer according to @p data_type. |
| 345 | * |
| 346 | * @warning The type of the value must match the specified data type. |
| 347 | * |
| 348 | * @param[out] ptr Pointer to memory where the @p value will be written. |
| 349 | * @param[in] value Value that will be written. |
| 350 | * @param[in] data_type Data type that will be written. |
| 351 | */ |
| 352 | template <typename T> |
| 353 | void store_value_with_data_type(void *ptr, T value, DataType data_type) |
| 354 | { |
| 355 | switch(data_type) |
| 356 | { |
| 357 | case DataType::U8: |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 358 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 359 | *reinterpret_cast<uint8_t *>(ptr) = value; |
| 360 | break; |
| 361 | case DataType::S8: |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 362 | case DataType::QASYMM8_SIGNED: |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 363 | case DataType::QSYMM8: |
| 364 | case DataType::QSYMM8_PER_CHANNEL: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 365 | *reinterpret_cast<int8_t *>(ptr) = value; |
| 366 | break; |
| 367 | case DataType::U16: |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 368 | case DataType::QASYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 369 | *reinterpret_cast<uint16_t *>(ptr) = value; |
| 370 | break; |
| 371 | case DataType::S16: |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 372 | case DataType::QSYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 373 | *reinterpret_cast<int16_t *>(ptr) = value; |
| 374 | break; |
| 375 | case DataType::U32: |
| 376 | *reinterpret_cast<uint32_t *>(ptr) = value; |
| 377 | break; |
| 378 | case DataType::S32: |
| 379 | *reinterpret_cast<int32_t *>(ptr) = value; |
| 380 | break; |
| 381 | case DataType::U64: |
| 382 | *reinterpret_cast<uint64_t *>(ptr) = value; |
| 383 | break; |
| 384 | case DataType::S64: |
| 385 | *reinterpret_cast<int64_t *>(ptr) = value; |
| 386 | break; |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 387 | case DataType::BFLOAT16: |
| 388 | *reinterpret_cast<bfloat16 *>(ptr) = bfloat16(value); |
| 389 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 390 | case DataType::F16: |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 391 | *reinterpret_cast<half *>(ptr) = value; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 392 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 393 | case DataType::F32: |
| 394 | *reinterpret_cast<float *>(ptr) = value; |
| 395 | break; |
| 396 | case DataType::F64: |
| 397 | *reinterpret_cast<double *>(ptr) = value; |
| 398 | break; |
| 399 | case DataType::SIZET: |
| 400 | *reinterpret_cast<size_t *>(ptr) = value; |
| 401 | break; |
| 402 | default: |
| 403 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /** Saturate a value of type T against the numeric limits of type U. |
| 408 | * |
| 409 | * @param[in] val Value to be saturated. |
| 410 | * |
| 411 | * @return saturated value. |
| 412 | */ |
| 413 | template <typename U, typename T> |
| 414 | T saturate_cast(T val) |
| 415 | { |
| 416 | if(val > static_cast<T>(std::numeric_limits<U>::max())) |
| 417 | { |
| 418 | val = static_cast<T>(std::numeric_limits<U>::max()); |
| 419 | } |
| 420 | if(val < static_cast<T>(std::numeric_limits<U>::lowest())) |
| 421 | { |
| 422 | val = static_cast<T>(std::numeric_limits<U>::lowest()); |
| 423 | } |
| 424 | return val; |
| 425 | } |
| 426 | |
| 427 | /** Find the signed promoted common type. |
| 428 | */ |
| 429 | template <typename... T> |
| 430 | struct common_promoted_signed_type |
| 431 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 432 | /** Common type */ |
| 433 | using common_type = typename std::common_type<T...>::type; |
| 434 | /** Promoted type */ |
| 435 | using promoted_type = traits::promote_t<common_type>; |
| 436 | /** Intermediate type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 437 | using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type; |
| 438 | }; |
| 439 | |
John Richardson | 3c5f949 | 2017-10-04 15:27:37 +0100 | [diff] [blame] | 440 | /** Find the unsigned promoted common type. |
| 441 | */ |
| 442 | template <typename... T> |
| 443 | struct common_promoted_unsigned_type |
| 444 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 445 | /** Common type */ |
| 446 | using common_type = typename std::common_type<T...>::type; |
| 447 | /** Promoted type */ |
| 448 | using promoted_type = traits::promote_t<common_type>; |
| 449 | /** Intermediate type */ |
John Richardson | 3c5f949 | 2017-10-04 15:27:37 +0100 | [diff] [blame] | 450 | using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type; |
| 451 | }; |
| 452 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 453 | /** Convert a linear index into n-dimensional coordinates. |
| 454 | * |
| 455 | * @param[in] shape Shape of the n-dimensional tensor. |
| 456 | * @param[in] index Linear index specifying the i-th element. |
| 457 | * |
| 458 | * @return n-dimensional coordinates. |
| 459 | */ |
| 460 | inline Coordinates index2coord(const TensorShape &shape, int index) |
| 461 | { |
| 462 | int num_elements = shape.total_size(); |
| 463 | |
| 464 | ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]"); |
| 465 | ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape"); |
| 466 | |
| 467 | Coordinates coord{ 0 }; |
| 468 | |
| 469 | for(int d = shape.num_dimensions() - 1; d >= 0; --d) |
| 470 | { |
| 471 | num_elements /= shape[d]; |
| 472 | coord.set(d, index / num_elements); |
| 473 | index %= num_elements; |
| 474 | } |
| 475 | |
| 476 | return coord; |
| 477 | } |
| 478 | |
| 479 | /** Linearise the given coordinate. |
| 480 | * |
| 481 | * Transforms the given coordinate into a linear offset in terms of |
| 482 | * elements. |
| 483 | * |
| 484 | * @param[in] shape Shape of the n-dimensional tensor. |
| 485 | * @param[in] coord The to be converted coordinate. |
| 486 | * |
| 487 | * @return Linear offset to the element. |
| 488 | */ |
| 489 | inline int coord2index(const TensorShape &shape, const Coordinates &coord) |
| 490 | { |
| 491 | ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape"); |
| 492 | ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate"); |
| 493 | |
| 494 | int index = 0; |
| 495 | int dim_size = 1; |
| 496 | |
| 497 | for(unsigned int i = 0; i < coord.num_dimensions(); ++i) |
| 498 | { |
| 499 | index += coord[i] * dim_size; |
| 500 | dim_size *= shape[i]; |
| 501 | } |
| 502 | |
| 503 | return index; |
| 504 | } |
| 505 | |
| 506 | /** Check if a coordinate is within a valid region */ |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 507 | inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 508 | { |
Moritz Pflanzer | 219c691 | 2017-09-23 19:22:51 +0100 | [diff] [blame] | 509 | for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d) |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 510 | { |
| 511 | if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 512 | { |
| 513 | return false; |
| 514 | } |
| 515 | } |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 516 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 517 | return true; |
| 518 | } |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 519 | |
| 520 | /** Create and initialize a tensor of the given type. |
| 521 | * |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 522 | * @param[in] shape Tensor shape. |
| 523 | * @param[in] data_type Data type. |
| 524 | * @param[in] num_channels (Optional) Number of channels. |
| 525 | * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types. |
| 526 | * @param[in] data_layout (Optional) Data layout. Default is NCHW. |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 527 | * @param[in] ctx (Optional) Pointer to the runtime context. |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 528 | * |
| 529 | * @return Initialized tensor of given type. |
| 530 | */ |
| 531 | template <typename T> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 532 | inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1, |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 533 | QuantizationInfo quantization_info = QuantizationInfo(), DataLayout data_layout = DataLayout::NCHW, IRuntimeContext *ctx = nullptr) |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 534 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 535 | T tensor(ctx); |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 536 | TensorInfo info(shape, num_channels, data_type); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 537 | info.set_quantization_info(quantization_info); |
Michalis Spyrou | cf581f5 | 2018-03-02 10:25:59 +0000 | [diff] [blame] | 538 | info.set_data_layout(data_layout); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 539 | tensor.allocator()->init(info); |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 540 | |
| 541 | return tensor; |
| 542 | } |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 543 | |
Ioan-Cristian Szabo | 2c35018 | 2017-12-20 16:27:37 +0000 | [diff] [blame] | 544 | /** Create and initialize a tensor of the given type. |
| 545 | * |
| 546 | * @param[in] shape Tensor shape. |
| 547 | * @param[in] format Format type. |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 548 | * @param[in] ctx (Optional) Pointer to the runtime context. |
Ioan-Cristian Szabo | 2c35018 | 2017-12-20 16:27:37 +0000 | [diff] [blame] | 549 | * |
| 550 | * @return Initialized tensor of given type. |
| 551 | */ |
| 552 | template <typename T> |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 553 | inline T create_tensor(const TensorShape &shape, Format format, IRuntimeContext *ctx = nullptr) |
Ioan-Cristian Szabo | 2c35018 | 2017-12-20 16:27:37 +0000 | [diff] [blame] | 554 | { |
| 555 | TensorInfo info(shape, format); |
| 556 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 557 | T tensor(ctx); |
Ioan-Cristian Szabo | 2c35018 | 2017-12-20 16:27:37 +0000 | [diff] [blame] | 558 | tensor.allocator()->init(info); |
| 559 | |
| 560 | return tensor; |
| 561 | } |
| 562 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 563 | /** Create and initialize a multi-image of the given type. |
| 564 | * |
| 565 | * @param[in] shape Tensor shape. |
| 566 | * @param[in] format Format type. |
| 567 | * |
| 568 | * @return Initialized tensor of given type. |
| 569 | */ |
| 570 | template <typename T> |
| 571 | inline T create_multi_image(const TensorShape &shape, Format format) |
| 572 | { |
| 573 | T multi_image; |
| 574 | multi_image.init(shape.x(), shape.y(), format); |
| 575 | |
| 576 | return multi_image; |
| 577 | } |
| 578 | |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 579 | /** Create and initialize a HOG (Histogram of Oriented Gradients) of the given type. |
| 580 | * |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 581 | * @param[in] hog_info HOGInfo object |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 582 | * |
| 583 | * @return Initialized HOG of given type. |
| 584 | */ |
| 585 | template <typename T> |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 586 | inline T create_HOG(const HOGInfo &hog_info) |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 587 | { |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 588 | T hog; |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 589 | hog.init(hog_info); |
| 590 | |
| 591 | return hog; |
| 592 | } |
| 593 | |
John Richardson | 8de9261 | 2018-02-22 14:09:31 +0000 | [diff] [blame] | 594 | /** Create and initialize a Pyramid of the given type. |
| 595 | * |
| 596 | * @param[in] pyramid_info The PyramidInfo object. |
| 597 | * |
| 598 | * @return Initialized Pyramid of given type. |
| 599 | */ |
| 600 | template <typename T> |
| 601 | inline T create_pyramid(const PyramidInfo &pyramid_info) |
| 602 | { |
| 603 | T pyramid; |
| 604 | pyramid.init_auto_padding(pyramid_info); |
| 605 | |
| 606 | return pyramid; |
| 607 | } |
| 608 | |
John Richardson | 32af1f8 | 2018-06-05 12:47:20 +0100 | [diff] [blame] | 609 | /** Initialize a convolution matrix. |
| 610 | * |
| 611 | * @param[in, out] conv The input convolution matrix. |
| 612 | * @param[in] width The width of the convolution matrix. |
| 613 | * @param[in] height The height of the convolution matrix. |
| 614 | * @param[in] seed The random seed to be used. |
| 615 | */ |
| 616 | inline void init_conv(int16_t *conv, unsigned int width, unsigned int height, std::random_device::result_type seed) |
| 617 | { |
| 618 | std::mt19937 gen(seed); |
| 619 | std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767); |
| 620 | |
| 621 | for(unsigned int i = 0; i < width * height; ++i) |
| 622 | { |
| 623 | conv[i] = distribution_int16(gen); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /** Initialize a separable convolution matrix. |
| 628 | * |
| 629 | * @param[in, out] conv The input convolution matrix. |
| 630 | * @param[in] width The width of the convolution matrix. |
| 631 | * @param[in] height The height of the convolution matrix. |
| 632 | * @param[in] seed The random seed to be used. |
| 633 | */ |
| 634 | inline void init_separable_conv(int16_t *conv, unsigned int width, unsigned int height, std::random_device::result_type seed) |
| 635 | { |
| 636 | std::mt19937 gen(seed); |
| 637 | // Set it between -128 and 127 to ensure the matrix does not overflow |
| 638 | std::uniform_int_distribution<int16_t> distribution_int16(-128, 127); |
| 639 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 640 | int16_t *conv_row = new int16_t[width]; |
| 641 | int16_t *conv_col = new int16_t[height]; |
John Richardson | 32af1f8 | 2018-06-05 12:47:20 +0100 | [diff] [blame] | 642 | |
| 643 | conv_row[0] = conv_col[0] = 1; |
| 644 | for(unsigned int i = 1; i < width; ++i) |
| 645 | { |
| 646 | conv_row[i] = distribution_int16(gen); |
| 647 | } |
| 648 | |
| 649 | for(unsigned int i = 1; i < height; ++i) |
| 650 | { |
| 651 | conv_col[i] = distribution_int16(gen); |
| 652 | } |
| 653 | |
| 654 | // Multiply two matrices |
| 655 | for(unsigned int i = 0; i < width; ++i) |
| 656 | { |
| 657 | for(unsigned int j = 0; j < height; ++j) |
| 658 | { |
| 659 | conv[i * width + j] = conv_col[i] * conv_row[j]; |
| 660 | } |
| 661 | } |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 662 | |
| 663 | delete[] conv_row; |
| 664 | delete[] conv_col; |
John Richardson | 32af1f8 | 2018-06-05 12:47:20 +0100 | [diff] [blame] | 665 | } |
| 666 | |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 667 | /** Create a vector with a uniform distribution of floating point values across the specified range. |
| 668 | * |
| 669 | * @param[in] num_values The number of values to be created. |
| 670 | * @param[in] min The minimum value in distribution (inclusive). |
| 671 | * @param[in] max The maximum value in distribution (inclusive). |
| 672 | * @param[in] seed The random seed to be used. |
| 673 | * |
| 674 | * @return A vector that contains the requested number of random floating point values |
| 675 | */ |
| 676 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 677 | inline std::vector<T> generate_random_real(unsigned int num_values, T min, T max, std::random_device::result_type seed) |
| 678 | { |
| 679 | std::vector<T> v(num_values); |
| 680 | std::mt19937 gen(seed); |
| 681 | std::uniform_real_distribution<T> dist(min, max); |
| 682 | |
| 683 | for(unsigned int i = 0; i < num_values; ++i) |
| 684 | { |
| 685 | v.at(i) = dist(gen); |
| 686 | } |
| 687 | |
| 688 | return v; |
| 689 | } |
| 690 | |
John Richardson | 8de9261 | 2018-02-22 14:09:31 +0000 | [diff] [blame] | 691 | /** Create a vector of random keypoints for pyramid representation. |
| 692 | * |
| 693 | * @param[in] shape The shape of the input tensor. |
| 694 | * @param[in] num_keypoints The number of keypoints to be created. |
| 695 | * @param[in] seed The random seed to be used. |
| 696 | * @param[in] num_levels The number of pyramid levels. |
| 697 | * |
| 698 | * @return A vector that contains the requested number of random keypoints |
| 699 | */ |
| 700 | inline std::vector<KeyPoint> generate_random_keypoints(const TensorShape &shape, size_t num_keypoints, std::random_device::result_type seed, size_t num_levels = 1) |
| 701 | { |
| 702 | std::vector<KeyPoint> keypoints; |
| 703 | std::mt19937 gen(seed); |
| 704 | |
| 705 | // Calculate distribution bounds |
| 706 | const auto min = static_cast<int>(std::pow(2, num_levels)); |
| 707 | const auto max_width = static_cast<int>(shape.x()); |
| 708 | const auto max_height = static_cast<int>(shape.y()); |
| 709 | |
| 710 | ARM_COMPUTE_ERROR_ON(min > max_width || min > max_height); |
| 711 | |
| 712 | // Create distributions |
| 713 | std::uniform_int_distribution<> dist_w(min, max_width); |
| 714 | std::uniform_int_distribution<> dist_h(min, max_height); |
| 715 | |
| 716 | for(unsigned int i = 0; i < num_keypoints; i++) |
| 717 | { |
| 718 | KeyPoint keypoint; |
| 719 | keypoint.x = dist_w(gen); |
| 720 | keypoint.y = dist_h(gen); |
| 721 | keypoint.tracking_status = 1; |
| 722 | |
| 723 | keypoints.push_back(keypoint); |
| 724 | } |
| 725 | |
| 726 | return keypoints; |
| 727 | } |
| 728 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 729 | template <typename T, typename ArrayAccessor_T> |
| 730 | inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v) |
| 731 | { |
| 732 | array.resize(v.size()); |
| 733 | std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T)); |
| 734 | } |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 735 | |
| 736 | /** Obtain numpy type string from DataType. |
| 737 | * |
| 738 | * @param[in] data_type Data type. |
| 739 | * |
| 740 | * @return numpy type string. |
| 741 | */ |
| 742 | inline std::string get_typestring(DataType data_type) |
| 743 | { |
| 744 | // Check endianness |
| 745 | const unsigned int i = 1; |
| 746 | const char *c = reinterpret_cast<const char *>(&i); |
| 747 | std::string endianness; |
| 748 | if(*c == 1) |
| 749 | { |
| 750 | endianness = std::string("<"); |
| 751 | } |
| 752 | else |
| 753 | { |
| 754 | endianness = std::string(">"); |
| 755 | } |
| 756 | const std::string no_endianness("|"); |
| 757 | |
| 758 | switch(data_type) |
| 759 | { |
| 760 | case DataType::U8: |
| 761 | return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t)); |
| 762 | case DataType::S8: |
| 763 | return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t)); |
| 764 | case DataType::U16: |
| 765 | return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t)); |
| 766 | case DataType::S16: |
| 767 | return endianness + "i" + support::cpp11::to_string(sizeof(int16_t)); |
| 768 | case DataType::U32: |
| 769 | return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t)); |
| 770 | case DataType::S32: |
| 771 | return endianness + "i" + support::cpp11::to_string(sizeof(int32_t)); |
| 772 | case DataType::U64: |
| 773 | return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t)); |
| 774 | case DataType::S64: |
| 775 | return endianness + "i" + support::cpp11::to_string(sizeof(int64_t)); |
| 776 | case DataType::F32: |
| 777 | return endianness + "f" + support::cpp11::to_string(sizeof(float)); |
| 778 | case DataType::F64: |
| 779 | return endianness + "f" + support::cpp11::to_string(sizeof(double)); |
| 780 | case DataType::SIZET: |
| 781 | return endianness + "u" + support::cpp11::to_string(sizeof(size_t)); |
| 782 | default: |
| 783 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 784 | } |
| 785 | } |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 786 | |
| 787 | /** Sync if necessary. |
| 788 | */ |
| 789 | template <typename TensorType> |
| 790 | inline void sync_if_necessary() |
| 791 | { |
| 792 | #ifdef ARM_COMPUTE_CL |
| 793 | if(opencl_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::CLTensor>::value) |
| 794 | { |
| 795 | CLScheduler::get().sync(); |
| 796 | } |
| 797 | #endif /* ARM_COMPUTE_CL */ |
| 798 | } |
| 799 | |
| 800 | /** Sync tensor if necessary. |
| 801 | * |
| 802 | * @note: If the destination tensor not being used on OpenGL ES, GPU will optimize out the operation. |
| 803 | * |
| 804 | * @param[in] tensor Tensor to be sync. |
| 805 | */ |
| 806 | template <typename TensorType> |
| 807 | inline void sync_tensor_if_necessary(TensorType &tensor) |
| 808 | { |
| 809 | #ifdef ARM_COMPUTE_GC |
| 810 | if(opengles31_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::GCTensor>::value) |
| 811 | { |
| 812 | // Force sync the tensor by calling map and unmap. |
| 813 | IGCTensor &t = dynamic_cast<IGCTensor &>(tensor); |
| 814 | t.map(); |
| 815 | t.unmap(); |
| 816 | } |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 817 | #else /* ARM_COMPUTE_GC */ |
| 818 | ARM_COMPUTE_UNUSED(tensor); |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 819 | #endif /* ARM_COMPUTE_GC */ |
| 820 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 821 | } // namespace test |
| 822 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 823 | #endif /* ARM_COMPUTE_TEST_UTILS_H */ |