ArmNN
 20.08
InferenceTestImage Class Reference

#include <InferenceTestImage.hpp>

Public Types

enum  WriteFormat { Png, Bmp, Tga }
 
enum  ResizingMethods { STB, BilinearAndNormalized }
 

Public Member Functions

 InferenceTestImage (const char *filePath)
 
 InferenceTestImage (InferenceTestImage &&)=delete
 
 InferenceTestImage (const InferenceTestImage &)=delete
 
InferenceTestImageoperator= (const InferenceTestImage &)=delete
 
InferenceTestImageoperator= (InferenceTestImage &&)=delete
 
unsigned int GetWidth () const
 
unsigned int GetHeight () const
 
unsigned int GetNumChannels () const
 
unsigned int GetNumElements () const
 
unsigned int GetSizeInBytes () const
 
std::tuple< uint8_t, uint8_t, uint8_t > GetPixelAs3Channels (unsigned int x, unsigned int y) const
 
void StbResize (InferenceTestImage &im, const unsigned int newWidth, const unsigned int newHeight)
 
std::vector< float > Resize (unsigned int newWidth, unsigned int newHeight, const armnn::CheckLocation &location, const ResizingMethods meth=ResizingMethods::STB, const std::array< float, 3 > &mean={{0.0, 0.0, 0.0}}, const std::array< float, 3 > &stddev={{1.0, 1.0, 1.0}}, const float scale=255.0f)
 
void Write (WriteFormat format, const char *filePath) const
 

Detailed Description

Definition at line 51 of file InferenceTestImage.hpp.

Member Enumeration Documentation

◆ ResizingMethods

enum ResizingMethods
strong
Enumerator
STB 
BilinearAndNormalized 

Definition at line 62 of file InferenceTestImage.hpp.

63  {
64  STB,
65  BilinearAndNormalized,
66  };

◆ WriteFormat

enum WriteFormat
strong
Enumerator
Png 
Bmp 
Tga 

Definition at line 54 of file InferenceTestImage.hpp.

55  {
56  Png,
57  Bmp,
58  Tga
59  };

Constructor & Destructor Documentation

◆ InferenceTestImage() [1/3]

InferenceTestImage ( const char *  filePath)
explicit

Definition at line 128 of file InferenceTestImage.cpp.

References GetSizeInBytes(), and armnn::numeric_cast().

129  : m_Width(0u)
130  , m_Height(0u)
131  , m_NumChannels(0u)
132 {
133  int width;
134  int height;
135  int channels;
136 
137  using StbImageDataPtr = std::unique_ptr<unsigned char, decltype(&stbi_image_free)>;
138  StbImageDataPtr stbData(stbi_load(filePath, &width, &height, &channels, 0), &stbi_image_free);
139 
140  if (stbData == nullptr)
141  {
142  throw InferenceTestImageLoadFailed(boost::str(boost::format("Could not load the image at %1%") % filePath));
143  }
144 
145  if (width == 0 || height == 0)
146  {
147  throw InferenceTestImageLoadFailed(boost::str(boost::format("Could not load empty image at %1%") % filePath));
148  }
149 
150  m_Width = boost::numeric_cast<unsigned int>(width);
151  m_Height = boost::numeric_cast<unsigned int>(height);
152  m_NumChannels = boost::numeric_cast<unsigned int>(channels);
153 
154  const unsigned int sizeInBytes = GetSizeInBytes();
155  m_Data.resize(sizeInBytes);
156  memcpy(m_Data.data(), stbData.get(), sizeInBytes);
157 }
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33
unsigned int GetSizeInBytes() const

◆ InferenceTestImage() [2/3]

◆ InferenceTestImage() [3/3]

InferenceTestImage ( const InferenceTestImage )
delete

Member Function Documentation

◆ GetHeight()

unsigned int GetHeight ( ) const
inline

Definition at line 76 of file InferenceTestImage.hpp.

