Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
John Kesapides | fb68ca1 | 2019-01-21 14:13:27 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | |
| 165 | bool DummyAccessor::access_tensor(ITensor &tensor) |
| 166 | { |
| 167 | ARM_COMPUTE_UNUSED(tensor); |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 168 | bool ret = _maximum == 0 || _iterator < _maximum; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 169 | if(_iterator == _maximum) |
| 170 | { |
| 171 | _iterator = 0; |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | _iterator++; |
| 176 | } |
| 177 | return ret; |
| 178 | } |
| 179 | |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 180 | 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] | 181 | : _npy_tensor(), _filename(std::move(npy_path)), _output_stream(output_stream) |
| 182 | { |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 183 | NumPyBinLoader loader(_filename, data_layout); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 184 | |
| 185 | TensorInfo info(shape, 1, data_type); |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 186 | info.set_data_layout(data_layout); |
| 187 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 188 | _npy_tensor.allocator()->init(info); |
| 189 | _npy_tensor.allocator()->allocate(); |
| 190 | |
| 191 | loader.access_tensor(_npy_tensor); |
| 192 | } |
| 193 | |
| 194 | template <typename T> |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 195 | void NumPyAccessor::access_numpy_tensor(ITensor &tensor, T tolerance) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 196 | { |
Gian Marco Iodice | ad486e2 | 2018-08-07 17:17:06 +0100 | [diff] [blame] | 197 | const int num_elements = tensor.info()->tensor_shape().total_size(); |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 198 | int num_mismatches = utils::compare_tensor<T>(tensor, _npy_tensor, tolerance); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 199 | float percentage_mismatches = static_cast<float>(num_mismatches) / num_elements; |
| 200 | |
| 201 | _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] | 202 | _output_stream << " " << num_elements - num_mismatches << " out of " << num_elements << " matches with the provided output[" << _filename << "]." << std::endl |
| 203 | << std::endl; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | bool NumPyAccessor::access_tensor(ITensor &tensor) |
| 207 | { |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 208 | 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] | 209 | ARM_COMPUTE_ERROR_ON(_npy_tensor.info()->dimension(0) != tensor.info()->dimension(0)); |
| 210 | |
| 211 | switch(tensor.info()->data_type()) |
| 212 | { |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 213 | case DataType::QASYMM8: |
| 214 | access_numpy_tensor<qasymm8_t>(tensor, 0); |
| 215 | break; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 216 | case DataType::F32: |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 217 | access_numpy_tensor<float>(tensor, 0.0001f); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 218 | break; |
| 219 | default: |
| 220 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 221 | } |
| 222 | |
| 223 | return false; |
| 224 | } |
| 225 | |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 226 | SaveNumPyAccessor::SaveNumPyAccessor(std::string npy_name, const bool is_fortran) |
| 227 | : _npy_name(std::move(npy_name)), _is_fortran(is_fortran) |
| 228 | { |
| 229 | } |
| 230 | |
| 231 | bool SaveNumPyAccessor::access_tensor(ITensor &tensor) |
| 232 | { |
| 233 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32); |
| 234 | |
| 235 | utils::save_to_npy(tensor, _npy_name, _is_fortran); |
| 236 | |
| 237 | return false; |
| 238 | } |
| 239 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 240 | ImageAccessor::ImageAccessor(std::string filename, bool bgr, std::unique_ptr<IPreprocessor> preprocessor) |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 241 | : _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] | 242 | { |
| 243 | } |
| 244 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 245 | bool ImageAccessor::access_tensor(ITensor &tensor) |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 246 | { |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 247 | if(!_already_loaded) |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 248 | { |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 249 | auto image_loader = utils::ImageLoaderFactory::create(_filename); |
| 250 | ARM_COMPUTE_EXIT_ON_MSG(image_loader == nullptr, "Unsupported image type"); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 251 | |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 252 | // Open image file |
| 253 | image_loader->open(_filename); |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 254 | |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 255 | // Get permutated shape and permutation parameters |
| 256 | TensorShape permuted_shape = tensor.info()->tensor_shape(); |
| 257 | arm_compute::PermutationVector perm; |
| 258 | if(tensor.info()->data_layout() != DataLayout::NCHW) |
| 259 | { |
| 260 | std::tie(permuted_shape, perm) = compute_permutation_parameters(tensor.info()->tensor_shape(), tensor.info()->data_layout()); |
| 261 | } |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 262 | ARM_COMPUTE_EXIT_ON_MSG_VAR(image_loader->width() != permuted_shape.x() || image_loader->height() != permuted_shape.y(), |
| 263 | "Failed to load image file: dimensions [%d,%d] not correct, expected [%" PRIu32 ",%" PRIu32 "].", |
| 264 | image_loader->width(), image_loader->height(), permuted_shape.x(), permuted_shape.y()); |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 265 | |
| 266 | // Fill the tensor with the PPM content (BGR) |
| 267 | image_loader->fill_planar_tensor(tensor, _bgr); |
| 268 | |
| 269 | // Preprocess tensor |
| 270 | if(_preprocessor) |
| 271 | { |
| 272 | _preprocessor->preprocess(tensor); |
| 273 | } |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 274 | } |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 275 | |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 276 | _already_loaded = !_already_loaded; |
| 277 | return _already_loaded; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 280 | ValidationInputAccessor::ValidationInputAccessor(const std::string &image_list, |
| 281 | std::string images_path, |
| 282 | std::unique_ptr<IPreprocessor> preprocessor, |
| 283 | bool bgr, |
| 284 | unsigned int start, |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 285 | unsigned int end, |
| 286 | std::ostream &output_stream) |
| 287 | : _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] | 288 | { |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 289 | ARM_COMPUTE_EXIT_ON_MSG(start > end, "Invalid validation range!"); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 290 | |
| 291 | std::ifstream ifs; |
| 292 | try |
| 293 | { |
| 294 | ifs.exceptions(std::ifstream::badbit); |
| 295 | ifs.open(image_list, std::ios::in | std::ios::binary); |
| 296 | |
| 297 | // Parse image names |
| 298 | unsigned int counter = 0; |
| 299 | for(std::string line; !std::getline(ifs, line).fail() && counter <= end; ++counter) |
| 300 | { |
| 301 | // Add image to process if withing range |
| 302 | if(counter >= start) |
| 303 | { |
| 304 | std::stringstream linestream(line); |
| 305 | std::string image_name; |
| 306 | |
| 307 | linestream >> image_name; |
| 308 | _images.emplace_back(std::move(image_name)); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | catch(const std::ifstream::failure &e) |
| 313 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 314 | 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] | 315 | } |
| 316 | } |
| 317 | |
| 318 | bool ValidationInputAccessor::access_tensor(arm_compute::ITensor &tensor) |
| 319 | { |
| 320 | bool ret = _offset < _images.size(); |
| 321 | if(ret) |
| 322 | { |
| 323 | utils::JPEGLoader jpeg; |
| 324 | |
| 325 | // Open JPEG file |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 326 | std::string image_name = _path + _images[_offset++]; |
| 327 | jpeg.open(image_name); |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 328 | _output_stream << "[" << _offset << "/" << _images.size() << "] Validating " << image_name << std::endl; |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 329 | |
| 330 | // Get permutated shape and permutation parameters |
| 331 | TensorShape permuted_shape = tensor.info()->tensor_shape(); |
| 332 | arm_compute::PermutationVector perm; |
| 333 | if(tensor.info()->data_layout() != DataLayout::NCHW) |
| 334 | { |
Anthony Barbier | 4ead11a | 2018-08-06 09:25:36 +0100 | [diff] [blame] | 335 | std::tie(permuted_shape, perm) = compute_permutation_parameters(tensor.info()->tensor_shape(), |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 336 | tensor.info()->data_layout()); |
| 337 | } |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 338 | ARM_COMPUTE_EXIT_ON_MSG_VAR(jpeg.width() != permuted_shape.x() || jpeg.height() != permuted_shape.y(), |
| 339 | "Failed to load image file: dimensions [%d,%d] not correct, expected [%" PRIu32 ",%" PRIu32 "].", |
| 340 | jpeg.width(), jpeg.height(), permuted_shape.x(), permuted_shape.y()); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 341 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 342 | // Fill the tensor with the JPEG content (BGR) |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 343 | jpeg.fill_planar_tensor(tensor, _bgr); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 344 | |
| 345 | // Preprocess tensor |
| 346 | if(_preprocessor) |
| 347 | { |
| 348 | _preprocessor->preprocess(tensor); |
| 349 | } |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | return ret; |
| 353 | } |
| 354 | |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 355 | ValidationOutputAccessor::ValidationOutputAccessor(const std::string &image_list, |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 356 | std::ostream &output_stream, |
| 357 | unsigned int start, |
| 358 | unsigned int end) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 359 | : _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] | 360 | { |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 361 | ARM_COMPUTE_EXIT_ON_MSG(start > end, "Invalid validation range!"); |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 362 | |
| 363 | std::ifstream ifs; |
| 364 | try |
| 365 | { |
| 366 | ifs.exceptions(std::ifstream::badbit); |
| 367 | ifs.open(image_list, std::ios::in | std::ios::binary); |
| 368 | |
| 369 | // Parse image correctly classified labels |
| 370 | unsigned int counter = 0; |
| 371 | for(std::string line; !std::getline(ifs, line).fail() && counter <= end; ++counter) |
| 372 | { |
| 373 | // Add label if within range |
| 374 | if(counter >= start) |
| 375 | { |
| 376 | std::stringstream linestream(line); |
| 377 | std::string image_name; |
| 378 | int result; |
| 379 | |
| 380 | linestream >> image_name >> result; |
| 381 | _results.emplace_back(result); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | catch(const std::ifstream::failure &e) |
| 386 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 387 | 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] | 388 | } |
| 389 | } |
| 390 | |
| 391 | void ValidationOutputAccessor::reset() |
| 392 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 393 | _offset = 0; |
| 394 | _positive_samples_top1 = 0; |
| 395 | _positive_samples_top5 = 0; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | bool ValidationOutputAccessor::access_tensor(arm_compute::ITensor &tensor) |
| 399 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 400 | bool ret = _offset < _results.size(); |
| 401 | if(ret) |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 402 | { |
| 403 | // Get results |
| 404 | std::vector<size_t> tensor_results; |
| 405 | switch(tensor.info()->data_type()) |
| 406 | { |
| 407 | case DataType::QASYMM8: |
| 408 | tensor_results = access_predictions_tensor<uint8_t>(tensor); |
| 409 | break; |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 410 | case DataType::F16: |
| 411 | tensor_results = access_predictions_tensor<half>(tensor); |
| 412 | break; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 413 | case DataType::F32: |
| 414 | tensor_results = access_predictions_tensor<float>(tensor); |
| 415 | break; |
| 416 | default: |
| 417 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 418 | } |
| 419 | |
| 420 | // Check if tensor results are within top-n accuracy |
| 421 | size_t correct_label = _results[_offset++]; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 422 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 423 | aggregate_sample(tensor_results, _positive_samples_top1, 1, correct_label); |
| 424 | aggregate_sample(tensor_results, _positive_samples_top5, 5, correct_label); |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // Report top_n accuracy |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 428 | if(_offset >= _results.size()) |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 429 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 430 | report_top_n(1, _results.size(), _positive_samples_top1); |
| 431 | report_top_n(5, _results.size(), _positive_samples_top5); |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | template <typename T> |
| 438 | std::vector<size_t> ValidationOutputAccessor::access_predictions_tensor(arm_compute::ITensor &tensor) |
| 439 | { |
| 440 | // Get the predicted class |
| 441 | std::vector<size_t> index; |
| 442 | |
| 443 | const auto output_net = reinterpret_cast<T *>(tensor.buffer() + tensor.info()->offset_first_element_in_bytes()); |
| 444 | const size_t num_classes = tensor.info()->dimension(0); |
| 445 | |
| 446 | index.resize(num_classes); |
| 447 | |
| 448 | // Sort results |
| 449 | std::iota(std::begin(index), std::end(index), static_cast<size_t>(0)); |
| 450 | std::sort(std::begin(index), std::end(index), |
| 451 | [&](size_t a, size_t b) |
| 452 | { |
| 453 | return output_net[a] > output_net[b]; |
| 454 | }); |
| 455 | |
| 456 | return index; |
| 457 | } |
| 458 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 459 | void ValidationOutputAccessor::aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label) |
| 460 | { |
| 461 | auto is_valid_label = [correct_label](size_t label) |
| 462 | { |
| 463 | return label == correct_label; |
| 464 | }; |
| 465 | |
| 466 | if(std::any_of(std::begin(res), std::begin(res) + top_n, is_valid_label)) |
| 467 | { |
| 468 | ++positive_samples; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void ValidationOutputAccessor::report_top_n(size_t top_n, size_t total_samples, size_t positive_samples) |
| 473 | { |
| 474 | size_t negative_samples = total_samples - positive_samples; |
| 475 | float accuracy = positive_samples / static_cast<float>(total_samples); |
| 476 | |
| 477 | _output_stream << "----------Top " << top_n << " accuracy ----------" << std::endl |
| 478 | << std::endl; |
| 479 | _output_stream << "Positive samples : " << positive_samples << std::endl; |
| 480 | _output_stream << "Negative samples : " << negative_samples << std::endl; |
| 481 | _output_stream << "Accuracy : " << accuracy << std::endl; |
| 482 | } |
| 483 | |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 484 | DetectionOutputAccessor::DetectionOutputAccessor(const std::string &labels_path, std::vector<TensorShape> &imgs_tensor_shapes, std::ostream &output_stream) |
| 485 | : _labels(), _tensor_shapes(std::move(imgs_tensor_shapes)), _output_stream(output_stream) |
| 486 | { |
| 487 | _labels.clear(); |
| 488 | |
| 489 | std::ifstream ifs; |
| 490 | |
| 491 | try |
| 492 | { |
| 493 | ifs.exceptions(std::ifstream::badbit); |
| 494 | ifs.open(labels_path, std::ios::in | std::ios::binary); |
| 495 | |
| 496 | for(std::string line; !std::getline(ifs, line).fail();) |
| 497 | { |
| 498 | _labels.emplace_back(line); |
| 499 | } |
| 500 | } |
| 501 | catch(const std::ifstream::failure &e) |
| 502 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 503 | 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] | 504 | } |
| 505 | } |
| 506 | |
| 507 | template <typename T> |
| 508 | void DetectionOutputAccessor::access_predictions_tensor(ITensor &tensor) |
| 509 | { |
| 510 | const size_t num_detection = tensor.info()->valid_region().shape.y(); |
| 511 | const auto output_prt = reinterpret_cast<T *>(tensor.buffer() + tensor.info()->offset_first_element_in_bytes()); |
| 512 | |
| 513 | if(num_detection > 0) |
| 514 | { |
| 515 | _output_stream << "---------------------- Detections ----------------------" << std::endl |
| 516 | << std::endl; |
| 517 | |
| 518 | _output_stream << std::left << std::setprecision(4) << std::setw(8) << "Image | " << std::setw(8) << "Label | " << std::setw(12) << "Confidence | " |
| 519 | << "[ xmin, ymin, xmax, ymax ]" << std::endl; |
| 520 | |
| 521 | for(size_t i = 0; i < num_detection; ++i) |
| 522 | { |
| 523 | auto im = static_cast<const int>(output_prt[i * 7]); |
| 524 | _output_stream << std::setw(8) << im << std::setw(8) |
| 525 | << _labels[output_prt[i * 7 + 1]] << std::setw(12) << output_prt[i * 7 + 2] |
| 526 | << " [" << (output_prt[i * 7 + 3] * _tensor_shapes[im].x()) |
| 527 | << ", " << (output_prt[i * 7 + 4] * _tensor_shapes[im].y()) |
| 528 | << ", " << (output_prt[i * 7 + 5] * _tensor_shapes[im].x()) |
| 529 | << ", " << (output_prt[i * 7 + 6] * _tensor_shapes[im].y()) |
| 530 | << "]" << std::endl; |
| 531 | } |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | _output_stream << "No detection found." << std::endl; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | bool DetectionOutputAccessor::access_tensor(ITensor &tensor) |
| 540 | { |
| 541 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32); |
| 542 | |
| 543 | switch(tensor.info()->data_type()) |
| 544 | { |
| 545 | case DataType::F32: |
| 546 | access_predictions_tensor<float>(tensor); |
| 547 | break; |
| 548 | default: |
| 549 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 550 | } |
| 551 | |
| 552 | return false; |
| 553 | } |
| 554 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 555 | TopNPredictionsAccessor::TopNPredictionsAccessor(const std::string &labels_path, size_t top_n, std::ostream &output_stream) |
| 556 | : _labels(), _output_stream(output_stream), _top_n(top_n) |
| 557 | { |
| 558 | _labels.clear(); |
| 559 | |
| 560 | std::ifstream ifs; |
| 561 | |
| 562 | try |
| 563 | { |
| 564 | ifs.exceptions(std::ifstream::badbit); |
| 565 | ifs.open(labels_path, std::ios::in | std::ios::binary); |
| 566 | |
| 567 | for(std::string line; !std::getline(ifs, line).fail();) |
| 568 | { |
| 569 | _labels.emplace_back(line); |
| 570 | } |
| 571 | } |
| 572 | catch(const std::ifstream::failure &e) |
| 573 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 574 | 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] | 575 | } |
| 576 | } |
| 577 | |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 578 | template <typename T> |
| 579 | void TopNPredictionsAccessor::access_predictions_tensor(ITensor &tensor) |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 580 | { |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 581 | // Get the predicted class |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 582 | std::vector<T> classes_prob; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 583 | std::vector<size_t> index; |
| 584 | |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 585 | 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] | 586 | const size_t num_classes = tensor.info()->dimension(0); |
| 587 | |
| 588 | classes_prob.resize(num_classes); |
| 589 | index.resize(num_classes); |
| 590 | |
| 591 | std::copy(output_net, output_net + num_classes, classes_prob.begin()); |
| 592 | |
| 593 | // Sort results |
| 594 | std::iota(std::begin(index), std::end(index), static_cast<size_t>(0)); |
| 595 | std::sort(std::begin(index), std::end(index), |
| 596 | [&](size_t a, size_t b) |
| 597 | { |
| 598 | return classes_prob[a] > classes_prob[b]; |
| 599 | }); |
| 600 | |
| 601 | _output_stream << "---------- Top " << _top_n << " predictions ----------" << std::endl |
| 602 | << std::endl; |
| 603 | for(size_t i = 0; i < _top_n; ++i) |
| 604 | { |
| 605 | _output_stream << std::fixed << std::setprecision(4) |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 606 | << +classes_prob[index.at(i)] |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 607 | << " - [id = " << index.at(i) << "]" |
| 608 | << ", " << _labels[index.at(i)] << std::endl; |
| 609 | } |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | bool TopNPredictionsAccessor::access_tensor(ITensor &tensor) |
| 613 | { |
| 614 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32, DataType::QASYMM8); |
| 615 | ARM_COMPUTE_ERROR_ON(_labels.size() != tensor.info()->dimension(0)); |
| 616 | |
| 617 | switch(tensor.info()->data_type()) |
| 618 | { |
| 619 | case DataType::QASYMM8: |
| 620 | access_predictions_tensor<uint8_t>(tensor); |
| 621 | break; |
| 622 | case DataType::F32: |
| 623 | access_predictions_tensor<float>(tensor); |
| 624 | break; |
| 625 | default: |
| 626 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 627 | } |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 628 | |
| 629 | return false; |
| 630 | } |
| 631 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 632 | RandomAccessor::RandomAccessor(PixelValue lower, PixelValue upper, std::random_device::result_type seed) |
| 633 | : _lower(lower), _upper(upper), _seed(seed) |
| 634 | { |
| 635 | } |
| 636 | |
| 637 | template <typename T, typename D> |
| 638 | void RandomAccessor::fill(ITensor &tensor, D &&distribution) |
| 639 | { |
| 640 | std::mt19937 gen(_seed); |
| 641 | |
hakanardo | f36ac35 | 2018-02-16 10:06:34 +0100 | [diff] [blame] | 642 | if(tensor.info()->padding().empty() && (dynamic_cast<SubTensor *>(&tensor) == nullptr)) |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 643 | { |
| 644 | for(size_t offset = 0; offset < tensor.info()->total_size(); offset += tensor.info()->element_size()) |
| 645 | { |
Michele Di Giorgio | 88731f0 | 2018-09-25 16:49:27 +0100 | [diff] [blame] | 646 | const auto value = static_cast<T>(distribution(gen)); |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 647 | *reinterpret_cast<T *>(tensor.buffer() + offset) = value; |
| 648 | } |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | // If tensor has padding accessing tensor elements through execution window. |
| 653 | Window window; |
| 654 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 655 | |
| 656 | execute_window_loop(window, [&](const Coordinates & id) |
| 657 | { |
Michele Di Giorgio | 88731f0 | 2018-09-25 16:49:27 +0100 | [diff] [blame] | 658 | const auto value = static_cast<T>(distribution(gen)); |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 659 | *reinterpret_cast<T *>(tensor.ptr_to_element(id)) = value; |
| 660 | }); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | bool RandomAccessor::access_tensor(ITensor &tensor) |
| 665 | { |
| 666 | switch(tensor.info()->data_type()) |
| 667 | { |
John Kesapides | fb68ca1 | 2019-01-21 14:13:27 +0000 | [diff] [blame] | 668 | case DataType::QASYMM8: |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 669 | case DataType::U8: |
| 670 | { |
| 671 | std::uniform_int_distribution<uint8_t> distribution_u8(_lower.get<uint8_t>(), _upper.get<uint8_t>()); |
| 672 | fill<uint8_t>(tensor, distribution_u8); |
| 673 | break; |
| 674 | } |
| 675 | case DataType::S8: |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 676 | { |
| 677 | std::uniform_int_distribution<int8_t> distribution_s8(_lower.get<int8_t>(), _upper.get<int8_t>()); |
| 678 | fill<int8_t>(tensor, distribution_s8); |
| 679 | break; |
| 680 | } |
| 681 | case DataType::U16: |
| 682 | { |
| 683 | std::uniform_int_distribution<uint16_t> distribution_u16(_lower.get<uint16_t>(), _upper.get<uint16_t>()); |
| 684 | fill<uint16_t>(tensor, distribution_u16); |
| 685 | break; |
| 686 | } |
| 687 | case DataType::S16: |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 688 | { |
| 689 | std::uniform_int_distribution<int16_t> distribution_s16(_lower.get<int16_t>(), _upper.get<int16_t>()); |
| 690 | fill<int16_t>(tensor, distribution_s16); |
| 691 | break; |
| 692 | } |
| 693 | case DataType::U32: |
| 694 | { |
| 695 | std::uniform_int_distribution<uint32_t> distribution_u32(_lower.get<uint32_t>(), _upper.get<uint32_t>()); |
| 696 | fill<uint32_t>(tensor, distribution_u32); |
| 697 | break; |
| 698 | } |
| 699 | case DataType::S32: |
| 700 | { |
| 701 | std::uniform_int_distribution<int32_t> distribution_s32(_lower.get<int32_t>(), _upper.get<int32_t>()); |
| 702 | fill<int32_t>(tensor, distribution_s32); |
| 703 | break; |
| 704 | } |
| 705 | case DataType::U64: |
| 706 | { |
| 707 | std::uniform_int_distribution<uint64_t> distribution_u64(_lower.get<uint64_t>(), _upper.get<uint64_t>()); |
| 708 | fill<uint64_t>(tensor, distribution_u64); |
| 709 | break; |
| 710 | } |
| 711 | case DataType::S64: |
| 712 | { |
| 713 | std::uniform_int_distribution<int64_t> distribution_s64(_lower.get<int64_t>(), _upper.get<int64_t>()); |
| 714 | fill<int64_t>(tensor, distribution_s64); |
| 715 | break; |
| 716 | } |
| 717 | case DataType::F16: |
| 718 | { |
John Kesapides | fb68ca1 | 2019-01-21 14:13:27 +0000 | [diff] [blame] | 719 | std::uniform_real_distribution<float> distribution_f16(_lower.get<half>(), _upper.get<half>()); |
Michele Di Giorgio | 88731f0 | 2018-09-25 16:49:27 +0100 | [diff] [blame] | 720 | fill<half>(tensor, distribution_f16); |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | case DataType::F32: |
| 724 | { |
| 725 | std::uniform_real_distribution<float> distribution_f32(_lower.get<float>(), _upper.get<float>()); |
| 726 | fill<float>(tensor, distribution_f32); |
| 727 | break; |
| 728 | } |
| 729 | case DataType::F64: |
| 730 | { |
| 731 | std::uniform_real_distribution<double> distribution_f64(_lower.get<double>(), _upper.get<double>()); |
| 732 | fill<double>(tensor, distribution_f64); |
| 733 | break; |
| 734 | } |
| 735 | default: |
| 736 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 737 | } |
| 738 | return true; |
| 739 | } |
| 740 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 741 | NumPyBinLoader::NumPyBinLoader(std::string filename, DataLayout file_layout) |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 742 | : _already_loaded(false), _filename(std::move(filename)), _file_layout(file_layout) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 743 | { |
| 744 | } |
| 745 | |
| 746 | bool NumPyBinLoader::access_tensor(ITensor &tensor) |
| 747 | { |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 748 | if(!_already_loaded) |
| 749 | { |
| 750 | utils::NPYLoader loader; |
| 751 | loader.open(_filename, _file_layout); |
| 752 | loader.fill_tensor(tensor); |
| 753 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 754 | |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 755 | _already_loaded = !_already_loaded; |
| 756 | return _already_loaded; |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 757 | } |