1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2020, 2023 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "tests/AssetsLibrary.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "Utils.h"
27*c217d954SCole Faust #include "utils/TypePrinter.h"
28*c217d954SCole Faust
29*c217d954SCole Faust #include "arm_compute/core/ITensor.h"
30*c217d954SCole Faust
31*c217d954SCole Faust #pragma GCC diagnostic push
32*c217d954SCole Faust #pragma GCC diagnostic ignored "-Wunused-parameter"
33*c217d954SCole Faust #include "libnpy/npy.hpp"
34*c217d954SCole Faust #pragma GCC diagnostic pop
35*c217d954SCole Faust
36*c217d954SCole Faust #include <cctype>
37*c217d954SCole Faust #include <fstream>
38*c217d954SCole Faust #include <limits>
39*c217d954SCole Faust #include <map>
40*c217d954SCole Faust #include <mutex>
41*c217d954SCole Faust #include <sstream>
42*c217d954SCole Faust #include <stdexcept>
43*c217d954SCole Faust #include <tuple>
44*c217d954SCole Faust #include <unordered_map>
45*c217d954SCole Faust #include <utility>
46*c217d954SCole Faust
47*c217d954SCole Faust namespace arm_compute
48*c217d954SCole Faust {
49*c217d954SCole Faust namespace test
50*c217d954SCole Faust {
51*c217d954SCole Faust namespace
52*c217d954SCole Faust {
53*c217d954SCole Faust template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
rgb_to_luminance(const RawTensor & src,RawTensor & dst)54*c217d954SCole Faust void rgb_to_luminance(const RawTensor &src, RawTensor &dst)
55*c217d954SCole Faust {
56*c217d954SCole Faust // Ensure in/out tensors have same image dimensions (independent of element size and number of channels)
57*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(src.num_elements() != dst.num_elements(), "Input and output images must have equal dimensions");
58*c217d954SCole Faust
59*c217d954SCole Faust const size_t num_elements = dst.num_elements();
60*c217d954SCole Faust
61*c217d954SCole Faust // Currently, input is always RGB888 (3 U8 channels per element). Output can be U8, U16/S16 or U32
62*c217d954SCole Faust // Note that src.data()[i] returns pointer to first channel of element[i], so RGB values have [0,1,2] offsets
63*c217d954SCole Faust for(size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
64*c217d954SCole Faust {
65*c217d954SCole Faust reinterpret_cast<T *>(dst.data())[j] = 0.2126f * src.data()[i] + 0.7152f * src.data()[i + 1] + 0.0722f * src.data()[i + 2];
66*c217d954SCole Faust }
67*c217d954SCole Faust }
68*c217d954SCole Faust
extract_r_from_rgb(const RawTensor & src,RawTensor & dst)69*c217d954SCole Faust void extract_r_from_rgb(const RawTensor &src, RawTensor &dst)
70*c217d954SCole Faust {
71*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
72*c217d954SCole Faust
73*c217d954SCole Faust const size_t num_elements = dst.num_elements();
74*c217d954SCole Faust
75*c217d954SCole Faust for(size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
76*c217d954SCole Faust {
77*c217d954SCole Faust dst.data()[j] = src.data()[i];
78*c217d954SCole Faust }
79*c217d954SCole Faust }
80*c217d954SCole Faust
extract_g_from_rgb(const RawTensor & src,RawTensor & dst)81*c217d954SCole Faust void extract_g_from_rgb(const RawTensor &src, RawTensor &dst)
82*c217d954SCole Faust {
83*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
84*c217d954SCole Faust
85*c217d954SCole Faust const size_t num_elements = dst.num_elements();
86*c217d954SCole Faust
87*c217d954SCole Faust for(size_t i = 1, j = 0; j < num_elements; i += 3, ++j)
88*c217d954SCole Faust {
89*c217d954SCole Faust dst.data()[j] = src.data()[i];
90*c217d954SCole Faust }
91*c217d954SCole Faust }
92*c217d954SCole Faust
extract_b_from_rgb(const RawTensor & src,RawTensor & dst)93*c217d954SCole Faust void extract_b_from_rgb(const RawTensor &src, RawTensor &dst)
94*c217d954SCole Faust {
95*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(src.size() != 3 * dst.size());
96*c217d954SCole Faust
97*c217d954SCole Faust const size_t num_elements = dst.num_elements();
98*c217d954SCole Faust
99*c217d954SCole Faust for(size_t i = 2, j = 0; j < num_elements; i += 3, ++j)
100*c217d954SCole Faust {
101*c217d954SCole Faust dst.data()[j] = src.data()[i];
102*c217d954SCole Faust }
103*c217d954SCole Faust }
104*c217d954SCole Faust
discard_comments(std::ifstream & fs)105*c217d954SCole Faust void discard_comments(std::ifstream &fs)
106*c217d954SCole Faust {
107*c217d954SCole Faust while(fs.peek() == '#')
108*c217d954SCole Faust {
109*c217d954SCole Faust fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
110*c217d954SCole Faust }
111*c217d954SCole Faust }
112*c217d954SCole Faust
discard_comments_and_spaces(std::ifstream & fs)113*c217d954SCole Faust void discard_comments_and_spaces(std::ifstream &fs)
114*c217d954SCole Faust {
115*c217d954SCole Faust while(true)
116*c217d954SCole Faust {
117*c217d954SCole Faust discard_comments(fs);
118*c217d954SCole Faust
119*c217d954SCole Faust if(isspace(fs.peek()) == 0)
120*c217d954SCole Faust {
121*c217d954SCole Faust break;
122*c217d954SCole Faust }
123*c217d954SCole Faust
124*c217d954SCole Faust fs.ignore(1);
125*c217d954SCole Faust }
126*c217d954SCole Faust }
127*c217d954SCole Faust
parse_netpbm_format_header(std::ifstream & fs,char number)128*c217d954SCole Faust std::tuple<unsigned int, unsigned int, int> parse_netpbm_format_header(std::ifstream &fs, char number)
129*c217d954SCole Faust {
130*c217d954SCole Faust // check file type magic number is valid
131*c217d954SCole Faust std::array<char, 2> magic_number{ { 0 } };
132*c217d954SCole Faust fs >> magic_number[0] >> magic_number[1];
133*c217d954SCole Faust
134*c217d954SCole Faust if(magic_number[0] != 'P' || magic_number[1] != number)
135*c217d954SCole Faust {
136*c217d954SCole Faust throw std::runtime_error("File type magic number not supported");
137*c217d954SCole Faust }
138*c217d954SCole Faust
139*c217d954SCole Faust discard_comments_and_spaces(fs);
140*c217d954SCole Faust
141*c217d954SCole Faust unsigned int width = 0;
142*c217d954SCole Faust fs >> width;
143*c217d954SCole Faust
144*c217d954SCole Faust discard_comments_and_spaces(fs);
145*c217d954SCole Faust
146*c217d954SCole Faust unsigned int height = 0;
147*c217d954SCole Faust fs >> height;
148*c217d954SCole Faust
149*c217d954SCole Faust discard_comments_and_spaces(fs);
150*c217d954SCole Faust
151*c217d954SCole Faust int max_value = 0;
152*c217d954SCole Faust fs >> max_value;
153*c217d954SCole Faust
154*c217d954SCole Faust if(!fs.good())
155*c217d954SCole Faust {
156*c217d954SCole Faust throw std::runtime_error("Cannot read image dimensions");
157*c217d954SCole Faust }
158*c217d954SCole Faust
159*c217d954SCole Faust if(max_value != 255)
160*c217d954SCole Faust {
161*c217d954SCole Faust throw std::runtime_error("RawTensor doesn't have 8-bit values");
162*c217d954SCole Faust }
163*c217d954SCole Faust
164*c217d954SCole Faust discard_comments(fs);
165*c217d954SCole Faust
166*c217d954SCole Faust if(isspace(fs.peek()) == 0)
167*c217d954SCole Faust {
168*c217d954SCole Faust throw std::runtime_error("Invalid image header");
169*c217d954SCole Faust }
170*c217d954SCole Faust
171*c217d954SCole Faust fs.ignore(1);
172*c217d954SCole Faust
173*c217d954SCole Faust return std::make_tuple(width, height, max_value);
174*c217d954SCole Faust }
175*c217d954SCole Faust
parse_ppm_header(std::ifstream & fs)176*c217d954SCole Faust std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs)
177*c217d954SCole Faust {
178*c217d954SCole Faust return parse_netpbm_format_header(fs, '6');
179*c217d954SCole Faust }
180*c217d954SCole Faust
parse_pgm_header(std::ifstream & fs)181*c217d954SCole Faust std::tuple<unsigned int, unsigned int, int> parse_pgm_header(std::ifstream &fs)
182*c217d954SCole Faust {
183*c217d954SCole Faust return parse_netpbm_format_header(fs, '5');
184*c217d954SCole Faust }
185*c217d954SCole Faust
check_image_size(std::ifstream & fs,size_t raw_size)186*c217d954SCole Faust void check_image_size(std::ifstream &fs, size_t raw_size)
187*c217d954SCole Faust {
188*c217d954SCole Faust const size_t current_position = fs.tellg();
189*c217d954SCole Faust fs.seekg(0, std::ios_base::end);
190*c217d954SCole Faust const size_t end_position = fs.tellg();
191*c217d954SCole Faust fs.seekg(current_position, std::ios_base::beg);
192*c217d954SCole Faust
193*c217d954SCole Faust if((end_position - current_position) < raw_size)
194*c217d954SCole Faust {
195*c217d954SCole Faust throw std::runtime_error("Not enough data in file");
196*c217d954SCole Faust }
197*c217d954SCole Faust }
198*c217d954SCole Faust
read_image_buffer(std::ifstream & fs,RawTensor & raw)199*c217d954SCole Faust void read_image_buffer(std::ifstream &fs, RawTensor &raw)
200*c217d954SCole Faust {
201*c217d954SCole Faust fs.read(reinterpret_cast<std::fstream::char_type *>(raw.data()), raw.size());
202*c217d954SCole Faust
203*c217d954SCole Faust if(!fs.good())
204*c217d954SCole Faust {
205*c217d954SCole Faust throw std::runtime_error("Failure while reading image buffer");
206*c217d954SCole Faust }
207*c217d954SCole Faust }
208*c217d954SCole Faust
load_ppm(const std::string & path)209*c217d954SCole Faust RawTensor load_ppm(const std::string &path)
210*c217d954SCole Faust {
211*c217d954SCole Faust std::ifstream file(path, std::ios::in | std::ios::binary);
212*c217d954SCole Faust
213*c217d954SCole Faust if(!file.good())
214*c217d954SCole Faust {
215*c217d954SCole Faust throw framework::FileNotFound("Could not load PPM image: " + path);
216*c217d954SCole Faust }
217*c217d954SCole Faust
218*c217d954SCole Faust unsigned int width = 0;
219*c217d954SCole Faust unsigned int height = 0;
220*c217d954SCole Faust
221*c217d954SCole Faust std::tie(width, height, std::ignore) = parse_ppm_header(file);
222*c217d954SCole Faust
223*c217d954SCole Faust RawTensor raw(TensorShape(width, height), Format::RGB888);
224*c217d954SCole Faust
225*c217d954SCole Faust check_image_size(file, raw.size());
226*c217d954SCole Faust read_image_buffer(file, raw);
227*c217d954SCole Faust
228*c217d954SCole Faust return raw;
229*c217d954SCole Faust }
230*c217d954SCole Faust
load_pgm(const std::string & path)231*c217d954SCole Faust RawTensor load_pgm(const std::string &path)
232*c217d954SCole Faust {
233*c217d954SCole Faust std::ifstream file(path, std::ios::in | std::ios::binary);
234*c217d954SCole Faust
235*c217d954SCole Faust if(!file.good())
236*c217d954SCole Faust {
237*c217d954SCole Faust throw framework::FileNotFound("Could not load PGM image: " + path);
238*c217d954SCole Faust }
239*c217d954SCole Faust
240*c217d954SCole Faust unsigned int width = 0;
241*c217d954SCole Faust unsigned int height = 0;
242*c217d954SCole Faust
243*c217d954SCole Faust std::tie(width, height, std::ignore) = parse_pgm_header(file);
244*c217d954SCole Faust
245*c217d954SCole Faust RawTensor raw(TensorShape(width, height), Format::U8);
246*c217d954SCole Faust
247*c217d954SCole Faust check_image_size(file, raw.size());
248*c217d954SCole Faust read_image_buffer(file, raw);
249*c217d954SCole Faust
250*c217d954SCole Faust return raw;
251*c217d954SCole Faust }
252*c217d954SCole Faust } // namespace
253*c217d954SCole Faust
AssetsLibrary(std::string path,std::random_device::result_type seed)254*c217d954SCole Faust AssetsLibrary::AssetsLibrary(std::string path, std::random_device::result_type seed) //NOLINT
255*c217d954SCole Faust : _library_path(std::move(path)),
256*c217d954SCole Faust _seed{ seed }
257*c217d954SCole Faust {
258*c217d954SCole Faust }
259*c217d954SCole Faust
path() const260*c217d954SCole Faust std::string AssetsLibrary::path() const
261*c217d954SCole Faust {
262*c217d954SCole Faust return _library_path;
263*c217d954SCole Faust }
264*c217d954SCole Faust
seed() const265*c217d954SCole Faust std::random_device::result_type AssetsLibrary::seed() const
266*c217d954SCole Faust {
267*c217d954SCole Faust return _seed;
268*c217d954SCole Faust }
269*c217d954SCole Faust
fill(RawTensor & raw,const std::string & name,Format format) const270*c217d954SCole Faust void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format) const
271*c217d954SCole Faust {
272*c217d954SCole Faust //FIXME: Should be done by swapping cached buffers
273*c217d954SCole Faust const RawTensor &src = get(name, format);
274*c217d954SCole Faust std::copy_n(src.data(), raw.size(), raw.data());
275*c217d954SCole Faust }
276*c217d954SCole Faust
fill(RawTensor & raw,const std::string & name,Channel channel) const277*c217d954SCole Faust void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Channel channel) const
278*c217d954SCole Faust {
279*c217d954SCole Faust fill(raw, name, get_format_for_channel(channel), channel);
280*c217d954SCole Faust }
281*c217d954SCole Faust
fill(RawTensor & raw,const std::string & name,Format format,Channel channel) const282*c217d954SCole Faust void AssetsLibrary::fill(RawTensor &raw, const std::string &name, Format format, Channel channel) const
283*c217d954SCole Faust {
284*c217d954SCole Faust const RawTensor &src = get(name, format, channel);
285*c217d954SCole Faust std::copy_n(src.data(), raw.size(), raw.data());
286*c217d954SCole Faust }
287*c217d954SCole Faust
get_loader(const std::string & extension) const288*c217d954SCole Faust const AssetsLibrary::Loader &AssetsLibrary::get_loader(const std::string &extension) const
289*c217d954SCole Faust {
290*c217d954SCole Faust static std::unordered_map<std::string, Loader> loaders =
291*c217d954SCole Faust {
292*c217d954SCole Faust { "ppm", load_ppm },
293*c217d954SCole Faust { "pgm", load_pgm }
294*c217d954SCole Faust };
295*c217d954SCole Faust
296*c217d954SCole Faust const auto it = loaders.find(extension);
297*c217d954SCole Faust
298*c217d954SCole Faust if(it != loaders.end())
299*c217d954SCole Faust {
300*c217d954SCole Faust return it->second;
301*c217d954SCole Faust }
302*c217d954SCole Faust else
303*c217d954SCole Faust {
304*c217d954SCole Faust throw std::invalid_argument("Cannot load image with extension '" + extension + "'");
305*c217d954SCole Faust }
306*c217d954SCole Faust }
307*c217d954SCole Faust
get_converter(Format src,Format dst) const308*c217d954SCole Faust const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, Format dst) const
309*c217d954SCole Faust {
310*c217d954SCole Faust static std::map<std::pair<Format, Format>, Converter> converters =
311*c217d954SCole Faust {
312*c217d954SCole Faust { std::make_pair(Format::RGB888, Format::U8), rgb_to_luminance<uint8_t> },
313*c217d954SCole Faust { std::make_pair(Format::RGB888, Format::U16), rgb_to_luminance<uint16_t> },
314*c217d954SCole Faust { std::make_pair(Format::RGB888, Format::S16), rgb_to_luminance<int16_t> },
315*c217d954SCole Faust { std::make_pair(Format::RGB888, Format::U32), rgb_to_luminance<uint32_t> }
316*c217d954SCole Faust };
317*c217d954SCole Faust
318*c217d954SCole Faust const auto it = converters.find(std::make_pair(src, dst));
319*c217d954SCole Faust
320*c217d954SCole Faust if(it != converters.end())
321*c217d954SCole Faust {
322*c217d954SCole Faust return it->second;
323*c217d954SCole Faust }
324*c217d954SCole Faust else
325*c217d954SCole Faust {
326*c217d954SCole Faust std::stringstream msg;
327*c217d954SCole Faust msg << "Cannot convert from format '" << src << "' to format '" << dst << "'\n";
328*c217d954SCole Faust throw std::invalid_argument(msg.str());
329*c217d954SCole Faust }
330*c217d954SCole Faust }
331*c217d954SCole Faust
get_converter(DataType src,Format dst) const332*c217d954SCole Faust const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, Format dst) const
333*c217d954SCole Faust {
334*c217d954SCole Faust static std::map<std::pair<DataType, Format>, Converter> converters = {};
335*c217d954SCole Faust
336*c217d954SCole Faust const auto it = converters.find(std::make_pair(src, dst));
337*c217d954SCole Faust
338*c217d954SCole Faust if(it != converters.end())
339*c217d954SCole Faust {
340*c217d954SCole Faust return it->second;
341*c217d954SCole Faust }
342*c217d954SCole Faust else
343*c217d954SCole Faust {
344*c217d954SCole Faust std::stringstream msg;
345*c217d954SCole Faust msg << "Cannot convert from data type '" << src << "' to format '" << dst << "'\n";
346*c217d954SCole Faust throw std::invalid_argument(msg.str());
347*c217d954SCole Faust }
348*c217d954SCole Faust }
349*c217d954SCole Faust
get_converter(DataType src,DataType dst) const350*c217d954SCole Faust const AssetsLibrary::Converter &AssetsLibrary::get_converter(DataType src, DataType dst) const
351*c217d954SCole Faust {
352*c217d954SCole Faust static std::map<std::pair<DataType, DataType>, Converter> converters = {};
353*c217d954SCole Faust
354*c217d954SCole Faust const auto it = converters.find(std::make_pair(src, dst));
355*c217d954SCole Faust
356*c217d954SCole Faust if(it != converters.end())
357*c217d954SCole Faust {
358*c217d954SCole Faust return it->second;
359*c217d954SCole Faust }
360*c217d954SCole Faust else
361*c217d954SCole Faust {
362*c217d954SCole Faust std::stringstream msg;
363*c217d954SCole Faust msg << "Cannot convert from data type '" << src << "' to data type '" << dst << "'\n";
364*c217d954SCole Faust throw std::invalid_argument(msg.str());
365*c217d954SCole Faust }
366*c217d954SCole Faust }
367*c217d954SCole Faust
get_converter(Format src,DataType dst) const368*c217d954SCole Faust const AssetsLibrary::Converter &AssetsLibrary::get_converter(Format src, DataType dst) const
369*c217d954SCole Faust {
370*c217d954SCole Faust static std::map<std::pair<Format, DataType>, Converter> converters = {};
371*c217d954SCole Faust
372*c217d954SCole Faust const auto it = converters.find(std::make_pair(src, dst));
373*c217d954SCole Faust
374*c217d954SCole Faust if(it != converters.end())
375*c217d954SCole Faust {
376*c217d954SCole Faust return it->second;
377*c217d954SCole Faust }
378*c217d954SCole Faust else
379*c217d954SCole Faust {
380*c217d954SCole Faust std::stringstream msg;
381*c217d954SCole Faust msg << "Cannot convert from format '" << src << "' to data type '" << dst << "'\n";
382*c217d954SCole Faust throw std::invalid_argument(msg.str());
383*c217d954SCole Faust }
384*c217d954SCole Faust }
385*c217d954SCole Faust
get_extractor(Format format,Channel channel) const386*c217d954SCole Faust const AssetsLibrary::Extractor &AssetsLibrary::get_extractor(Format format, Channel channel) const
387*c217d954SCole Faust {
388*c217d954SCole Faust static std::map<std::pair<Format, Channel>, Extractor> extractors =
389*c217d954SCole Faust {
390*c217d954SCole Faust { std::make_pair(Format::RGB888, Channel::R), extract_r_from_rgb },
391*c217d954SCole Faust { std::make_pair(Format::RGB888, Channel::G), extract_g_from_rgb },
392*c217d954SCole Faust { std::make_pair(Format::RGB888, Channel::B), extract_b_from_rgb }
393*c217d954SCole Faust };
394*c217d954SCole Faust
395*c217d954SCole Faust const auto it = extractors.find(std::make_pair(format, channel));
396*c217d954SCole Faust
397*c217d954SCole Faust if(it != extractors.end())
398*c217d954SCole Faust {
399*c217d954SCole Faust return it->second;
400*c217d954SCole Faust }
401*c217d954SCole Faust else
402*c217d954SCole Faust {
403*c217d954SCole Faust std::stringstream msg;
404*c217d954SCole Faust msg << "Cannot extract channel '" << channel << "' from format '" << format << "'\n";
405*c217d954SCole Faust throw std::invalid_argument(msg.str());
406*c217d954SCole Faust }
407*c217d954SCole Faust }
408*c217d954SCole Faust
load_image(const std::string & name) const409*c217d954SCole Faust RawTensor AssetsLibrary::load_image(const std::string &name) const
410*c217d954SCole Faust {
411*c217d954SCole Faust #ifdef _WIN32
412*c217d954SCole Faust const std::string image_path = ("\\images\\");
413*c217d954SCole Faust #else /* _WIN32 */
414*c217d954SCole Faust const std::string image_path = ("/images/");
415*c217d954SCole Faust #endif /* _WIN32 */
416*c217d954SCole Faust
417*c217d954SCole Faust const std::string path = _library_path + image_path + name;
418*c217d954SCole Faust const std::string extension = path.substr(path.find_last_of('.') + 1);
419*c217d954SCole Faust return (*get_loader(extension))(path);
420*c217d954SCole Faust }
421*c217d954SCole Faust
find_or_create_raw_tensor(const std::string & name,Format format) const422*c217d954SCole Faust const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format) const
423*c217d954SCole Faust {
424*c217d954SCole Faust std::lock_guard<arm_compute::Mutex> guard(_format_lock);
425*c217d954SCole Faust
426*c217d954SCole Faust const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format));
427*c217d954SCole Faust
428*c217d954SCole Faust if(ptr != nullptr)
429*c217d954SCole Faust {
430*c217d954SCole Faust return *ptr;
431*c217d954SCole Faust }
432*c217d954SCole Faust
433*c217d954SCole Faust RawTensor raw = load_image(name);
434*c217d954SCole Faust
435*c217d954SCole Faust if(raw.format() != format)
436*c217d954SCole Faust {
437*c217d954SCole Faust //FIXME: Remove unnecessary copy
438*c217d954SCole Faust RawTensor dst(raw.shape(), format);
439*c217d954SCole Faust (*get_converter(raw.format(), format))(raw, dst);
440*c217d954SCole Faust raw = std::move(dst);
441*c217d954SCole Faust }
442*c217d954SCole Faust
443*c217d954SCole Faust return _cache.add(std::forward_as_tuple(name, format), std::move(raw));
444*c217d954SCole Faust }
445*c217d954SCole Faust
find_or_create_raw_tensor(const std::string & name,Format format,Channel channel) const446*c217d954SCole Faust const RawTensor &AssetsLibrary::find_or_create_raw_tensor(const std::string &name, Format format, Channel channel) const
447*c217d954SCole Faust {
448*c217d954SCole Faust std::lock_guard<arm_compute::Mutex> guard(_channel_lock);
449*c217d954SCole Faust
450*c217d954SCole Faust const RawTensor *ptr = _cache.find(std::forward_as_tuple(name, format, channel));
451*c217d954SCole Faust
452*c217d954SCole Faust if(ptr != nullptr)
453*c217d954SCole Faust {
454*c217d954SCole Faust return *ptr;
455*c217d954SCole Faust }
456*c217d954SCole Faust
457*c217d954SCole Faust const RawTensor &src = get(name, format);
458*c217d954SCole Faust //FIXME: Need to change shape to match channel
459*c217d954SCole Faust RawTensor dst(src.shape(), get_channel_format(channel));
460*c217d954SCole Faust
461*c217d954SCole Faust (*get_extractor(format, channel))(src, dst);
462*c217d954SCole Faust
463*c217d954SCole Faust return _cache.add(std::forward_as_tuple(name, format, channel), std::move(dst));
464*c217d954SCole Faust }
465*c217d954SCole Faust
get_image_shape(const std::string & name)466*c217d954SCole Faust TensorShape AssetsLibrary::get_image_shape(const std::string &name)
467*c217d954SCole Faust {
468*c217d954SCole Faust return load_image(name).shape();
469*c217d954SCole Faust }
470*c217d954SCole Faust
get(const std::string & name) const471*c217d954SCole Faust const RawTensor &AssetsLibrary::get(const std::string &name) const
472*c217d954SCole Faust {
473*c217d954SCole Faust //FIXME: Format should be derived from the image name. Not be fixed to RGB.
474*c217d954SCole Faust return find_or_create_raw_tensor(name, Format::RGB888);
475*c217d954SCole Faust }
476*c217d954SCole Faust
get(const std::string & name)477*c217d954SCole Faust RawTensor AssetsLibrary::get(const std::string &name)
478*c217d954SCole Faust {
479*c217d954SCole Faust //FIXME: Format should be derived from the image name. Not be fixed to RGB.
480*c217d954SCole Faust return RawTensor(find_or_create_raw_tensor(name, Format::RGB888));
481*c217d954SCole Faust }
482*c217d954SCole Faust
get(const std::string & name,DataType data_type,int num_channels) const483*c217d954SCole Faust RawTensor AssetsLibrary::get(const std::string &name, DataType data_type, int num_channels) const
484*c217d954SCole Faust {
485*c217d954SCole Faust const RawTensor &raw = get(name);
486*c217d954SCole Faust
487*c217d954SCole Faust return RawTensor(raw.shape(), data_type, num_channels);
488*c217d954SCole Faust }
489*c217d954SCole Faust
get(const std::string & name,Format format) const490*c217d954SCole Faust const RawTensor &AssetsLibrary::get(const std::string &name, Format format) const
491*c217d954SCole Faust {
492*c217d954SCole Faust return find_or_create_raw_tensor(name, format);
493*c217d954SCole Faust }
494*c217d954SCole Faust
get(const std::string & name,Format format)495*c217d954SCole Faust RawTensor AssetsLibrary::get(const std::string &name, Format format)
496*c217d954SCole Faust {
497*c217d954SCole Faust return RawTensor(find_or_create_raw_tensor(name, format));
498*c217d954SCole Faust }
499*c217d954SCole Faust
get(const std::string & name,Channel channel) const500*c217d954SCole Faust const RawTensor &AssetsLibrary::get(const std::string &name, Channel channel) const
501*c217d954SCole Faust {
502*c217d954SCole Faust return get(name, get_format_for_channel(channel), channel);
503*c217d954SCole Faust }
504*c217d954SCole Faust
get(const std::string & name,Channel channel)505*c217d954SCole Faust RawTensor AssetsLibrary::get(const std::string &name, Channel channel)
506*c217d954SCole Faust {
507*c217d954SCole Faust return RawTensor(get(name, get_format_for_channel(channel), channel));
508*c217d954SCole Faust }
509*c217d954SCole Faust
get(const std::string & name,Format format,Channel channel) const510*c217d954SCole Faust const RawTensor &AssetsLibrary::get(const std::string &name, Format format, Channel channel) const
511*c217d954SCole Faust {
512*c217d954SCole Faust return find_or_create_raw_tensor(name, format, channel);
513*c217d954SCole Faust }
514*c217d954SCole Faust
get(const std::string & name,Format format,Channel channel)515*c217d954SCole Faust RawTensor AssetsLibrary::get(const std::string &name, Format format, Channel channel)
516*c217d954SCole Faust {
517*c217d954SCole Faust return RawTensor(find_or_create_raw_tensor(name, format, channel));
518*c217d954SCole Faust }
519*c217d954SCole Faust
520*c217d954SCole Faust namespace detail
521*c217d954SCole Faust {
validate_npy_header(std::ifstream & stream,const std::string & expect_typestr,const TensorShape & expect_shape)522*c217d954SCole Faust inline void validate_npy_header(std::ifstream &stream, const std::string &expect_typestr, const TensorShape &expect_shape)
523*c217d954SCole Faust {
524*c217d954SCole Faust ARM_COMPUTE_UNUSED(expect_typestr);
525*c217d954SCole Faust ARM_COMPUTE_UNUSED(expect_shape);
526*c217d954SCole Faust
527*c217d954SCole Faust std::string header_s = npy::read_header(stream);
528*c217d954SCole Faust
529*c217d954SCole Faust // Parse header
530*c217d954SCole Faust npy::header_t header = npy::parse_header(header_s);
531*c217d954SCole Faust
532*c217d954SCole Faust std::vector<unsigned long> shape = header.shape;
533*c217d954SCole Faust bool fortran_order = header.fortran_order;
534*c217d954SCole Faust std::string typestr = header.dtype.str();
535*c217d954SCole Faust
536*c217d954SCole Faust // Check if the typestring matches the given one
537*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(typestr != expect_typestr, "Typestrings mismatch");
538*c217d954SCole Faust
539*c217d954SCole Faust // Validate tensor shape
540*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(shape.size() != expect_shape.num_dimensions(), "Tensor ranks mismatch");
541*c217d954SCole Faust if(fortran_order)
542*c217d954SCole Faust {
543*c217d954SCole Faust for(size_t i = 0; i < shape.size(); ++i)
544*c217d954SCole Faust {
545*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(expect_shape[i] != shape[i], "Tensor dimensions mismatch");
546*c217d954SCole Faust }
547*c217d954SCole Faust }
548*c217d954SCole Faust else
549*c217d954SCole Faust {
550*c217d954SCole Faust for(size_t i = 0; i < shape.size(); ++i)
551*c217d954SCole Faust {
552*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(expect_shape[i] != shape[shape.size() - i - 1], "Tensor dimensions mismatch");
553*c217d954SCole Faust }
554*c217d954SCole Faust }
555*c217d954SCole Faust }
556*c217d954SCole Faust } // namespace detail
557*c217d954SCole Faust } // namespace test
558*c217d954SCole Faust } // namespace arm_compute
559