blob: 1cbd3b4e3d311fd03b44619e3a3fb2cc47b6b03f [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
123std::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
171RawTensor load_ppm(const std::string &path)
172{
173 std::ifstream file(path, std::ios::in | std::ios::binary);
174
175 if(!file.good())
176 {
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100177 throw framework::FileNotFound("Could not load PPM image: " + path);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 }
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
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100209AssetsLibrary::AssetsLibrary(std::string path, std::random_device::result_type seed) //NOLINT
Anthony Barbierac69aa12017-07-03 17:39:37 +0100210 : _library_path(std::move(path)),
211 _seed{ seed }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212{
213}
214
John Richardson70f946b2017-10-02 16:52:16 +0100215std::string AssetsLibrary::path() const
216{
217 return _library_path;
218}
219
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100220std::random_device::result_type AssetsLibrary::seed() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221{
222 return _seed;
223}
224
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100225void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226{
227 //FIXME: Should be done by swapping cached buffers
228 const RawTensor &src = get(name, format);
229 std::copy_n(src.data(), raw.size(), raw.data());
230}
231
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100232void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233{
234 fill(raw, name, get_format_for_channel(channel), channel);
235}
236
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100237void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238{
239 const RawTensor &src = get(name, format, channel);
240 std::copy_n(src.data(), raw.size(), raw.data());
241}
242
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100243const AssetsLibrary::Loader &AssetsLibrary::get_loader(const std::string &extension) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244{
245 static std::unordered_map<std::string, Loader> loaders =
246 {
247 { "ppm", load_ppm }
248 };
249
250 const auto it = loaders.find(extension);
251
252 if(it != loaders.end())
253 {
254 return it->second;
255 }
256 else
257 {
258 throw std::invalid_argument("Cannot load image with extension '" + extension + "'");
259 }
260}
261
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100262const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, Format dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100263{
264 static std::map<std::pair<Format, Format>, Converter> converters =
265 {
Giorgio Arenafda46182017-06-16 13:57:33 +0100266 { std::make_pair(Format::RGB888, Format::U8), rgb_to_luminance<uint8_t> },
267 { std::make_pair(Format::RGB888, Format::U16), rgb_to_luminance<uint16_t> },
268 { std::make_pair(Format::RGB888, Format::S16), rgb_to_luminance<int16_t> },
269 { std::make_pair(Format::RGB888, Format::U32), rgb_to_luminance<uint32_t> }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270 };
271
272 const auto it = converters.find(std::make_pair(src, dst));
273
274 if(it != converters.end())
275 {
276 return it->second;
277 }
278 else
279 {
280 std::stringstream msg;
281 msg << "Cannot convert from format '" << src << "' to format '" << dst << "'\n";
282 throw std::invalid_argument(msg.str());
283 }
284}
285
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100286const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, Format dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287{
288 static std::map<std::pair<DataType, Format>, Converter> converters = {};
289
290 const auto it = converters.find(std::make_pair(src, dst));
291
292 if(it != converters.end())
293 {
294 return it->second;
295 }
296 else
297 {
298 std::stringstream msg;
299 msg << "Cannot convert from data type '" << src << "' to format '" << dst << "'\n";
300 throw std::invalid_argument(msg.str());
301 }
302}
303
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100304const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, DataType dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100305{
306 static std::map<std::pair<DataType, DataType>, Converter> converters = {};
307
308 const auto it = converters.find(std::make_pair(src, dst));
309
310 if(it != converters.end())
311 {
312 return it->second;
313 }
314 else
315 {
316 std::stringstream msg;
317 msg << "Cannot convert from data type '" << src << "' to data type '" << dst << "'\n";
318 throw std::invalid_argument(msg.str());
319 }
320}
321
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100322const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, DataType dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323{
324 static std::map<std::pair<Format, DataType>, Converter> converters = {};
325
326 const auto it = converters.find(std::make_pair(src, dst));
327
328 if(it != converters.end())
329 {
330 return it->second;
331 }
332 else
333 {
334 std::stringstream msg;
335 msg << "Cannot convert from format '" << src << "' to data type '" << dst << "'\n";
336 throw std::invalid_argument(msg.str());
337 }
338}
339
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100340const AssetsLibrary::Extractor &AssetsLibrary::get_extractor(Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100341{
342 static std::map<std::pair<Format, Channel>, Extractor> extractors =
343 {
344 { std::make_pair(Format::RGB888, Channel::R), extract_r_from_rgb },
Abe Mbise562fe0f2018-02-09 14:13:02 +0000345 { std::make_pair(Format::RGB888, Channel::G), extract_g_from_rgb },
346 { std::make_pair(Format::RGB888, Channel::B), extract_b_from_rgb }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100347 };
348
349 const auto it = extractors.find(std::make_pair(format, channel));
350
351 if(it != extractors.end())
352 {
353 return it->second;
354 }
355 else
356 {
357 std::stringstream msg;
358 msg << "Cannot extract channel '" << channel << "' from format '" << format << "'\n";
359 throw std::invalid_argument(msg.str());
360 }
361}
362
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100363RawTensor AssetsLibrary::load_image(const std::string &name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100364{
365#ifdef _WIN32
366 const std::string image_path = ("\\images\\");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100367#else /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100368 const std::string image_path = ("/images/");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100369#endif /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100370
371 const std::string path = _library_path + image_path + name;
372 const std::string extension = path.substr(path.find_last_of('.') + 1);
373 return (*get_loader(extension))(path);
374}
375
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100376const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100377{
378 std::lock_guard<std::mutex> guard(_format_lock);
379
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100380 const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100381
382 if(ptr != nullptr)
383 {
384 return *ptr;
385 }
386
387 RawTensor raw = load_image(name);
388
389 if(raw.format() != format)
390 {
391 //FIXME: Remove unnecessary copy
392 RawTensor dst(raw.shape(), format);
393 (*get_converter(raw.format(), format))(raw, dst);
394 raw = std::move(dst);
395 }
396
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100397 return _cache.add(std::forward_as_tuple(name, format), std::move(raw));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100398}
399
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100400const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100401{
402 std::lock_guard<std::mutex> guard(_channel_lock);
403
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100404 const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format, channel));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100405
406 if(ptr != nullptr)
407 {
408 return *ptr;
409 }
410
411 const RawTensor &src = get(name, format);
412 //FIXME: Need to change shape to match channel
413 RawTensor dst(src.shape(), get_channel_format(channel));
414
415 (*get_extractor(format, channel))(src, dst);
416
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100417 return _cache.add(std::forward_as_tuple(name, format, channel), std::move(dst));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418}
419
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100420TensorShape AssetsLibrary::get_image_shape(const std::string &name)
Giorgio Arenafda46182017-06-16 13:57:33 +0100421{
422 return load_image(name).shape();
423}
424
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100425const RawTensor &AssetsLibrary::get(const std::string &name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426{
427 //FIXME: Format should be derived from the image name. Not be fixed to RGB.
428 return find_or_create_raw_tensor(name, Format::RGB888);
429}
430
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100431RawTensor AssetsLibrary::get(const std::string &name)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100432{
433 //FIXME: Format should be derived from the image name. Not be fixed to RGB.
434 return RawTensor(find_or_create_raw_tensor(name, Format::RGB888));
435}
436
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100437RawTensor AssetsLibrary::get(const std::string &name, DataType data_type, int num_channels) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100438{
439 const RawTensor &raw = get(name);
440
441 return RawTensor(raw.shape(), data_type, num_channels);
442}
443
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100444const RawTensor &AssetsLibrary::get(const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100445{
446 return find_or_create_raw_tensor(name, format);
447}
448
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100449RawTensor AssetsLibrary::get(const std::string &name, Format format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100450{
451 return RawTensor(find_or_create_raw_tensor(name, format));
452}
453
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100454const RawTensor &AssetsLibrary::get(const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100455{
456 return get(name, get_format_for_channel(channel), channel);
457}
458
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100459RawTensor AssetsLibrary::get(const std::string &name, Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100460{
461 return RawTensor(get(name, get_format_for_channel(channel), channel));
462}
463
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100464const RawTensor &AssetsLibrary::get(const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100465{
466 return find_or_create_raw_tensor(name, format, channel);
467}
468
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100469RawTensor AssetsLibrary::get(const std::string &name, Format format, Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100470{
471 return RawTensor(find_or_create_raw_tensor(name, format, channel));
472}
473} // namespace test
474} // namespace arm_compute