blob: 62de78cf26956680c3fb353ffddf1593d1bcff74 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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
Matthew Bentham470bc1e2020-03-09 10:55:40 +000031#pragma GCC diagnostic push
32#pragma GCC diagnostic ignored "-Wunused-parameter"
33#include "libnpy/npy.hpp"
34#pragma GCC diagnostic pop
35
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include <cctype>
37#include <fstream>
38#include <limits>
39#include <map>
40#include <mutex>
41#include <sstream>
42#include <stdexcept>
43#include <tuple>
44#include <unordered_map>
45#include <utility>
46
47namespace arm_compute
48{
49namespace test
50{
51namespace
52{
Giorgio Arenafda46182017-06-16 13:57:33 +010053template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
54void rgb_to_luminance(const RawTensor &src, RawTensor &dst)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055{
Abe Mbise562fe0f2018-02-09 14:13:02 +000056 // Ensure in/out tensors have same image dimensions (independent of element size and number of channels)
57 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 +010058
Abe Mbise562fe0f2018-02-09 14:13:02 +000059 const size_t num_elements = dst.num_elements();
60
61 // Currently, input is always RGB888 (3 U8 channels per element). Output can be U8, U16/S16 or U32
62 // Note that src.data()[i] returns pointer to first channel of element[i], so RGB values have [0,1,2] offsets
63 for(size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 {
Abe Mbise562fe0f2018-02-09 14:13:02 +000065 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 +010066 }
67}
68
69void extract_r_from_rgb(const RawTensor &src, RawTensor &dst)
70{
Abe Mbise562fe0f2018-02-09 14:13:02 +000071 ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072
Abe Mbise562fe0f2018-02-09 14:13:02 +000073 const size_t num_elements = dst.num_elements();
74
75 for(size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 {
77 dst.data()[j] = src.data()[i];
78 }
79}
80
81void extract_g_from_rgb(const RawTensor &src, RawTensor &dst)
82{
Abe Mbise562fe0f2018-02-09 14:13:02 +000083 ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084
Abe Mbise562fe0f2018-02-09 14:13:02 +000085 const size_t num_elements = dst.num_elements();
86
87 for(size_t i = 1, j = 0; j < num_elements; i += 3, ++j)
88 {
89 dst.data()[j] = src.data()[i];
90 }
91}
92
93void extract_b_from_rgb(const RawTensor &src, RawTensor &dst)
94{
95 ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
96
97 const size_t num_elements = dst.num_elements();
98
99 for(size_t i = 2, j = 0; j < num_elements; i += 3, ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 {
101 dst.data()[j] = src.data()[i];
102 }
103}
104
105void discard_comments(std::ifstream &fs)
106{
107 while(fs.peek() == '#')
108 {
109 fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
110 }
111}
112
113void discard_comments_and_spaces(std::ifstream &fs)
114{
115 while(true)
116 {
117 discard_comments(fs);
118
119 if(isspace(fs.peek()) == 0)
120 {
121 break;
122 }
123
124 fs.ignore(1);
125 }
126}
127
John Richardson8de92612018-02-22 14:09:31 +0000128std::tuple<unsigned int, unsigned int, int> parse_netpbm_format_header(std::ifstream &fs, char number)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129{
John Richardson8de92612018-02-22 14:09:31 +0000130 // check file type magic number is valid
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 std::array<char, 2> magic_number{ { 0 } };
132 fs >> magic_number[0] >> magic_number[1];
133
John Richardson8de92612018-02-22 14:09:31 +0000134 if(magic_number[0] != 'P' || magic_number[1] != number)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 {
John Richardson8de92612018-02-22 14:09:31 +0000136 throw std::runtime_error("File type magic number not supported");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 }
138
139 discard_comments_and_spaces(fs);
140
141 unsigned int width = 0;
142 fs >> width;
143
144 discard_comments_and_spaces(fs);
145
146 unsigned int height = 0;
147 fs >> height;
148
149 discard_comments_and_spaces(fs);
150
151 int max_value = 0;
152 fs >> max_value;
153
154 if(!fs.good())
155 {
156 throw std::runtime_error("Cannot read image dimensions");
157 }
158
159 if(max_value != 255)
160 {
161 throw std::runtime_error("RawTensor doesn't have 8-bit values");
162 }
163
164 discard_comments(fs);
165
166 if(isspace(fs.peek()) == 0)
167 {
John Richardson8de92612018-02-22 14:09:31 +0000168 throw std::runtime_error("Invalid image header");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 }
170
171 fs.ignore(1);
172
173 return std::make_tuple(width, height, max_value);
174}
175
John Richardson8de92612018-02-22 14:09:31 +0000176std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs)
177{
178 return parse_netpbm_format_header(fs, '6');
179}
180
181std::tuple<unsigned int, unsigned int, int> parse_pgm_header(std::ifstream &fs)
182{
183 return parse_netpbm_format_header(fs, '5');
184}
185
186void check_image_size(std::ifstream &fs, size_t raw_size)
187{
188 const size_t current_position = fs.tellg();
189 fs.seekg(0, std::ios_base::end);
190 const size_t end_position = fs.tellg();
191 fs.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
199void read_image_buffer(std::ifstream &fs, RawTensor &raw)
200{
201 fs.read(reinterpret_cast<std::fstream::char_type *>(raw.data()), raw.size());
202
203 if(!fs.good())
204 {
205 throw std::runtime_error("Failure while reading image buffer");
206 }
207}
208
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209RawTensor load_ppm(const std::string &path)
210{
211 std::ifstream file(path, std::ios::in | std::ios::binary);
212
213 if(!file.good())
214 {
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100215 throw framework::FileNotFound("Could not load PPM image: " + path);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 }
217
218 unsigned int width = 0;
219 unsigned int height = 0;
220
221 std::tie(width, height, std::ignore) = parse_ppm_header(file);
222
223 RawTensor raw(TensorShape(width, height), Format::RGB888);
224
John Richardson8de92612018-02-22 14:09:31 +0000225 check_image_size(file, raw.size());
226 read_image_buffer(file, raw);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227
John Richardson8de92612018-02-22 14:09:31 +0000228 return raw;
229}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230
John Richardson8de92612018-02-22 14:09:31 +0000231RawTensor load_pgm(const std::string &path)
232{
233 std::ifstream file(path, std::ios::in | std::ios::binary);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
235 if(!file.good())
236 {
John Richardson8de92612018-02-22 14:09:31 +0000237 throw framework::FileNotFound("Could not load PGM image: " + path);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238 }
239
John Richardson8de92612018-02-22 14:09:31 +0000240 unsigned int width = 0;
241 unsigned int height = 0;
242
243 std::tie(width, height, std::ignore) = parse_pgm_header(file);
244
245 RawTensor raw(TensorShape(width, height), Format::U8);
246
247 check_image_size(file, raw.size());
248 read_image_buffer(file, raw);
249
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100250 return raw;
251}
252} // namespace
253
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100254AssetsLibrary::AssetsLibrary(std::string path, std::random_device::result_type seed) //NOLINT
Anthony Barbierac69aa12017-07-03 17:39:37 +0100255 : _library_path(std::move(path)),
256 _seed{ seed }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257{
258}
259
John Richardson70f946b2017-10-02 16:52:16 +0100260std::string AssetsLibrary::path() const
261{
262 return _library_path;
263}
264
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100265std::random_device::result_type AssetsLibrary::seed() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100266{
267 return _seed;
268}
269
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100270void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100271{
272 //FIXME: Should be done by swapping cached buffers
273 const RawTensor &src = get(name, format);
274 std::copy_n(src.data(), raw.size(), raw.data());
275}
276
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100277void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100278{
279 fill(raw, name, get_format_for_channel(channel), channel);
280}
281
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100282void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100283{
284 const RawTensor &src = get(name, format, channel);
285 std::copy_n(src.data(), raw.size(), raw.data());
286}
287
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100288const AssetsLibrary::Loader &AssetsLibrary::get_loader(const std::string &extension) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289{
290 static std::unordered_map<std::string, Loader> loaders =
291 {
John Richardson8de92612018-02-22 14:09:31 +0000292 { "ppm", load_ppm },
293 { "pgm", load_pgm }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100294 };
295
296 const auto it = loaders.find(extension);
297
298 if(it != loaders.end())
299 {
300 return it->second;
301 }
302 else
303 {
304 throw std::invalid_argument("Cannot load image with extension '" + extension + "'");
305 }
306}
307
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100308const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, Format dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309{
310 static std::map<std::pair<Format, Format>, Converter> converters =
311 {
Giorgio Arenafda46182017-06-16 13:57:33 +0100312 { std::make_pair(Format::RGB888, Format::U8), rgb_to_luminance<uint8_t> },
313 { std::make_pair(Format::RGB888, Format::U16), rgb_to_luminance<uint16_t> },
314 { std::make_pair(Format::RGB888, Format::S16), rgb_to_luminance<int16_t> },
315 { std::make_pair(Format::RGB888, Format::U32), rgb_to_luminance<uint32_t> }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316 };
317
318 const auto it = converters.find(std::make_pair(src, dst));
319
320 if(it != converters.end())
321 {
322 return it->second;
323 }
324 else
325 {
326 std::stringstream msg;
327 msg << "Cannot convert from format '" << src << "' to format '" << dst << "'\n";
328 throw std::invalid_argument(msg.str());
329 }
330}
331
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100332const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, Format dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333{
334 static std::map<std::pair<DataType, Format>, Converter> converters = {};
335
336 const auto it = converters.find(std::make_pair(src, dst));
337
338 if(it != converters.end())
339 {
340 return it->second;
341 }
342 else
343 {
344 std::stringstream msg;
345 msg << "Cannot convert from data type '" << src << "' to format '" << dst << "'\n";
346 throw std::invalid_argument(msg.str());
347 }
348}
349
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100350const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, DataType dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351{
352 static std::map<std::pair<DataType, DataType>, Converter> converters = {};
353
354 const auto it = converters.find(std::make_pair(src, dst));
355
356 if(it != converters.end())
357 {
358 return it->second;
359 }
360 else
361 {
362 std::stringstream msg;
363 msg << "Cannot convert from data type '" << src << "' to data type '" << dst << "'\n";
364 throw std::invalid_argument(msg.str());
365 }
366}
367
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100368const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, DataType dst) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100369{
370 static std::map<std::pair<Format, DataType>, Converter> converters = {};
371
372 const auto it = converters.find(std::make_pair(src, dst));
373
374 if(it != converters.end())
375 {
376 return it->second;
377 }
378 else
379 {
380 std::stringstream msg;
381 msg << "Cannot convert from format '" << src << "' to data type '" << dst << "'\n";
382 throw std::invalid_argument(msg.str());
383 }
384}
385
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100386const AssetsLibrary::Extractor &AssetsLibrary::get_extractor(Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100387{
388 static std::map<std::pair<Format, Channel>, Extractor> extractors =
389 {
390 { std::make_pair(Format::RGB888, Channel::R), extract_r_from_rgb },
Abe Mbise562fe0f2018-02-09 14:13:02 +0000391 { std::make_pair(Format::RGB888, Channel::G), extract_g_from_rgb },
392 { std::make_pair(Format::RGB888, Channel::B), extract_b_from_rgb }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100393 };
394
395 const auto it = extractors.find(std::make_pair(format, channel));
396
397 if(it != extractors.end())
398 {
399 return it->second;
400 }
401 else
402 {
403 std::stringstream msg;
404 msg << "Cannot extract channel '" << channel << "' from format '" << format << "'\n";
405 throw std::invalid_argument(msg.str());
406 }
407}
408
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100409RawTensor AssetsLibrary::load_image(const std::string &name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100410{
411#ifdef _WIN32
412 const std::string image_path = ("\\images\\");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100413#else /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100414 const std::string image_path = ("/images/");
Anthony Barbierac69aa12017-07-03 17:39:37 +0100415#endif /* _WIN32 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100416
417 const std::string path = _library_path + image_path + name;
418 const std::string extension = path.substr(path.find_last_of('.') + 1);
419 return (*get_loader(extension))(path);
420}
421
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100422const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100423{
Georgios Pinitas421405b2018-10-26 19:05:32 +0100424 std::lock_guard<arm_compute::Mutex> guard(_format_lock);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100425
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100426 const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100427
428 if(ptr != nullptr)
429 {
430 return *ptr;
431 }
432
433 RawTensor raw = load_image(name);
434
435 if(raw.format() != format)
436 {
437 //FIXME: Remove unnecessary copy
438 RawTensor dst(raw.shape(), format);
439 (*get_converter(raw.format(), format))(raw, dst);
440 raw = std::move(dst);
441 }
442
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100443 return _cache.add(std::forward_as_tuple(name, format), std::move(raw));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100444}
445
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100446const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100447{
Georgios Pinitas421405b2018-10-26 19:05:32 +0100448 std::lock_guard<arm_compute::Mutex> guard(_channel_lock);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100449
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100450 const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format, channel));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100451
452 if(ptr != nullptr)
453 {
454 return *ptr;
455 }
456
457 const RawTensor &src = get(name, format);
458 //FIXME: Need to change shape to match channel
459 RawTensor dst(src.shape(), get_channel_format(channel));
460
461 (*get_extractor(format, channel))(src, dst);
462
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100463 return _cache.add(std::forward_as_tuple(name, format, channel), std::move(dst));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100464}
465
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100466TensorShape AssetsLibrary::get_image_shape(const std::string &name)
Giorgio Arenafda46182017-06-16 13:57:33 +0100467{
468 return load_image(name).shape();
469}
470
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100471const RawTensor &AssetsLibrary::get(const std::string &name) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100472{
473 //FIXME: Format should be derived from the image name. Not be fixed to RGB.
474 return find_or_create_raw_tensor(name, Format::RGB888);
475}
476
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100477RawTensor AssetsLibrary::get(const std::string &name)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100478{
479 //FIXME: Format should be derived from the image name. Not be fixed to RGB.
480 return RawTensor(find_or_create_raw_tensor(name, Format::RGB888));
481}
482
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100483RawTensor AssetsLibrary::get(const std::string &name, DataType data_type, int num_channels) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100484{
485 const RawTensor &raw = get(name);
486
487 return RawTensor(raw.shape(), data_type, num_channels);
488}
489
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100490const RawTensor &AssetsLibrary::get(const std::string &name, Format format) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100491{
492 return find_or_create_raw_tensor(name, format);
493}
494
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100495RawTensor AssetsLibrary::get(const std::string &name, Format format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100496{
497 return RawTensor(find_or_create_raw_tensor(name, format));
498}
499
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100500const RawTensor &AssetsLibrary::get(const std::string &name, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501{
502 return get(name, get_format_for_channel(channel), channel);
503}
504
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100505RawTensor AssetsLibrary::get(const std::string &name, Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100506{
507 return RawTensor(get(name, get_format_for_channel(channel), channel));
508}
509
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100510const RawTensor &AssetsLibrary::get(const std::string &name, Format format, Channel channel) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100511{
512 return find_or_create_raw_tensor(name, format, channel);
513}
514
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100515RawTensor AssetsLibrary::get(const std::string &name, Format format, Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100516{
517 return RawTensor(find_or_create_raw_tensor(name, format, channel));
518}
Matthew Bentham470bc1e2020-03-09 10:55:40 +0000519
520namespace detail
521{
522inline void validate_npy_header(std::ifstream &stream, const std::string &expect_typestr, const TensorShape &expect_shape)
523{
524 ARM_COMPUTE_UNUSED(expect_typestr);
525 ARM_COMPUTE_UNUSED(expect_shape);
526
527 std::string header = npy::read_header(stream);
528
529 // Parse header
530 std::vector<unsigned long> shape;
531 bool fortran_order = false;
532 std::string typestr;
533 npy::parse_header(header, typestr, fortran_order, shape);
534
535 // Check if the typestring matches the given one
536 ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch");
537
538 // Validate tensor shape
539 ARM_COMPUTE_ERROR_ON_MSG(shape.size() != expect_shape.num_dimensions(), "Tensor ranks mismatch");
540 if(fortran_order)
541 {
542 for(size_t i = 0; i < shape.size(); ++i)
543 {
544 ARM_COMPUTE_ERROR_ON_MSG(expect_shape[i] != shape[i], "Tensor dimensions mismatch");
545 }
546 }
547 else
548 {
549 for(size_t i = 0; i < shape.size(); ++i)
550 {
551 ARM_COMPUTE_ERROR_ON_MSG(expect_shape[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch");
552 }
553 }
554}
555} // namespace detail
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100556} // namespace test
557} // namespace arm_compute