Referenced by GetImageDataAsNormalizedFloats(), GetImageDataInArmNnLayoutAsFloats(), StbResize(), and Write().

76 { return m_Height; }

◆ GetNumChannels()

unsigned int GetNumChannels ( ) const
inline

Definition at line 77 of file InferenceTestImage.hpp.

Referenced by GetPixelAs3Channels(), StbResize(), and Write().

77 { return m_NumChannels; }

◆ GetNumElements()

unsigned int GetNumElements ( ) const
inline

Definition at line 78 of file InferenceTestImage.hpp.

78 { return GetWidth() * GetHeight() * GetNumChannels(); }
unsigned int GetNumChannels() const
unsigned int GetWidth() const
unsigned int GetHeight() const

◆ GetPixelAs3Channels()

std::tuple< uint8_t, uint8_t, uint8_t > GetPixelAs3Channels ( unsigned int  x,
unsigned int  y 
) const

Definition at line 159 of file InferenceTestImage.cpp.

References ARMNN_ASSERT, GetNumChannels(), GetSizeInBytes(), and GetWidth().

Referenced by GetImageDataAsNormalizedFloats(), and GetImageDataInArmNnLayoutAsFloats().

160 {
161  if (x >= m_Width || y >= m_Height)
162  {
163  throw InferenceTestImageOutOfBoundsAccess(boost::str(boost::format("Attempted out of bounds image access. "
164  "Requested (%1%, %2%). Maximum valid coordinates (%3%, %4%).") % x % y % (m_Width - 1) % (m_Height - 1)));
165  }
166 
167  const unsigned int pixelOffset = x * GetNumChannels() + y * GetWidth() * GetNumChannels();
168  const uint8_t* const pixelData = m_Data.data() + pixelOffset;
169  ARMNN_ASSERT(pixelData <= (m_Data.data() + GetSizeInBytes()));
170 
171  std::array<uint8_t, 3> outPixelData;
172  outPixelData.fill(0);
173 
174  const unsigned int maxChannelsInPixel = std::min(GetNumChannels(), static_cast<unsigned int>(outPixelData.size()));
175  for (unsigned int c = 0; c < maxChannelsInPixel; ++c)
176  {
177  outPixelData[c] = pixelData[c];
178  }
179 
180  return std::make_tuple(outPixelData[0], outPixelData[1], outPixelData[2]);
181 }
unsigned int GetNumChannels() const
unsigned int GetWidth() const
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
unsigned int GetSizeInBytes() const

◆ GetSizeInBytes()

unsigned int GetSizeInBytes ( ) const
inline

Definition at line 79 of file InferenceTestImage.hpp.

References armnn::Resize().

Referenced by GetPixelAs3Channels(), and InferenceTestImage().

79 { return GetNumElements() * GetSingleElementSizeInBytes(); }
unsigned int GetNumElements() const

◆ GetWidth()

unsigned int GetWidth ( ) const
inline

◆ operator=() [1/2]

InferenceTestImage& operator= ( const InferenceTestImage )
delete

◆ operator=() [2/2]

InferenceTestImage& operator= ( InferenceTestImage &&  )
delete

◆ Resize()

std::vector< float > Resize ( unsigned int  newWidth,
unsigned int  newHeight,
const armnn::CheckLocation location,
const ResizingMethods  meth = ResizingMethods::STB,
const std::array< float, 3 > &  mean = {{0.0, 0.0, 0.0}},
const std::array< float, 3 > &  stddev = {{1.0, 1.0, 1.0}},
const float  scale = 255.0f 
)

Definition at line 210 of file InferenceTestImage.cpp.

References CheckLocation::AsString(), BilinearAndNormalized, STB, and StbResize().

Referenced by CaffePreprocessor::GetTestCaseData(), and YoloDatabase::GetTestCaseData().

