Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | #include "TensorLibrary.h" |
| 25 | |
| 26 | #include "TypePrinter.h" |
| 27 | #include "UserConfiguration.h" |
| 28 | #include "Utils.h" |
| 29 | |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | |
| 32 | #include <cctype> |
| 33 | #include <fstream> |
| 34 | #include <limits> |
| 35 | #include <map> |
| 36 | #include <mutex> |
| 37 | #include <sstream> |
| 38 | #include <stdexcept> |
| 39 | #include <tuple> |
| 40 | #include <unordered_map> |
| 41 | #include <utility> |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | namespace test |
| 46 | { |
| 47 | namespace |
| 48 | { |
Giorgio Arena | fda4618 | 2017-06-16 13:57:33 +0100 | [diff] [blame] | 49 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> |
| 50 | void rgb_to_luminance(const RawTensor &src, RawTensor &dst) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | { |
| 52 | const size_t min_size = std::min(src.size(), dst.size()); |
| 53 | |
| 54 | for(size_t i = 0, j = 0; i < min_size; i += 3, ++j) |
| 55 | { |
Giorgio Arena | fda4618 | 2017-06-16 13:57:33 +0100 | [diff] [blame] | 56 | reinterpret_cast<T *>(dst.data())[j] = 0.2126f * src.data()[i + 0] + 0.7152f * src.data()[i + 1] + 0.0722f * src.data()[i + 2]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | void extract_r_from_rgb(const RawTensor &src, RawTensor &dst) |
| 61 | { |
| 62 | const size_t min_size = std::min(src.size(), dst.size()); |
| 63 | |
| 64 | for(size_t i = 0, j = 0; i < min_size; i += 3, ++j) |
| 65 | { |
| 66 | dst.data()[j] = src.data()[i]; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void extract_g_from_rgb(const RawTensor &src, RawTensor &dst) |
| 71 | { |
| 72 | const size_t min_size = std::min(src.size(), dst.size()); |
| 73 | |
| 74 | for(size_t i = 1, j = 0; i < min_size; i += 3, ++j) |
| 75 | { |
| 76 | dst.data()[j] = src.data()[i]; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void discard_comments(std::ifstream &fs) |
| 81 | { |
| 82 | while(fs.peek() == '#') |
| 83 | { |
| 84 | fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void discard_comments_and_spaces(std::ifstream &fs) |
| 89 | { |
| 90 | while(true) |
| 91 | { |
| 92 | discard_comments(fs); |
| 93 | |
| 94 | if(isspace(fs.peek()) == 0) |
| 95 | { |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | fs.ignore(1); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs) |
| 104 | { |
| 105 | // Check the PPM magic number is valid |
| 106 | std::array<char, 2> magic_number{ { 0 } }; |
| 107 | fs >> magic_number[0] >> magic_number[1]; |
| 108 | |
| 109 | if(magic_number[0] != 'P' || magic_number[1] != '6') |
| 110 | { |
| 111 | throw std::runtime_error("Only raw PPM format is suported"); |
| 112 | } |
| 113 | |
| 114 | discard_comments_and_spaces(fs); |
| 115 | |
| 116 | unsigned int width = 0; |
| 117 | fs >> width; |
| 118 | |
| 119 | discard_comments_and_spaces(fs); |
| 120 | |
| 121 | unsigned int height = 0; |
| 122 | fs >> height; |
| 123 | |
| 124 | discard_comments_and_spaces(fs); |
| 125 | |
| 126 | int max_value = 0; |
| 127 | fs >> max_value; |
| 128 | |
| 129 | if(!fs.good()) |
| 130 | { |
| 131 | throw std::runtime_error("Cannot read image dimensions"); |
| 132 | } |
| 133 | |
| 134 | if(max_value != 255) |
| 135 | { |
| 136 | throw std::runtime_error("RawTensor doesn't have 8-bit values"); |
| 137 | } |
| 138 | |
| 139 | discard_comments(fs); |
| 140 | |
| 141 | if(isspace(fs.peek()) == 0) |
| 142 | { |
| 143 | throw std::runtime_error("Invalid PPM header"); |
| 144 | } |
| 145 | |
| 146 | fs.ignore(1); |
| 147 | |
| 148 | return std::make_tuple(width, height, max_value); |
| 149 | } |
| 150 | |
| 151 | RawTensor load_ppm(const std::string &path) |
| 152 | { |
| 153 | std::ifstream file(path, std::ios::in | std::ios::binary); |
| 154 | |
| 155 | if(!file.good()) |
| 156 | { |
| 157 | throw std::runtime_error("Could not load PPM image: " + path); |
| 158 | } |
| 159 | |
| 160 | unsigned int width = 0; |
| 161 | unsigned int height = 0; |
| 162 | |
| 163 | std::tie(width, height, std::ignore) = parse_ppm_header(file); |
| 164 | |
| 165 | RawTensor raw(TensorShape(width, height), Format::RGB888); |
| 166 | |
| 167 | // Check if the file is large enough to fill the image |
| 168 | const size_t current_position = file.tellg(); |
| 169 | file.seekg(0, std::ios_base::end); |
| 170 | const size_t end_position = file.tellg(); |
| 171 | file.seekg(current_position, std::ios_base::beg); |
| 172 | |
| 173 | if((end_position - current_position) < raw.size()) |
| 174 | { |
| 175 | throw std::runtime_error("Not enough data in file"); |
| 176 | } |
| 177 | |
| 178 | file.read(reinterpret_cast<std::fstream::char_type *>(raw.data()), raw.size()); |
| 179 | |
| 180 | if(!file.good()) |
| 181 | { |
| 182 | throw std::runtime_error("Failure while reading image buffer"); |
| 183 | } |
| 184 | |
| 185 | return raw; |
| 186 | } |
| 187 | } // namespace |
| 188 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 189 | TensorLibrary::TensorLibrary(std::string path) //NOLINT |
| 190 | : _library_path(std::move(path)), |
| 191 | _seed{ std::random_device()() } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 192 | { |
| 193 | } |
| 194 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 195 | TensorLibrary::TensorLibrary(std::string path, std::random_device::result_type seed) //NOLINT |
| 196 | : _library_path(std::move(path)), |
| 197 | _seed{ seed } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 198 | { |
| 199 | } |
| 200 | |
| 201 | std::random_device::result_type TensorLibrary::seed() const |
| 202 | { |
| 203 | return _seed; |
| 204 | } |
| 205 | |
| 206 | void TensorLibrary::fill(RawTensor &raw, const std::string &name, Format format) const |
| 207 | { |
| 208 | //FIXME: Should be done by swapping cached buffers |
| 209 | const RawTensor &src = get(name, format); |
| 210 | std::copy_n(src.data(), raw.size(), raw.data()); |
| 211 | } |
| 212 | |
| 213 | void TensorLibrary::fill(RawTensor &raw, const std::string &name, Channel channel) const |
| 214 | { |
| 215 | fill(raw, name, get_format_for_channel(channel), channel); |
| 216 | } |
| 217 | |
| 218 | void TensorLibrary::fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const |
| 219 | { |
| 220 | const RawTensor &src = get(name, format, channel); |
| 221 | std::copy_n(src.data(), raw.size(), raw.data()); |
| 222 | } |
| 223 | |
| 224 | const TensorLibrary::Loader &TensorLibrary::get_loader(const std::string &extension) const |
| 225 | { |
| 226 | static std::unordered_map<std::string, Loader> loaders = |
| 227 | { |
| 228 | { "ppm", load_ppm } |
| 229 | }; |
| 230 | |
| 231 | const auto it = loaders.find(extension); |
| 232 | |
| 233 | if(it != loaders.end()) |
| 234 | { |
| 235 | return it->second; |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | throw std::invalid_argument("Cannot load image with extension '" + extension + "'"); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | const TensorLibrary::Converter &TensorLibrary::get_converter(Format src, Format dst) const |
| 244 | { |
| 245 | static std::map<std::pair<Format, Format>, Converter> converters = |
| 246 | { |
Giorgio Arena | fda4618 | 2017-06-16 13:57:33 +0100 | [diff] [blame] | 247 | { std::make_pair(Format::RGB888, Format::U8), rgb_to_luminance<uint8_t> }, |
| 248 | { std::make_pair(Format::RGB888, Format::U16), rgb_to_luminance<uint16_t> }, |
| 249 | { std::make_pair(Format::RGB888, Format::S16), rgb_to_luminance<int16_t> }, |
| 250 | { std::make_pair(Format::RGB888, Format::U32), rgb_to_luminance<uint32_t> } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | const auto it = converters.find(std::make_pair(src, dst)); |
| 254 | |
| 255 | if(it != converters.end()) |
| 256 | { |
| 257 | return it->second; |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | std::stringstream msg; |
| 262 | msg << "Cannot convert from format '" << src << "' to format '" << dst << "'\n"; |
| 263 | throw std::invalid_argument(msg.str()); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | const TensorLibrary::Converter &TensorLibrary::get_converter(DataType src, Format dst) const |
| 268 | { |
| 269 | static std::map<std::pair<DataType, Format>, Converter> converters = {}; |
| 270 | |
| 271 | const auto it = converters.find(std::make_pair(src, dst)); |
| 272 | |
| 273 | if(it != converters.end()) |
| 274 | { |
| 275 | return it->second; |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | std::stringstream msg; |
| 280 | msg << "Cannot convert from data type '" << src << "' to format '" << dst << "'\n"; |
| 281 | throw std::invalid_argument(msg.str()); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | const TensorLibrary::Converter &TensorLibrary::get_converter(DataType src, DataType dst) const |
| 286 | { |
| 287 | static std::map<std::pair<DataType, DataType>, Converter> converters = {}; |
| 288 | |
| 289 | const auto it = converters.find(std::make_pair(src, dst)); |
| 290 | |
| 291 | if(it != converters.end()) |
| 292 | { |
| 293 | return it->second; |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | std::stringstream msg; |
| 298 | msg << "Cannot convert from data type '" << src << "' to data type '" << dst << "'\n"; |
| 299 | throw std::invalid_argument(msg.str()); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | const TensorLibrary::Converter &TensorLibrary::get_converter(Format src, DataType dst) const |
| 304 | { |
| 305 | static std::map<std::pair<Format, DataType>, Converter> converters = {}; |
| 306 | |
| 307 | const auto it = converters.find(std::make_pair(src, dst)); |
| 308 | |
| 309 | if(it != converters.end()) |
| 310 | { |
| 311 | return it->second; |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | std::stringstream msg; |
| 316 | msg << "Cannot convert from format '" << src << "' to data type '" << dst << "'\n"; |
| 317 | throw std::invalid_argument(msg.str()); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | const TensorLibrary::Extractor &TensorLibrary::get_extractor(Format format, Channel channel) const |
| 322 | { |
| 323 | static std::map<std::pair<Format, Channel>, Extractor> extractors = |
| 324 | { |
| 325 | { std::make_pair(Format::RGB888, Channel::R), extract_r_from_rgb }, |
| 326 | { std::make_pair(Format::RGB888, Channel::G), extract_g_from_rgb } |
| 327 | }; |
| 328 | |
| 329 | const auto it = extractors.find(std::make_pair(format, channel)); |
| 330 | |
| 331 | if(it != extractors.end()) |
| 332 | { |
| 333 | return it->second; |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | std::stringstream msg; |
| 338 | msg << "Cannot extract channel '" << channel << "' from format '" << format << "'\n"; |
| 339 | throw std::invalid_argument(msg.str()); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | RawTensor TensorLibrary::load_image(const std::string &name) const |
| 344 | { |
| 345 | #ifdef _WIN32 |
| 346 | const std::string image_path = ("\\images\\"); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 347 | #else /* _WIN32 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 348 | const std::string image_path = ("/images/"); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 349 | #endif /* _WIN32 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 350 | |
| 351 | const std::string path = _library_path + image_path + name; |
| 352 | const std::string extension = path.substr(path.find_last_of('.') + 1); |
| 353 | return (*get_loader(extension))(path); |
| 354 | } |
| 355 | |
| 356 | const RawTensor &TensorLibrary::find_or_create_raw_tensor(const std::string &name, Format format) const |
| 357 | { |
| 358 | std::lock_guard<std::mutex> guard(_format_lock); |
| 359 | |
| 360 | const RawTensor *ptr = _cache.find(std::make_tuple(name, format)); |
| 361 | |
| 362 | if(ptr != nullptr) |
| 363 | { |
| 364 | return *ptr; |
| 365 | } |
| 366 | |
| 367 | RawTensor raw = load_image(name); |
| 368 | |
| 369 | if(raw.format() != format) |
| 370 | { |
| 371 | //FIXME: Remove unnecessary copy |
| 372 | RawTensor dst(raw.shape(), format); |
| 373 | (*get_converter(raw.format(), format))(raw, dst); |
| 374 | raw = std::move(dst); |
| 375 | } |
| 376 | |
| 377 | return _cache.add(std::make_tuple(name, format), std::move(raw)); |
| 378 | } |
| 379 | |
| 380 | const RawTensor &TensorLibrary::find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const |
| 381 | { |
| 382 | std::lock_guard<std::mutex> guard(_channel_lock); |
| 383 | |
| 384 | const RawTensor *ptr = _cache.find(std::make_tuple(name, format, channel)); |
| 385 | |
| 386 | if(ptr != nullptr) |
| 387 | { |
| 388 | return *ptr; |
| 389 | } |
| 390 | |
| 391 | const RawTensor &src = get(name, format); |
| 392 | //FIXME: Need to change shape to match channel |
| 393 | RawTensor dst(src.shape(), get_channel_format(channel)); |
| 394 | |
| 395 | (*get_extractor(format, channel))(src, dst); |
| 396 | |
| 397 | return _cache.add(std::make_tuple(name, format, channel), std::move(dst)); |
| 398 | } |
| 399 | |
Giorgio Arena | fda4618 | 2017-06-16 13:57:33 +0100 | [diff] [blame] | 400 | TensorShape TensorLibrary::get_image_shape(const std::string &name) |
| 401 | { |
| 402 | return load_image(name).shape(); |
| 403 | } |
| 404 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 405 | RawTensor TensorLibrary::get(const TensorShape &shape, DataType data_type, int num_channels, int fixed_point_position) |
| 406 | { |
| 407 | return RawTensor(shape, data_type, num_channels, fixed_point_position); |
| 408 | } |
| 409 | |
| 410 | RawTensor TensorLibrary::get(const TensorShape &shape, Format format) |
| 411 | { |
| 412 | return RawTensor(shape, format); |
| 413 | } |
| 414 | |
| 415 | const RawTensor &TensorLibrary::get(const std::string &name) const |
| 416 | { |
| 417 | //FIXME: Format should be derived from the image name. Not be fixed to RGB. |
| 418 | return find_or_create_raw_tensor(name, Format::RGB888); |
| 419 | } |
| 420 | |
| 421 | RawTensor TensorLibrary::get(const std::string &name) |
| 422 | { |
| 423 | //FIXME: Format should be derived from the image name. Not be fixed to RGB. |
| 424 | return RawTensor(find_or_create_raw_tensor(name, Format::RGB888)); |
| 425 | } |
| 426 | |
| 427 | RawTensor TensorLibrary::get(const std::string &name, DataType data_type, int num_channels) const |
| 428 | { |
| 429 | const RawTensor &raw = get(name); |
| 430 | |
| 431 | return RawTensor(raw.shape(), data_type, num_channels); |
| 432 | } |
| 433 | |
| 434 | const RawTensor &TensorLibrary::get(const std::string &name, Format format) const |
| 435 | { |
| 436 | return find_or_create_raw_tensor(name, format); |
| 437 | } |
| 438 | |
| 439 | RawTensor TensorLibrary::get(const std::string &name, Format format) |
| 440 | { |
| 441 | return RawTensor(find_or_create_raw_tensor(name, format)); |
| 442 | } |
| 443 | |
| 444 | const RawTensor &TensorLibrary::get(const std::string &name, Channel channel) const |
| 445 | { |
| 446 | return get(name, get_format_for_channel(channel), channel); |
| 447 | } |
| 448 | |
| 449 | RawTensor TensorLibrary::get(const std::string &name, Channel channel) |
| 450 | { |
| 451 | return RawTensor(get(name, get_format_for_channel(channel), channel)); |
| 452 | } |
| 453 | |
| 454 | const RawTensor &TensorLibrary::get(const std::string &name, Format format, Channel channel) const |
| 455 | { |
| 456 | return find_or_create_raw_tensor(name, format, channel); |
| 457 | } |
| 458 | |
| 459 | RawTensor TensorLibrary::get(const std::string &name, Format format, Channel channel) |
| 460 | { |
| 461 | return RawTensor(find_or_create_raw_tensor(name, format, channel)); |
| 462 | } |
| 463 | } // namespace test |
| 464 | } // namespace arm_compute |