24 #ifndef __UTILS_UTILS_H__ 25 #define __UTILS_UTILS_H__ 33 #include "libnpy/npy.hpp" 70 virtual void do_setup(
int argc,
char **argv) {};
86 int run_example(
int argc,
char **argv, std::unique_ptr<Example> example);
91 return run_example(argc, argv, support::cpp14::make_unique<T>());
111 std::tuple<unsigned int, unsigned int, int>
parse_ppm_header(std::ifstream &fs);
120 std::tuple<std::vector<unsigned long>, bool, std::string>
parse_npy_header(std::ifstream &fs);
131 const unsigned int i = 1;
132 const char *
c =
reinterpret_cast<const char *
>(&i);
133 std::string endianness;
136 endianness = std::string(
"<");
140 endianness = std::string(
">");
142 const std::string no_endianness(
"|");
179 template <
typename T>
180 inline void map(T &tensor,
bool blocking)
190 template <
typename T>
196 #ifdef ARM_COMPUTE_CL 204 tensor.
map(blocking);
223 distribution.
map(blocking);
232 distribution.
unmap();
236 #ifdef ARM_COMPUTE_GC 244 tensor.
map(blocking);
263 : _fs(), _width(0), _height(0)
270 void open(
const std::string &ppm_filename)
275 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
276 _fs.open(ppm_filename, std::ios::in | std::ios::binary);
278 unsigned int max_val = 0;
281 ARM_COMPUTE_ERROR_ON_MSG(max_val >= 256,
"2 bytes per colour channel not supported in file %s", ppm_filename.c_str());
283 catch(std::runtime_error &e)
292 return _fs.is_open();
300 template <
typename T>
308 image.allocator()->init(image_info);
317 template <
typename T>
321 ARM_COMPUTE_ERROR_ON(image.info()->dimension(0) != _width || image.info()->dimension(1) != _height);
329 const size_t current_position = _fs.tellg();
330 _fs.seekg(0, std::ios_base::end);
331 const size_t end_position = _fs.tellg();
332 _fs.seekg(current_position, std::ios_base::beg);
334 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < image.info()->tensor_shape().total_size() * image.info()->element_size(),
335 "Not enough data in file");
338 switch(image.info()->format())
350 unsigned char red = 0;
351 unsigned char green = 0;
352 unsigned char blue = 0;
360 *out.
ptr() = 0.2126f * red + 0.7152f * green + 0.0722f * blue;
378 _fs.read(reinterpret_cast<std::fstream::char_type *>(out.
ptr()), _width * image.info()->element_size());
391 catch(
const std::ifstream::failure &e)
404 template <
typename T>
410 const DataLayout data_layout = tensor.info()->data_layout();
411 const TensorShape tensor_shape = tensor.info()->tensor_shape();
424 const size_t current_position = _fs.tellg();
425 _fs.seekg(0, std::ios_base::end);
426 const size_t end_position = _fs.tellg();
427 _fs.seekg(current_position, std::ios_base::beg);
430 "Not enough data in file");
443 stride_z = tensor.info()->strides_in_bytes()[2];
450 stride_z = tensor.info()->strides_in_bytes()[0];
455 unsigned char red = 0;
456 unsigned char green = 0;
457 unsigned char blue = 0;
465 switch(tensor.info()->data_type())
469 *(out.
ptr() + 0 * stride_z) = bgr ? blue : red;
470 *(out.
ptr() + 1 * stride_z) = green;
471 *(out.
ptr() + 2 * stride_z) = bgr ? red : blue;
476 *
reinterpret_cast<float *
>(out.
ptr() + 0 * stride_z) = static_cast<float>(bgr ? blue : red);
477 *
reinterpret_cast<float *
>(out.
ptr() + 1 * stride_z) = static_cast<float>(green);
478 *
reinterpret_cast<float *
>(out.
ptr() + 2 * stride_z) = static_cast<float>(bgr ? red : blue);
492 catch(
const std::ifstream::failure &e)
514 unsigned int _width, _height;
523 : _fs(), _shape(), _fortran_order(false), _typestring()
531 void open(
const std::string &npy_filename)
536 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
537 _fs.open(npy_filename, std::ios::in | std::ios::binary);
541 catch(
const std::ifstream::failure &e)
549 return _fs.is_open();
555 return _fortran_order;
563 template <
typename T>
572 for(
size_t i = 0; i < _shape.size(); ++i)
574 shape.
set(i, _shape.at(i));
578 tensor.allocator()->init(tensor_info);
587 template <
typename T>
598 const size_t current_position = _fs.tellg();
599 _fs.seekg(0, std::ios_base::end);
600 const size_t end_position = _fs.tellg();
601 _fs.seekg(current_position, std::ios_base::beg);
603 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
604 "Not enough data in file");
608 std::string expect_typestr =
get_typestring(tensor.info()->data_type());
615 for(
size_t i = 0; i < _shape.size(); ++i)
622 for(
size_t i = 0; i < _shape.size(); ++i)
624 ARM_COMPUTE_ERROR_ON_MSG(tensor.info()->tensor_shape()[i] != _shape[_shape.size() - i - 1],
"Tensor dimensions mismatch");
628 switch(tensor.info()->data_type())
633 if(tensor.info()->padding().empty())
636 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
646 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(
id)), tensor.info()->element_size());
659 catch(
const std::ifstream::failure &e)
667 std::vector<unsigned long> _shape;
669 std::string _typestring;
681 template <
typename T>
691 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
692 fs.open(ppm_filename, std::ios::out | std::ios::binary);
694 const unsigned int width = tensor.info()->tensor_shape()[0];
695 const unsigned int height = tensor.info()->tensor_shape()[1];
698 << width <<
" " << height <<
" 255\n";
703 switch(tensor.info()->format())
715 const unsigned char value = *in.
ptr();
717 fs << value << value << value;
733 fs.write(reinterpret_cast<std::fstream::char_type *>(in.
ptr()), width * tensor.info()->element_size());
746 catch(
const std::ofstream::failure &e)
762 template <
typename T>
763 void save_to_npy(T &tensor,
const std::string &npy_filename,
bool fortran_order)
772 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
773 fs.open(npy_filename, std::ios::out | std::ios::binary);
775 const unsigned int width = tensor.info()->tensor_shape()[0];
776 const unsigned int height = tensor.info()->tensor_shape()[1];
777 std::vector<npy::ndarray_len_t>
shape(2);
781 shape[0] = height, shape[1] = width;
785 shape[0] = width, shape[1] = height;
791 switch(tensor.info()->data_type())
795 std::vector<float> tmp;
796 npy::Typestring typestring_o{ tmp };
797 std::string typestring = typestring_o.str();
799 std::ofstream stream(npy_filename, std::ofstream::binary);
800 npy::write_header(stream, typestring, fortran_order, shape);
810 stream.write(reinterpret_cast<const char *>(in.
ptr()),
sizeof(
float));
823 catch(
const std::ofstream::failure &e)
834 template <
typename T>
843 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
845 fs.open(filename, std::ios::in | std::ios::binary);
849 throw std::runtime_error(
"Could not load binary data: " + filename);
859 for(
unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
868 fs.read(reinterpret_cast<std::fstream::char_type *>(in.
ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
875 catch(
const std::ofstream::failure &e)
881 template <
typename T>
884 std::random_device rd;
885 std::mt19937 gen(rd());
894 switch(tensor.info()->data_type())
898 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
902 *
reinterpret_cast<float *
>(it.
ptr()) = dist(gen);
917 template <
typename T>
920 dst.allocator()->init(
TensorInfo(
TensorShape(src1.info()->dimension(0), src0.info()->dimension(1)), 1, dt));
935 template <
typename T>
941 int num_mismatches = 0;
947 Iterator itensor1(&tensor1, window);
948 Iterator itensor2(&tensor2, window);
952 if(
std::abs(*reinterpret_cast<T *>(itensor1.
ptr()) - *reinterpret_cast<T *>(itensor2.
ptr())) > 0.00001)
962 return num_mismatches;
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.
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
void save_to_ppm(T &tensor, const std::string &ppm_filename)
Template helper function to save a tensor image to a PPM file.
int run_example(int argc, char **argv, std::unique_ptr< Example > example)
Run an example and handle the potential exceptions it throws.
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)
Setup the example.
bool is_fortran()
Return true if a NPY file is in fortran order.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
void init_tensor(T &tensor, arm_compute::DataType dt)
Initialise the tensor's metadata with the dimensions of the NPY file currently open.
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.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
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.
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.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...)
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer.
virtual void do_teardown()
Teardown the example.
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.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
virtual void do_run()
Run the example.
1 channel, 1 U32 per channel
int compare_tensor(ITensor &tensor1, ITensor &tensor2)
Compare to tensor.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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...
quantized, asymmetric fixed-point 8-bit number
NPYLoader()
Default constructor.
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.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
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
Num samples, channels, height, width.
#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.
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.
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.
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
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.
64-bit floating-point number
fixed_point< T > abs(fixed_point< T > x)
void init_image(T &image, arm_compute::Format format)
Initialise an image's metadata with the dimensions of the PPM file currently open.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
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)
DataLayout
Supported tensor data layouts.
Describe a multidimensional execution window.
Class to load the content of a PPM file into an Image.
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Basic implementation of the OpenCL tensor interface.