39 #include <unordered_map> 48 template <typename T, typename std::enable_if<std::is_integral<T>::value,
int>::type = 0>
49 void rgb_to_luminance(
const RawTensor &
src, RawTensor &
dst)
52 ARM_COMPUTE_ERROR_ON_MSG(src.num_elements() != dst.num_elements(),
"Input and output images must have equal dimensions");
54 const size_t num_elements = dst.num_elements();
58 for(
size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
60 reinterpret_cast<T *
>(dst.data())[j] = 0.2126f * src.data()[i] + 0.7152f * src.data()[i + 1] + 0.0722f * src.data()[i + 2];
64 void extract_r_from_rgb(
const RawTensor &src, RawTensor &dst)
68 const size_t num_elements = dst.num_elements();
70 for(
size_t i = 0, j = 0; j < num_elements; i += 3, ++j)
72 dst.data()[j] = src.data()[i];
76 void extract_g_from_rgb(
const RawTensor &src, RawTensor &dst)
80 const size_t num_elements = dst.num_elements();
82 for(
size_t i = 1, j = 0; j < num_elements; i += 3, ++j)
84 dst.data()[j] = src.data()[i];
88 void extract_b_from_rgb(
const RawTensor &src, RawTensor &dst)
92 const size_t num_elements = dst.num_elements();
94 for(
size_t i = 2, j = 0; j < num_elements; i += 3, ++j)
96 dst.data()[j] = src.data()[i];
100 void discard_comments(std::ifstream &fs)
102 while(fs.peek() ==
'#')
108 void discard_comments_and_spaces(std::ifstream &fs)
112 discard_comments(fs);
114 if(isspace(fs.peek()) == 0)
123 std::tuple<unsigned int, unsigned int, int> parse_netpbm_format_header(std::ifstream &fs,
char number)
126 std::array<char, 2> magic_number{ { 0 } };
127 fs >> magic_number[0] >> magic_number[1];
129 if(magic_number[0] !=
'P' || magic_number[1] != number)
131 throw std::runtime_error(
"File type magic number not supported");
134 discard_comments_and_spaces(fs);
136 unsigned int width = 0;
139 discard_comments_and_spaces(fs);
141 unsigned int height = 0;
144 discard_comments_and_spaces(fs);
151 throw std::runtime_error(
"Cannot read image dimensions");
156 throw std::runtime_error(
"RawTensor doesn't have 8-bit values");
159 discard_comments(fs);
161 if(isspace(fs.peek()) == 0)
163 throw std::runtime_error(
"Invalid image header");
168 return std::make_tuple(width, height, max_value);
171 std::tuple<unsigned int, unsigned int, int>
parse_ppm_header(std::ifstream &fs)
173 return parse_netpbm_format_header(fs,
'6');
176 std::tuple<unsigned int, unsigned int, int> parse_pgm_header(std::ifstream &fs)
178 return parse_netpbm_format_header(fs,
'5');
181 void check_image_size(std::ifstream &fs,
size_t raw_size)
183 const size_t current_position = fs.tellg();
184 fs.seekg(0, std::ios_base::end);
185 const size_t end_position = fs.tellg();
186 fs.seekg(current_position, std::ios_base::beg);
188 if((end_position - current_position) < raw_size)
190 throw std::runtime_error(
"Not enough data in file");
194 void read_image_buffer(std::ifstream &fs, RawTensor &raw)
196 fs.read(reinterpret_cast<std::fstream::char_type *>(raw.data()), raw.size());
200 throw std::runtime_error(
"Failure while reading image buffer");
204 RawTensor load_ppm(
const std::string &path)
206 std::ifstream file(path, std::ios::in | std::ios::binary);
210 throw framework::FileNotFound(
"Could not load PPM image: " + path);
213 unsigned int width = 0;
214 unsigned int height = 0;
220 check_image_size(file, raw.size());
221 read_image_buffer(file, raw);
226 RawTensor load_pgm(
const std::string &path)
228 std::ifstream file(path, std::ios::in | std::ios::binary);
232 throw framework::FileNotFound(
"Could not load PGM image: " + path);
235 unsigned int width = 0;
236 unsigned int height = 0;
238 std::tie(width, height, std::ignore) = parse_pgm_header(file);
240 RawTensor raw(TensorShape(width, height),
Format::U8);
242 check_image_size(file, raw.size());
243 read_image_buffer(file, raw);
250 : _library_path(std::move(path)),
257 return _library_path;
267 const RawTensor &src =
get(name, format);
278 const RawTensor &src =
get(name, format, channel);
282 const AssetsLibrary::Loader &AssetsLibrary::get_loader(
const std::string &extension)
const 284 static std::unordered_map<std::string, Loader> loaders =
290 const auto it = loaders.find(extension);
292 if(it != loaders.end())
298 throw std::invalid_argument(
"Cannot load image with extension '" + extension +
"'");
302 const AssetsLibrary::Converter &AssetsLibrary::get_converter(
Format src,
Format dst)
const 304 static std::map<std::pair<Format, Format>, Converter> converters =
312 const auto it = converters.find(std::make_pair(src, dst));
314 if(it != converters.end())
320 std::stringstream msg;
321 msg <<
"Cannot convert from format '" << src <<
"' to format '" << dst <<
"'\n";
322 throw std::invalid_argument(msg.str());
326 const AssetsLibrary::Converter &AssetsLibrary::get_converter(
DataType src,
Format dst)
const 328 static std::map<std::pair<DataType, Format>, Converter> converters = {};
330 const auto it = converters.find(std::make_pair(src, dst));
332 if(it != converters.end())
338 std::stringstream msg;
339 msg <<
"Cannot convert from data type '" << src <<
"' to format '" << dst <<
"'\n";
340 throw std::invalid_argument(msg.str());
344 const AssetsLibrary::Converter &AssetsLibrary::get_converter(
DataType src,
DataType dst)
const 346 static std::map<std::pair<DataType, DataType>, Converter> converters = {};
348 const auto it = converters.find(std::make_pair(src, dst));
350 if(it != converters.end())
356 std::stringstream msg;
357 msg <<
"Cannot convert from data type '" << src <<
"' to data type '" << dst <<
"'\n";
358 throw std::invalid_argument(msg.str());
362 const AssetsLibrary::Converter &AssetsLibrary::get_converter(
Format src,
DataType dst)
const 364 static std::map<std::pair<Format, DataType>, Converter> converters = {};
366 const auto it = converters.find(std::make_pair(src, dst));
368 if(it != converters.end())
374 std::stringstream msg;
375 msg <<
"Cannot convert from format '" << src <<
"' to data type '" << dst <<
"'\n";
376 throw std::invalid_argument(msg.str());
380 const AssetsLibrary::Extractor &AssetsLibrary::get_extractor(
Format format,
Channel channel)
const 382 static std::map<std::pair<Format, Channel>, Extractor> extractors =
389 const auto it = extractors.find(std::make_pair(format, channel));
391 if(it != extractors.end())
397 std::stringstream msg;
398 msg <<
"Cannot extract channel '" << channel <<
"' from format '" << format <<
"'\n";
399 throw std::invalid_argument(msg.str());
403 RawTensor AssetsLibrary::load_image(
const std::string &name)
const 406 const std::string image_path = (
"\\images\\");
408 const std::string image_path = (
"/images/");
411 const std::string path = _library_path + image_path + name;
412 const std::string extension = path.substr(path.find_last_of(
'.') + 1);
413 return (*get_loader(extension))(path);
416 const RawTensor &AssetsLibrary::find_or_create_raw_tensor(
const std::string &name,
Format format)
const 418 std::lock_guard<std::mutex> guard(_format_lock);
420 const RawTensor *ptr = _cache.
find(std::forward_as_tuple(name, format));
429 if(raw.
format() != format)
432 (*get_converter(raw.
format(), format))(raw,
dst);
433 raw = std::move(dst);
436 return _cache.
add(std::forward_as_tuple(name, format), std::move(raw));
439 const RawTensor &AssetsLibrary::find_or_create_raw_tensor(
const std::string &name,
Format format,
Channel channel)
const 441 std::lock_guard<std::mutex> guard(_channel_lock);
443 const RawTensor *ptr = _cache.
find(std::forward_as_tuple(name, format, channel));
450 const RawTensor &src =
get(name, format);
453 (*get_extractor(format, channel))(src, dst);
455 return _cache.
add(std::forward_as_tuple(name, format, channel), std::move(dst));
460 return load_image(name).
shape();
482 return find_or_create_raw_tensor(name, format);
487 return RawTensor(find_or_create_raw_tensor(name, format));
502 return find_or_create_raw_tensor(name, format, channel);
507 return RawTensor(find_or_create_raw_tensor(name, format, channel));
Subclass of SimpleTensor using uint8_t as value type.
void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Fills the specified tensor with random values drawn from distribution.
size_t size() const override
Total size of the tensor in bytes.
Format get_format_for_channel(Channel channel)
Look up the format corresponding to a channel.
1 channel, 1 U8 per channel
void fill(RawTensor &raw, const std::string &name, Channel channel) const
Fills the raw tensor with the content of the specified channel extracted from the given image...
Format format() const override
Image format of the tensor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
1 channel, 1 U16 per channel
std::random_device::result_type seed() const
Seed that is used to fill tensors with random values.
TensorShape shape() const override
Shape of the tensor.
TensorShape get_image_shape(const std::string &name)
Provides a tensor shape for the specified image.
This file contains all available output stages for GEMMLowp on OpenCL.
std::string path() const
Path to assets directory used to initialise library.
3 channels, 1 U8 per channel
Format get_channel_format(Channel channel)
Return the format of a channel.
const T * data() const
Constant pointer to the underlying buffer.
1 channel, 1 U32 per channel
Channel
Available channels.
Format
Image colour formats.
const RawTensor & get(const std::string &name) const
Provides a constant raw tensor for the specified image.
1 channel, 1 S16 per channel
RawTensor & add(std::tuple< const std::string &, Format > key, RawTensor raw)
Add the given tensor to the cache.
RawTensor * find(std::tuple< const std::string &, Format > key)
Search the cache for a tensor of created from the specified image and format.
std::tuple< unsigned int, unsigned int, int > parse_ppm_header(std::ifstream &fs)
Parse the ppm header from an input file stream.
fixed_point< T > max(fixed_point< T > x, fixed_point< T > y)
DataType
Available data types.
AssetsLibrary(std::string path, std::random_device::result_type seed)
Initialises the library with a path to the assets directory.
convolution configure & src
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)