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