Compute Library
 21.05
IImageLoader Class Referenceabstract

Image loader interface. More...

#include <ImageLoader.h>

Public Member Functions

 IImageLoader ()
 Default Constructor. More...
 
virtual ~IImageLoader ()=default
 Virtual base destructor. More...
 
unsigned int width () const
 Return the width of the currently open image file. More...
 
unsigned int height () const
 Return the height of the currently open image file. More...
 
virtual bool is_open ()=0
 Return true if the image file is currently open. More...
 
virtual void open (const std::string &filename)=0
 Open an image file and reads its metadata (Width, height) More...
 
virtual void close ()=0
 Closes an image file. More...
 
template<typename T >
void init_image (T &image, Format format)
 Initialise an image's metadata with the dimensions of the image file currently open. More...
 
template<typename T >
void fill_image (T &image)
 Fill an image with the content of the currently open image file. More...
 
template<typename T >
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 image file. More...
 

Detailed Description

Image loader interface.

Definition at line 126 of file ImageLoader.h.

Constructor & Destructor Documentation

◆ IImageLoader()

IImageLoader ( )
inline

Default Constructor.

Definition at line 130 of file ImageLoader.h.

131  : _feeder(nullptr), _width(0), _height(0)
132  {
133  }

◆ ~IImageLoader()

virtual ~IImageLoader ( )
virtualdefault

Virtual base destructor.

Member Function Documentation

◆ close()

virtual void close ( )
pure virtual

Closes an image file.

Implemented in JPEGLoader, and PPMLoader.

◆ fill_image()

void fill_image ( T &  image)
inline

Fill an image with the content of the currently open image file.

Note
If the image is a CLImage, the function maps and unmaps the image
Parameters
[in,out]imageImage to fill (Must be allocated, and of matching dimensions with the opened image file).

Definition at line 177 of file ImageLoader.h.

178  {
180  ARM_COMPUTE_ERROR_ON(image.info()->dimension(0) != _width || image.info()->dimension(1) != _height);
182  ARM_COMPUTE_ERROR_ON(_feeder.get() == nullptr);
183  try
184  {
185  // Map buffer if creating a CLTensor
186  map(image, true);
187 
188  // Validate feeding data
189  validate_info(image.info());
190 
191  switch(image.info()->format())
192  {
193  case Format::U8:
194  {
195  // We need to convert the data from RGB to grayscale:
196  // Iterate through every pixel of the image
197  Window window;
198  window.set(Window::DimX, Window::Dimension(0, _width, 1));
199  window.set(Window::DimY, Window::Dimension(0, _height, 1));
200 
201  Iterator out(&image, window);
202 
203  unsigned char red = 0;
204  unsigned char green = 0;
205  unsigned char blue = 0;
206 
207  execute_window_loop(window, [&](const Coordinates &)
208  {
209  red = _feeder->get();
210  green = _feeder->get();
211  blue = _feeder->get();
212 
213  *out.ptr() = 0.2126f * red + 0.7152f * green + 0.0722f * blue;
214  },
215  out);
216 
217  break;
218  }
219  case Format::RGB888:
220  {
221  // There is no format conversion needed: we can simply copy the content of the input file to the image one row at the time.
222  // Create a vertical window to iterate through the image's rows:
223  Window window;
224  window.set(Window::DimY, Window::Dimension(0, _height, 1));
225 
226  Iterator out(&image, window);
227  size_t row_size = _width * image.info()->element_size();
228 
229  execute_window_loop(window, [&](const Coordinates &)
230  {
231  _feeder->get_row(out.ptr(), row_size);
232  },
233  out);
234 
235  break;
236  }
237  default:
238  ARM_COMPUTE_ERROR("Unsupported format");
239  }
240 
241  // Unmap buffer if creating a CLTensor
242  unmap(image);
243  }
244  catch(const std::ifstream::failure &e)
245  {
246  ARM_COMPUTE_ERROR_VAR("Loading image file: %s", e.what());
247  }
248  }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
#define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t,...)
Definition: Validate.h:639
virtual bool is_open()=0
Return true if the image file is currently open.
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:223
3 channels, 1 U8 per channel
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:212
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...
Definition: Helpers.inl:77

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN, ARM_COMPUTE_ERROR_VAR, Window::DimX, Window::DimY, arm_compute::execute_window_loop(), caffe_mnist_image_extractor::image, IImageLoader::is_open(), arm_compute::utils::map(), Iterator::ptr(), arm_compute::RGB888, Window::set(), arm_compute::U8, and arm_compute::utils::unmap().

◆ fill_planar_tensor()

void fill_planar_tensor ( T &  tensor,
bool  bgr = false 
)
inline

Fill a tensor with 3 planes (one for each channel) with the content of the currently open image file.

Note
If the image is a CLImage, the function maps and unmaps the image
Parameters
[in,out]tensorTensor with 3 planes to fill (Must be allocated, and of matching dimensions with the opened image). Data types supported: U8/F16/F32
[in]bgr(Optional) Fill the first plane with blue channel (default = false)

Definition at line 257 of file ImageLoader.h.

