Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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" |
hakanardo | f36ac35 | 2018-02-16 10:06:34 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/SubTensor.h" |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 30 | #include "utils/ImageLoader.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 31 | #include "utils/Utils.h" |
| 32 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 33 | #include <iomanip> |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 34 | |
| 35 | using namespace arm_compute::graph_utils; |
| 36 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 37 | namespace |
| 38 | { |
| 39 | std::pair<arm_compute::TensorShape, arm_compute::PermutationVector> compute_permutation_paramaters(const arm_compute::TensorShape &shape, |
| 40 | arm_compute::DataLayout data_layout) |
| 41 | { |
| 42 | // Set permutation parameters if needed |
| 43 | arm_compute::TensorShape permuted_shape = shape; |
| 44 | arm_compute::PermutationVector perm; |
| 45 | // Permute only if num_dimensions greater than 2 |
| 46 | if(shape.num_dimensions() > 2) |
| 47 | { |
| 48 | perm = (data_layout == arm_compute::DataLayout::NHWC) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U); |
| 49 | |
| 50 | arm_compute::PermutationVector perm_shape = (data_layout == arm_compute::DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U); |
| 51 | arm_compute::permute(permuted_shape, perm_shape); |
| 52 | } |
| 53 | |
| 54 | return std::make_pair(permuted_shape, perm); |
| 55 | } |
| 56 | } // namespace |
| 57 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 58 | void TFPreproccessor::preprocess(ITensor &tensor) |
| 59 | { |
| 60 | Window window; |
| 61 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 62 | |
| 63 | execute_window_loop(window, [&](const Coordinates & id) |
| 64 | { |
| 65 | const float value = *reinterpret_cast<float *>(tensor.ptr_to_element(id)); |
| 66 | float res = value / 255.f; // Normalize to [0, 1] |
| 67 | res = (res - 0.5f) * 2.f; // Map to [-1, 1] |
| 68 | *reinterpret_cast<float *>(tensor.ptr_to_element(id)) = res; |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | CaffePreproccessor::CaffePreproccessor(std::array<float, 3> mean, bool bgr) |
| 73 | : _mean(mean), _bgr(bgr) |
| 74 | { |
| 75 | if(_bgr) |
| 76 | { |
| 77 | std::swap(_mean[0], _mean[2]); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void CaffePreproccessor::preprocess(ITensor &tensor) |
| 82 | { |
| 83 | Window window; |
| 84 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 85 | |
| 86 | execute_window_loop(window, [&](const Coordinates & id) |
| 87 | { |
| 88 | const float value = *reinterpret_cast<float *>(tensor.ptr_to_element(id)) - _mean[id.z()]; |
| 89 | *reinterpret_cast<float *>(tensor.ptr_to_element(id)) = value; |
| 90 | }); |
| 91 | } |
| 92 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 93 | PPMWriter::PPMWriter(std::string name, unsigned int maximum) |
| 94 | : _name(std::move(name)), _iterator(0), _maximum(maximum) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | bool PPMWriter::access_tensor(ITensor &tensor) |
| 99 | { |
| 100 | std::stringstream ss; |
| 101 | ss << _name << _iterator << ".ppm"; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 102 | |
| 103 | arm_compute::utils::save_to_ppm(tensor, ss.str()); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 104 | |
| 105 | _iterator++; |
| 106 | if(_maximum == 0) |
| 107 | { |
| 108 | return true; |
| 109 | } |
| 110 | return _iterator < _maximum; |
| 111 | } |
| 112 | |
| 113 | DummyAccessor::DummyAccessor(unsigned int maximum) |
| 114 | : _iterator(0), _maximum(maximum) |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | bool DummyAccessor::access_tensor(ITensor &tensor) |
| 119 | { |
| 120 | ARM_COMPUTE_UNUSED(tensor); |
| 121 | bool ret = _maximum == 0 || _iterator < _maximum; |
| 122 | if(_iterator == _maximum) |
| 123 | { |
| 124 | _iterator = 0; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | _iterator++; |
| 129 | } |
| 130 | return ret; |
| 131 | } |
| 132 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 133 | NumPyAccessor::NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream) |
| 134 | : _npy_tensor(), _filename(std::move(npy_path)), _output_stream(output_stream) |
| 135 | { |
| 136 | NumPyBinLoader loader(_filename); |
| 137 | |
| 138 | TensorInfo info(shape, 1, data_type); |
| 139 | _npy_tensor.allocator()->init(info); |
| 140 | _npy_tensor.allocator()->allocate(); |
| 141 | |
| 142 | loader.access_tensor(_npy_tensor); |
| 143 | } |
| 144 | |
| 145 | template <typename T> |
| 146 | void NumPyAccessor::access_numpy_tensor(ITensor &tensor) |
| 147 | { |
| 148 | const int num_elements = tensor.info()->total_size(); |
| 149 | int num_mismatches = utils::compare_tensor<T>(tensor, _npy_tensor); |
| 150 | float percentage_mismatches = static_cast<float>(num_mismatches) / num_elements; |
| 151 | |
| 152 | _output_stream << "Results: " << 100.f - (percentage_mismatches * 100) << " % matches with the provided output[" << _filename << "]." << std::endl; |
| 153 | } |
| 154 | |
| 155 | bool NumPyAccessor::access_tensor(ITensor &tensor) |
| 156 | { |
| 157 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32); |
| 158 | ARM_COMPUTE_ERROR_ON(_npy_tensor.info()->dimension(0) != tensor.info()->dimension(0)); |
| 159 | |
| 160 | switch(tensor.info()->data_type()) |
| 161 | { |
| 162 | case DataType::F32: |
| 163 | access_numpy_tensor<float>(tensor); |
| 164 | break; |
| 165 | default: |
| 166 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 167 | } |
| 168 | |
| 169 | return false; |
| 170 | } |
| 171 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 172 | PPMAccessor::PPMAccessor(std::string ppm_path, bool bgr, std::unique_ptr<IPreprocessor> preprocessor) |
| 173 | : _ppm_path(std::move(ppm_path)), _bgr(bgr), _preprocessor(std::move(preprocessor)) |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 174 | { |
| 175 | } |
| 176 | |
| 177 | bool PPMAccessor::access_tensor(ITensor &tensor) |
| 178 | { |
| 179 | utils::PPMLoader ppm; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 180 | |
| 181 | // Open PPM file |
| 182 | ppm.open(_ppm_path); |
| 183 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 184 | // Get permutated shape and permutation parameters |
| 185 | TensorShape permuted_shape = tensor.info()->tensor_shape(); |
| 186 | arm_compute::PermutationVector perm; |
| 187 | if(tensor.info()->data_layout() != DataLayout::NCHW) |
| 188 | { |
| 189 | std::tie(permuted_shape, perm) = compute_permutation_paramaters(tensor.info()->tensor_shape(), tensor.info()->data_layout()); |
| 190 | } |
| 191 | ARM_COMPUTE_ERROR_ON_MSG(ppm.width() != permuted_shape.x() || ppm.height() != permuted_shape.y(), |
| 192 | "Failed to load image file: dimensions [%d,%d] not correct, expected [%d,%d].", ppm.width(), ppm.height(), permuted_shape.x(), permuted_shape.y()); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 193 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 194 | // Fill the tensor with the PPM content (BGR) |
| 195 | ppm.fill_planar_tensor(tensor, _bgr); |
| 196 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 197 | // Preprocess tensor |
| 198 | if(_preprocessor) |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 199 | { |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 200 | _preprocessor->preprocess(tensor); |
| 201 | } |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 206 | ValidationInputAccessor::ValidationInputAccessor(const std::string &image_list, |
| 207 | std::string images_path, |
| 208 | bool bgr, |
| 209 | unsigned int start, |
| 210 | unsigned int end) |
| 211 | : _path(std::move(images_path)), _images(), _bgr(bgr), _offset(0) |
| 212 | { |
| 213 | ARM_COMPUTE_ERROR_ON_MSG(start > end, "Invalid validation range!"); |
| 214 | |
| 215 | std::ifstream ifs; |
| 216 | try |
| 217 | { |
| 218 | ifs.exceptions(std::ifstream::badbit); |
| 219 | ifs.open(image_list, std::ios::in | std::ios::binary); |
| 220 | |
| 221 | // Parse image names |
| 222 | unsigned int counter = 0; |
| 223 | for(std::string line; !std::getline(ifs, line).fail() && counter <= end; ++counter) |
| 224 | { |
| 225 | // Add image to process if withing range |
| 226 | if(counter >= start) |
| 227 | { |
| 228 | std::stringstream linestream(line); |
| 229 | std::string image_name; |
| 230 | |
| 231 | linestream >> image_name; |
| 232 | _images.emplace_back(std::move(image_name)); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | catch(const std::ifstream::failure &e) |
| 237 | { |
| 238 | ARM_COMPUTE_ERROR("Accessing %s: %s", image_list.c_str(), e.what()); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | bool ValidationInputAccessor::access_tensor(arm_compute::ITensor &tensor) |
| 243 | { |
| 244 | bool ret = _offset < _images.size(); |
| 245 | if(ret) |
| 246 | { |
| 247 | utils::JPEGLoader jpeg; |
| 248 | |
| 249 | // Open JPEG file |
| 250 | jpeg.open(_path + _images[_offset++]); |
| 251 | |
| 252 | // Get permutated shape and permutation parameters |
| 253 | TensorShape permuted_shape = tensor.info()->tensor_shape(); |
| 254 | arm_compute::PermutationVector perm; |
| 255 | if(tensor.info()->data_layout() != DataLayout::NCHW) |
| 256 | { |
| 257 | std::tie(permuted_shape, perm) = compute_permutation_paramaters(tensor.info()->tensor_shape(), |
| 258 | tensor.info()->data_layout()); |
| 259 | } |
| 260 | ARM_COMPUTE_ERROR_ON_MSG(jpeg.width() != permuted_shape.x() || jpeg.height() != permuted_shape.y(), |
| 261 | "Failed to load image file: dimensions [%d,%d] not correct, expected [%d,%d].", |
| 262 | jpeg.width(), jpeg.height(), permuted_shape.x(), permuted_shape.y()); |
| 263 | |
| 264 | // Fill the tensor with the PPM content (BGR) |
| 265 | jpeg.fill_planar_tensor(tensor, _bgr); |
| 266 | } |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 271 | TopNPredictionsAccessor::TopNPredictionsAccessor(const std::string &labels_path, size_t top_n, std::ostream &output_stream) |
| 272 | : _labels(), _output_stream(output_stream), _top_n(top_n) |
| 273 | { |
| 274 | _labels.clear(); |
| 275 | |
| 276 | std::ifstream ifs; |
| 277 | |
| 278 | try |
| 279 | { |
| 280 | ifs.exceptions(std::ifstream::badbit); |
| 281 | ifs.open(labels_path, std::ios::in | std::ios::binary); |
| 282 | |
| 283 | for(std::string line; !std::getline(ifs, line).fail();) |
| 284 | { |
| 285 | _labels.emplace_back(line); |
| 286 | } |
| 287 | } |
| 288 | catch(const std::ifstream::failure &e) |
| 289 | { |
| 290 | ARM_COMPUTE_ERROR("Accessing %s: %s", labels_path.c_str(), e.what()); |
| 291 | } |
| 292 | } |
| 293 | |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 294 | template <typename T> |
| 295 | void TopNPredictionsAccessor::access_predictions_tensor(ITensor &tensor) |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 296 | { |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 297 | // Get the predicted class |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 298 | std::vector<T> classes_prob; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 299 | std::vector<size_t> index; |
| 300 | |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 301 | 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] | 302 | const size_t num_classes = tensor.info()->dimension(0); |
| 303 | |
| 304 | classes_prob.resize(num_classes); |
| 305 | index.resize(num_classes); |
| 306 | |
| 307 | std::copy(output_net, output_net + num_classes, classes_prob.begin()); |
| 308 | |
| 309 | // Sort results |
| 310 | std::iota(std::begin(index), std::end(index), static_cast<size_t>(0)); |
| 311 | std::sort(std::begin(index), std::end(index), |
| 312 | [&](size_t a, size_t b) |
| 313 | { |
| 314 | return classes_prob[a] > classes_prob[b]; |
| 315 | }); |
| 316 | |
| 317 | _output_stream << "---------- Top " << _top_n << " predictions ----------" << std::endl |
| 318 | << std::endl; |
| 319 | for(size_t i = 0; i < _top_n; ++i) |
| 320 | { |
| 321 | _output_stream << std::fixed << std::setprecision(4) |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 322 | << +classes_prob[index.at(i)] |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 323 | << " - [id = " << index.at(i) << "]" |
| 324 | << ", " << _labels[index.at(i)] << std::endl; |
| 325 | } |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | bool TopNPredictionsAccessor::access_tensor(ITensor &tensor) |
| 329 | { |
| 330 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32, DataType::QASYMM8); |
| 331 | ARM_COMPUTE_ERROR_ON(_labels.size() != tensor.info()->dimension(0)); |
| 332 | |
| 333 | switch(tensor.info()->data_type()) |
| 334 | { |
| 335 | case DataType::QASYMM8: |
| 336 | access_predictions_tensor<uint8_t>(tensor); |
| 337 | break; |
| 338 | case DataType::F32: |
| 339 | access_predictions_tensor<float>(tensor); |
| 340 | break; |
| 341 | default: |
| 342 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 343 | } |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 344 | |
| 345 | return false; |
| 346 | } |
| 347 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 348 | RandomAccessor::RandomAccessor(PixelValue lower, PixelValue upper, std::random_device::result_type seed) |
| 349 | : _lower(lower), _upper(upper), _seed(seed) |
| 350 | { |
| 351 | } |
| 352 | |
| 353 | template <typename T, typename D> |
| 354 | void RandomAccessor::fill(ITensor &tensor, D &&distribution) |
| 355 | { |
| 356 | std::mt19937 gen(_seed); |
| 357 | |
hakanardo | f36ac35 | 2018-02-16 10:06:34 +0100 | [diff] [blame] | 358 | if(tensor.info()->padding().empty() && (dynamic_cast<SubTensor *>(&tensor) == nullptr)) |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 359 | { |
| 360 | for(size_t offset = 0; offset < tensor.info()->total_size(); offset += tensor.info()->element_size()) |
| 361 | { |
| 362 | const T value = distribution(gen); |
| 363 | *reinterpret_cast<T *>(tensor.buffer() + offset) = value; |
| 364 | } |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | // If tensor has padding accessing tensor elements through execution window. |
| 369 | Window window; |
| 370 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 371 | |
| 372 | execute_window_loop(window, [&](const Coordinates & id) |
| 373 | { |
| 374 | const T value = distribution(gen); |
| 375 | *reinterpret_cast<T *>(tensor.ptr_to_element(id)) = value; |
| 376 | }); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | bool RandomAccessor::access_tensor(ITensor &tensor) |
| 381 | { |
| 382 | switch(tensor.info()->data_type()) |
| 383 | { |
| 384 | case DataType::U8: |
| 385 | { |
| 386 | std::uniform_int_distribution<uint8_t> distribution_u8(_lower.get<uint8_t>(), _upper.get<uint8_t>()); |
| 387 | fill<uint8_t>(tensor, distribution_u8); |
| 388 | break; |
| 389 | } |
| 390 | case DataType::S8: |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 391 | { |
| 392 | std::uniform_int_distribution<int8_t> distribution_s8(_lower.get<int8_t>(), _upper.get<int8_t>()); |
| 393 | fill<int8_t>(tensor, distribution_s8); |
| 394 | break; |
| 395 | } |
| 396 | case DataType::U16: |
| 397 | { |
| 398 | std::uniform_int_distribution<uint16_t> distribution_u16(_lower.get<uint16_t>(), _upper.get<uint16_t>()); |
| 399 | fill<uint16_t>(tensor, distribution_u16); |
| 400 | break; |
| 401 | } |
| 402 | case DataType::S16: |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 403 | { |
| 404 | std::uniform_int_distribution<int16_t> distribution_s16(_lower.get<int16_t>(), _upper.get<int16_t>()); |
| 405 | fill<int16_t>(tensor, distribution_s16); |
| 406 | break; |
| 407 | } |
| 408 | case DataType::U32: |
| 409 | { |
| 410 | std::uniform_int_distribution<uint32_t> distribution_u32(_lower.get<uint32_t>(), _upper.get<uint32_t>()); |
| 411 | fill<uint32_t>(tensor, distribution_u32); |
| 412 | break; |
| 413 | } |
| 414 | case DataType::S32: |
| 415 | { |
| 416 | std::uniform_int_distribution<int32_t> distribution_s32(_lower.get<int32_t>(), _upper.get<int32_t>()); |
| 417 | fill<int32_t>(tensor, distribution_s32); |
| 418 | break; |
| 419 | } |
| 420 | case DataType::U64: |
| 421 | { |
| 422 | std::uniform_int_distribution<uint64_t> distribution_u64(_lower.get<uint64_t>(), _upper.get<uint64_t>()); |
| 423 | fill<uint64_t>(tensor, distribution_u64); |
| 424 | break; |
| 425 | } |
| 426 | case DataType::S64: |
| 427 | { |
| 428 | std::uniform_int_distribution<int64_t> distribution_s64(_lower.get<int64_t>(), _upper.get<int64_t>()); |
| 429 | fill<int64_t>(tensor, distribution_s64); |
| 430 | break; |
| 431 | } |
| 432 | case DataType::F16: |
| 433 | { |
| 434 | std::uniform_real_distribution<float> distribution_f16(_lower.get<float>(), _upper.get<float>()); |
| 435 | fill<float>(tensor, distribution_f16); |
| 436 | break; |
| 437 | } |
| 438 | case DataType::F32: |
| 439 | { |
| 440 | std::uniform_real_distribution<float> distribution_f32(_lower.get<float>(), _upper.get<float>()); |
| 441 | fill<float>(tensor, distribution_f32); |
| 442 | break; |
| 443 | } |
| 444 | case DataType::F64: |
| 445 | { |
| 446 | std::uniform_real_distribution<double> distribution_f64(_lower.get<double>(), _upper.get<double>()); |
| 447 | fill<double>(tensor, distribution_f64); |
| 448 | break; |
| 449 | } |
| 450 | default: |
| 451 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 452 | } |
| 453 | return true; |
| 454 | } |
| 455 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 456 | NumPyBinLoader::NumPyBinLoader(std::string filename, DataLayout file_layout) |
| 457 | : _filename(std::move(filename)), _file_layout(file_layout) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 458 | { |
| 459 | } |
| 460 | |
| 461 | bool NumPyBinLoader::access_tensor(ITensor &tensor) |
| 462 | { |
| 463 | const TensorShape tensor_shape = tensor.info()->tensor_shape(); |
| 464 | std::vector<unsigned long> shape; |
| 465 | |
| 466 | // Open file |
| 467 | std::ifstream stream(_filename, std::ios::in | std::ios::binary); |
| 468 | ARM_COMPUTE_ERROR_ON_MSG(!stream.good(), "Failed to load binary data"); |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 469 | std::string header = npy::read_header(stream); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 470 | |
| 471 | // Parse header |
| 472 | bool fortran_order = false; |
| 473 | std::string typestr; |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 474 | npy::parse_header(header, typestr, fortran_order, shape); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 475 | |
| 476 | // Check if the typestring matches the given one |
| 477 | std::string expect_typestr = arm_compute::utils::get_typestring(tensor.info()->data_type()); |
| 478 | ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch"); |
| 479 | |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 480 | // Reverse vector in case of non fortran order |
| 481 | if(!fortran_order) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 482 | { |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 483 | std::reverse(shape.begin(), shape.end()); |
| 484 | } |
| 485 | |
| 486 | // Correct dimensions (Needs to match TensorShape dimension corrections) |
| 487 | if(shape.size() != tensor_shape.num_dimensions()) |
| 488 | { |
| 489 | for(int i = static_cast<int>(shape.size()) - 1; i > 0; --i) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 490 | { |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 491 | if(shape[i] == 1) |
| 492 | { |
| 493 | shape.pop_back(); |
| 494 | } |
| 495 | else |
| 496 | { |
| 497 | break; |
| 498 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 499 | } |
| 500 | } |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 501 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 502 | bool are_layouts_different = (_file_layout != tensor.info()->data_layout()); |
| 503 | |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 504 | // Validate tensor ranks |
| 505 | ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor_shape.num_dimensions(), "Tensor ranks mismatch"); |
| 506 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 507 | // Set permutation parameters if needed |
| 508 | TensorShape permuted_shape = tensor_shape; |
| 509 | arm_compute::PermutationVector perm; |
| 510 | if(are_layouts_different) |
| 511 | { |
| 512 | std::tie(permuted_shape, perm) = compute_permutation_paramaters(tensor_shape, tensor.info()->data_layout()); |
| 513 | } |
| 514 | |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 515 | // Validate shapes |
| 516 | for(size_t i = 0; i < shape.size(); ++i) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 517 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 518 | ARM_COMPUTE_ERROR_ON_MSG(permuted_shape[i] != shape[i], "Tensor dimensions mismatch"); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 521 | // Validate shapes and copy tensor |
| 522 | if(!are_layouts_different || perm.num_dimensions() <= 2) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 523 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 524 | // Read data |
| 525 | if(tensor.info()->padding().empty() && (dynamic_cast<SubTensor *>(&tensor) == nullptr)) |
| 526 | { |
| 527 | // If tensor has no padding read directly from stream. |
| 528 | stream.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size()); |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | // If tensor has padding accessing tensor elements through execution window. |
| 533 | Window window; |
| 534 | window.use_tensor_dimensions(tensor_shape); |
| 535 | |
| 536 | execute_window_loop(window, [&](const Coordinates & id) |
| 537 | { |
| 538 | stream.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size()); |
| 539 | }); |
| 540 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 541 | } |
| 542 | else |
| 543 | { |
| 544 | // If tensor has padding accessing tensor elements through execution window. |
| 545 | Window window; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 546 | window.use_tensor_dimensions(permuted_shape); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 547 | |
| 548 | execute_window_loop(window, [&](const Coordinates & id) |
| 549 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 550 | Coordinates coords(id); |
| 551 | arm_compute::permute(coords, perm); |
| 552 | stream.read(reinterpret_cast<char *>(tensor.ptr_to_element(coords)), tensor.info()->element_size()); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 553 | }); |
| 554 | } |
| 555 | return true; |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 556 | } |