blob: be565d9aaf09dc549a768f6cdc8ecd9ebd41ba7d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 */
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +010024#include "AssetsLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
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
43namespace arm_compute
44{
45namespace test
46{
47namespace
48{
Giorgio Arenafda46182017-06-16 13:57:33 +010049template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
50void rgb_to_luminance(const RawTensor &src, RawTensor &dst)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051{
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 Arenafda46182017-06-16 13:57:33 +010056 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 Barbier6ff3b192017-09-04 18:44:23 +010057 }
58}
59
60void 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
70void 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
80void discard_comments(std::ifstream &fs)
81{
82 while(fs.peek() == '#')
83 {
84 fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
85 }
86}
87
88void 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
103std::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
151RawTensor 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
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100189AssetsLibrary::AssetsLibrary(std::string path, std::random_device::result_type seed) //NOLINT
Anthony Barbierac69aa12017-07-03 17:39:37 +0100190 : _library_path(std::move(path)),
191 _seed{ seed }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192{
193}
194
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100195std::random_device::result_type AssetsLibrary::seed() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196{
197 return _seed;
198}
199
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100200void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201{
202 //FIXME: Should be done by swapping cached buffers
203 const RawTensor &src = get(name, format);
204 std::copy_n(src.data(), raw.size(), raw.data());
205}
206
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100207void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208{
209 fill(raw, name, get_format_for_channel(channel), channel);
210}
211
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100212void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213{
214 const RawTensor &src = get(name, format, channel);
215 std::copy_n(src.data(), raw.size(), raw.data());
216}
217
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100218const AssetsLibrary::Loader &AssetsLibrary::get_loader(const std::string &extension) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219{
220 static std::unordered_map<std::string, Loader> loaders =
221 {
222 { "ppm", load_ppm }
223 };
224
225 const auto it = loaders.find(extension);
226
227 if(it != loaders.end())
228 {
229 return it->second;
230 }
231 else
232 {
233 throw std::invalid_argument("Cannot load image with extension '" + extension + "'");
234 }
235}
236
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100237const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, Format dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238{
239 static std::map<std::pair<Format, Format>, Converter> converters =
240 {
Giorgio Arenafda46182017-06-16 13:57:33 +0100241 { std::make_pair(Format::RGB888, Format::U8), rgb_to_luminance<uint8_t> },
242 { std::make_pair(Format::RGB888, Format::U16), rgb_to_luminance<uint16_t> },
243 { std::make_pair(Format::RGB888, Format::S16), rgb_to_luminance<int16_t> },
244 { std::make_pair(Format::RGB888, Format::U32), rgb_to_luminance<uint32_t> }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100245 };
246
247 const auto it = converters.find(std::make_pair(src, dst));
248
249 if(it != converters.end())
250 {
251 return it->second;
252 }
253 else
254 {
255 std::stringstream msg;
256 msg << "Cannot convert from format '" << src << "' to format '" << dst << "'\n";
257 throw std::invalid_argument(msg.str());
258 }
259}
260
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100261const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, Format dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262{
263 static std::map<std::pair<DataType, Format>, Converter> converters = {};
264
265 const auto it = converters.find(std::make_pair(src, dst));
266
267 if(it != converters.end())
268 {
269 return it->second;
270 }
271 else
272 {
273 std::stringstream msg;
274 msg << "Cannot convert from data type '" << src << "' to format '" << dst << "'\n";
275 throw std::invalid_argument(msg.str());
276 }
277}
278
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100279const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, DataType dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100280{
281 static std::map<std::pair<DataType, DataType>, Converter> converters = {};
282
283 const auto it = converters.find(std::make_pair(src, dst));
284
285 if(it != converters.end())
286 {
287 return it->second;
288 }
289 else
290 {
291 std::stringstream msg;
292 msg << "Cannot convert from data type '" << src << "' to data type '" << dst << "'\n";
293 throw std::invalid_argument(msg.str());
294 }
295}
296
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100297const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, DataType dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100298{
299 static std::map<std::pair<Format, DataType>, Converter> converters = {};
300
301 const auto it = converters.find(std::make_pair(src, dst));
302
303 if(it != converters.end())
304 {
305 return it->second;
306 }
307 else
308 {
309 std::stringstream msg;
310 msg << "Cannot convert from format '" << src << "' to data type '" << dst << "'\n";
311 throw std::invalid_argument(msg.str());
312 }
313}
314
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100315const AssetsLibrary::Extractor &AssetsLibrary::get_extractor(Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316{
317 static std::map<std::pair<Format, Channel>, Extractor> extractors =
318 {
319 { std::make_pair(Format::RGB888, Channel::R), extract_r_from_rgb },
320 { std::make_pair(Format::RGB888, Channel::G), extract_g_from_rgb }
321 };
322
323 const auto it = extractors.find(std::make_pair(format, channel));
324
325 if(it != extractors.end())
326 {
327 return it->second;
328 }
329 else
330 {
331 std::stringstream msg;
332 msg << "Cannot extract channel '" << channel << "' from format '" << format << "'\n";
333 throw std::invalid_argument(msg.str());
334 }
335}
336
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100337RawTensor AssetsLibrary::load_image(const std::string &name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338{
339#ifdef _WIN32
340 const std::string image_path = ("\\images\\");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100341#else /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100342 const std::string image_path = ("/images/");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100343#endif /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344
345 const std::string path = _library_path + image_path + name;
346 const std::string extension = path.substr(path.find_last_of('.') + 1);
347 return (*get_loader(extension))(path);
348}
349
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100350const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351{
352 std::lock_guard<std::mutex> guard(_format_lock);
353
354 const RawTensor *ptr = _cache.find(std::make_tuple(name, format));
355
356 if(ptr != nullptr)
357 {
358 return *ptr;
359 }
360
361 RawTensor raw = load_image(name);
362
363 if(raw.format() != format)
364 {
365 //FIXME: Remove unnecessary copy
366 RawTensor dst(raw.shape(), format);
367 (*get_converter(raw.format(), format))(raw, dst);
368 raw = std::move(dst);
369 }
370
371 return _cache.add(std::make_tuple(name, format), std::move(raw));
372}
373
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100374const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100375{
376 std::lock_guard<std::mutex> guard(_channel_lock);
377
378 const RawTensor *ptr = _cache.find(std::make_tuple(name, format, channel));
379
380 if(ptr != nullptr)
381 {
382 return *ptr;
383 }
384
385 const RawTensor &src = get(name, format);
386 //FIXME: Need to change shape to match channel
387 RawTensor dst(src.shape(), get_channel_format(channel));
388
389 (*get_extractor(format, channel))(src, dst);
390
391 return _cache.add(std::make_tuple(name, format, channel), std::move(dst));
392}
393
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100394TensorShape AssetsLibrary::get_image_shape(const std::string &name)
Giorgio Arenafda46182017-06-16 13:57:33 +0100395{
396 return load_image(name).shape();
397}
398
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100399const RawTensor &AssetsLibrary::get(const std::string &name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100400{
401 //FIXME: Format should be derived from the image name. Not be fixed to RGB.
402 return find_or_create_raw_tensor(name, Format::RGB888);
403}
404
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100405RawTensor AssetsLibrary::get(const std::string &name)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100406{
407 //FIXME: Format should be derived from the image name. Not be fixed to RGB.
408 return RawTensor(find_or_create_raw_tensor(name, Format::RGB888));
409}
410
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100411RawTensor AssetsLibrary::get(const std::string &name, DataType data_type, int num_channels) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100412{
413 const RawTensor &raw = get(name);
414
415 return RawTensor(raw.shape(), data_type, num_channels);
416}
417
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100418const RawTensor &AssetsLibrary::get(const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100419{
420 return find_or_create_raw_tensor(name, format);
421}
422
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100423RawTensor AssetsLibrary::get(const std::string &name, Format format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100424{
425 return RawTensor(find_or_create_raw_tensor(name, format));
426}
427
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100428const RawTensor &AssetsLibrary::get(const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100429{
430 return get(name, get_format_for_channel(channel), channel);
431}
432
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100433RawTensor AssetsLibrary::get(const std::string &name, Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100434{
435 return RawTensor(get(name, get_format_for_channel(channel), channel));
436}
437
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100438const RawTensor &AssetsLibrary::get(const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439{
440 return find_or_create_raw_tensor(name, format, channel);
441}
442
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100443RawTensor AssetsLibrary::get(const std::string &name, Format format, Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100444{
445 return RawTensor(find_or_create_raw_tensor(name, format, channel));
446}
447} // namespace test
448} // namespace arm_compute