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