Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "utils/GraphUtils.h" |
hakanardo | f36ac35 | 2018-02-16 10:06:34 +0100 | [diff] [blame] | 26 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/Logger.h" |
hakanardo | f36ac35 | 2018-02-16 10:06:34 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/SubTensor.h" |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 31 | |
| 32 | #pragma GCC diagnostic push |
| 33 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 34 | #include "utils/ImageLoader.h" |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 35 | #pragma GCC diagnostic pop |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 36 | #include "utils/Utils.h" |
| 37 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 38 | #include <inttypes.h> |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 39 | #include <iomanip> |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 40 | #include <limits> |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 41 | |
| 42 | using namespace arm_compute::graph_utils; |
| 43 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 44 | namespace |
| 45 | { |
Anthony Barbier | 4ead11a | 2018-08-06 09:25:36 +0100 | [diff] [blame] | 46 | std::pair<arm_compute::TensorShape, arm_compute::PermutationVector> compute_permutation_parameters(const arm_compute::TensorShape &shape, |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 47 | arm_compute::DataLayout data_layout) |
| 48 | { |
| 49 | // Set permutation parameters if needed |
| 50 | arm_compute::TensorShape permuted_shape = shape; |
| 51 | arm_compute::PermutationVector perm; |
| 52 | // Permute only if num_dimensions greater than 2 |
| 53 | if(shape.num_dimensions() > 2) |
| 54 | { |
| 55 | perm = (data_layout == arm_compute::DataLayout::NHWC) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U); |
| 56 | |
| 57 | arm_compute::PermutationVector perm_shape = (data_layout == arm_compute::DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U); |
| 58 | arm_compute::permute(permuted_shape, perm_shape); |
| 59 | } |
| 60 | |
| 61 | return std::make_pair(permuted_shape, perm); |
| 62 | } |
| 63 | } // namespace |
| 64 | |
Georgios Pinitas | be2772a | 2018-08-17 15:33:39 +0100 | [diff] [blame] | 65 | TFPreproccessor::TFPreproccessor(float min_range, float max_range) |
| 66 | : _min_range(min_range), _max_range(max_range) |
| 67 | { |
| 68 | } |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 69 | void TFPreproccessor::preprocess(ITensor &tensor) |
| 70 | { |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 71 | if(tensor.info()->data_type() == DataType::F32) |
| 72 | { |
| 73 | preprocess_typed<float>(tensor); |
| 74 | } |
| 75 | else if(tensor.info()->data_type() == DataType::F16) |
| 76 | { |
| 77 | preprocess_typed<half>(tensor); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | template <typename T> |
| 86 | void TFPreproccessor::preprocess_typed(ITensor &tensor) |
| 87 | { |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 88 | Window window; |
| 89 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 90 | |
Georgios Pinitas | be2772a | 2018-08-17 15:33:39 +0100 | [diff] [blame] | 91 | const float range = _max_range - _min_range; |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 92 | execute_window_loop(window, [&](const Coordinates & id) |
| 93 | { |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 94 | const T value = *reinterpret_cast<T *>(tensor.ptr_to_element(id)); |
| 95 | float res = value / 255.f; // Normalize to [0, 1] |
| 96 | res = res * range + _min_range; // Map to [min_range, max_range] |
| 97 | *reinterpret_cast<T *>(tensor.ptr_to_element(id)) = res; |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 98 | }); |
| 99 | } |
| 100 | |
Georgios Pinitas | b54c644 | 2019-04-03 13:18:14 +0100 | [diff] [blame] | 101 | CaffePreproccessor::CaffePreproccessor(std::array<float, 3> mean, bool bgr, float scale) |
| 102 | : _mean(mean), _bgr(bgr), _scale(scale) |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 103 | { |
| 104 | if(_bgr) |
| 105 | { |
| 106 | std::swap(_mean[0], _mean[2]); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void CaffePreproccessor::preprocess(ITensor &tensor) |
| 111 | { |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 112 | if(tensor.info()->data_type() == DataType::F32) |
| 113 | { |
| 114 | preprocess_typed<float>(tensor); |
| 115 | } |
| 116 | else if(tensor.info()->data_type() == DataType::F16) |
| 117 | { |
| 118 | preprocess_typed<half>(tensor); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | template <typename T> |
| 127 | void CaffePreproccessor::preprocess_typed(ITensor &tensor) |
| 128 | { |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 129 | Window window; |
| 130 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 131 | const int channel_idx = get_data_layout_dimension_index(tensor.info()->data_layout(), DataLayoutDimension::CHANNEL); |
| 132 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 133 | execute_window_loop(window, [&](const Coordinates & id) |
| 134 | { |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 135 | const T value = *reinterpret_cast<T *>(tensor.ptr_to_element(id)) - T(_mean[id[channel_idx]]); |
| 136 | *reinterpret_cast<T *>(tensor.ptr_to_element(id)) = value * T(_scale); |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 137 | }); |
| 138 | } |
| 139 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 140 | PPMWriter::PPMWriter(std::string name, unsigned int maximum) |
| 141 | : _name(std::move(name)), _iterator(0), _maximum(maximum) |
| 142 | { |
| 143 | } |
| 144 | |
| 145 | bool PPMWriter::access_tensor(ITensor &tensor) |
| 146 | { |
| 147 | std::stringstream ss; |
| 148 | ss << _name << _iterator << ".ppm"; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 149 | |
| 150 | arm_compute::utils::save_to_ppm(tensor, ss.str()); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 151 | |
| 152 | _iterator++; |
| 153 | if(_maximum == 0) |
| 154 | { |
| 155 | return true; |
| 156 | } |
| 157 | return _iterator < _maximum; |
| 158 | } |
| 159 | |
| 160 | DummyAccessor::DummyAccessor(unsigned int maximum) |
| 161 | : _iterator(0), _maximum(maximum) |
| 162 | { |
| 163 | } |
| 164 | |
Giorgio Arena | 9c67d38 | 2021-08-20 15:24:03 +0100 | [diff] [blame] | 165 | bool DummyAccessor::access_tensor_data() |
| 166 | { |
| 167 | return false; |
| 168 | } |
| 169 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 170 | bool DummyAccessor::access_tensor(ITensor &tensor) |
| 171 | { |
| 172 | ARM_COMPUTE_UNUSED(tensor); |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 173 | bool ret = _maximum == 0 || _iterator < _maximum; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 174 | if(_iterator == _maximum) |
| 175 | { |
| 176 | _iterator = 0; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | _iterator++; |
| 181 | } |
| 182 | return ret; |
| 183 | } |
| 184 | |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 185 | NumPyAccessor::NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, DataLayout data_layout, std::ostream &output_stream) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 186 | : _npy_tensor(), _filename(std::move(npy_path)), _output_stream(output_stream) |
| 187 | { |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 188 | NumPyBinLoader loader(_filename, data_layout); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 189 | |
| 190 | TensorInfo info(shape, 1, data_type); |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 191 | info.set_data_layout(data_layout); |
| 192 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 193 | _npy_tensor.allocator()->init(info); |
| 194 | _npy_tensor.allocator()->allocate(); |
| 195 | |
| 196 | loader.access_tensor(_npy_tensor); |
| 197 | } |
| 198 | |
| 199 | template <typename T> |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 200 | void NumPyAccessor::access_numpy_tensor(ITensor &tensor, T tolerance) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 201 | { |
Gian Marco Iodice | ad486e2 | 2018-08-07 17:17:06 +0100 | [diff] [blame] | 202 | const int num_elements = tensor.info()->tensor_shape().total_size(); |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 203 | int num_mismatches = utils::compare_tensor<T>(tensor, _npy_tensor, tolerance); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 204 | float percentage_mismatches = static_cast<float>(num_mismatches) / num_elements; |
| 205 | |
| 206 | _output_stream << "Results: " << 100.f - (percentage_mismatches * 100) << " % matches with the provided output[" << _filename << "]." << std::endl; |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 207 | _output_stream << " " << num_elements - num_mismatches << " out of " << num_elements << " matches with the provided output[" << _filename << "]." << std::endl |
| 208 | << std::endl; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | bool NumPyAccessor::access_tensor(ITensor &tensor) |
| 212 | { |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 213 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32, DataType::QASYMM8); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 214 | ARM_COMPUTE_ERROR_ON(_npy_tensor.info()->dimension(0) != tensor.info()->dimension(0)); |
| 215 | |
| 216 | switch(tensor.info()->data_type()) |
| 217 | { |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 218 | case DataType::QASYMM8: |
| 219 | access_numpy_tensor<qasymm8_t>(tensor, 0); |
| 220 | break; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 221 | case DataType::F32: |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 222 | access_numpy_tensor<float>(tensor, 0.0001f); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 223 | break; |
| 224 | default: |
| 225 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 226 | } |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
Isabella Gottardi | cd4e9ab | 2019-11-05 17:50:27 +0000 | [diff] [blame] | 231 | #ifdef ARM_COMPUTE_ASSERTS_ENABLED |
| 232 | PrintAccessor::PrintAccessor(std::ostream &output_stream, IOFormatInfo io_fmt) |
| 233 | : _output_stream(output_stream), _io_fmt(io_fmt) |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | bool PrintAccessor::access_tensor(ITensor &tensor) |
| 238 | { |
| 239 | tensor.print(_output_stream, _io_fmt); |
| 240 | return false; |
| 241 | } |
| 242 | #endif /* ARM_COMPUTE_ASSERTS_ENABLED */ |
| 243 | |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 244 | SaveNumPyAccessor::SaveNumPyAccessor(std::string npy_name, const bool is_fortran) |
| 245 | : _npy_name(std::move(npy_name)), _is_fortran(is_fortran) |
| 246 | { |
| 247 | } |
| 248 | |
| 249 | bool SaveNumPyAccessor::access_tensor(ITensor &tensor) |
| 250 | { |
| 251 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32); |
| 252 | |
| 253 | utils::save_to_npy(tensor, _npy_name, _is_fortran); |
| 254 | |
| 255 | return false; |
| 256 | } |
| 257 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 258 | ImageAccessor::ImageAccessor(std::string filename, bool bgr, std::unique_ptr<IPreprocessor> preprocessor) |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 259 | : _already_loaded(false), _filename(std::move(filename)), _bgr(bgr), _preprocessor(std::move(preprocessor)) |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 260 | { |
| 261 | } |
| 262 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 263 | bool ImageAccessor::access_tensor(ITensor &tensor) |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 264 | { |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 265 | if(!_already_loaded) |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 266 | { |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 267 | auto image_loader = utils::ImageLoaderFactory::create(_filename); |
| 268 | ARM_COMPUTE_EXIT_ON_MSG(image_loader == nullptr, "Unsupported image type"); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 269 | |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 270 | // Open image file |
| 271 | image_loader->open(_filename); |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 272 | |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 273 | // Get permutated shape and permutation parameters |
| 274 | TensorShape permuted_shape = tensor.info()->tensor_shape(); |
| 275 | arm_compute::PermutationVector perm; |
| 276 | if(tensor.info()->data_layout() != DataLayout::NCHW) |
| 277 | { |
| 278 | std::tie(permuted_shape, perm) = compute_permutation_parameters(tensor.info()->tensor_shape(), tensor.info()->data_layout()); |
| 279 | } |
Sheri Zhang | d80792a | 2020-11-05 10:43:37 +0000 | [diff] [blame] | 280 | |
| 281 | #ifdef __arm__ |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 282 | ARM_COMPUTE_EXIT_ON_MSG_VAR(image_loader->width() != permuted_shape.x() || image_loader->height() != permuted_shape.y(), |
| 283 | "Failed to load image file: dimensions [%d,%d] not correct, expected [%" PRIu32 ",%" PRIu32 "].", |
| 284 | image_loader->width(), image_loader->height(), permuted_shape.x(), permuted_shape.y()); |
Sheri Zhang | d80792a | 2020-11-05 10:43:37 +0000 | [diff] [blame] | 285 | #else // __arm__ |
| 286 | ARM_COMPUTE_EXIT_ON_MSG_VAR(image_loader->width() != permuted_shape.x() || image_loader->height() != permuted_shape.y(), |
| 287 | "Failed to load image file: dimensions [%d,%d] not correct, expected [%" PRIu64 ",%" PRIu64 "].", |
Georgios Pinitas | 4551403 | 2020-12-30 00:03:09 +0000 | [diff] [blame] | 288 | image_loader->width(), image_loader->height(), |
| 289 | static_cast<uint64_t>(permuted_shape.x()), static_cast<uint64_t>(permuted_shape.y())); |
Sheri Zhang | d80792a | 2020-11-05 10:43:37 +0000 | [diff] [blame] | 290 | #endif // __arm__ |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 291 | |
| 292 | // Fill the tensor with the PPM content (BGR) |
| 293 | image_loader->fill_planar_tensor(tensor, _bgr); |
| 294 | |
| 295 | // Preprocess tensor |
| 296 | if(_preprocessor) |
| 297 | { |
| 298 | _preprocessor->preprocess(tensor); |
| 299 | } |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 300 | } |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 301 | |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 302 | _already_loaded = !_already_loaded; |
| 303 | return _already_loaded; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 306 | ValidationInputAccessor::ValidationInputAccessor(const std::string &image_list, |
| 307 | std::string images_path, |
| 308 | std::unique_ptr<IPreprocessor> preprocessor, |
| 309 | bool bgr, |
| 310 | unsigned int start, |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 311 | unsigned int end, |
| 312 | std::ostream &output_stream) |
| 313 | : _path(std::move(images_path)), _images(), _preprocessor(std::move(preprocessor)), _bgr(bgr), _offset(0), _output_stream(output_stream) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 314 | { |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 315 | ARM_COMPUTE_EXIT_ON_MSG(start > end, "Invalid validation range!"); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 316 | |
| 317 | std::ifstream ifs; |
| 318 | try |
| 319 | { |
| 320 | ifs.exceptions(std::ifstream::badbit); |
| 321 | ifs.open(image_list, std::ios::in | std::ios::binary); |
| 322 | |
| 323 | // Parse image names |
| 324 | unsigned int counter = 0; |
| 325 | for(std::string line; !std::getline(ifs, line).fail() && counter <= end; ++counter) |
| 326 | { |
| 327 | // Add image to process if withing range |
| 328 | if(counter >= start) |
| 329 | { |
| 330 | std::stringstream linestream(line); |
| 331 | std::string image_name; |
| 332 | |
| 333 | linestream >> image_name; |
| 334 | _images.emplace_back(std::move(image_name)); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | catch(const std::ifstream::failure &e) |
| 339 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 340 | ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", image_list.c_str(), e.what()); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | bool ValidationInputAccessor::access_tensor(arm_compute::ITensor &tensor) |
| 345 | { |
| 346 | bool ret = _offset < _images.size(); |
| 347 | if(ret) |
| 348 | { |
| 349 | utils::JPEGLoader jpeg; |
| 350 | |
| 351 | // Open JPEG file |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 352 | std::string image_name = _path + _images[_offset++]; |
| 353 | jpeg.open(image_name); |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 354 | _output_stream << "[" << _offset << "/" << _images.size() << "] Validating " << image_name << std::endl; |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 355 | |
| 356 | // Get permutated shape and permutation parameters |
| 357 | TensorShape permuted_shape = tensor.info()->tensor_shape(); |
| 358 | arm_compute::PermutationVector perm; |
| 359 | if(tensor.info()->data_layout() != DataLayout::NCHW) |
| 360 | { |
Anthony Barbier | 4ead11a | 2018-08-06 09:25:36 +0100 | [diff] [blame] | 361 | std::tie(permuted_shape, perm) = compute_permutation_parameters(tensor.info()->tensor_shape(), |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 362 | tensor.info()->data_layout()); |
| 363 | } |
Sheri Zhang | d80792a | 2020-11-05 10:43:37 +0000 | [diff] [blame] | 364 | |
| 365 | #ifdef __arm__ |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 366 | ARM_COMPUTE_EXIT_ON_MSG_VAR(jpeg.width() != permuted_shape.x() || jpeg.height() != permuted_shape.y(), |
| 367 | "Failed to load image file: dimensions [%d,%d] not correct, expected [%" PRIu32 ",%" PRIu32 "].", |
| 368 | jpeg.width(), jpeg.height(), permuted_shape.x(), permuted_shape.y()); |
Sheri Zhang | d80792a | 2020-11-05 10:43:37 +0000 | [diff] [blame] | 369 | #else // __arm__ |
| 370 | ARM_COMPUTE_EXIT_ON_MSG_VAR(jpeg.width() != permuted_shape.x() || jpeg.height() != permuted_shape.y(), |
| 371 | "Failed to load image file: dimensions [%d,%d] not correct, expected [%" PRIu64 ",%" PRIu64 "].", |
Georgios Pinitas | 4551403 | 2020-12-30 00:03:09 +0000 | [diff] [blame] | 372 | jpeg.width(), jpeg.height(), |
| 373 | static_cast<uint64_t>(permuted_shape.x()), static_cast<uint64_t>(permuted_shape.y())); |
Sheri Zhang | d80792a | 2020-11-05 10:43:37 +0000 | [diff] [blame] | 374 | #endif // __arm__ |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 375 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 376 | // Fill the tensor with the JPEG content (BGR) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 377 | jpeg.fill_planar_tensor(tensor, _bgr); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 378 | |
| 379 | // Preprocess tensor |
| 380 | if(_preprocessor) |
| 381 | { |
| 382 | _preprocessor->preprocess(tensor); |
| 383 | } |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return ret; |
| 387 | } |
| 388 | |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 389 | ValidationOutputAccessor::ValidationOutputAccessor(const std::string &image_list, |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 390 | std::ostream &output_stream, |
| 391 | unsigned int start, |
| 392 | unsigned int end) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 393 | : _results(), _output_stream(output_stream), _offset(0), _positive_samples_top1(0), _positive_samples_top5(0) |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 394 | { |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 395 | ARM_COMPUTE_EXIT_ON_MSG(start > end, "Invalid validation range!"); |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 396 | |
| 397 | std::ifstream ifs; |
| 398 | try |
| 399 | { |
| 400 | ifs.exceptions(std::ifstream::badbit); |
| 401 | ifs.open(image_list, std::ios::in | std::ios::binary); |
| 402 | |
| 403 | // Parse image correctly classified labels |
| 404 | unsigned int counter = 0; |
| 405 | for(std::string line; !std::getline(ifs, line).fail() && counter <= end; ++counter) |
| 406 | { |
| 407 | // Add label if within range |
| 408 | if(counter >= start) |
| 409 | { |
| 410 | std::stringstream linestream(line); |
| 411 | std::string image_name; |
| 412 | int result; |
| 413 | |
| 414 | linestream >> image_name >> result; |
| 415 | _results.emplace_back(result); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | catch(const std::ifstream::failure &e) |
| 420 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 421 | ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", image_list.c_str(), e.what()); |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | void ValidationOutputAccessor::reset() |
| 426 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 427 | _offset = 0; |
| 428 | _positive_samples_top1 = 0; |
| 429 | _positive_samples_top5 = 0; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | bool ValidationOutputAccessor::access_tensor(arm_compute::ITensor &tensor) |
| 433 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 434 | bool ret = _offset < _results.size(); |
| 435 | if(ret) |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 436 | { |
| 437 | // Get results |
| 438 | std::vector<size_t> tensor_results; |
| 439 | switch(tensor.info()->data_type()) |
| 440 | { |
| 441 | case DataType::QASYMM8: |
| 442 | tensor_results = access_predictions_tensor<uint8_t>(tensor); |
| 443 | break; |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 444 | case DataType::F16: |
| 445 | tensor_results = access_predictions_tensor<half>(tensor); |
| 446 | break; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 447 | case DataType::F32: |
| 448 | tensor_results = access_predictions_tensor<float>(tensor); |
| 449 | break; |
| 450 | default: |
| 451 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 452 | } |
| 453 | |
| 454 | // Check if tensor results are within top-n accuracy |
| 455 | size_t correct_label = _results[_offset++]; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 456 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 457 | aggregate_sample(tensor_results, _positive_samples_top1, 1, correct_label); |
| 458 | aggregate_sample(tensor_results, _positive_samples_top5, 5, correct_label); |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Report top_n accuracy |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 462 | if(_offset >= _results.size()) |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 463 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 464 | report_top_n(1, _results.size(), _positive_samples_top1); |
| 465 | report_top_n(5, _results.size(), _positive_samples_top5); |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | template <typename T> |
| 472 | std::vector<size_t> ValidationOutputAccessor::access_predictions_tensor(arm_compute::ITensor &tensor) |
| 473 | { |
| 474 | // Get the predicted class |
| 475 | std::vector<size_t> index; |
| 476 | |
| 477 | const auto output_net = reinterpret_cast<T *>(tensor.buffer() + tensor.info()->offset_first_element_in_bytes()); |
| 478 | const size_t num_classes = tensor.info()->dimension(0); |
| 479 | |
| 480 | index.resize(num_classes); |
| 481 | |
| 482 | // Sort results |
| 483 | std::iota(std::begin(index), std::end(index), static_cast<size_t>(0)); |
| 484 | std::sort(std::begin(index), std::end(index), |
| 485 | [&](size_t a, size_t b) |
| 486 | { |
| 487 | return output_net[a] > output_net[b]; |
| 488 | }); |
| 489 | |
| 490 | return index; |
| 491 | } |
| 492 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 493 | void ValidationOutputAccessor::aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label) |
| 494 | { |
| 495 | auto is_valid_label = [correct_label](size_t label) |
| 496 | { |
| 497 | return label == correct_label; |
| 498 | }; |
| 499 | |
| 500 | if(std::any_of(std::begin(res), std::begin(res) + top_n, is_valid_label)) |
| 501 | { |
| 502 | ++positive_samples; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void ValidationOutputAccessor::report_top_n(size_t top_n, size_t total_samples, size_t positive_samples) |
| 507 | { |
| 508 | size_t negative_samples = total_samples - positive_samples; |
| 509 | float accuracy = positive_samples / static_cast<float>(total_samples); |
| 510 | |
| 511 | _output_stream << "----------Top " << top_n << " accuracy ----------" << std::endl |
| 512 | << std::endl; |
| 513 | _output_stream << "Positive samples : " << positive_samples << std::endl; |
| 514 | _output_stream << "Negative samples : " << negative_samples << std::endl; |
| 515 | _output_stream << "Accuracy : " << accuracy << std::endl; |
| 516 | } |
| 517 | |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 518 | DetectionOutputAccessor::DetectionOutputAccessor(const std::string &labels_path, std::vector<TensorShape> &imgs_tensor_shapes, std::ostream &output_stream) |
| 519 | : _labels(), _tensor_shapes(std::move(imgs_tensor_shapes)), _output_stream(output_stream) |
| 520 | { |
| 521 | _labels.clear(); |
| 522 | |
| 523 | std::ifstream ifs; |
| 524 | |
| 525 | try |
| 526 | { |
| 527 | ifs.exceptions(std::ifstream::badbit); |
| 528 | ifs.open(labels_path, std::ios::in | std::ios::binary); |
| 529 | |
| 530 | for(std::string line; !std::getline(ifs, line).fail();) |
| 531 | { |
| 532 | _labels.emplace_back(line); |
| 533 | } |
| 534 | } |
| 535 | catch(const std::ifstream::failure &e) |
| 536 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 537 | ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", labels_path.c_str(), e.what()); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | template <typename T> |
| 542 | void DetectionOutputAccessor::access_predictions_tensor(ITensor &tensor) |
| 543 | { |
| 544 | const size_t num_detection = tensor.info()->valid_region().shape.y(); |
| 545 | const auto output_prt = reinterpret_cast<T *>(tensor.buffer() + tensor.info()->offset_first_element_in_bytes()); |
| 546 | |
| 547 | if(num_detection > 0) |
| 548 | { |
| 549 | _output_stream << "---------------------- Detections ----------------------" << std::endl |
| 550 | << std::endl; |
| 551 | |
| 552 | _output_stream << std::left << std::setprecision(4) << std::setw(8) << "Image | " << std::setw(8) << "Label | " << std::setw(12) << "Confidence | " |
| 553 | << "[ xmin, ymin, xmax, ymax ]" << std::endl; |
| 554 | |
| 555 | for(size_t i = 0; i < num_detection; ++i) |
| 556 | { |
| 557 | auto im = static_cast<const int>(output_prt[i * 7]); |
| 558 | _output_stream << std::setw(8) << im << std::setw(8) |
| 559 | << _labels[output_prt[i * 7 + 1]] << std::setw(12) << output_prt[i * 7 + 2] |
| 560 | << " [" << (output_prt[i * 7 + 3] * _tensor_shapes[im].x()) |
| 561 | << ", " << (output_prt[i * 7 + 4] * _tensor_shapes[im].y()) |
| 562 | << ", " << (output_prt[i * 7 + 5] * _tensor_shapes[im].x()) |
| 563 | << ", " << (output_prt[i * 7 + 6] * _tensor_shapes[im].y()) |
| 564 | << "]" << std::endl; |
| 565 | } |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | _output_stream << "No detection found." << std::endl; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | bool DetectionOutputAccessor::access_tensor(ITensor &tensor) |
| 574 | { |
| 575 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32); |
| 576 | |
| 577 | switch(tensor.info()->data_type()) |
| 578 | { |
| 579 | case DataType::F32: |
| 580 | access_predictions_tensor<float>(tensor); |
| 581 | break; |
| 582 | default: |
| 583 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 584 | } |
| 585 | |
| 586 | return false; |
| 587 | } |
| 588 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 589 | TopNPredictionsAccessor::TopNPredictionsAccessor(const std::string &labels_path, size_t top_n, std::ostream &output_stream) |
| 590 | : _labels(), _output_stream(output_stream), _top_n(top_n) |
| 591 | { |
| 592 | _labels.clear(); |
| 593 | |
| 594 | std::ifstream ifs; |
| 595 | |
| 596 | try |
| 597 | { |
| 598 | ifs.exceptions(std::ifstream::badbit); |
| 599 | ifs.open(labels_path, std::ios::in | std::ios::binary); |
| 600 | |
| 601 | for(std::string line; !std::getline(ifs, line).fail();) |
| 602 | { |
| 603 | _labels.emplace_back(line); |
| 604 | } |
| 605 | } |
| 606 | catch(const std::ifstream::failure &e) |
| 607 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 608 | ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", labels_path.c_str(), e.what()); |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 612 | template <typename T> |
| 613 | void TopNPredictionsAccessor::access_predictions_tensor(ITensor &tensor) |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 614 | { |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 615 | // Get the predicted class |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 616 | std::vector<T> classes_prob; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 617 | std::vector<size_t> index; |
| 618 | |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 619 | const auto output_net = reinterpret_cast<T *>(tensor.buffer() + tensor.info()->offset_first_element_in_bytes()); |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 620 | const size_t num_classes = tensor.info()->dimension(0); |
| 621 | |
| 622 | classes_prob.resize(num_classes); |
| 623 | index.resize(num_classes); |
| 624 | |
| 625 | std::copy(output_net, output_net + num_classes, classes_prob.begin()); |
| 626 | |
| 627 | // Sort results |
| 628 | std::iota(std::begin(index), std::end(index), static_cast<size_t>(0)); |
| 629 | std::sort(std::begin(index), std::end(index), |
| 630 | [&](size_t a, size_t b) |
| 631 | { |
| 632 | return classes_prob[a] > classes_prob[b]; |
| 633 | }); |
| 634 | |
| 635 | _output_stream << "---------- Top " << _top_n << " predictions ----------" << std::endl |
| 636 | << std::endl; |
| 637 | for(size_t i = 0; i < _top_n; ++i) |
| 638 | { |
| 639 | _output_stream << std::fixed << std::setprecision(4) |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 640 | << +classes_prob[index.at(i)] |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 641 | << " - [id = " << index.at(i) << "]" |
| 642 | << ", " << _labels[index.at(i)] << std::endl; |
| 643 | } |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | bool TopNPredictionsAccessor::access_tensor(ITensor &tensor) |
| 647 | { |
| 648 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32, DataType::QASYMM8); |
| 649 | ARM_COMPUTE_ERROR_ON(_labels.size() != tensor.info()->dimension(0)); |
| 650 | |
| 651 | switch(tensor.info()->data_type()) |
| 652 | { |
| 653 | case DataType::QASYMM8: |
| 654 | access_predictions_tensor<uint8_t>(tensor); |
| 655 | break; |
| 656 | case DataType::F32: |
| 657 | access_predictions_tensor<float>(tensor); |
| 658 | break; |
| 659 | default: |
| 660 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 661 | } |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 662 | |
| 663 | return false; |
| 664 | } |
| 665 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 666 | RandomAccessor::RandomAccessor(PixelValue lower, PixelValue upper, std::random_device::result_type seed) |
| 667 | : _lower(lower), _upper(upper), _seed(seed) |
| 668 | { |
| 669 | } |
| 670 | |
| 671 | template <typename T, typename D> |
| 672 | void RandomAccessor::fill(ITensor &tensor, D &&distribution) |
| 673 | { |
| 674 | std::mt19937 gen(_seed); |
| 675 | |
hakanardo | f36ac35 | 2018-02-16 10:06:34 +0100 | [diff] [blame] | 676 | if(tensor.info()->padding().empty() && (dynamic_cast<SubTensor *>(&tensor) == nullptr)) |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 677 | { |
| 678 | for(size_t offset = 0; offset < tensor.info()->total_size(); offset += tensor.info()->element_size()) |
| 679 | { |
Michele Di Giorgio | 88731f0 | 2018-09-25 16:49:27 +0100 | [diff] [blame] | 680 | const auto value = static_cast<T>(distribution(gen)); |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 681 | *reinterpret_cast<T *>(tensor.buffer() + offset) = value; |
| 682 | } |
| 683 | } |
| 684 | else |
| 685 | { |
| 686 | // If tensor has padding accessing tensor elements through execution window. |
| 687 | Window window; |
| 688 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 689 | |
| 690 | execute_window_loop(window, [&](const Coordinates & id) |
| 691 | { |
Michele Di Giorgio | 88731f0 | 2018-09-25 16:49:27 +0100 | [diff] [blame] | 692 | const auto value = static_cast<T>(distribution(gen)); |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 693 | *reinterpret_cast<T *>(tensor.ptr_to_element(id)) = value; |
| 694 | }); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | bool RandomAccessor::access_tensor(ITensor &tensor) |
| 699 | { |
| 700 | switch(tensor.info()->data_type()) |
| 701 | { |
John Kesapides | fb68ca1 | 2019-01-21 14:13:27 +0000 | [diff] [blame] | 702 | case DataType::QASYMM8: |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 703 | case DataType::U8: |
| 704 | { |
| 705 | std::uniform_int_distribution<uint8_t> distribution_u8(_lower.get<uint8_t>(), _upper.get<uint8_t>()); |
| 706 | fill<uint8_t>(tensor, distribution_u8); |
| 707 | break; |
| 708 | } |
| 709 | case DataType::S8: |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 710 | { |
| 711 | std::uniform_int_distribution<int8_t> distribution_s8(_lower.get<int8_t>(), _upper.get<int8_t>()); |
| 712 | fill<int8_t>(tensor, distribution_s8); |
| 713 | break; |
| 714 | } |
| 715 | case DataType::U16: |
| 716 | { |
| 717 | std::uniform_int_distribution<uint16_t> distribution_u16(_lower.get<uint16_t>(), _upper.get<uint16_t>()); |
| 718 | fill<uint16_t>(tensor, distribution_u16); |
| 719 | break; |
| 720 | } |
| 721 | case DataType::S16: |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 722 | { |
| 723 | std::uniform_int_distribution<int16_t> distribution_s16(_lower.get<int16_t>(), _upper.get<int16_t>()); |
| 724 | fill<int16_t>(tensor, distribution_s16); |
| 725 | break; |
| 726 | } |
| 727 | case DataType::U32: |
| 728 | { |
| 729 | std::uniform_int_distribution<uint32_t> distribution_u32(_lower.get<uint32_t>(), _upper.get<uint32_t>()); |
| 730 | fill<uint32_t>(tensor, distribution_u32); |
| 731 | break; |
| 732 | } |
| 733 | case DataType::S32: |
| 734 | { |
| 735 | std::uniform_int_distribution<int32_t> distribution_s32(_lower.get<int32_t>(), _upper.get<int32_t>()); |
| 736 | fill<int32_t>(tensor, distribution_s32); |
| 737 | break; |
| 738 | } |
| 739 | case DataType::U64: |
| 740 | { |
| 741 | std::uniform_int_distribution<uint64_t> distribution_u64(_lower.get<uint64_t>(), _upper.get<uint64_t>()); |
| 742 | fill<uint64_t>(tensor, distribution_u64); |
| 743 | break; |
| 744 | } |
| 745 | case DataType::S64: |
| 746 | { |
| 747 | std::uniform_int_distribution<int64_t> distribution_s64(_lower.get<int64_t>(), _upper.get<int64_t>()); |
| 748 | fill<int64_t>(tensor, distribution_s64); |
| 749 | break; |
| 750 | } |
| 751 | case DataType::F16: |
| 752 | { |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 753 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution_f16(_lower.get<float>(), _upper.get<float>()); |
Michele Di Giorgio | 88731f0 | 2018-09-25 16:49:27 +0100 | [diff] [blame] | 754 | fill<half>(tensor, distribution_f16); |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 755 | break; |
| 756 | } |
| 757 | case DataType::F32: |
| 758 | { |
| 759 | std::uniform_real_distribution<float> distribution_f32(_lower.get<float>(), _upper.get<float>()); |
| 760 | fill<float>(tensor, distribution_f32); |
| 761 | break; |
| 762 | } |
| 763 | case DataType::F64: |
| 764 | { |
| 765 | std::uniform_real_distribution<double> distribution_f64(_lower.get<double>(), _upper.get<double>()); |
| 766 | fill<double>(tensor, distribution_f64); |
| 767 | break; |
| 768 | } |
| 769 | default: |
| 770 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 771 | } |
| 772 | return true; |
| 773 | } |
| 774 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 775 | NumPyBinLoader::NumPyBinLoader(std::string filename, DataLayout file_layout) |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 776 | : _already_loaded(false), _filename(std::move(filename)), _file_layout(file_layout) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 777 | { |
| 778 | } |
| 779 | |
| 780 | bool NumPyBinLoader::access_tensor(ITensor &tensor) |
| 781 | { |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 782 | if(!_already_loaded) |
| 783 | { |
| 784 | utils::NPYLoader loader; |
| 785 | loader.open(_filename, _file_layout); |
| 786 | loader.fill_tensor(tensor); |
| 787 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 788 | |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 789 | _already_loaded = !_already_loaded; |
| 790 | return _already_loaded; |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 791 | } |