Compute Library
 21.08
ValidationInputAccessor Class Referencefinal

Input Accessor used for network validation. More...

#include <GraphUtils.h>

Collaboration diagram for ValidationInputAccessor:
[legend]

Public Member Functions

 ValidationInputAccessor (const std::string &image_list, std::string images_path, std::unique_ptr< IPreprocessor > preprocessor=nullptr, bool bgr=true, unsigned int start=0, unsigned int end=0, std::ostream &output_stream=std::cout)
 Constructor. More...
 
bool access_tensor (ITensor &tensor) override
 Interface to be implemented to access a given tensor. More...
 
- Public Member Functions inherited from ITensorAccessor
virtual ~ITensorAccessor ()=default
 Default virtual destructor. More...
 

Detailed Description

Input Accessor used for network validation.

Definition at line 254 of file GraphUtils.h.

Constructor & Destructor Documentation

◆ ValidationInputAccessor()

ValidationInputAccessor ( const std::string &  image_list,
std::string  images_path,
std::unique_ptr< IPreprocessor preprocessor = nullptr,
bool  bgr = true,
unsigned int  start = 0,
unsigned int  end = 0,
std::ostream &  output_stream = std::cout 
)

Constructor.

Parameters
[in]image_listFile containing all the images to validate
[in]images_pathPath to images.
[in]bgr(Optional) Fill the first plane with blue channel (default = false - RGB format)
[in]preprocessor(Optional) Image pre-processing object (default = nullptr)
[in]start(Optional) Start range
[in]end(Optional) End range
[out]output_stream(Optional) Output stream
Note
Range is defined as [start, end]

Definition at line 301 of file GraphUtils.cpp.

References ARM_COMPUTE_ERROR_VAR, ARM_COMPUTE_EXIT_ON_MSG, and arm_compute::mlgo::parser::end().

308  : _path(std::move(images_path)), _images(), _preprocessor(std::move(preprocessor)), _bgr(bgr), _offset(0), _output_stream(output_stream)
309 {
310  ARM_COMPUTE_EXIT_ON_MSG(start > end, "Invalid validation range!");
311 
312  std::ifstream ifs;
313  try
314  {
315  ifs.exceptions(std::ifstream::badbit);
316  ifs.open(image_list, std::ios::in | std::ios::binary);
317 
318  // Parse image names
319  unsigned int counter = 0;
320  for(std::string line; !std::getline(ifs, line).fail() && counter <= end; ++counter)
321  {
322  // Add image to process if withing range
323  if(counter >= start)
324  {
325  std::stringstream linestream(line);
326  std::string image_name;
327 
328  linestream >> image_name;
329  _images.emplace_back(std::move(image_name));
330  }
331  }
332  }
333  catch(const std::ifstream::failure &e)
334  {
335  ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", image_list.c_str(), e.what());
336  }
337 }
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
#define ARM_COMPUTE_EXIT_ON_MSG(cond, msg)
If the condition is true, the given message is printed and program exits.
Definition: Error.h:379
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290

Member Function Documentation

◆ access_tensor()

bool access_tensor ( ITensor tensor)
overridevirtual

Interface to be implemented to access a given tensor.

Parameters
[in]tensorTensor to be accessed
Returns
True if access is successful else false

Implements ITensorAccessor.

Definition at line 339 of file GraphUtils.cpp.

References ARM_COMPUTE_EXIT_ON_MSG_VAR, ITensorInfo::data_layout(), IImageLoader::fill_planar_tensor(), IImageLoader::height(), ITensor::info(), arm_compute::NCHW, JPEGLoader::open(), ITensorInfo::tensor_shape(), IImageLoader::width(), Dimensions< T >::x(), and Dimensions< T >::y().

340 {
341  bool ret = _offset < _images.size();
342  if(ret)
343  {
344  utils::JPEGLoader jpeg;
345 
346  // Open JPEG file
347  std::string image_name = _path + _images[_offset++];
348  jpeg.open(image_name);
349  _output_stream << "[" << _offset << "/" << _images.size() << "] Validating " << image_name << std::endl;
350 
351  // Get permutated shape and permutation parameters
352  TensorShape permuted_shape = tensor.info()->tensor_shape();
354  if(tensor.info()->data_layout() != DataLayout::NCHW)
355  {
356  std::tie(permuted_shape, perm) = compute_permutation_parameters(tensor.info()->tensor_shape(),
357  tensor.info()->data_layout());
358  }
359 
360 #ifdef __arm__
361  ARM_COMPUTE_EXIT_ON_MSG_VAR(jpeg.width() != permuted_shape.x() || jpeg.height() != permuted_shape.y(),
362  "Failed to load image file: dimensions [%d,%d] not correct, expected [%" PRIu32 ",%" PRIu32 "].",
363  jpeg.width(), jpeg.height(), permuted_shape.x(), permuted_shape.y());
364 #else // __arm__
365  ARM_COMPUTE_EXIT_ON_MSG_VAR(jpeg.width() != permuted_shape.x() || jpeg.height() != permuted_shape.y(),
366  "Failed to load image file: dimensions [%d,%d] not correct, expected [%" PRIu64 ",%" PRIu64 "].",
367  jpeg.width(), jpeg.height(),
368  static_cast<uint64_t>(permuted_shape.x()), static_cast<uint64_t>(permuted_shape.y()));
369 #endif // __arm__
370 
371  // Fill the tensor with the JPEG content (BGR)
372  jpeg.fill_planar_tensor(tensor, _bgr);
373 
374  // Preprocess tensor
375  if(_preprocessor)
376  {
377  _preprocessor->preprocess(tensor);
378  }
379  }
380 
381  return ret;
382 }
#define ARM_COMPUTE_EXIT_ON_MSG_VAR(cond, msg,...)
If the condition is true, the given message is printed and program exits.
Definition: Error.h:395
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Num samples, channels, height, width.
Strides of an item in bytes.
Definition: Strides.h:37
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.

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