ArmNN  NotReleased
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 127 of file InferenceTestImage.cpp.

References GetSizeInBytes().

128  : m_Width(0u)
129  , m_Height(0u)
130  , m_NumChannels(0u)
131 {
132  int width;
133  int height;
134  int channels;
135 
136  using StbImageDataPtr = std::unique_ptr<unsigned char, decltype(&stbi_image_free)>;
137  StbImageDataPtr stbData(stbi_load(filePath, &width, &height, &channels, 0), &stbi_image_free);
138 
139  if (stbData == nullptr)
140  {
141  throw InferenceTestImageLoadFailed(boost::str(boost::format("Could not load the image at %1%") % filePath));
142  }
143 
144  if (width == 0 || height == 0)
145  {
146  throw InferenceTestImageLoadFailed(boost::str(boost::format("Could not load empty image at %1%") % filePath));
147  }
148 
149  m_Width = boost::numeric_cast<unsigned int>(width);
150  m_Height = boost::numeric_cast<unsigned int>(height);
151  m_NumChannels = boost::numeric_cast<unsigned int>(channels);
152 
153  const unsigned int sizeInBytes = GetSizeInBytes();
154  m_Data.resize(sizeInBytes);
155  memcpy(m_Data.data(), stbData.get(), sizeInBytes);
156 }
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 GetWidth() const
unsigned int GetNumChannels() 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 158 of file InferenceTestImage.cpp.

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

Referenced by GetImageDataAsNormalizedFloats(), and GetImageDataInArmNnLayoutAsFloats().

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

◆ GetSizeInBytes()

unsigned int GetSizeInBytes ( ) const
inline

Definition at line 79 of file InferenceTestImage.hpp.

References armnn::Resize(), and armnnUtils::Sockets::Write().

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 209 of file InferenceTestImage.cpp.

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

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

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

◆ StbResize()

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

Definition at line 183 of file InferenceTestImage.cpp.

References GetHeight(), GetNumChannels(), and GetWidth().

Referenced by Resize().

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

◆ Write()

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

Definition at line 243 of file InferenceTestImage.cpp.

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

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

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