24 #ifndef __UTILS_UTILS_H__ 25 #define __UTILS_UTILS_H__ 33 #include "libnpy/npy.hpp" 65 virtual void do_setup(
int argc,
char **argv) {};
105 std::tuple<unsigned int, unsigned int, int>
parse_ppm_header(std::ifstream &fs);
114 std::tuple<std::vector<unsigned long>, bool, std::string>
parse_npy_header(std::ifstream &fs);
125 const unsigned int i = 1;
126 const char *
c =
reinterpret_cast<const char *
>(&i);
127 std::string endianness;
130 endianness = std::string(
"<");
134 endianness = std::string(
">");
136 const std::string no_endianness(
"|");
173 template <
typename T>
174 inline void map(T &tensor,
bool blocking)
184 template <
typename T>
190 #ifdef ARM_COMPUTE_CL 198 tensor.
map(blocking);
217 distribution.
map(blocking);
226 distribution.
unmap();
230 #ifdef ARM_COMPUTE_GC 238 tensor.
map(blocking);
257 : _fs(), _width(0), _height(0)
264 void open(
const std::string &ppm_filename)
269 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
270 _fs.open(ppm_filename, std::ios::in | std::ios::binary);
272 unsigned int max_val = 0;
275 ARM_COMPUTE_ERROR_ON_MSG(max_val >= 256,
"2 bytes per colour channel not supported in file %s", ppm_filename.c_str());
277 catch(std::runtime_error &e)
286 return _fs.is_open();
294 template <
typename T>
302 image.allocator()->init(image_info);
311 template <
typename T>
315 ARM_COMPUTE_ERROR_ON(image.info()->dimension(0) != _width || image.info()->dimension(1) != _height);
323 const size_t current_position = _fs.tellg();
324 _fs.seekg(0, std::ios_base::end);
325 const size_t end_position = _fs.tellg();
326 _fs.seekg(current_position, std::ios_base::beg);
328 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < image.info()->tensor_shape().total_size() * image.info()->element_size(),
329 "Not enough data in file");
332 switch(image.info()->format())
344 unsigned char red = 0;
345 unsigned char green = 0;
346 unsigned char blue = 0;
354 *out.
ptr() = 0.2126f * red + 0.7152f * green + 0.0722f * blue;
372 _fs.read(reinterpret_cast<std::fstream::char_type *>(out.
ptr()), _width * image.info()->element_size());
385 catch(
const std::ifstream::failure &e)
398 template <
typename T>
403 ARM_COMPUTE_ERROR_ON(tensor.info()->dimension(0) != _width || tensor.info()->dimension(1) != _height || tensor.info()->dimension(2) != 3);
411 const size_t current_position = _fs.tellg();
412 _fs.seekg(0, std::ios_base::end);
413 const size_t end_position = _fs.tellg();
414 _fs.seekg(current_position, std::ios_base::beg);
417 "Not enough data in file");
428 unsigned char red = 0;
429 unsigned char green = 0;
430 unsigned char blue = 0;
432 size_t stride_z = tensor.info()->strides_in_bytes()[2];
440 switch(tensor.info()->data_type())
444 *(out.
ptr() + 0 * stride_z) = bgr ? blue : red;
445 *(out.
ptr() + 1 * stride_z) = green;
446 *(out.
ptr() + 2 * stride_z) = bgr ? red : blue;
451 *
reinterpret_cast<float *
>(out.
ptr() + 0 * stride_z) = static_cast<float>(bgr ? blue : red);
452 *
reinterpret_cast<float *
>(out.
ptr() + 1 * stride_z) = static_cast<float>(green);
453 *
reinterpret_cast<float *
>(out.
ptr() + 2 * stride_z) = static_cast<float>(bgr ? red : blue);
467 catch(
const std::ifstream::failure &e)
489 unsigned int _width, _height;
496 : _fs(), _shape(), _fortran_order(false), _typestring()
504 void open(
const std::string &npy_filename)
509 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
510 _fs.open(npy_filename, std::ios::in | std::ios::binary);
514 catch(
const std::ifstream::failure &e)
522 return _fs.is_open();
528 return _fortran_order;
536 template <
typename T>
545 for(
size_t i = 0; i < _shape.size(); ++i)
547 shape.
set(i, _shape.at(i));
551 tensor.allocator()->init(tensor_info);
560 template <
typename T>
571 const size_t current_position = _fs.tellg();
572 _fs.seekg(0, std::ios_base::end);
573 const size_t end_position = _fs.tellg();
574 _fs.seekg(current_position, std::ios_base::beg);
576 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
577 "Not enough data in file");
581 std::string expect_typestr =
get_typestring(tensor.info()->data_type());
588 for(
size_t i = 0; i < _shape.size(); ++i)
595 for(
size_t i = 0; i < _shape.size(); ++i)
597 ARM_COMPUTE_ERROR_ON_MSG(tensor.info()->tensor_shape()[i] != _shape[_shape.size() - i - 1],
"Tensor dimensions mismatch");
601 switch(tensor.info()->data_type())
606 if(tensor.info()->padding().empty())
609 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
619 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(
id)), tensor.info()->element_size());
632 catch(
const std::ifstream::failure &e)
640 std::vector<unsigned long> _shape;
642 std::string _typestring;
654 template <
typename T>
664 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
665 fs.open(ppm_filename, std::ios::out | std::ios::binary);
667 const unsigned int width = tensor.info()->tensor_shape()[0];
668 const unsigned int height = tensor.info()->tensor_shape()[1];
671 << width <<
" " << height <<
" 255\n";
676 switch(tensor.info()->format())
688 const unsigned char value = *in.
ptr();
690 fs << value << value <<
value;
706 fs.write(reinterpret_cast<std::fstream::char_type *>(in.
ptr()), width * tensor.info()->element_size());
719 catch(
const std::ofstream::failure &e)
735 template <
typename T>
736 void save_to_npy(T &tensor,
const std::string &npy_filename,
bool fortran_order)
745 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
746 fs.open(npy_filename, std::ios::out | std::ios::binary);
748 const unsigned int width = tensor.info()->tensor_shape()[0];
749 const unsigned int height = tensor.info()->tensor_shape()[1];
750 std::vector<npy::ndarray_len_t>
shape(2);
754 shape[0] = height, shape[1] = width;
758 shape[0] = width, shape[1] = height;
764 switch(tensor.info()->data_type())
768 std::vector<float> tmp;
769 npy::Typestring typestring_o{ tmp };
770 std::string typestring = typestring_o.str();
772 std::ofstream stream(npy_filename, std::ofstream::binary);
773 npy::write_header(stream, typestring, fortran_order, shape);
783 stream.write(reinterpret_cast<const char *>(in.
ptr()),
sizeof(
float));
796 catch(
const std::ofstream::failure &e)
807 template <
typename T>
816 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
818 fs.open(filename, std::ios::in | std::ios::binary);
822 throw std::runtime_error(
"Could not load binary data: " + filename);
832 for(
unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
841 fs.read(reinterpret_cast<std::fstream::char_type *>(in.
ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
848 catch(
const std::ofstream::failure &e)
854 template <
typename T>
857 std::random_device rd;
858 std::mt19937 gen(rd());
860 TensorShape shape(tensor.info()->dimension(0), tensor.info()->dimension(1));
870 switch(tensor.info()->data_type())
874 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
878 *
reinterpret_cast<float *
>(it.
ptr()) = dist(gen);
893 template <
typename T>
896 dst.allocator()->init(
TensorInfo(
TensorShape(src1.info()->dimension(0), src0.info()->dimension(1)), 1, dt));
unsigned int height() const
Return the height of the currently open PPM file.
void fill_image(T &image)
Fill an image with the content of the currently open PPM file.
void save_to_ppm(T &tensor, const std::string &ppm_filename)
Template helper function to save a tensor image to a PPM file.
uint64_t get_mem_free_from_meminfo()
This function returns the amount of memory free reading from /proc/meminfo.
virtual void do_setup(int argc, char **argv)
bool is_fortran()
Return true if a NPY file is in fortran order.
int run_example(int argc, char **argv, Example &example)
Run an example and handle the potential exceptions it throws.
void init_tensor(T &tensor, arm_compute::DataType dt)
Initialise the tensor's metadata with the dimensions of the NPY file currently open.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
1 channel, 1 U8 per channel
std::string to_string(T &&value)
Convert integer and float values to string.
#define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t,...)
void fill_tensor(T &tensor)
Fill a tensor with the content of the currently open NPY file.
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
1 channel, 1 F32 per channel
Describe one of the image's dimensions with a start, end and step.
1 channel, 1 U16 per channel
Interface for NEON tensor.
Interface for OpenGL ES tensor.
CLDistribution1D object class.
#define ARM_COMPUTE_UNUSED(var)
To avoid unused variables warnings.
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
This file contains all available output stages for GEMMLowp on OpenCL.
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer.
virtual void do_teardown()
void unmap(T &tensor)
Unmaps a tensor if needed.
void fill_random_tensor(T &tensor, float lower_bound, float upper_bound)
1 channel, 1 S32 per channel
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
3 channels, 1 U8 per channel
std::string get_typestring(DataType data_type)
Obtain numpy type string from DataType.
void open(const std::string &ppm_filename)
Open a PPM file and reads its metadata (Width, height)
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
1 channel, 1 U32 per channel
Format
Image colour formats.
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&...iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer.
std::tuple< std::vector< unsigned long >, bool, std::string > parse_npy_header(std::ifstream &fs)
Parse the npy header from an input file stream.
bool is_open()
Return true if a PPM file is currently open.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer.
bool is_open()
Return true if a NPY file is currently open.
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
Template helper function to save a tensor image to a NPY file.
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer on the given queue. ...
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
TensorShape & set(size_t dimension, size_t value)
Accessor to set the value of one of the dimensions.
virtual ~Example()=default
Default destructor.
void map(T &tensor, bool blocking)
Maps a tensor if needed.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
void fill_planar_tensor(T &tensor, bool bgr=false)
Fill a tensor with 3 planes (one for each channel) with the content of the currently open PPM file...
Detection window used for the object detection.
void draw_detection_rectangle(ITensor *tensor, const DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b)
Draw a RGB rectangular window for the detected object.
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer on the given queue.
std::tuple< unsigned int, unsigned int, int > parse_ppm_header(std::ifstream &fs)
Parse the ppm header from an input file stream.
Store the tensor's metadata.
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
void load_trained_data(T &tensor, const std::string &filename)
Load the tensor with pre-trained data from a binary file.
void open(const std::string &npy_filename)
Open a NPY file and reads its metadata.
void init_image(T &image, arm_compute::Format format)
Initialise an image's metadata with the dimensions of the PPM file currently open.
unsigned int width() const
Return the width of the currently open PPM file.
Iterator updated by execute_window_loop for each window element.
DataType
Available data types.
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer.
void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt)
Describe a multidimensional execution window.
Class to load the content of a PPM file into an Image.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Basic implementation of the OpenCL tensor interface.