217 {
218  std::vector<float> out;
219  if (newWidth == 0 || newHeight == 0)
220  {
221  throw InferenceTestImageResizeFailed(boost::str(boost::format("None of the dimensions passed to a resize "
222  "operation can be zero. Requested width: %1%. Requested height: %2%.") % newWidth % newHeight));
223  }
224 
225  switch (meth) {
227  {
228  StbResize(*this, newWidth, newHeight);
229  break;
230  }
232  {
233  out = ResizeBilinearAndNormalize(*this, newWidth, newHeight, scale, mean, stddev);
234  break;
235  }
236  default:
237  throw InferenceTestImageResizeFailed(boost::str(
238  boost::format("Unknown resizing method asked ArmNN only supports {STB, BilinearAndNormalized} %1%")
239  % location.AsString()));
240  }
241  return out;
242 }
std::string AsString() const
Definition: Exceptions.hpp:29
void StbResize(InferenceTestImage &im, const unsigned int newWidth, const unsigned int newHeight)

◆ StbResize()

void StbResize ( InferenceTestImage im,
const unsigned int  newWidth,
const unsigned int  newHeight 
)

Definition at line 184 of file InferenceTestImage.cpp.

References GetHeight(), GetNumChannels(), GetWidth(), and armnn::numeric_cast().

Referenced by Resize().

185 {
186  std::vector<uint8_t> newData;
187  newData.resize(newWidth * newHeight * im.GetNumChannels() * im.GetSingleElementSizeInBytes());
188 
189  // boost::numeric_cast<>() is used for user-provided data (protecting about overflows).
190  // static_cast<> is ok for internal data (assumes that, when internal data was originally provided by a user,
191  // a boost::numeric_cast<>() handled the conversion).
192  const int nW = boost::numeric_cast<int>(newWidth);
193  const int nH = boost::numeric_cast<int>(newHeight);
194 
195  const int w = static_cast<int>(im.GetWidth());
196  const int h = static_cast<int>(im.GetHeight());
197  const int numChannels = static_cast<int>(im.GetNumChannels());
198 
199  const int res = stbir_resize_uint8(im.m_Data.data(), w, h, 0, newData.data(), nW, nH, 0, numChannels);
200  if (res == 0)
201  {
202  throw InferenceTestImageResizeFailed("The resizing operation failed");
203  }
204 
205  im.m_Data.swap(newData);
206  im.m_Width = newWidth;
207  im.m_Height = newHeight;
208 }
unsigned int GetNumChannels() const
unsigned int GetWidth() const
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33
unsigned int GetHeight() const

◆ Write()

void Write ( WriteFormat  format,
const char *  filePath 
) const

Definition at line 244 of file InferenceTestImage.cpp.

References Bmp, GetHeight(), GetNumChannels(), GetWidth(), Png, and Tga.

245 {
246  const int w = static_cast<int>(GetWidth());
247  const int h = static_cast<int>(GetHeight());
248  const int numChannels = static_cast<int>(GetNumChannels());
249  int res = 0;
250 
251  switch (format)
252  {
253  case WriteFormat::Png:
254  {
255  res = stbi_write_png(filePath, w, h, numChannels, m_Data.data(), 0);
256  break;
257  }
258  case WriteFormat::Bmp:
259  {
260  res = stbi_write_bmp(filePath, w, h, numChannels, m_Data.data());
261  break;
262  }
263  case WriteFormat::Tga:
264  {
265  res = stbi_write_tga(filePath, w, h, numChannels, m_Data.data());
266  break;
267  }
268  default:
269  throw InferenceTestImageWriteFailed(boost::str(boost::format("Unknown format %1%")
270  % static_cast<int>(format)));
271  }
272 
273  if (res == 0)
274  {
275  throw InferenceTestImageWriteFailed(boost::str(boost::format("An error occurred when writing to file %1%")
276  % filePath));
277  }
278 }
unsigned int GetNumChannels() const
unsigned int GetWidth() const
unsigned int GetHeight() const

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