ArmNN
 20.05
InferenceTestImage.cpp File Reference
#include "InferenceTestImage.hpp"
#include <armnn/utility/Assert.hpp>
#include <armnn/utility/IgnoreUnused.hpp>
#include <boost/format.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <array>
#include <stb/stb_image.h>
#include <stb/stb_image_resize.h>
#include <stb/stb_image_write.h>

Go to the source code of this file.

Macros

#define STB_IMAGE_IMPLEMENTATION
 
#define STB_IMAGE_RESIZE_IMPLEMENTATION
 
#define STB_IMAGE_WRITE_IMPLEMENTATION
 

Functions

template<typename TProcessValueCallable >
std::vector< float > GetImageDataInArmNnLayoutAsFloats (ImageChannelLayout channelLayout, const InferenceTestImage &image, TProcessValueCallable processValue)
 
std::vector< float > GetImageDataInArmNnLayoutAsNormalizedFloats (ImageChannelLayout layout, const InferenceTestImage &image)
 
std::vector< float > GetImageDataInArmNnLayoutAsFloatsSubtractingMean (ImageChannelLayout layout, const InferenceTestImage &image, const std::array< float, 3 > &mean)
 
std::vector< float > GetImageDataAsNormalizedFloats (ImageChannelLayout layout, const InferenceTestImage &image)
 

Macro Definition Documentation

◆ STB_IMAGE_IMPLEMENTATION

#define STB_IMAGE_IMPLEMENTATION

Definition at line 15 of file InferenceTestImage.cpp.

◆ STB_IMAGE_RESIZE_IMPLEMENTATION

#define STB_IMAGE_RESIZE_IMPLEMENTATION

Definition at line 18 of file InferenceTestImage.cpp.

◆ STB_IMAGE_WRITE_IMPLEMENTATION

#define STB_IMAGE_WRITE_IMPLEMENTATION

Definition at line 21 of file InferenceTestImage.cpp.

Function Documentation

◆ GetImageDataAsNormalizedFloats()

std::vector<float> GetImageDataAsNormalizedFloats ( ImageChannelLayout  layout,
const InferenceTestImage image 
)

Definition at line 335 of file InferenceTestImage.cpp.

References B, G, InferenceTestImage::GetHeight(), InferenceTestImage::GetPixelAs3Channels(), InferenceTestImage::GetWidth(), and R.

337 {
338  std::vector<float> imageData;
339  const unsigned int h = image.GetHeight();
340  const unsigned int w = image.GetWidth();
341 
342  const unsigned int rDstIndex = GetImageChannelIndex(layout, ImageChannel::R);
343  const unsigned int gDstIndex = GetImageChannelIndex(layout, ImageChannel::G);
344  const unsigned int bDstIndex = GetImageChannelIndex(layout, ImageChannel::B);
345 
346  imageData.resize(h * w * 3);
347  unsigned int offset = 0;
348 
349  for (unsigned int j = 0; j < h; ++j)
350  {
351  for (unsigned int i = 0; i < w; ++i)
352  {
353  uint8_t r, g, b;
354  std::tie(r, g, b) = image.GetPixelAs3Channels(i, j);
355 
356  imageData[offset+rDstIndex] = float(r) / 255.0f;
357  imageData[offset+gDstIndex] = float(g) / 255.0f;
358  imageData[offset+bDstIndex] = float(b) / 255.0f;
359  offset += 3;
360  }
361  }
362 
363  return imageData;
364 }
unsigned int GetWidth() const
std::tuple< uint8_t, uint8_t, uint8_t > GetPixelAs3Channels(unsigned int x, unsigned int y) const
unsigned int GetHeight() const

◆ GetImageDataInArmNnLayoutAsFloats()

std::vector<float> GetImageDataInArmNnLayoutAsFloats ( ImageChannelLayout  channelLayout,
const InferenceTestImage image,
TProcessValueCallable  processValue 
)

Definition at line 281 of file InferenceTestImage.cpp.

References B, G, InferenceTestImage::GetHeight(), InferenceTestImage::GetPixelAs3Channels(), InferenceTestImage::GetWidth(), and R.

Referenced by GetImageDataInArmNnLayoutAsFloatsSubtractingMean(), and GetImageDataInArmNnLayoutAsNormalizedFloats().

284 {
285  const unsigned int h = image.GetHeight();
286  const unsigned int w = image.GetWidth();
287 
288  std::vector<float> imageData;
289  imageData.resize(h * w * 3);
290 
291  for (unsigned int j = 0; j < h; ++j)
292  {
293  for (unsigned int i = 0; i < w; ++i)
294  {
295  uint8_t r, g, b;
296  std::tie(r, g, b) = image.GetPixelAs3Channels(i, j);
297 
298  // ArmNN order: C, H, W
299  const unsigned int rDstIndex = GetImageChannelIndex(channelLayout, ImageChannel::R) * h * w + j * w + i;
300  const unsigned int gDstIndex = GetImageChannelIndex(channelLayout, ImageChannel::G) * h * w + j * w + i;
301  const unsigned int bDstIndex = GetImageChannelIndex(channelLayout, ImageChannel::B) * h * w + j * w + i;
302 
303  imageData[rDstIndex] = processValue(ImageChannel::R, float(r));
304  imageData[gDstIndex] = processValue(ImageChannel::G, float(g));
305  imageData[bDstIndex] = processValue(ImageChannel::B, float(b));
306  }
307  }
308 
309  return imageData;
310 }
unsigned int GetWidth() const
std::tuple< uint8_t, uint8_t, uint8_t > GetPixelAs3Channels(unsigned int x, unsigned int y) const
unsigned int GetHeight() const

◆ GetImageDataInArmNnLayoutAsFloatsSubtractingMean()

std::vector<float> GetImageDataInArmNnLayoutAsFloatsSubtractingMean ( ImageChannelLayout  layout,
const InferenceTestImage image,
const std::array< float, 3 > &  mean 
)

Definition at line 323 of file InferenceTestImage.cpp.

References GetImageDataInArmNnLayoutAsFloats().

Referenced by CaffePreprocessor::GetTestCaseData().

326 {
327  return GetImageDataInArmNnLayoutAsFloats(layout, image,
328  [layout, &mean](ImageChannel channel, float value)
329  {
330  const unsigned int channelIndex = GetImageChannelIndex(layout, channel);
331  return value - mean[channelIndex];
332  });
333 }
std::vector< float > GetImageDataInArmNnLayoutAsFloats(ImageChannelLayout channelLayout, const InferenceTestImage &image, TProcessValueCallable processValue)

◆ GetImageDataInArmNnLayoutAsNormalizedFloats()

std::vector<float> GetImageDataInArmNnLayoutAsNormalizedFloats ( ImageChannelLayout  layout,
const InferenceTestImage image 
)

Definition at line 312 of file InferenceTestImage.cpp.

References GetImageDataInArmNnLayoutAsFloats(), and armnn::IgnoreUnused().

Referenced by YoloDatabase::GetTestCaseData().

314 {
315  return GetImageDataInArmNnLayoutAsFloats(layout, image,
316  [](ImageChannel channel, float value)
317  {
318  armnn::IgnoreUnused(channel);
319  return value / 255.f;
320  });
321 }
void IgnoreUnused(Ts &&...)
std::vector< float > GetImageDataInArmNnLayoutAsFloats(ImageChannelLayout channelLayout, const InferenceTestImage &image, TProcessValueCallable processValue)