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