Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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_TENSOR_LIBRARY_H |
| 25 | #define ARM_COMPUTE_TEST_TENSOR_LIBRARY_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Coordinates.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/TensorShape.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/utils/misc/Random.h" |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 35 | #pragma GCC diagnostic push |
| 36 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 37 | #include "libnpy/npy.hpp" |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 38 | #pragma GCC diagnostic pop |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 39 | #include "tests/RawTensor.h" |
| 40 | #include "tests/TensorCache.h" |
| 41 | #include "tests/Utils.h" |
Anthony Barbier | f6705ec | 2017-09-28 12:01:10 +0100 | [diff] [blame] | 42 | #include "tests/framework/Exceptions.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | |
| 44 | #include <algorithm> |
| 45 | #include <cstddef> |
| 46 | #include <fstream> |
| 47 | #include <random> |
| 48 | #include <string> |
| 49 | #include <type_traits> |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 50 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | |
| 52 | namespace arm_compute |
| 53 | { |
| 54 | namespace test |
| 55 | { |
| 56 | /** Factory class to create and fill tensors. |
| 57 | * |
| 58 | * Allows to initialise tensors from loaded images or by specifying the shape |
| 59 | * explicitly. Furthermore, provides methods to fill tensors with the content of |
| 60 | * loaded images or with random values. |
| 61 | */ |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 62 | class AssetsLibrary final |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | { |
| 64 | public: |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 65 | using RangePair = std::pair<float, float>; |
| 66 | |
| 67 | public: |
John Richardson | 70f946b | 2017-10-02 16:52:16 +0100 | [diff] [blame] | 68 | /** Initialises the library with a @p path to the assets directory. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | * Furthermore, sets the seed for the random generator to @p seed. |
| 70 | * |
John Richardson | 70f946b | 2017-10-02 16:52:16 +0100 | [diff] [blame] | 71 | * @param[in] path Path to load assets from. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | * @param[in] seed Seed used to initialise the random number generator. |
| 73 | */ |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 74 | AssetsLibrary(std::string path, std::random_device::result_type seed); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 76 | /** Path to assets directory used to initialise library. |
| 77 | * |
| 78 | * @return the path to the assets directory. |
| 79 | */ |
John Richardson | 70f946b | 2017-10-02 16:52:16 +0100 | [diff] [blame] | 80 | std::string path() const; |
| 81 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 82 | /** Seed that is used to fill tensors with random values. |
| 83 | * |
| 84 | * @return the initial random seed. |
| 85 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | std::random_device::result_type seed() const; |
| 87 | |
Giorgio Arena | fda4618 | 2017-06-16 13:57:33 +0100 | [diff] [blame] | 88 | /** Provides a tensor shape for the specified image. |
| 89 | * |
| 90 | * @param[in] name Image file used to look up the raw tensor. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 91 | * |
| 92 | * @return the tensor shape for the specified image. |
Giorgio Arena | fda4618 | 2017-06-16 13:57:33 +0100 | [diff] [blame] | 93 | */ |
| 94 | TensorShape get_image_shape(const std::string &name); |
| 95 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 96 | /** Provides a constant raw tensor for the specified image. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | * |
| 98 | * @param[in] name Image file used to look up the raw tensor. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 99 | * |
| 100 | * @return a raw tensor for the specified image. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | */ |
| 102 | const RawTensor &get(const std::string &name) const; |
| 103 | |
| 104 | /** Provides a raw tensor for the specified image. |
| 105 | * |
| 106 | * @param[in] name Image file used to look up the raw tensor. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 107 | * |
| 108 | * @return a raw tensor for the specified image. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | */ |
| 110 | RawTensor get(const std::string &name); |
| 111 | |
| 112 | /** Creates an uninitialised raw tensor with the given @p data_type and @p |
| 113 | * num_channels. The shape is derived from the specified image. |
| 114 | * |
| 115 | * @param[in] name Image file used to initialise the tensor. |
| 116 | * @param[in] data_type Data type used to initialise the tensor. |
| 117 | * @param[in] num_channels Number of channels used to initialise the tensor. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 118 | * |
| 119 | * @return a raw tensor for the specified image. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | */ |
| 121 | RawTensor get(const std::string &name, DataType data_type, int num_channels = 1) const; |
| 122 | |
| 123 | /** Provides a contant raw tensor for the specified image after it has been |
| 124 | * converted to @p format. |
| 125 | * |
| 126 | * @param[in] name Image file used to look up the raw tensor. |
| 127 | * @param[in] format Format used to look up the raw tensor. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 128 | * |
| 129 | * @return a raw tensor for the specified image. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | */ |
| 131 | const RawTensor &get(const std::string &name, Format format) const; |
| 132 | |
| 133 | /** Provides a raw tensor for the specified image after it has been |
| 134 | * converted to @p format. |
| 135 | * |
| 136 | * @param[in] name Image file used to look up the raw tensor. |
| 137 | * @param[in] format Format used to look up the raw tensor. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 138 | * |
| 139 | * @return a raw tensor for the specified image. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | */ |
| 141 | RawTensor get(const std::string &name, Format format); |
| 142 | |
| 143 | /** Provides a contant raw tensor for the specified channel after it has |
| 144 | * been extracted form the given image. |
| 145 | * |
| 146 | * @param[in] name Image file used to look up the raw tensor. |
| 147 | * @param[in] channel Channel used to look up the raw tensor. |
| 148 | * |
| 149 | * @note The channel has to be unambiguous so that the format can be |
| 150 | * inferred automatically. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 151 | * |
| 152 | * @return a raw tensor for the specified image channel. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | */ |
| 154 | const RawTensor &get(const std::string &name, Channel channel) const; |
| 155 | |
| 156 | /** Provides a raw tensor for the specified channel after it has been |
| 157 | * extracted form the given image. |
| 158 | * |
| 159 | * @param[in] name Image file used to look up the raw tensor. |
| 160 | * @param[in] channel Channel used to look up the raw tensor. |
| 161 | * |
| 162 | * @note The channel has to be unambiguous so that the format can be |
| 163 | * inferred automatically. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 164 | * |
| 165 | * @return a raw tensor for the specified image channel. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | */ |
| 167 | RawTensor get(const std::string &name, Channel channel); |
| 168 | |
| 169 | /** Provides a constant raw tensor for the specified channel after it has |
| 170 | * been extracted form the given image formatted to @p format. |
| 171 | * |
| 172 | * @param[in] name Image file used to look up the raw tensor. |
| 173 | * @param[in] format Format used to look up the raw tensor. |
| 174 | * @param[in] channel Channel used to look up the raw tensor. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 175 | * |
| 176 | * @return a raw tensor for the specified image channel. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | */ |
| 178 | const RawTensor &get(const std::string &name, Format format, Channel channel) const; |
| 179 | |
| 180 | /** Provides a raw tensor for the specified channel after it has been |
| 181 | * extracted form the given image formatted to @p format. |
| 182 | * |
| 183 | * @param[in] name Image file used to look up the raw tensor. |
| 184 | * @param[in] format Format used to look up the raw tensor. |
| 185 | * @param[in] channel Channel used to look up the raw tensor. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 186 | * |
| 187 | * @return a raw tensor for the specified image channel. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | */ |
| 189 | RawTensor get(const std::string &name, Format format, Channel channel); |
| 190 | |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 191 | /** Puts garbage values all around the tensor for testing purposes |
| 192 | * |
| 193 | * @param[in, out] tensor To be filled tensor. |
| 194 | * @param[in] distribution Distribution used to fill the tensor's surroundings. |
| 195 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 196 | */ |
| 197 | template <typename T, typename D> |
| 198 | void fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const; |
| 199 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 200 | /** Fills the specified @p tensor with random values drawn from @p |
| 201 | * distribution. |
| 202 | * |
| 203 | * @param[in, out] tensor To be filled tensor. |
| 204 | * @param[in] distribution Distribution used to fill the tensor. |
| 205 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 206 | * |
| 207 | * @note The @p distribution has to provide operator(Generator &) which |
| 208 | * will be used to draw samples. |
| 209 | */ |
| 210 | template <typename T, typename D> |
| 211 | void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const; |
| 212 | |
Pablo Tello | e96e4f0 | 2018-12-21 16:47:23 +0000 | [diff] [blame] | 213 | template <typename T, typename D> |
| 214 | void fill_boxes(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const; |
| 215 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | /** Fills the specified @p raw tensor with random values drawn from @p |
| 217 | * distribution. |
| 218 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 219 | * @param[in, out] vec To be filled vector. |
| 220 | * @param[in] distribution Distribution used to fill the tensor. |
| 221 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 222 | * |
| 223 | * @note The @p distribution has to provide operator(Generator &) which |
| 224 | * will be used to draw samples. |
| 225 | */ |
| 226 | template <typename T, typename D> |
| 227 | void fill(std::vector<T> &vec, D &&distribution, std::random_device::result_type seed_offset) const; |
| 228 | |
| 229 | /** Fills the specified @p raw tensor with random values drawn from @p |
| 230 | * distribution. |
| 231 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 232 | * @param[in, out] raw To be filled raw. |
| 233 | * @param[in] distribution Distribution used to fill the tensor. |
| 234 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 235 | * |
| 236 | * @note The @p distribution has to provide operator(Generator &) which |
| 237 | * will be used to draw samples. |
| 238 | */ |
| 239 | template <typename D> |
| 240 | void fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const; |
| 241 | |
| 242 | /** Fills the specified @p tensor with the content of the specified image |
| 243 | * converted to the given format. |
| 244 | * |
| 245 | * @param[in, out] tensor To be filled tensor. |
| 246 | * @param[in] name Image file used to fill the tensor. |
| 247 | * @param[in] format Format of the image used to fill the tensor. |
| 248 | * |
| 249 | * @warning No check is performed that the specified format actually |
| 250 | * matches the format of the tensor. |
| 251 | */ |
| 252 | template <typename T> |
| 253 | void fill(T &&tensor, const std::string &name, Format format) const; |
| 254 | |
| 255 | /** Fills the raw tensor with the content of the specified image |
| 256 | * converted to the given format. |
| 257 | * |
| 258 | * @param[in, out] raw To be filled raw tensor. |
| 259 | * @param[in] name Image file used to fill the tensor. |
| 260 | * @param[in] format Format of the image used to fill the tensor. |
| 261 | * |
| 262 | * @warning No check is performed that the specified format actually |
| 263 | * matches the format of the tensor. |
| 264 | */ |
| 265 | void fill(RawTensor &raw, const std::string &name, Format format) const; |
| 266 | |
| 267 | /** Fills the specified @p tensor with the content of the specified channel |
| 268 | * extracted from the given image. |
| 269 | * |
| 270 | * @param[in, out] tensor To be filled tensor. |
| 271 | * @param[in] name Image file used to fill the tensor. |
| 272 | * @param[in] channel Channel of the image used to fill the tensor. |
| 273 | * |
| 274 | * @note The channel has to be unambiguous so that the format can be |
| 275 | * inferred automatically. |
| 276 | * |
| 277 | * @warning No check is performed that the specified format actually |
| 278 | * matches the format of the tensor. |
| 279 | */ |
| 280 | template <typename T> |
| 281 | void fill(T &&tensor, const std::string &name, Channel channel) const; |
| 282 | |
| 283 | /** Fills the raw tensor with the content of the specified channel |
| 284 | * extracted from the given image. |
| 285 | * |
| 286 | * @param[in, out] raw To be filled raw tensor. |
| 287 | * @param[in] name Image file used to fill the tensor. |
| 288 | * @param[in] channel Channel of the image used to fill the tensor. |
| 289 | * |
| 290 | * @note The channel has to be unambiguous so that the format can be |
| 291 | * inferred automatically. |
| 292 | * |
| 293 | * @warning No check is performed that the specified format actually |
| 294 | * matches the format of the tensor. |
| 295 | */ |
| 296 | void fill(RawTensor &raw, const std::string &name, Channel channel) const; |
| 297 | |
| 298 | /** Fills the specified @p tensor with the content of the specified channel |
| 299 | * extracted from the given image after it has been converted to the given |
| 300 | * format. |
| 301 | * |
| 302 | * @param[in, out] tensor To be filled tensor. |
| 303 | * @param[in] name Image file used to fill the tensor. |
| 304 | * @param[in] format Format of the image used to fill the tensor. |
| 305 | * @param[in] channel Channel of the image used to fill the tensor. |
| 306 | * |
| 307 | * @warning No check is performed that the specified format actually |
| 308 | * matches the format of the tensor. |
| 309 | */ |
| 310 | template <typename T> |
| 311 | void fill(T &&tensor, const std::string &name, Format format, Channel channel) const; |
| 312 | |
| 313 | /** Fills the raw tensor with the content of the specified channel |
| 314 | * extracted from the given image after it has been converted to the given |
| 315 | * format. |
| 316 | * |
| 317 | * @param[in, out] raw To be filled raw tensor. |
| 318 | * @param[in] name Image file used to fill the tensor. |
| 319 | * @param[in] format Format of the image used to fill the tensor. |
| 320 | * @param[in] channel Channel of the image used to fill the tensor. |
| 321 | * |
| 322 | * @warning No check is performed that the specified format actually |
| 323 | * matches the format of the tensor. |
| 324 | */ |
| 325 | void fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const; |
| 326 | |
Alex Gilday | 345ab18 | 2018-01-09 11:40:19 +0000 | [diff] [blame] | 327 | /** Fills the specified @p tensor with the content of the raw tensor. |
| 328 | * |
| 329 | * @param[in, out] tensor To be filled tensor. |
| 330 | * @param[in] raw Raw tensor used to fill the tensor. |
| 331 | * |
| 332 | * @warning No check is performed that the specified format actually |
| 333 | * matches the format of the tensor. |
| 334 | */ |
| 335 | template <typename T> |
| 336 | void fill(T &&tensor, RawTensor raw) const; |
| 337 | |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 338 | /** Fill a tensor with uniform distribution |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 339 | * |
| 340 | * @param[in, out] tensor To be filled tensor. |
| 341 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 342 | */ |
| 343 | template <typename T> |
| 344 | void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const; |
| 345 | |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 346 | /** Fill a tensor with uniform distribution |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 347 | * |
| 348 | * @param[in, out] tensor To be filled tensor. |
| 349 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 350 | * @param[in] low lowest value in the range (inclusive) |
| 351 | * @param[in] high highest value in the range (inclusive) |
| 352 | * |
| 353 | * @note @p low and @p high must be of the same type as the data type of @p tensor |
| 354 | */ |
| 355 | template <typename T, typename D> |
| 356 | void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high) const; |
| 357 | |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 358 | /** Fill a tensor with uniform distribution across the specified range |
| 359 | * |
| 360 | * @param[in, out] tensor To be filled tensor. |
| 361 | * @param[in] seed_offset The offset will be added to the global seed before initialising the random generator. |
| 362 | * @param[in] excluded_range_pairs Ranges to exclude from the generator |
| 363 | */ |
| 364 | template <typename T> |
| 365 | void fill_tensor_uniform_ranged(T &&tensor, |
| 366 | std::random_device::result_type seed_offset, |
| 367 | const std::vector<AssetsLibrary::RangePair> &excluded_range_pairs) const; |
| 368 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 369 | /** Fills the specified @p tensor with data loaded from .npy (numpy binary) in specified path. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 370 | * |
| 371 | * @param[in, out] tensor To be filled tensor. |
| 372 | * @param[in] name Data file. |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 373 | * |
| 374 | * @note The numpy array stored in the binary .npy file must be row-major in the sense that it |
| 375 | * must store elements within a row consecutively in the memory, then rows within a 2D slice, |
| 376 | * then 2D slices within a 3D slice and so on. Note that it imposes no restrictions on what |
| 377 | * indexing convention is used in the numpy array. That is, the numpy array can be either fortran |
| 378 | * style or C style as long as it adheres to the rule above. |
| 379 | * |
| 380 | * More concretely, the orders of dimensions for each style are as follows: |
| 381 | * C-style (numpy default): |
| 382 | * array[HigherDims..., Z, Y, X] |
| 383 | * Fortran style: |
| 384 | * array[X, Y, Z, HigherDims...] |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 385 | */ |
| 386 | template <typename T> |
| 387 | void fill_layer_data(T &&tensor, std::string name) const; |
| 388 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 389 | /** Fill a tensor with a constant value |
| 390 | * |
| 391 | * @param[in, out] tensor To be filled tensor. |
| 392 | * @param[in] value Value to be assigned to all elements of the input tensor. |
| 393 | * |
| 394 | * @note @p value must be of the same type as the data type of @p tensor |
| 395 | */ |
| 396 | template <typename T, typename D> |
| 397 | void fill_tensor_value(T &&tensor, D value) const; |
| 398 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 399 | private: |
| 400 | // Function prototype to convert between image formats. |
| 401 | using Converter = void (*)(const RawTensor &src, RawTensor &dst); |
| 402 | // Function prototype to extract a channel from an image. |
| 403 | using Extractor = void (*)(const RawTensor &src, RawTensor &dst); |
| 404 | // Function prototype to load an image file. |
| 405 | using Loader = RawTensor (*)(const std::string &path); |
| 406 | |
| 407 | const Converter &get_converter(Format src, Format dst) const; |
| 408 | const Converter &get_converter(DataType src, Format dst) const; |
| 409 | const Converter &get_converter(Format src, DataType dst) const; |
| 410 | const Converter &get_converter(DataType src, DataType dst) const; |
| 411 | const Extractor &get_extractor(Format format, Channel) const; |
| 412 | const Loader &get_loader(const std::string &extension) const; |
| 413 | |
| 414 | /** Creates a raw tensor from the specified image. |
| 415 | * |
| 416 | * @param[in] name To be loaded image file. |
| 417 | * |
| 418 | * @note If use_single_image is true @p name is ignored and the user image |
| 419 | * is loaded instead. |
| 420 | */ |
| 421 | RawTensor load_image(const std::string &name) const; |
| 422 | |
| 423 | /** Provides a raw tensor for the specified image and format. |
| 424 | * |
| 425 | * @param[in] name Image file used to look up the raw tensor. |
| 426 | * @param[in] format Format used to look up the raw tensor. |
| 427 | * |
| 428 | * If the tensor has already been requested before the cached version will |
| 429 | * be returned. Otherwise the tensor will be added to the cache. |
| 430 | * |
| 431 | * @note If use_single_image is true @p name is ignored and the user image |
| 432 | * is loaded instead. |
| 433 | */ |
| 434 | const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format) const; |
| 435 | |
| 436 | /** Provides a raw tensor for the specified image, format and channel. |
| 437 | * |
| 438 | * @param[in] name Image file used to look up the raw tensor. |
| 439 | * @param[in] format Format used to look up the raw tensor. |
| 440 | * @param[in] channel Channel used to look up the raw tensor. |
| 441 | * |
| 442 | * If the tensor has already been requested before the cached version will |
| 443 | * be returned. Otherwise the tensor will be added to the cache. |
| 444 | * |
| 445 | * @note If use_single_image is true @p name is ignored and the user image |
| 446 | * is loaded instead. |
| 447 | */ |
| 448 | const RawTensor &find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const; |
| 449 | |
| 450 | mutable TensorCache _cache{}; |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 451 | mutable arm_compute::Mutex _format_lock{}; |
| 452 | mutable arm_compute::Mutex _channel_lock{}; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 453 | const std::string _library_path; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 454 | std::random_device::result_type _seed; |
| 455 | }; |
| 456 | |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 457 | namespace detail |
| 458 | { |
| 459 | template <typename T> |
| 460 | inline std::vector<std::pair<T, T>> convert_range_pair(const std::vector<AssetsLibrary::RangePair> &excluded_range_pairs) |
| 461 | { |
| 462 | std::vector<std::pair<T, T>> converted; |
| 463 | std::transform(excluded_range_pairs.begin(), |
| 464 | excluded_range_pairs.end(), |
| 465 | std::back_inserter(converted), |
| 466 | [](const AssetsLibrary::RangePair & p) |
| 467 | { |
| 468 | return std::pair<T, T>(static_cast<T>(p.first), static_cast<T>(p.second)); |
| 469 | }); |
| 470 | return converted; |
| 471 | } |
| 472 | } // namespace detail |
| 473 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 474 | template <typename T, typename D> |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 475 | void AssetsLibrary::fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const |
| 476 | { |
| 477 | const PaddingSize padding_size = tensor.padding(); |
| 478 | |
| 479 | Window window; |
| 480 | window.set(0, Window::Dimension(-padding_size.left, tensor.shape()[0] + padding_size.right, 1)); |
Gian Marco | 5420b28 | 2017-11-29 10:41:38 +0000 | [diff] [blame] | 481 | if(tensor.shape().num_dimensions() > 1) |
| 482 | { |
| 483 | window.set(1, Window::Dimension(-padding_size.top, tensor.shape()[1] + padding_size.bottom, 1)); |
| 484 | } |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 485 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 486 | std::mt19937 gen(_seed + seed_offset); |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 487 | |
| 488 | execute_window_loop(window, [&](const Coordinates & id) |
| 489 | { |
| 490 | TensorShape shape = tensor.shape(); |
| 491 | |
| 492 | // If outside of valid region |
| 493 | if(id.x() < 0 || id.x() >= static_cast<int>(shape.x()) || id.y() < 0 || id.y() >= static_cast<int>(shape.y())) |
| 494 | { |
| 495 | using ResultType = typename std::remove_reference<D>::type::result_type; |
| 496 | const ResultType value = distribution(gen); |
| 497 | void *const out_ptr = tensor(id); |
| 498 | store_value_with_data_type(out_ptr, value, tensor.data_type()); |
| 499 | } |
| 500 | }); |
| 501 | } |
| 502 | |
| 503 | template <typename T, typename D> |
Pablo Tello | e96e4f0 | 2018-12-21 16:47:23 +0000 | [diff] [blame] | 504 | void AssetsLibrary::fill_boxes(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const |
| 505 | { |
| 506 | using ResultType = typename std::remove_reference<D>::type::result_type; |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 507 | std::mt19937 gen(_seed + seed_offset); |
| 508 | TensorShape shape(tensor.shape()); |
| 509 | const uint32_t num_boxes = tensor.num_elements() / 4; |
Pablo Tello | e96e4f0 | 2018-12-21 16:47:23 +0000 | [diff] [blame] | 510 | // Iterate over all elements |
| 511 | std::uniform_real_distribution<> size_dist(0.f, 1.f); |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 512 | for(uint32_t element_idx = 0; element_idx < num_boxes * 4; element_idx += 4) |
Pablo Tello | e96e4f0 | 2018-12-21 16:47:23 +0000 | [diff] [blame] | 513 | { |
| 514 | const ResultType delta = size_dist(gen); |
| 515 | const ResultType epsilon = size_dist(gen); |
| 516 | const ResultType left = distribution(gen); |
| 517 | const ResultType top = distribution(gen); |
| 518 | const ResultType right = left + delta; |
| 519 | const ResultType bottom = top + epsilon; |
| 520 | const std::tuple<ResultType, ResultType, ResultType, ResultType> box(left, top, right, bottom); |
| 521 | Coordinates x1 = index2coord(shape, element_idx); |
| 522 | Coordinates y1 = index2coord(shape, element_idx + 1); |
| 523 | Coordinates x2 = index2coord(shape, element_idx + 2); |
| 524 | Coordinates y2 = index2coord(shape, element_idx + 3); |
| 525 | ResultType &target_value_x1 = reinterpret_cast<ResultType *>(tensor(x1))[0]; |
| 526 | ResultType &target_value_y1 = reinterpret_cast<ResultType *>(tensor(y1))[0]; |
| 527 | ResultType &target_value_x2 = reinterpret_cast<ResultType *>(tensor(x2))[0]; |
| 528 | ResultType &target_value_y2 = reinterpret_cast<ResultType *>(tensor(y2))[0]; |
| 529 | store_value_with_data_type(&target_value_x1, std::get<0>(box), tensor.data_type()); |
| 530 | store_value_with_data_type(&target_value_y1, std::get<1>(box), tensor.data_type()); |
| 531 | store_value_with_data_type(&target_value_x2, std::get<2>(box), tensor.data_type()); |
| 532 | store_value_with_data_type(&target_value_y2, std::get<3>(box), tensor.data_type()); |
| 533 | } |
| 534 | fill_borders_with_garbage(tensor, distribution, seed_offset); |
| 535 | } |
| 536 | |
| 537 | template <typename T, typename D> |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 538 | void AssetsLibrary::fill(std::vector<T> &vec, D &&distribution, std::random_device::result_type seed_offset) const |
| 539 | { |
| 540 | ARM_COMPUTE_ERROR_ON_MSG(vec.empty(), "Vector must not be empty"); |
| 541 | |
| 542 | using ResultType = typename std::remove_reference<D>::type::result_type; |
| 543 | |
| 544 | std::mt19937 gen(_seed + seed_offset); |
| 545 | for(size_t i = 0; i < vec.size(); ++i) |
| 546 | { |
| 547 | const ResultType value = distribution(gen); |
| 548 | |
| 549 | vec[i] = value; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | template <typename T, typename D> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 554 | void AssetsLibrary::fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 555 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 556 | using ResultType = typename std::remove_reference<D>::type::result_type; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 557 | |
| 558 | std::mt19937 gen(_seed + seed_offset); |
| 559 | |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 560 | const bool is_nhwc = tensor.data_layout() == DataLayout::NHWC; |
| 561 | TensorShape shape(tensor.shape()); |
| 562 | |
| 563 | if(is_nhwc) |
| 564 | { |
| 565 | // Ensure that the equivalent tensors will be filled for both data layouts |
| 566 | permute(shape, PermutationVector(1U, 2U, 0U)); |
| 567 | } |
| 568 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 569 | // Iterate over all elements |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 570 | const uint32_t num_elements = tensor.num_elements(); |
| 571 | for(uint32_t element_idx = 0; element_idx < num_elements; ++element_idx) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 572 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 573 | Coordinates id = index2coord(shape, element_idx); |
| 574 | |
| 575 | if(is_nhwc) |
| 576 | { |
| 577 | // Write in the correct id for permuted shapes |
| 578 | permute(id, PermutationVector(2U, 0U, 1U)); |
| 579 | } |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 580 | |
| 581 | // Iterate over all channels |
| 582 | for(int channel = 0; channel < tensor.num_channels(); ++channel) |
| 583 | { |
| 584 | const ResultType value = distribution(gen); |
Kohei Takahashi | cedb78f | 2018-08-23 10:23:52 +0900 | [diff] [blame] | 585 | ResultType &target_value = reinterpret_cast<ResultType *>(tensor(id))[channel]; |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 586 | |
| 587 | store_value_with_data_type(&target_value, value, tensor.data_type()); |
| 588 | } |
| 589 | } |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 590 | |
| 591 | fill_borders_with_garbage(tensor, distribution, seed_offset); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | template <typename D> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 595 | void AssetsLibrary::fill(RawTensor &raw, D &&distribution, std::random_device::result_type seed_offset) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 596 | { |
| 597 | std::mt19937 gen(_seed + seed_offset); |
| 598 | |
| 599 | for(size_t offset = 0; offset < raw.size(); offset += raw.element_size()) |
| 600 | { |
| 601 | using ResultType = typename std::remove_reference<D>::type::result_type; |
| 602 | const ResultType value = distribution(gen); |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 603 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 604 | store_value_with_data_type(raw.data() + offset, value, raw.data_type()); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 609 | void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 610 | { |
| 611 | const RawTensor &raw = get(name, format); |
| 612 | |
| 613 | for(size_t offset = 0; offset < raw.size(); offset += raw.element_size()) |
| 614 | { |
| 615 | const Coordinates id = index2coord(raw.shape(), offset / raw.element_size()); |
| 616 | |
Moritz Pflanzer | 82e70a1 | 2017-08-08 16:20:45 +0100 | [diff] [blame] | 617 | const RawTensor::value_type *const raw_ptr = raw.data() + offset; |
| 618 | const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 619 | std::copy_n(raw_ptr, raw.element_size(), out_ptr); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 624 | void AssetsLibrary::fill(T &&tensor, const std::string &name, Channel channel) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 625 | { |
| 626 | fill(std::forward<T>(tensor), name, get_format_for_channel(channel), channel); |
| 627 | } |
| 628 | |
| 629 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 630 | void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format, Channel channel) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 631 | { |
| 632 | const RawTensor &raw = get(name, format, channel); |
| 633 | |
| 634 | for(size_t offset = 0; offset < raw.size(); offset += raw.element_size()) |
| 635 | { |
| 636 | const Coordinates id = index2coord(raw.shape(), offset / raw.element_size()); |
| 637 | |
Moritz Pflanzer | 82e70a1 | 2017-08-08 16:20:45 +0100 | [diff] [blame] | 638 | const RawTensor::value_type *const raw_ptr = raw.data() + offset; |
| 639 | const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 640 | std::copy_n(raw_ptr, raw.element_size(), out_ptr); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | template <typename T> |
Alex Gilday | 345ab18 | 2018-01-09 11:40:19 +0000 | [diff] [blame] | 645 | void AssetsLibrary::fill(T &&tensor, RawTensor raw) const |
| 646 | { |
| 647 | for(size_t offset = 0; offset < raw.size(); offset += raw.element_size()) |
| 648 | { |
| 649 | const Coordinates id = index2coord(raw.shape(), offset / raw.element_size()); |
| 650 | |
| 651 | const RawTensor::value_type *const raw_ptr = raw.data() + offset; |
| 652 | const auto out_ptr = static_cast<RawTensor::value_type *>(tensor(id)); |
| 653 | std::copy_n(raw_ptr, raw.element_size(), out_ptr); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 658 | void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 659 | { |
| 660 | switch(tensor.data_type()) |
| 661 | { |
| 662 | case DataType::U8: |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 663 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 664 | { |
| 665 | std::uniform_int_distribution<uint8_t> distribution_u8(std::numeric_limits<uint8_t>::lowest(), std::numeric_limits<uint8_t>::max()); |
| 666 | fill(tensor, distribution_u8, seed_offset); |
| 667 | break; |
| 668 | } |
| 669 | case DataType::S8: |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 670 | case DataType::QSYMM8: |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 671 | case DataType::QSYMM8_PER_CHANNEL: |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 672 | case DataType::QASYMM8_SIGNED: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 673 | { |
| 674 | std::uniform_int_distribution<int8_t> distribution_s8(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::max()); |
| 675 | fill(tensor, distribution_s8, seed_offset); |
| 676 | break; |
| 677 | } |
| 678 | case DataType::U16: |
| 679 | { |
| 680 | std::uniform_int_distribution<uint16_t> distribution_u16(std::numeric_limits<uint16_t>::lowest(), std::numeric_limits<uint16_t>::max()); |
| 681 | fill(tensor, distribution_u16, seed_offset); |
| 682 | break; |
| 683 | } |
| 684 | case DataType::S16: |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 685 | case DataType::QSYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 686 | { |
| 687 | std::uniform_int_distribution<int16_t> distribution_s16(std::numeric_limits<int16_t>::lowest(), std::numeric_limits<int16_t>::max()); |
| 688 | fill(tensor, distribution_s16, seed_offset); |
| 689 | break; |
| 690 | } |
| 691 | case DataType::U32: |
| 692 | { |
| 693 | std::uniform_int_distribution<uint32_t> distribution_u32(std::numeric_limits<uint32_t>::lowest(), std::numeric_limits<uint32_t>::max()); |
| 694 | fill(tensor, distribution_u32, seed_offset); |
| 695 | break; |
| 696 | } |
| 697 | case DataType::S32: |
| 698 | { |
| 699 | std::uniform_int_distribution<int32_t> distribution_s32(std::numeric_limits<int32_t>::lowest(), std::numeric_limits<int32_t>::max()); |
| 700 | fill(tensor, distribution_s32, seed_offset); |
| 701 | break; |
| 702 | } |
| 703 | case DataType::U64: |
| 704 | { |
| 705 | std::uniform_int_distribution<uint64_t> distribution_u64(std::numeric_limits<uint64_t>::lowest(), std::numeric_limits<uint64_t>::max()); |
| 706 | fill(tensor, distribution_u64, seed_offset); |
| 707 | break; |
| 708 | } |
| 709 | case DataType::S64: |
| 710 | { |
| 711 | std::uniform_int_distribution<int64_t> distribution_s64(std::numeric_limits<int64_t>::lowest(), std::numeric_limits<int64_t>::max()); |
| 712 | fill(tensor, distribution_s64, seed_offset); |
| 713 | break; |
| 714 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 715 | case DataType::F16: |
SiCong Li | 02dfb2c | 2017-07-27 17:59:20 +0100 | [diff] [blame] | 716 | { |
| 717 | // It doesn't make sense to check [-inf, inf], so hard code it to a big number |
| 718 | std::uniform_real_distribution<float> distribution_f16(-100.f, 100.f); |
| 719 | fill(tensor, distribution_f16, seed_offset); |
| 720 | break; |
| 721 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 722 | case DataType::F32: |
| 723 | { |
| 724 | // It doesn't make sense to check [-inf, inf], so hard code it to a big number |
| 725 | std::uniform_real_distribution<float> distribution_f32(-1000.f, 1000.f); |
| 726 | fill(tensor, distribution_f32, seed_offset); |
| 727 | break; |
| 728 | } |
| 729 | case DataType::F64: |
| 730 | { |
| 731 | // It doesn't make sense to check [-inf, inf], so hard code it to a big number |
| 732 | std::uniform_real_distribution<double> distribution_f64(-1000.f, 1000.f); |
| 733 | fill(tensor, distribution_f64, seed_offset); |
| 734 | break; |
| 735 | } |
| 736 | case DataType::SIZET: |
| 737 | { |
| 738 | std::uniform_int_distribution<size_t> distribution_sizet(std::numeric_limits<size_t>::lowest(), std::numeric_limits<size_t>::max()); |
| 739 | fill(tensor, distribution_sizet, seed_offset); |
| 740 | break; |
| 741 | } |
| 742 | default: |
| 743 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 744 | } |
| 745 | } |
| 746 | |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 747 | template <typename T> |
| 748 | void AssetsLibrary::fill_tensor_uniform_ranged(T &&tensor, |
| 749 | std::random_device::result_type seed_offset, |
| 750 | const std::vector<AssetsLibrary::RangePair> &excluded_range_pairs) const |
| 751 | { |
| 752 | using namespace arm_compute::utils::random; |
| 753 | |
| 754 | switch(tensor.data_type()) |
| 755 | { |
| 756 | case DataType::U8: |
| 757 | case DataType::QASYMM8: |
| 758 | { |
| 759 | const auto converted_pairs = detail::convert_range_pair<uint8_t>(excluded_range_pairs); |
| 760 | RangedUniformDistribution<uint8_t> distribution_u8(std::numeric_limits<uint8_t>::lowest(), |
| 761 | std::numeric_limits<uint8_t>::max(), |
| 762 | converted_pairs); |
| 763 | fill(tensor, distribution_u8, seed_offset); |
| 764 | break; |
| 765 | } |
| 766 | case DataType::S8: |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 767 | case DataType::QSYMM8: |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 768 | { |
| 769 | const auto converted_pairs = detail::convert_range_pair<int8_t>(excluded_range_pairs); |
| 770 | RangedUniformDistribution<int8_t> distribution_s8(std::numeric_limits<int8_t>::lowest(), |
| 771 | std::numeric_limits<int8_t>::max(), |
| 772 | converted_pairs); |
| 773 | fill(tensor, distribution_s8, seed_offset); |
| 774 | break; |
| 775 | } |
| 776 | case DataType::U16: |
| 777 | { |
| 778 | const auto converted_pairs = detail::convert_range_pair<uint16_t>(excluded_range_pairs); |
| 779 | RangedUniformDistribution<uint16_t> distribution_u16(std::numeric_limits<uint16_t>::lowest(), |
| 780 | std::numeric_limits<uint16_t>::max(), |
| 781 | converted_pairs); |
| 782 | fill(tensor, distribution_u16, seed_offset); |
| 783 | break; |
| 784 | } |
| 785 | case DataType::S16: |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 786 | case DataType::QSYMM16: |
Georgios Pinitas | 587708b | 2018-12-31 15:43:52 +0000 | [diff] [blame] | 787 | { |
| 788 | const auto converted_pairs = detail::convert_range_pair<int16_t>(excluded_range_pairs); |
| 789 | RangedUniformDistribution<int16_t> distribution_s16(std::numeric_limits<int16_t>::lowest(), |
| 790 | std::numeric_limits<int16_t>::max(), |
| 791 | converted_pairs); |
| 792 | fill(tensor, distribution_s16, seed_offset); |
| 793 | break; |
| 794 | } |
| 795 | case DataType::U32: |
| 796 | { |
| 797 | const auto converted_pairs = detail::convert_range_pair<uint32_t>(excluded_range_pairs); |
| 798 | RangedUniformDistribution<uint32_t> distribution_u32(std::numeric_limits<uint32_t>::lowest(), |
| 799 | std::numeric_limits<uint32_t>::max(), |
| 800 | converted_pairs); |
| 801 | fill(tensor, distribution_u32, seed_offset); |
| 802 | break; |
| 803 | } |
| 804 | case DataType::S32: |
| 805 | { |
| 806 | const auto converted_pairs = detail::convert_range_pair<int32_t>(excluded_range_pairs); |
| 807 | RangedUniformDistribution<int32_t> distribution_s32(std::numeric_limits<int32_t>::lowest(), |
| 808 | std::numeric_limits<int32_t>::max(), |
| 809 | converted_pairs); |
| 810 | fill(tensor, distribution_s32, seed_offset); |
| 811 | break; |
| 812 | } |
| 813 | case DataType::F16: |
| 814 | { |
| 815 | // It doesn't make sense to check [-inf, inf], so hard code it to a big number |
| 816 | const auto converted_pairs = detail::convert_range_pair<float>(excluded_range_pairs); |
| 817 | RangedUniformDistribution<float> distribution_f16(-100.f, 100.f, converted_pairs); |
| 818 | fill(tensor, distribution_f16, seed_offset); |
| 819 | break; |
| 820 | } |
| 821 | case DataType::F32: |
| 822 | { |
| 823 | // It doesn't make sense to check [-inf, inf], so hard code it to a big number |
| 824 | const auto converted_pairs = detail::convert_range_pair<float>(excluded_range_pairs); |
| 825 | RangedUniformDistribution<float> distribution_f32(-1000.f, 1000.f, converted_pairs); |
| 826 | fill(tensor, distribution_f32, seed_offset); |
| 827 | break; |
| 828 | } |
| 829 | default: |
| 830 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 831 | } |
| 832 | } |
| 833 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 834 | template <typename T, typename D> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 835 | void AssetsLibrary::fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 836 | { |
| 837 | switch(tensor.data_type()) |
| 838 | { |
| 839 | case DataType::U8: |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 840 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 841 | { |
| 842 | ARM_COMPUTE_ERROR_ON(!(std::is_same<uint8_t, D>::value)); |
| 843 | std::uniform_int_distribution<uint8_t> distribution_u8(low, high); |
| 844 | fill(tensor, distribution_u8, seed_offset); |
| 845 | break; |
| 846 | } |
| 847 | case DataType::S8: |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 848 | case DataType::QSYMM8: |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 849 | case DataType::QASYMM8_SIGNED: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 850 | { |
| 851 | ARM_COMPUTE_ERROR_ON(!(std::is_same<int8_t, D>::value)); |
| 852 | std::uniform_int_distribution<int8_t> distribution_s8(low, high); |
| 853 | fill(tensor, distribution_s8, seed_offset); |
| 854 | break; |
| 855 | } |
| 856 | case DataType::U16: |
| 857 | { |
| 858 | ARM_COMPUTE_ERROR_ON(!(std::is_same<uint16_t, D>::value)); |
| 859 | std::uniform_int_distribution<uint16_t> distribution_u16(low, high); |
| 860 | fill(tensor, distribution_u16, seed_offset); |
| 861 | break; |
| 862 | } |
| 863 | case DataType::S16: |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 864 | case DataType::QSYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 865 | { |
| 866 | ARM_COMPUTE_ERROR_ON(!(std::is_same<int16_t, D>::value)); |
| 867 | std::uniform_int_distribution<int16_t> distribution_s16(low, high); |
| 868 | fill(tensor, distribution_s16, seed_offset); |
| 869 | break; |
| 870 | } |
| 871 | case DataType::U32: |
| 872 | { |
| 873 | ARM_COMPUTE_ERROR_ON(!(std::is_same<uint32_t, D>::value)); |
| 874 | std::uniform_int_distribution<uint32_t> distribution_u32(low, high); |
| 875 | fill(tensor, distribution_u32, seed_offset); |
| 876 | break; |
| 877 | } |
| 878 | case DataType::S32: |
| 879 | { |
| 880 | ARM_COMPUTE_ERROR_ON(!(std::is_same<int32_t, D>::value)); |
| 881 | std::uniform_int_distribution<int32_t> distribution_s32(low, high); |
| 882 | fill(tensor, distribution_s32, seed_offset); |
| 883 | break; |
| 884 | } |
| 885 | case DataType::U64: |
| 886 | { |
| 887 | ARM_COMPUTE_ERROR_ON(!(std::is_same<uint64_t, D>::value)); |
| 888 | std::uniform_int_distribution<uint64_t> distribution_u64(low, high); |
| 889 | fill(tensor, distribution_u64, seed_offset); |
| 890 | break; |
| 891 | } |
| 892 | case DataType::S64: |
| 893 | { |
| 894 | ARM_COMPUTE_ERROR_ON(!(std::is_same<int64_t, D>::value)); |
| 895 | std::uniform_int_distribution<int64_t> distribution_s64(low, high); |
| 896 | fill(tensor, distribution_s64, seed_offset); |
| 897 | break; |
| 898 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 899 | case DataType::F16: |
| 900 | { |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 901 | std::uniform_real_distribution<float> distribution_f16(low, high); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 902 | fill(tensor, distribution_f16, seed_offset); |
| 903 | break; |
| 904 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 905 | case DataType::F32: |
| 906 | { |
| 907 | ARM_COMPUTE_ERROR_ON(!(std::is_same<float, D>::value)); |
| 908 | std::uniform_real_distribution<float> distribution_f32(low, high); |
| 909 | fill(tensor, distribution_f32, seed_offset); |
| 910 | break; |
| 911 | } |
| 912 | case DataType::F64: |
| 913 | { |
| 914 | ARM_COMPUTE_ERROR_ON(!(std::is_same<double, D>::value)); |
| 915 | std::uniform_real_distribution<double> distribution_f64(low, high); |
| 916 | fill(tensor, distribution_f64, seed_offset); |
| 917 | break; |
| 918 | } |
| 919 | case DataType::SIZET: |
| 920 | { |
| 921 | ARM_COMPUTE_ERROR_ON(!(std::is_same<size_t, D>::value)); |
| 922 | std::uniform_int_distribution<size_t> distribution_sizet(low, high); |
| 923 | fill(tensor, distribution_sizet, seed_offset); |
| 924 | break; |
| 925 | } |
| 926 | default: |
| 927 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | template <typename T> |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 932 | void AssetsLibrary::fill_layer_data(T &&tensor, std::string name) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 933 | { |
| 934 | #ifdef _WIN32 |
| 935 | const std::string path_separator("\\"); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 936 | #else /* _WIN32 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 937 | const std::string path_separator("/"); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 938 | #endif /* _WIN32 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 939 | const std::string path = _library_path + path_separator + name; |
| 940 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 941 | std::vector<unsigned long> shape; |
| 942 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 943 | // Open file |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 944 | std::ifstream stream(path, std::ios::in | std::ios::binary); |
Anthony Barbier | f6705ec | 2017-09-28 12:01:10 +0100 | [diff] [blame] | 945 | if(!stream.good()) |
| 946 | { |
| 947 | throw framework::FileNotFound("Could not load npy file: " + path); |
| 948 | } |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 949 | std::string header = npy::read_header(stream); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 950 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 951 | // Parse header |
| 952 | bool fortran_order = false; |
| 953 | std::string typestr; |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 954 | npy::parse_header(header, typestr, fortran_order, shape); |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 955 | |
| 956 | // Check if the typestring matches the given one |
| 957 | std::string expect_typestr = get_typestring(tensor.data_type()); |
| 958 | ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch"); |
| 959 | |
| 960 | // Validate tensor shape |
| 961 | ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor.shape().num_dimensions(), "Tensor ranks mismatch"); |
| 962 | if(fortran_order) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 963 | { |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 964 | for(size_t i = 0; i < shape.size(); ++i) |
| 965 | { |
| 966 | ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[i], "Tensor dimensions mismatch"); |
| 967 | } |
| 968 | } |
| 969 | else |
| 970 | { |
| 971 | for(size_t i = 0; i < shape.size(); ++i) |
| 972 | { |
| 973 | ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch"); |
| 974 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 975 | } |
| 976 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 977 | // Read data |
| 978 | if(tensor.padding().empty()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 979 | { |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 980 | // If tensor has no padding read directly from stream. |
| 981 | stream.read(reinterpret_cast<char *>(tensor.data()), tensor.size()); |
| 982 | } |
| 983 | else |
| 984 | { |
| 985 | // If tensor has padding accessing tensor elements through execution window. |
| 986 | Window window; |
| 987 | window.use_tensor_dimensions(tensor.shape()); |
| 988 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 989 | execute_window_loop(window, [&](const Coordinates & id) |
| 990 | { |
| 991 | stream.read(reinterpret_cast<char *>(tensor(id)), tensor.element_size()); |
| 992 | }); |
| 993 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 994 | } |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 995 | |
| 996 | template <typename T, typename D> |
| 997 | void AssetsLibrary::fill_tensor_value(T &&tensor, D value) const |
| 998 | { |
| 999 | fill_tensor_uniform(tensor, 0, value, value); |
| 1000 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1001 | } // namespace test |
| 1002 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 1003 | #endif /* ARM_COMPUTE_TEST_TENSOR_LIBRARY_H */ |