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