Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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" |
| 26 | #include "utils/Utils.h" |
| 27 | |
| 28 | #ifdef ARM_COMPUTE_CL |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 31 | #endif /* ARM_COMPUTE_CL */ |
| 32 | |
| 33 | #include "arm_compute/core/Error.h" |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/PixelValue.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 35 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 36 | #include <algorithm> |
| 37 | #include <iomanip> |
| 38 | #include <ostream> |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 39 | #include <random> |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 40 | |
| 41 | using namespace arm_compute::graph_utils; |
| 42 | |
| 43 | PPMWriter::PPMWriter(std::string name, unsigned int maximum) |
| 44 | : _name(std::move(name)), _iterator(0), _maximum(maximum) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | bool PPMWriter::access_tensor(ITensor &tensor) |
| 49 | { |
| 50 | std::stringstream ss; |
| 51 | ss << _name << _iterator << ".ppm"; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 52 | |
| 53 | arm_compute::utils::save_to_ppm(tensor, ss.str()); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 54 | |
| 55 | _iterator++; |
| 56 | if(_maximum == 0) |
| 57 | { |
| 58 | return true; |
| 59 | } |
| 60 | return _iterator < _maximum; |
| 61 | } |
| 62 | |
| 63 | DummyAccessor::DummyAccessor(unsigned int maximum) |
| 64 | : _iterator(0), _maximum(maximum) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | bool DummyAccessor::access_tensor(ITensor &tensor) |
| 69 | { |
| 70 | ARM_COMPUTE_UNUSED(tensor); |
| 71 | bool ret = _maximum == 0 || _iterator < _maximum; |
| 72 | if(_iterator == _maximum) |
| 73 | { |
| 74 | _iterator = 0; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | _iterator++; |
| 79 | } |
| 80 | return ret; |
| 81 | } |
| 82 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 83 | PPMAccessor::PPMAccessor(const std::string &ppm_path, bool bgr, float mean_r, float mean_g, float mean_b) |
| 84 | : _ppm_path(ppm_path), _bgr(bgr), _mean_r(mean_r), _mean_g(mean_g), _mean_b(mean_b) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | bool PPMAccessor::access_tensor(ITensor &tensor) |
| 89 | { |
| 90 | utils::PPMLoader ppm; |
| 91 | const float mean[3] = |
| 92 | { |
| 93 | _bgr ? _mean_b : _mean_r, |
| 94 | _mean_g, |
| 95 | _bgr ? _mean_r : _mean_b |
| 96 | }; |
| 97 | |
| 98 | // Open PPM file |
| 99 | ppm.open(_ppm_path); |
| 100 | |
| 101 | // Fill the tensor with the PPM content (BGR) |
| 102 | ppm.fill_planar_tensor(tensor, _bgr); |
| 103 | |
| 104 | // Subtract the mean value from each channel |
| 105 | Window window; |
| 106 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 107 | |
| 108 | execute_window_loop(window, [&](const Coordinates & id) |
| 109 | { |
| 110 | const float value = *reinterpret_cast<float *>(tensor.ptr_to_element(id)) - mean[id.z()]; |
| 111 | *reinterpret_cast<float *>(tensor.ptr_to_element(id)) = value; |
| 112 | }); |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | TopNPredictionsAccessor::TopNPredictionsAccessor(const std::string &labels_path, size_t top_n, std::ostream &output_stream) |
| 118 | : _labels(), _output_stream(output_stream), _top_n(top_n) |
| 119 | { |
| 120 | _labels.clear(); |
| 121 | |
| 122 | std::ifstream ifs; |
| 123 | |
| 124 | try |
| 125 | { |
| 126 | ifs.exceptions(std::ifstream::badbit); |
| 127 | ifs.open(labels_path, std::ios::in | std::ios::binary); |
| 128 | |
| 129 | for(std::string line; !std::getline(ifs, line).fail();) |
| 130 | { |
| 131 | _labels.emplace_back(line); |
| 132 | } |
| 133 | } |
| 134 | catch(const std::ifstream::failure &e) |
| 135 | { |
| 136 | ARM_COMPUTE_ERROR("Accessing %s: %s", labels_path.c_str(), e.what()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | bool TopNPredictionsAccessor::access_tensor(ITensor &tensor) |
| 141 | { |
| 142 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32); |
| 143 | ARM_COMPUTE_ERROR_ON(_labels.size() != tensor.info()->dimension(0)); |
| 144 | |
| 145 | // Get the predicted class |
| 146 | std::vector<float> classes_prob; |
| 147 | std::vector<size_t> index; |
| 148 | |
| 149 | const auto output_net = reinterpret_cast<float *>(tensor.buffer() + tensor.info()->offset_first_element_in_bytes()); |
| 150 | const size_t num_classes = tensor.info()->dimension(0); |
| 151 | |
| 152 | classes_prob.resize(num_classes); |
| 153 | index.resize(num_classes); |
| 154 | |
| 155 | std::copy(output_net, output_net + num_classes, classes_prob.begin()); |
| 156 | |
| 157 | // Sort results |
| 158 | std::iota(std::begin(index), std::end(index), static_cast<size_t>(0)); |
| 159 | std::sort(std::begin(index), std::end(index), |
| 160 | [&](size_t a, size_t b) |
| 161 | { |
| 162 | return classes_prob[a] > classes_prob[b]; |
| 163 | }); |
| 164 | |
| 165 | _output_stream << "---------- Top " << _top_n << " predictions ----------" << std::endl |
| 166 | << std::endl; |
| 167 | for(size_t i = 0; i < _top_n; ++i) |
| 168 | { |
| 169 | _output_stream << std::fixed << std::setprecision(4) |
| 170 | << classes_prob[index.at(i)] |
| 171 | << " - [id = " << index.at(i) << "]" |
| 172 | << ", " << _labels[index.at(i)] << std::endl; |
| 173 | } |
| 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 178 | RandomAccessor::RandomAccessor(PixelValue lower, PixelValue upper, std::random_device::result_type seed) |
| 179 | : _lower(lower), _upper(upper), _seed(seed) |
| 180 | { |
| 181 | } |
| 182 | |
| 183 | template <typename T, typename D> |
| 184 | void RandomAccessor::fill(ITensor &tensor, D &&distribution) |
| 185 | { |
| 186 | std::mt19937 gen(_seed); |
| 187 | |
| 188 | if(tensor.info()->padding().empty()) |
| 189 | { |
| 190 | for(size_t offset = 0; offset < tensor.info()->total_size(); offset += tensor.info()->element_size()) |
| 191 | { |
| 192 | const T value = distribution(gen); |
| 193 | *reinterpret_cast<T *>(tensor.buffer() + offset) = value; |
| 194 | } |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | // If tensor has padding accessing tensor elements through execution window. |
| 199 | Window window; |
| 200 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 201 | |
| 202 | execute_window_loop(window, [&](const Coordinates & id) |
| 203 | { |
| 204 | const T value = distribution(gen); |
| 205 | *reinterpret_cast<T *>(tensor.ptr_to_element(id)) = value; |
| 206 | }); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | bool RandomAccessor::access_tensor(ITensor &tensor) |
| 211 | { |
| 212 | switch(tensor.info()->data_type()) |
| 213 | { |
| 214 | case DataType::U8: |
| 215 | { |
| 216 | std::uniform_int_distribution<uint8_t> distribution_u8(_lower.get<uint8_t>(), _upper.get<uint8_t>()); |
| 217 | fill<uint8_t>(tensor, distribution_u8); |
| 218 | break; |
| 219 | } |
| 220 | case DataType::S8: |
| 221 | case DataType::QS8: |
| 222 | { |
| 223 | std::uniform_int_distribution<int8_t> distribution_s8(_lower.get<int8_t>(), _upper.get<int8_t>()); |
| 224 | fill<int8_t>(tensor, distribution_s8); |
| 225 | break; |
| 226 | } |
| 227 | case DataType::U16: |
| 228 | { |
| 229 | std::uniform_int_distribution<uint16_t> distribution_u16(_lower.get<uint16_t>(), _upper.get<uint16_t>()); |
| 230 | fill<uint16_t>(tensor, distribution_u16); |
| 231 | break; |
| 232 | } |
| 233 | case DataType::S16: |
| 234 | case DataType::QS16: |
| 235 | { |
| 236 | std::uniform_int_distribution<int16_t> distribution_s16(_lower.get<int16_t>(), _upper.get<int16_t>()); |
| 237 | fill<int16_t>(tensor, distribution_s16); |
| 238 | break; |
| 239 | } |
| 240 | case DataType::U32: |
| 241 | { |
| 242 | std::uniform_int_distribution<uint32_t> distribution_u32(_lower.get<uint32_t>(), _upper.get<uint32_t>()); |
| 243 | fill<uint32_t>(tensor, distribution_u32); |
| 244 | break; |
| 245 | } |
| 246 | case DataType::S32: |
| 247 | { |
| 248 | std::uniform_int_distribution<int32_t> distribution_s32(_lower.get<int32_t>(), _upper.get<int32_t>()); |
| 249 | fill<int32_t>(tensor, distribution_s32); |
| 250 | break; |
| 251 | } |
| 252 | case DataType::U64: |
| 253 | { |
| 254 | std::uniform_int_distribution<uint64_t> distribution_u64(_lower.get<uint64_t>(), _upper.get<uint64_t>()); |
| 255 | fill<uint64_t>(tensor, distribution_u64); |
| 256 | break; |
| 257 | } |
| 258 | case DataType::S64: |
| 259 | { |
| 260 | std::uniform_int_distribution<int64_t> distribution_s64(_lower.get<int64_t>(), _upper.get<int64_t>()); |
| 261 | fill<int64_t>(tensor, distribution_s64); |
| 262 | break; |
| 263 | } |
| 264 | case DataType::F16: |
| 265 | { |
| 266 | std::uniform_real_distribution<float> distribution_f16(_lower.get<float>(), _upper.get<float>()); |
| 267 | fill<float>(tensor, distribution_f16); |
| 268 | break; |
| 269 | } |
| 270 | case DataType::F32: |
| 271 | { |
| 272 | std::uniform_real_distribution<float> distribution_f32(_lower.get<float>(), _upper.get<float>()); |
| 273 | fill<float>(tensor, distribution_f32); |
| 274 | break; |
| 275 | } |
| 276 | case DataType::F64: |
| 277 | { |
| 278 | std::uniform_real_distribution<double> distribution_f64(_lower.get<double>(), _upper.get<double>()); |
| 279 | fill<double>(tensor, distribution_f64); |
| 280 | break; |
| 281 | } |
| 282 | default: |
| 283 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 288 | NumPyBinLoader::NumPyBinLoader(std::string filename) |
| 289 | : _filename(std::move(filename)) |
| 290 | { |
| 291 | } |
| 292 | |
| 293 | bool NumPyBinLoader::access_tensor(ITensor &tensor) |
| 294 | { |
| 295 | const TensorShape tensor_shape = tensor.info()->tensor_shape(); |
| 296 | std::vector<unsigned long> shape; |
| 297 | |
| 298 | // Open file |
| 299 | std::ifstream stream(_filename, std::ios::in | std::ios::binary); |
| 300 | ARM_COMPUTE_ERROR_ON_MSG(!stream.good(), "Failed to load binary data"); |
| 301 | // Check magic bytes and version number |
| 302 | unsigned char v_major = 0; |
| 303 | unsigned char v_minor = 0; |
| 304 | npy::read_magic(stream, &v_major, &v_minor); |
| 305 | |
| 306 | // Read header |
| 307 | std::string header; |
| 308 | if(v_major == 1 && v_minor == 0) |
| 309 | { |
| 310 | header = npy::read_header_1_0(stream); |
| 311 | } |
| 312 | else if(v_major == 2 && v_minor == 0) |
| 313 | { |
| 314 | header = npy::read_header_2_0(stream); |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | ARM_COMPUTE_ERROR("Unsupported file format version"); |
| 319 | } |
| 320 | |
| 321 | // Parse header |
| 322 | bool fortran_order = false; |
| 323 | std::string typestr; |
| 324 | npy::ParseHeader(header, typestr, &fortran_order, shape); |
| 325 | |
| 326 | // Check if the typestring matches the given one |
| 327 | std::string expect_typestr = arm_compute::utils::get_typestring(tensor.info()->data_type()); |
| 328 | ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch"); |
| 329 | |
| 330 | // Validate tensor shape |
| 331 | ARM_COMPUTE_ERROR_ON_MSG(shape.size() != tensor_shape.num_dimensions(), "Tensor ranks mismatch"); |
| 332 | if(fortran_order) |
| 333 | { |
| 334 | for(size_t i = 0; i < shape.size(); ++i) |
| 335 | { |
| 336 | ARM_COMPUTE_ERROR_ON_MSG(tensor_shape[i] != shape[i], "Tensor dimensions mismatch"); |
| 337 | } |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | for(size_t i = 0; i < shape.size(); ++i) |
| 342 | { |
| 343 | ARM_COMPUTE_ERROR_ON_MSG(tensor_shape[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch"); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Read data |
| 348 | if(tensor.info()->padding().empty()) |
| 349 | { |
| 350 | // If tensor has no padding read directly from stream. |
| 351 | stream.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size()); |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | // If tensor has padding accessing tensor elements through execution window. |
| 356 | Window window; |
| 357 | window.use_tensor_dimensions(tensor_shape); |
| 358 | |
| 359 | execute_window_loop(window, [&](const Coordinates & id) |
| 360 | { |
| 361 | stream.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size()); |
| 362 | }); |
| 363 | } |
| 364 | return true; |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 365 | } |