blob: ee876f91e3a11fe0f769845085e8ff4a286fdfc3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Abe Mbise562fe0f2018-02-09 14:13:02 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010024#include "tests/AssetsLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "Utils.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010027#include "utils/TypePrinter.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028
29#include "arm_compute/core/ITensor.h"
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
42namespace arm_compute
43{
44namespace test
45{
46namespace
47{
Giorgio Arenafda46182017-06-16 13:57:33 +010048template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
49void rgb_to_luminance(const RawTensor &src, RawTensor &dst)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050{
Abe Mbise562fe0f2018-02-09 14:13:02 +000051 // 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");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053
Abe Mbise562fe0f2018-02-09 14:13:02 +000054 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)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 {
Abe Mbise562fe0f2018-02-09 14:13:02 +000060 reinterpret_cast<T *>(dst.data())[j] = 0.2126f * src.data()[i] + 0.7152f * src.data()[i + 1] + 0.0722f * src.data()[i + 2];
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 }
62}
63
64void extract_r_from_rgb(const RawTensor &src, RawTensor &dst)
65{
Abe Mbise562fe0f2018-02-09 14:13:02 +000066 ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067
Abe Mbise562fe0f2018-02-09 14:13:02 +000068 const size_t num_elements = dst.num_elements();
69
70 for(size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 {
72 dst.data()[j] = src.data()[i];
73 }
74}
75
76void extract_g_from_rgb(const RawTensor &src, RawTensor &dst)
77{
Abe Mbise562fe0f2018-02-09 14:13:02 +000078 ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079
Abe Mbise562fe0f2018-02-09 14:13:02 +000080 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
88void 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)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 {
96 dst.data()[j] = src.data()[i];
97 }
98}
99
100void discard_comments(std::ifstream &fs)
101{
102 while(fs.peek() == '#')
103 {
104 fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
105 }
106}
107
108void 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
John Richardson8de92612018-02-22 14:09:31 +0000123std::tuple<unsigned int, unsigned int, int> parse_netpbm_format_header(std::ifstream &fs, char number)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124{
John Richardson8de92612018-02-22 14:09:31 +0000125 // check file type magic number is valid
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 std::array<char, 2> magic_number{ { 0 } };
127 fs >> magic_number[0] >> magic_number[1];
128
John Richardson8de92612018-02-22 14:09:31 +0000129 if(magic_number[0] != 'P' || magic_number[1] != number)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 {
John Richardson8de92612018-02-22 14:09:31 +0000131 throw std::runtime_error("File type magic number not supported");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 }
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 {
John Richardson8de92612018-02-22 14:09:31 +0000163 throw std::runtime_error("Invalid image header");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 }
165
166 fs.ignore(1);
167
168 return std::make_tuple(width, height, max_value);
169}
170
John Richardson8de92612018-02-22 14:09:31 +0000171std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs)
172{
173 return parse_netpbm_format_header(fs, '6');
174}
175
176std::tuple<unsigned int, unsigned int, int> parse_pgm_header(std::ifstream &fs)
177{
178 return parse_netpbm_format_header(fs, '5');
179}
180
181void 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
194void 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
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204RawTensor load_ppm(const std::string &path)
205{
206 std::ifstream file(path, std::ios::in | std::ios::binary);
207
208 if(!file.good())
209 {
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100210 throw framework::FileNotFound("Could not load PPM image: " + path);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 }
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
John Richardson8de92612018-02-22 14:09:31 +0000220 check_image_size(file, raw.size());
221 read_image_buffer(file, raw);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222
John Richardson8de92612018-02-22 14:09:31 +0000223 return raw;
224}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225
John Richardson8de92612018-02-22 14:09:31 +0000226RawTensor load_pgm(const std::string &path)
227{
228 std::ifstream file(path, std::ios::in | std::ios::binary);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229
230 if(!file.good())
231 {
John Richardson8de92612018-02-22 14:09:31 +0000232 throw framework::FileNotFound("Could not load PGM image: " + path);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233 }
234
John Richardson8de92612018-02-22 14:09:31 +0000235 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
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100245 return raw;
246}
247} // namespace
248
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100249AssetsLibrary::AssetsLibrary(std::string path, std::random_device::result_type seed) //NOLINT
Anthony Barbierac69aa12017-07-03 17:39:37 +0100250 : _library_path(std::move(path)),
251 _seed{ seed }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252{
253}
254
John Richardson70f946b2017-10-02 16:52:16 +0100255std::string AssetsLibrary::path() const
256{
257 return _library_path;
258}
259
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100260std::random_device::result_type AssetsLibrary::seed() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261{
262 return _seed;
263}
264
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100265void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100266{
267 //FIXME: Should be done by swapping cached buffers
268 const RawTensor &src = get(name, format);
269 std::copy_n(src.data(), raw.size(), raw.data());
270}
271
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100272void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273{
274 fill(raw, name, get_format_for_channel(channel), channel);
275}
276
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100277void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100278{
279 const RawTensor &src = get(name, format, channel);
280 std::copy_n(src.data(), raw.size(), raw.data());
281}
282
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100283const AssetsLibrary::Loader &AssetsLibrary::get_loader(const std::string &extension) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284{
285 static std::unordered_map<std::string, Loader> loaders =
286 {
John Richardson8de92612018-02-22 14:09:31 +0000287 { "ppm", load_ppm },
288 { "pgm", load_pgm }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289 };
290
291 const auto it = loaders.find(extension);
292
293 if(it != loaders.end())
294 {
295 return it->second;
296 }
297 else
298 {
299 throw std::invalid_argument("Cannot load image with extension '" + extension + "'");
300 }
301}
302
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100303const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, Format dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100304{
305 static std::map<std::pair<Format, Format>, Converter> converters =
306 {
Giorgio Arenafda46182017-06-16 13:57:33 +0100307 { std::make_pair(Format::RGB888, Format::U8), rgb_to_luminance<uint8_t> },
308 { std::make_pair(Format::RGB888, Format::U16), rgb_to_luminance<uint16_t> },
309 { std::make_pair(Format::RGB888, Format::S16), rgb_to_luminance<int16_t> },
310 { std::make_pair(Format::RGB888, Format::U32), rgb_to_luminance<uint32_t> }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311 };
312
313 const auto it = converters.find(std::make_pair(src, dst));
314
315 if(it != converters.end())
316 {
317 return it->second;
318 }
319 else
320 {
321 std::stringstream msg;
322 msg << "Cannot convert from format '" << src << "' to format '" << dst << "'\n";
323 throw std::invalid_argument(msg.str());
324 }
325}
326
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100327const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, Format dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100328{
329 static std::map<std::pair<DataType, Format>, Converter> converters = {};
330
331 const auto it = converters.find(std::make_pair(src, dst));
332
333 if(it != converters.end())
334 {
335 return it->second;
336 }
337 else
338 {
339 std::stringstream msg;
340 msg << "Cannot convert from data type '" << src << "' to format '" << dst << "'\n";
341 throw std::invalid_argument(msg.str());
342 }
343}
344
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100345const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, DataType dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100346{
347 static std::map<std::pair<DataType, DataType>, Converter> converters = {};
348
349 const auto it = converters.find(std::make_pair(src, dst));
350
351 if(it != converters.end())
352 {
353 return it->second;
354 }
355 else
356 {
357 std::stringstream msg;
358 msg << "Cannot convert from data type '" << src << "' to data type '" << dst << "'\n";
359 throw std::invalid_argument(msg.str());
360 }
361}
362
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100363const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, DataType dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100364{
365 static std::map<std::pair<Format, DataType>, Converter> converters = {};
366
367 const auto it = converters.find(std::make_pair(src, dst));
368
369 if(it != converters.end())
370 {
371 return it->second;
372 }
373 else
374 {
375 std::stringstream msg;
376 msg << "Cannot convert from format '" << src << "' to data type '" << dst << "'\n";
377 throw std::invalid_argument(msg.str());
378 }
379}
380
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100381const AssetsLibrary::Extractor &AssetsLibrary::get_extractor(Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100382{
383 static std::map<std::pair<Format, Channel>, Extractor> extractors =
384 {
385 { std::make_pair(Format::RGB888, Channel::R), extract_r_from_rgb },
Abe Mbise562fe0f2018-02-09 14:13:02 +0000386 { std::make_pair(Format::RGB888, Channel::G), extract_g_from_rgb },
387 { std::make_pair(Format::RGB888, Channel::B), extract_b_from_rgb }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388 };
389
390 const auto it = extractors.find(std::make_pair(format, channel));
391
392 if(it != extractors.end())
393 {
394 return it->second;
395 }
396 else
397 {
398 std::stringstream msg;
399 msg << "Cannot extract channel '" << channel << "' from format '" << format << "'\n";
400 throw std::invalid_argument(msg.str());
401 }
402}
403
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100404RawTensor AssetsLibrary::load_image(const std::string &name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100405{
406#ifdef _WIN32
407 const std::string image_path = ("\\images\\");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100408#else /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100409 const std::string image_path = ("/images/");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100410#endif /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411
412 const std::string path = _library_path + image_path + name;
413 const std::string extension = path.substr(path.find_last_of('.') + 1);
414 return (*get_loader(extension))(path);
415}
416
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100417const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418{
419 std::lock_guard<std::mutex> guard(_format_lock);
420
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100421 const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100422
423 if(ptr != nullptr)
424 {
425 return *ptr;
426 }
427
428 RawTensor raw = load_image(name);
429
430 if(raw.format() != format)
431 {
432 //FIXME: Remove unnecessary copy
433 RawTensor dst(raw.shape(), format);
434 (*get_converter(raw.format(), format))(raw, dst);
435 raw = std::move(dst);
436 }
437
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100438 return _cache.add(std::forward_as_tuple(name, format), std::move(raw));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439}
440
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100441const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100442{
443 std::lock_guard<std::mutex> guard(_channel_lock);
444
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100445 const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format, channel));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100446
447 if(ptr != nullptr)
448 {
449 return *ptr;
450 }
451
452 const RawTensor &src = get(name, format);
453 //FIXME: Need to change shape to match channel
454 RawTensor dst(src.shape(), get_channel_format(channel));
455
456 (*get_extractor(format, channel))(src, dst);
457
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100458 return _cache.add(std::forward_as_tuple(name, format, channel), std::move(dst));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100459}
460
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100461TensorShape AssetsLibrary::get_image_shape(const std::string &name)
Giorgio Arenafda46182017-06-16 13:57:33 +0100462{
463 return load_image(name).shape();
464}
465
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100466const RawTensor &AssetsLibrary::get(const std::string &name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100467{
468 //FIXME: Format should be derived from the image name. Not be fixed to RGB.
469 return find_or_create_raw_tensor(name, Format::RGB888);
470}
471
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100472RawTensor AssetsLibrary::get(const std::string &name)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100473{
474 //FIXME: Format should be derived from the image name. Not be fixed to RGB.
475 return RawTensor(find_or_create_raw_tensor(name, Format::RGB888));
476}
477
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100478RawTensor AssetsLibrary::get(const std::string &name, DataType data_type, int num_channels) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100479{
480 const RawTensor &raw = get(name);
481
482 return RawTensor(raw.shape(), data_type, num_channels);
483}
484
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100485const RawTensor &AssetsLibrary::get(const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100486{
487 return find_or_create_raw_tensor(name, format);
488}
489
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100490RawTensor AssetsLibrary::get(const std::string &name, Format format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100491{
492 return RawTensor(find_or_create_raw_tensor(name, format));
493}
494
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100495const RawTensor &AssetsLibrary::get(const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100496{
497 return get(name, get_format_for_channel(channel), channel);
498}
499
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100500RawTensor AssetsLibrary::get(const std::string &name, Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501{
502 return RawTensor(get(name, get_format_for_channel(channel), channel));
503}
504
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100505const RawTensor &AssetsLibrary::get(const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100506{
507 return find_or_create_raw_tensor(name, format, channel);
508}
509
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100510RawTensor AssetsLibrary::get(const std::string &name, Format format, Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100511{
512 return RawTensor(find_or_create_raw_tensor(name, format, channel));
513}
514} // namespace test
515} // namespace arm_compute