258  {
261 
262  const DataLayout data_layout = tensor.info()->data_layout();
263  const TensorShape tensor_shape = tensor.info()->tensor_shape();
264 
265  ARM_COMPUTE_UNUSED(tensor_shape);
269 
270  ARM_COMPUTE_ERROR_ON(_feeder.get() == nullptr);
271 
272  try
273  {
274  // Map buffer if creating a CLTensor
275  map(tensor, true);
276 
277  // Validate feeding data
278  validate_info(tensor.info());
279 
280  // Stride across channels
281  size_t stride_z = 0;
282 
283  // Iterate through every pixel of the image
284  Window window;
286  {
287  window.set(Window::DimX, Window::Dimension(0, _width, 1));
288  window.set(Window::DimY, Window::Dimension(0, _height, 1));
289  window.set(Window::DimZ, Window::Dimension(0, 1, 1));
290  stride_z = tensor.info()->strides_in_bytes()[2];
291  }
292  else
293  {
294  window.set(Window::DimX, Window::Dimension(0, 1, 1));
295  window.set(Window::DimY, Window::Dimension(0, _width, 1));
296  window.set(Window::DimZ, Window::Dimension(0, _height, 1));
297  stride_z = tensor.info()->strides_in_bytes()[0];
298  }
299 
300  Iterator out(&tensor, window);
301 
302  unsigned char red = 0;
303  unsigned char green = 0;
304  unsigned char blue = 0;
305 
306  execute_window_loop(window, [&](const Coordinates &)
307  {
308  red = _feeder->get();
309  green = _feeder->get();
310  blue = _feeder->get();
311 
312  switch(tensor.info()->data_type())
313  {
314  case DataType::U8:
315  case DataType::QASYMM8:
316  {
317  *(out.ptr() + 0 * stride_z) = bgr ? blue : red;
318  *(out.ptr() + 1 * stride_z) = green;
319  *(out.ptr() + 2 * stride_z) = bgr ? red : blue;
320  break;
321  }
322  case DataType::F32:
323  {
324  *reinterpret_cast<float *>(out.ptr() + 0 * stride_z) = static_cast<float>(bgr ? blue : red);
325  *reinterpret_cast<float *>(out.ptr() + 1 * stride_z) = static_cast<float>(green);
326  *reinterpret_cast<float *>(out.ptr() + 2 * stride_z) = static_cast<float>(bgr ? red : blue);
327  break;
328  }
329  case DataType::F16:
330  {
331  *reinterpret_cast<half *>(out.ptr() + 0 * stride_z) = static_cast<half>(bgr ? blue : red);
332  *reinterpret_cast<half *>(out.ptr() + 1 * stride_z) = static_cast<half>(green);
333  *reinterpret_cast<half *>(out.ptr() + 2 * stride_z) = static_cast<half>(bgr ? red : blue);
334  break;
335  }
336  default:
337  {
338  ARM_COMPUTE_ERROR("Unsupported data type");
339  }
340  }
341  },
342  out);
343 
344  // Unmap buffer if creating a CLTensor
345  unmap(tensor);
346  }
347  catch(const std::ifstream::failure &e)
348  {
349  ARM_COMPUTE_ERROR_VAR("Loading image file: %s", e.what());
350  }
351  }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
const DataLayout data_layout
Definition: Im2Col.cpp:151
virtual bool is_open()=0
Return true if the image file is currently open.
1 channel, 1 F16 per channel
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:223
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
quantized, asymmetric fixed-point 8-bit number unsigned
Num samples, channels, height, width.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:786
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:212
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
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...
Definition: Helpers.inl:77
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
DataLayout
[DataLayout enum definition]
Definition: Types.h:114

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_VAR, ARM_COMPUTE_UNUSED, arm_compute::CHANNEL, arm_compute::test::validation::data_layout, Window::DimX, Window::DimY, Window::DimZ, arm_compute::execute_window_loop(), arm_compute::F16, arm_compute::F32, arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, IImageLoader::is_open(), arm_compute::utils::map(), arm_compute::NCHW, Iterator::ptr(), arm_compute::QASYMM8, Window::set(), arm_compute::U8, arm_compute::utils::unmap(), and arm_compute::WIDTH.

Referenced by ValidationInputAccessor::access_tensor().

◆ height()

unsigned int height ( ) const
inline

Return the height of the currently open image file.

Definition at line 142 of file ImageLoader.h.

143  {
144  return _height;
145  }

Referenced by ValidationInputAccessor::access_tensor(), and JPEGLoader::open().

◆ init_image()

void init_image ( T &  image,
Format  format 
)
inline

Initialise an image's metadata with the dimensions of the image file currently open.

Parameters
[out]imageImage to initialise
[in]formatFormat to use for the image (Must be RGB888 or U8)

Definition at line 161 of file ImageLoader.h.

162  {
164  ARM_COMPUTE_ERROR_ON(format != Format::RGB888 && format != Format::U8);
165 
166  // Use the size of the input image
167  TensorInfo image_info(_width, _height, format);
168  image.allocator()->init(image_info);
169  }
1 channel, 1 U8 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
virtual bool is_open()=0
Return true if the image file is currently open.
3 channels, 1 U8 per channel

References ARM_COMPUTE_ERROR_ON, caffe_mnist_image_extractor::image, IImageLoader::is_open(), arm_compute::RGB888, and arm_compute::U8.

◆ is_open()

virtual bool is_open ( )
pure virtual

Return true if the image file is currently open.

Implemented in JPEGLoader, and PPMLoader.

Referenced by IImageLoader::fill_image(), IImageLoader::fill_planar_tensor(), and IImageLoader::init_image().

◆ open()

virtual void open ( const std::string &  filename)
pure virtual

Open an image file and reads its metadata (Width, height)

Parameters
[in]filenameFile to open

Implemented in JPEGLoader, and PPMLoader.

◆ width()

unsigned int width ( ) const
inline

Return the width of the currently open image file.

Definition at line 137 of file ImageLoader.h.

138  {
139  return _width;
140  }

Referenced by ValidationInputAccessor::access_tensor(), and JPEGLoader::open().


The documentation for this class was generated from the following file: