X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=documentation%2F_assets_library_8cpp_source.xhtml;h=57ed4a2011049d8891a64d7334122742f70730bc;hb=HEAD;hp=11c3ea6342dff0a513837fe8ff6c33284bba843a;hpb=67c8c91522e5be8156b77f57e63c0253535c902a;p=platform%2Fupstream%2Farmcl.git diff --git a/documentation/_assets_library_8cpp_source.xhtml b/documentation/_assets_library_8cpp_source.xhtml index 11c3ea6..57ed4a2 100644 --- a/documentation/_assets_library_8cpp_source.xhtml +++ b/documentation/_assets_library_8cpp_source.xhtml @@ -40,7 +40,7 @@
Compute Library -  18.03 +  18.05
@@ -117,54 +117,53 @@ $(document).ready(function(){initNavTree('_assets_library_8cpp_source.xhtml','')
AssetsLibrary.cpp
-Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2018 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 "tests/AssetsLibrary.h"
25 
26 #include "Utils.h"
27 #include "utils/TypePrinter.h"
28 
30 
31 #include <cctype>
32 #include <fstream>
33 #include <limits>
34 #include <map>
35 #include <mutex>
36 #include <sstream>
37 #include <stdexcept>
38 #include <tuple>
39 #include <unordered_map>
40 #include <utility>
41 
42 namespace arm_compute
43 {
44 namespace test
45 {
46 namespace
47 {
49 void rgb_to_luminance(const RawTensor &src, RawTensor &dst)
50 {
51  // Ensure in/out tensors have same image dimensions (independent of element size and number of channels)
52  ARM_COMPUTE_ERROR_ON_MSG(src.num_elements() != dst.num_elements(), "Input and output images must have equal dimensions");
53 
54  const size_t num_elements = dst.num_elements();
55 
56  // Currently, input is always RGB888 (3 U8 channels per element). Output can be U8, U16/S16 or U32
57  // Note that src.data()[i] returns pointer to first channel of element[i], so RGB values have [0,1,2] offsets
58  for(size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
59  {
60  reinterpret_cast<T *>(dst.data())[j] = 0.2126f * src.data()[i] + 0.7152f * src.data()[i + 1] + 0.0722f * src.data()[i + 2];
61  }
62 }
63 
64 void extract_r_from_rgb(const RawTensor &src, RawTensor &dst)
65 {
66  ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
67 
68  const size_t num_elements = dst.num_elements();
69 
70  for(size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
71  {
72  dst.data()[j] = src.data()[i];
73  }
74 }
75 
76 void extract_g_from_rgb(const RawTensor &src, RawTensor &dst)
77 {
78  ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
79 
80  const size_t num_elements = dst.num_elements();
81 
82  for(size_t i = 1, j = 0; j < num_elements; i += 3, ++j)
83  {
84  dst.data()[j] = src.data()[i];
85  }
86 }
87 
88 void extract_b_from_rgb(const RawTensor &src, RawTensor &dst)
89 {
90  ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
91 
92  const size_t num_elements = dst.num_elements();
93 
94  for(size_t i = 2, j = 0; j < num_elements; i += 3, ++j)
95  {
96  dst.data()[j] = src.data()[i];
97  }
98 }
99 
100 void discard_comments(std::ifstream &fs)
101 {
102  while(fs.peek() == '#')
103  {
105  }
106 }
107 
108 void discard_comments_and_spaces(std::ifstream &fs)
109 {
110  while(true)
111  {
112  discard_comments(fs);
113 
114  if(isspace(fs.peek()) == 0)
115  {
116  break;
117  }
118 
119  fs.ignore(1);
120  }
121 }
122 
123 std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs)
124 {
125  // Check the PPM magic number is valid
126  std::array<char, 2> magic_number{ { 0 } };
127  fs >> magic_number[0] >> magic_number[1];
128 
129  if(magic_number[0] != 'P' || magic_number[1] != '6')
130  {
131  throw std::runtime_error("Only raw PPM format is suported");
132  }
133 
134  discard_comments_and_spaces(fs);
135 
136  unsigned int width = 0;
137  fs >> width;
138 
139  discard_comments_and_spaces(fs);
140 
141  unsigned int height = 0;
142  fs >> height;
143 
144  discard_comments_and_spaces(fs);
145 
146  int max_value = 0;
147  fs >> max_value;
148 
149  if(!fs.good())
150  {
151  throw std::runtime_error("Cannot read image dimensions");
152  }
153 
154  if(max_value != 255)
155  {
156  throw std::runtime_error("RawTensor doesn't have 8-bit values");
157  }
158 
159  discard_comments(fs);
160 
161  if(isspace(fs.peek()) == 0)
162  {
163  throw std::runtime_error("Invalid PPM header");
164  }
165 
166  fs.ignore(1);
167 
168  return std::make_tuple(width, height, max_value);
169 }
170 
171 RawTensor load_ppm(const std::string &path)
172 {
173  std::ifstream file(path, std::ios::in | std::ios::binary);
174 
175  if(!file.good())
176  {
177  throw framework::FileNotFound("Could not load PPM image: " + path);
178  }
179 
180  unsigned int width = 0;
181  unsigned int height = 0;
182 
183  std::tie(width, height, std::ignore) = parse_ppm_header(file);
184 
185  RawTensor raw(TensorShape(width, height), Format::RGB888);
186 
187  // Check if the file is large enough to fill the image
188  const size_t current_position = file.tellg();
189  file.seekg(0, std::ios_base::end);
190  const size_t end_position = file.tellg();
191  file.seekg(current_position, std::ios_base::beg);
192 
193  if((end_position - current_position) < raw.size())
194  {
195  throw std::runtime_error("Not enough data in file");
196  }
197 
198  file.read(reinterpret_cast<std::fstream::char_type *>(raw.data()), raw.size());
199 
200  if(!file.good())
201  {
202  throw std::runtime_error("Failure while reading image buffer");
203  }
204 
205  return raw;
206 }
207 } // namespace
208 
209 AssetsLibrary::AssetsLibrary(std::string path, std::random_device::result_type seed) //NOLINT
210  : _library_path(std::move(path)),
211  _seed{ seed }
212 {
213 }
214 
215 std::string AssetsLibrary::path() const
216 {
217  return _library_path;
218 }
219 
220 std::random_device::result_type AssetsLibrary::seed() const
221 {
222  return _seed;
223 }
224 
225 void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format) const
226 {
227  const RawTensor &src = get(name, format);
228  std::copy_n(src.data(), raw.size(), raw.data());
229 }
230 
231 void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Channel channel) const
232 {
233  fill(raw, name, get_format_for_channel(channel), channel);
234 }
235 
236 void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const
237 {
238  const RawTensor &src = get(name, format, channel);
239  std::copy_n(src.data(), raw.size(), raw.data());
240 }
241 
242 const AssetsLibrary::Loader &AssetsLibrary::get_loader(const std::string &extension) const
243 {
244  static std::unordered_map<std::string, Loader> loaders =
245  {
246  { "ppm", load_ppm }
247  };
248 
249  const auto it = loaders.find(extension);
250 
251  if(it != loaders.end())
252  {
253  return it->second;
254  }
255  else
256  {
257  throw std::invalid_argument("Cannot load image with extension '" + extension + "'");
258  }
259 }
260 
261 const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, Format dst) const
262 {
263  static std::map<std::pair<Format, Format>, Converter> converters =
264  {
265  { std::make_pair(Format::RGB888, Format::U8), rgb_to_luminance<uint8_t> },
266  { std::make_pair(Format::RGB888, Format::U16), rgb_to_luminance<uint16_t> },
267  { std::make_pair(Format::RGB888, Format::S16), rgb_to_luminance<int16_t> },
268  { std::make_pair(Format::RGB888, Format::U32), rgb_to_luminance<uint32_t> }
269  };
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 format '" << src << "' to format '" << dst << "'\n";
281  throw std::invalid_argument(msg.str());
282  }
283 }
284 
285 const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, Format dst) const
286 {
287  static std::map<std::pair<DataType, Format>, 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 format '" << dst << "'\n";
299  throw std::invalid_argument(msg.str());
300  }
301 }
302 
303 const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, DataType dst) const
304 {
305  static std::map<std::pair<DataType, 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 data type '" << src << "' to data type '" << dst << "'\n";
317  throw std::invalid_argument(msg.str());
318  }
319 }
320 
321 const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, DataType dst) const
322 {
323  static std::map<std::pair<Format, DataType>, Converter> converters = {};
324 
325  const auto it = converters.find(std::make_pair(src, dst));
326 
327  if(it != converters.end())
328  {
329  return it->second;
330  }
331  else
332  {
333  std::stringstream msg;
334  msg << "Cannot convert from format '" << src << "' to data type '" << dst << "'\n";
335  throw std::invalid_argument(msg.str());
336  }
337 }
338 
339 const AssetsLibrary::Extractor &AssetsLibrary::get_extractor(Format format, Channel channel) const
340 {
341  static std::map<std::pair<Format, Channel>, Extractor> extractors =
342  {
343  { std::make_pair(Format::RGB888, Channel::R), extract_r_from_rgb },
344  { std::make_pair(Format::RGB888, Channel::G), extract_g_from_rgb },
345  { std::make_pair(Format::RGB888, Channel::B), extract_b_from_rgb }
346  };
347 
348  const auto it = extractors.find(std::make_pair(format, channel));
349 
350  if(it != extractors.end())
351  {
352  return it->second;
353  }
354  else
355  {
356  std::stringstream msg;
357  msg << "Cannot extract channel '" << channel << "' from format '" << format << "'\n";
358  throw std::invalid_argument(msg.str());
359  }
360 }
361 
362 RawTensor AssetsLibrary::load_image(const std::string &name) const
363 {
364 #ifdef _WIN32
365  const std::string image_path = ("\\images\\");
366 #else /* _WIN32 */
367  const std::string image_path = ("/images/");
368 #endif /* _WIN32 */
369 
370  const std::string path = _library_path + image_path + name;
371  const std::string extension = path.substr(path.find_last_of('.') + 1);
372  return (*get_loader(extension))(path);
373 }
374 
375 const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format) const
376 {
377  std::lock_guard<std::mutex> guard(_format_lock);
378 
379  const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format));
380 
381  if(ptr != nullptr)
382  {
383  return *ptr;
384  }
385 
386  RawTensor raw = load_image(name);
387 
388  if(raw.format() != format)
389  {
390  RawTensor dst(raw.shape(), format);
391  (*get_converter(raw.format(), format))(raw, dst);
392  raw = std::move(dst);
393  }
394 
395  return _cache.add(std::forward_as_tuple(name, format), std::move(raw));
396 }
397 
398 const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const
399 {
400  std::lock_guard<std::mutex> guard(_channel_lock);
401 
402  const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format, channel));
403 
404  if(ptr != nullptr)
405  {
406  return *ptr;
407  }
408 
409  const RawTensor &src = get(name, format);
410  RawTensor dst(src.shape(), get_channel_format(channel));
411 
412  (*get_extractor(format, channel))(src, dst);
413 
414  return _cache.add(std::forward_as_tuple(name, format, channel), std::move(dst));
415 }
416 
418 {
419  return load_image(name).shape();
420 }
421 
422 const RawTensor &AssetsLibrary::get(const std::string &name) const
423 {
424  return find_or_create_raw_tensor(name, Format::RGB888);
425 }
426 
427 RawTensor AssetsLibrary::get(const std::string &name)
428 {
429  return RawTensor(find_or_create_raw_tensor(name, Format::RGB888));
430 }
431 
432 RawTensor AssetsLibrary::get(const std::string &name, DataType data_type, int num_channels) const
433 {
434  const RawTensor &raw = get(name);
435 
436  return RawTensor(raw.shape(), data_type, num_channels);
437 }
438 
439 const RawTensor &AssetsLibrary::get(const std::string &name, Format format) const
440 {
441  return find_or_create_raw_tensor(name, format);
442 }
443 
444 RawTensor AssetsLibrary::get(const std::string &name, Format format)
445 {
446  return RawTensor(find_or_create_raw_tensor(name, format));
447 }
448 
449 const RawTensor &AssetsLibrary::get(const std::string &name, Channel channel) const
450 {
451  return get(name, get_format_for_channel(channel), channel);
452 }
453 
454 RawTensor AssetsLibrary::get(const std::string &name, Channel channel)
455 {
456  return RawTensor(get(name, get_format_for_channel(channel), channel));
457 }
458 
459 const RawTensor &AssetsLibrary::get(const std::string &name, Format format, Channel channel) const
460 {
461  return find_or_create_raw_tensor(name, format, channel);
462 }
463 
464 RawTensor AssetsLibrary::get(const std::string &name, Format format, Channel channel)
465 {
466  return RawTensor(find_or_create_raw_tensor(name, format, channel));
467 }
468 } // namespace test
469 } // namespace arm_compute
Subclass of SimpleTensor using uint8_t as value type.
Definition: RawTensor.h:38
+Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2018 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 "tests/AssetsLibrary.h"
25 
26 #include "Utils.h"
27 #include "utils/TypePrinter.h"
28 
30 
31 #include <cctype>
32 #include <fstream>
33 #include <limits>
34 #include <map>
35 #include <mutex>
36 #include <sstream>
37 #include <stdexcept>
38 #include <tuple>
39 #include <unordered_map>
40 #include <utility>
41 
42 namespace arm_compute
43 {
44 namespace test
45 {
46 namespace
47 {
48 template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
49 void rgb_to_luminance(const RawTensor &src, RawTensor &dst)
50 {
51  // Ensure in/out tensors have same image dimensions (independent of element size and number of channels)
52  ARM_COMPUTE_ERROR_ON_MSG(src.num_elements() != dst.num_elements(), "Input and output images must have equal dimensions");
53 
54  const size_t num_elements = dst.num_elements();
55 
56  // Currently, input is always RGB888 (3 U8 channels per element). Output can be U8, U16/S16 or U32
57  // Note that src.data()[i] returns pointer to first channel of element[i], so RGB values have [0,1,2] offsets
58  for(size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
59  {
60  reinterpret_cast<T *>(dst.data())[j] = 0.2126f * src.data()[i] + 0.7152f * src.data()[i + 1] + 0.0722f * src.data()[i + 2];
61  }
62 }
63 
64 void extract_r_from_rgb(const RawTensor &src, RawTensor &dst)
65 {
66  ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
67 
68  const size_t num_elements = dst.num_elements();
69 
70  for(size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
71  {
72  dst.data()[j] = src.data()[i];
73  }
74 }
75 
76 void extract_g_from_rgb(const RawTensor &src, RawTensor &dst)
77 {
78  ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
79 
80  const size_t num_elements = dst.num_elements();
81 
82  for(size_t i = 1, j = 0; j < num_elements; i += 3, ++j)
83  {
84  dst.data()[j] = src.data()[i];
85  }
86 }
87 
88 void extract_b_from_rgb(const RawTensor &src, RawTensor &dst)
89 {
90  ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
91 
92  const size_t num_elements = dst.num_elements();
93 
94  for(size_t i = 2, j = 0; j < num_elements; i += 3, ++j)
95  {
96  dst.data()[j] = src.data()[i];
97  }
98 }
99 
100 void discard_comments(std::ifstream &fs)
101 {
102  while(fs.peek() == '#')
103  {
105  }
106 }
107 
108 void discard_comments_and_spaces(std::ifstream &fs)
109 {
110  while(true)
111  {
112  discard_comments(fs);
113 
114  if(isspace(fs.peek()) == 0)
115  {
116  break;
117  }
118 
119  fs.ignore(1);
120  }
121 }
122 
123 std::tuple<unsigned int, unsigned int, int> parse_netpbm_format_header(std::ifstream &fs, char number)
124 {
125  // check file type magic number is valid
126  std::array<char, 2> magic_number{ { 0 } };
127  fs >> magic_number[0] >> magic_number[1];
128 
129  if(magic_number[0] != 'P' || magic_number[1] != number)
130  {
131  throw std::runtime_error("File type magic number not supported");
132  }
133 
134  discard_comments_and_spaces(fs);
135 
136  unsigned int width = 0;
137  fs >> width;
138 
139  discard_comments_and_spaces(fs);
140 
141  unsigned int height = 0;
142  fs >> height;
143 
144  discard_comments_and_spaces(fs);
145 
146  int max_value = 0;
147  fs >> max_value;
148 
149  if(!fs.good())
150  {
151  throw std::runtime_error("Cannot read image dimensions");
152  }
153 
154  if(max_value != 255)
155  {
156  throw std::runtime_error("RawTensor doesn't have 8-bit values");
157  }
158 
159  discard_comments(fs);
160 
161  if(isspace(fs.peek()) == 0)
162  {
163  throw std::runtime_error("Invalid image header");
164  }
165 
166  fs.ignore(1);
167 
168  return std::make_tuple(width, height, max_value);
169 }
170 
171 std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs)
172 {
173  return parse_netpbm_format_header(fs, '6');
174 }
175 
176 std::tuple<unsigned int, unsigned int, int> parse_pgm_header(std::ifstream &fs)
177 {
178  return parse_netpbm_format_header(fs, '5');
179 }
180 
181 void check_image_size(std::ifstream &fs, size_t raw_size)
182 {
183  const size_t current_position = fs.tellg();
184  fs.seekg(0, std::ios_base::end);
185  const size_t end_position = fs.tellg();
186  fs.seekg(current_position, std::ios_base::beg);
187 
188  if((end_position - current_position) < raw_size)
189  {
190  throw std::runtime_error("Not enough data in file");
191  }
192 }
193 
194 void read_image_buffer(std::ifstream &fs, RawTensor &raw)
195 {
196  fs.read(reinterpret_cast<std::fstream::char_type *>(raw.data()), raw.size());
197 
198  if(!fs.good())
199  {
200  throw std::runtime_error("Failure while reading image buffer");
201  }
202 }
203 
204 RawTensor load_ppm(const std::string &path)
205 {
206  std::ifstream file(path, std::ios::in | std::ios::binary);
207 
208  if(!file.good())
209  {
210  throw framework::FileNotFound("Could not load PPM image: " + path);
211  }
212 
213  unsigned int width = 0;
214  unsigned int height = 0;
215 
216  std::tie(width, height, std::ignore) = parse_ppm_header(file);
217 
218  RawTensor raw(TensorShape(width, height), Format::RGB888);
219 
220  check_image_size(file, raw.size());
221  read_image_buffer(file, raw);
222 
223  return raw;
224 }
225 
226 RawTensor load_pgm(const std::string &path)
227 {
228  std::ifstream file(path, std::ios::in | std::ios::binary);
229 
230  if(!file.good())
231  {
232  throw framework::FileNotFound("Could not load PGM image: " + path);
233  }
234 
235  unsigned int width = 0;
236  unsigned int height = 0;
237 
238  std::tie(width, height, std::ignore) = parse_pgm_header(file);
239 
240  RawTensor raw(TensorShape(width, height), Format::U8);
241 
242  check_image_size(file, raw.size());
243  read_image_buffer(file, raw);
244 
245  return raw;
246 }
247 } // namespace
248 
249 AssetsLibrary::AssetsLibrary(std::string path, std::random_device::result_type seed) //NOLINT
250  : _library_path(std::move(path)),
251  _seed{ seed }
252 {
253 }
254 
255 std::string AssetsLibrary::path() const
256 {
257  return _library_path;
258 }
259 
260 std::random_device::result_type AssetsLibrary::seed() const
261 {
262  return _seed;
263 }
264 
265 void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format) const
266 {
267  const RawTensor &src = get(name, format);
268  std::copy_n(src.data(), raw.size(), raw.data());
269 }
270 
271 void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Channel channel) const
272 {
273  fill(raw, name, get_format_for_channel(channel), channel);
274 }
275 
276 void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const
277 {
278  const RawTensor &src = get(name, format, channel);
279  std::copy_n(src.data(), raw.size(), raw.data());
280 }
281 
282 const AssetsLibrary::Loader &AssetsLibrary::get_loader(const std::string &extension) const
283 {
284  static std::unordered_map<std::string, Loader> loaders =
285  {
286  { "ppm", load_ppm },
287  { "pgm", load_pgm }
288  };
289 
290  const auto it = loaders.find(extension);
291 
292  if(it != loaders.end())
293  {
294  return it->second;
295  }
296  else
297  {
298  throw std::invalid_argument("Cannot load image with extension '" + extension + "'");
299  }
300 }
301 
302 const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, Format dst) const
303 {
304  static std::map<std::pair<Format, Format>, Converter> converters =
305  {
306  { std::make_pair(Format::RGB888, Format::U8), rgb_to_luminance<uint8_t> },
307  { std::make_pair(Format::RGB888, Format::U16), rgb_to_luminance<uint16_t> },
308  { std::make_pair(Format::RGB888, Format::S16), rgb_to_luminance<int16_t> },
309  { std::make_pair(Format::RGB888, Format::U32), rgb_to_luminance<uint32_t> }
310  };
311 
312  const auto it = converters.find(std::make_pair(src, dst));
313 
314  if(it != converters.end())
315  {
316  return it->second;
317  }
318  else
319  {
320  std::stringstream msg;
321  msg << "Cannot convert from format '" << src << "' to format '" << dst << "'\n";
322  throw std::invalid_argument(msg.str());
323  }
324 }
325 
326 const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, Format dst) const
327 {
328  static std::map<std::pair<DataType, Format>, Converter> converters = {};
329 
330  const auto it = converters.find(std::make_pair(src, dst));
331 
332  if(it != converters.end())
333  {
334  return it->second;
335  }
336  else
337  {
338  std::stringstream msg;
339  msg << "Cannot convert from data type '" << src << "' to format '" << dst << "'\n";
340  throw std::invalid_argument(msg.str());
341  }
342 }
343 
344 const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, DataType dst) const
345 {
346  static std::map<std::pair<DataType, DataType>, Converter> converters = {};
347 
348  const auto it = converters.find(std::make_pair(src, dst));
349 
350  if(it != converters.end())
351  {
352  return it->second;
353  }
354  else
355  {
356  std::stringstream msg;
357  msg << "Cannot convert from data type '" << src << "' to data type '" << dst << "'\n";
358  throw std::invalid_argument(msg.str());
359  }
360 }
361 
362 const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, DataType dst) const
363 {
364  static std::map<std::pair<Format, DataType>, Converter> converters = {};
365 
366  const auto it = converters.find(std::make_pair(src, dst));
367 
368  if(it != converters.end())
369  {
370  return it->second;
371  }
372  else
373  {
374  std::stringstream msg;
375  msg << "Cannot convert from format '" << src << "' to data type '" << dst << "'\n";
376  throw std::invalid_argument(msg.str());
377  }
378 }
379 
380 const AssetsLibrary::Extractor &AssetsLibrary::get_extractor(Format format, Channel channel) const
381 {
382  static std::map<std::pair<Format, Channel>, Extractor> extractors =
383  {
384  { std::make_pair(Format::RGB888, Channel::R), extract_r_from_rgb },
385  { std::make_pair(Format::RGB888, Channel::G), extract_g_from_rgb },
386  { std::make_pair(Format::RGB888, Channel::B), extract_b_from_rgb }
387  };
388 
389  const auto it = extractors.find(std::make_pair(format, channel));
390 
391  if(it != extractors.end())
392  {
393  return it->second;
394  }
395  else
396  {
397  std::stringstream msg;
398  msg << "Cannot extract channel '" << channel << "' from format '" << format << "'\n";
399  throw std::invalid_argument(msg.str());
400  }
401 }
402 
403 RawTensor AssetsLibrary::load_image(const std::string &name) const
404 {
405 #ifdef _WIN32
406  const std::string image_path = ("\\images\\");
407 #else /* _WIN32 */
408  const std::string image_path = ("/images/");
409 #endif /* _WIN32 */
410 
411  const std::string path = _library_path + image_path + name;
412  const std::string extension = path.substr(path.find_last_of('.') + 1);
413  return (*get_loader(extension))(path);
414 }
415 
416 const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format) const
417 {
418  std::lock_guard<std::mutex> guard(_format_lock);
419 
420  const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format));
421 
422  if(ptr != nullptr)
423  {
424  return *ptr;
425  }
426 
427  RawTensor raw = load_image(name);
428 
429  if(raw.format() != format)
430  {
431  RawTensor dst(raw.shape(), format);
432  (*get_converter(raw.format(), format))(raw, dst);
433  raw = std::move(dst);
434  }
435 
436  return _cache.add(std::forward_as_tuple(name, format), std::move(raw));
437 }
438 
439 const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const
440 {
441  std::lock_guard<std::mutex> guard(_channel_lock);
442 
443  const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format, channel));
444 
445  if(ptr != nullptr)
446  {
447  return *ptr;
448  }
449 
450  const RawTensor &src = get(name, format);
451  RawTensor dst(src.shape(), get_channel_format(channel));
452 
453  (*get_extractor(format, channel))(src, dst);
454 
455  return _cache.add(std::forward_as_tuple(name, format, channel), std::move(dst));
456 }
457 
459 {
460  return load_image(name).shape();
461 }
462 
463 const RawTensor &AssetsLibrary::get(const std::string &name) const
464 {
465  return find_or_create_raw_tensor(name, Format::RGB888);
466 }
467 
468 RawTensor AssetsLibrary::get(const std::string &name)
469 {
470  return RawTensor(find_or_create_raw_tensor(name, Format::RGB888));
471 }
472 
473 RawTensor AssetsLibrary::get(const std::string &name, DataType data_type, int num_channels) const
474 {
475  const RawTensor &raw = get(name);
476 
477  return RawTensor(raw.shape(), data_type, num_channels);
478 }
479 
480 const RawTensor &AssetsLibrary::get(const std::string &name, Format format) const
481 {
482  return find_or_create_raw_tensor(name, format);
483 }
484 
485 RawTensor AssetsLibrary::get(const std::string &name, Format format)
486 {
487  return RawTensor(find_or_create_raw_tensor(name, format));
488 }
489 
490 const RawTensor &AssetsLibrary::get(const std::string &name, Channel channel) const
491 {
492  return get(name, get_format_for_channel(channel), channel);
493 }
494 
495 RawTensor AssetsLibrary::get(const std::string &name, Channel channel)
496 {
497  return RawTensor(get(name, get_format_for_channel(channel), channel));
498 }
499 
500 const RawTensor &AssetsLibrary::get(const std::string &name, Format format, Channel channel) const
501 {
502  return find_or_create_raw_tensor(name, format, channel);
503 }
504 
505 RawTensor AssetsLibrary::get(const std::string &name, Format format, Channel channel)
506 {
507  return RawTensor(find_or_create_raw_tensor(name, format, channel));
508 }
509 } // namespace test
510 } // namespace arm_compute
Subclass of SimpleTensor using uint8_t as value type.
Definition: RawTensor.h:38
Shape of a tensor.
Definition: TensorShape.h:39
-
void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Fills the specified tensor with random values drawn from distribution.
-
size_t size() const override
Total size of the tensor in bytes.
Definition: SimpleTensor.h:269
+
void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Fills the specified tensor with random values drawn from distribution.
+
size_t size() const override
Total size of the tensor in bytes.
Definition: SimpleTensor.h:318
-
Format get_format_for_channel(Channel channel)
Look up the format corresponding to a channel.
Definition: Utils.h:138
+
Format get_format_for_channel(Channel channel)
Look up the format corresponding to a channel.
Definition: Utils.h:148
-
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:306
1 channel, 1 U8 per channel
-
Format format() const override
Image format of the tensor.
Definition: SimpleTensor.h:276
+
void fill(RawTensor &raw, const std::string &name, Channel channel) const
Fills the raw tensor with the content of the specified channel extracted from the given image...
+
Format format() const override
Image format of the tensor.
Definition: SimpleTensor.h:325
+
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:328
1 channel, 1 U16 per channel
-
std::random_device::result_type seed() const
Seed that is used to fill tensors with random values.
-
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:245
+
std::random_device::result_type seed() const
Seed that is used to fill tensors with random values.
+
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:294
-
TensorShape get_image_shape(const std::string &name)
Provides a tensor shape for the specified image.
+
TensorShape get_image_shape(const std::string &name)
Provides a tensor shape for the specified image.
This file contains all available output stages for GEMMLowp on OpenCL.
-
std::string path() const
Path to assets directory used to initialise library.
+
std::string path() const
Path to assets directory used to initialise library.
3 channels, 1 U8 per channel
-
Format get_channel_format(Channel channel)
Return the format of a channel.
Definition: Utils.h:157
-
const T * data() const
Constant pointer to the underlying buffer.
Definition: SimpleTensor.h:343
+
Format get_channel_format(Channel channel)
Return the format of a channel.
Definition: Utils.h:167
+
const T * data() const
Constant pointer to the underlying buffer.
Definition: SimpleTensor.h:398
1 channel, 1 U32 per channel
- -
Channel
Available channels.
Definition: Types.h:401
+
Channel
Available channels.
Definition: Types.h:481
Format
Image colour formats.
Definition: Types.h:50
+ -
const RawTensor & get(const std::string &name) const
Provides a contant raw tensor for the specified image.
+
const RawTensor & get(const std::string &name) const
Provides a constant raw tensor for the specified image.
1 channel, 1 S16 per channel
-
void * value
Definition: hwc.hpp:278
RawTensor & add(std::tuple< const std::string &, Format > key, RawTensor raw)
Add the given tensor to the cache.
Definition: TensorCache.h:105
-
RawTensor * find(std::tuple< const std::string &, Format > key)
Definition: TensorCache.h:93
-
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:297
- +
RawTensor * find(std::tuple< const std::string &, Format > key)
Search the cache for a tensor of created from the specified image and format.
Definition: TensorCache.h:93
std::tuple< unsigned int, unsigned int, int > parse_ppm_header(std::ifstream &fs)
Parse the ppm header from an input file stream.
Definition: Utils.cpp:144
-
fixed_point< T > max(fixed_point< T > x, fixed_point< T > y)
Definition: FixedPoint.h:889
+
fixed_point< T > max(fixed_point< T > x, fixed_point< T > y)
Definition: FixedPoint.h:902
DataType
Available data types.
Definition: Types.h:72
-
AssetsLibrary(std::string path, std::random_device::result_type seed)
Initialises the library with a path to the assets directory.
+
AssetsLibrary(std::string path, std::random_device::result_type seed)
Initialises the library with a path to the assets directory.
convolution configure & src
+
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:319