ArmNN
 22.05.01
ImageTensorGenerator.hpp File Reference
#include "../InferenceTestImage.hpp"
#include <armnn/TypesUtils.hpp>
#include <armnnUtils/TContainer.hpp>
#include <armnnUtils/Permute.hpp>
#include <algorithm>
#include <fstream>
#include <iterator>
#include <string>

Go to the source code of this file.

Classes

struct  NormalizationParameters
 

Enumerations

enum  SupportedFrontend { TFLite = 0 }
 

Functions

NormalizationParameters GetNormalizationParameters (const SupportedFrontend &modelFormat, const armnn::DataType &outputType)
 Get normalization parameters. More...
 
template<typename ElemType >
std::vector< ElemType > PrepareImageTensor (const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize=1, const armnn::DataLayout &outputLayout=armnn::DataLayout::NHWC)
 Prepare raw image tensor data by loading the image from imagePath and preprocessing it. More...
 
template<>
std::vector< float > PrepareImageTensor< float > (const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)
 
template<>
std::vector< int > PrepareImageTensor< int > (const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)
 
template<>
std::vector< uint8_t > PrepareImageTensor< uint8_t > (const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)
 
template<>
std::vector< int8_t > PrepareImageTensor< int8_t > (const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)
 
template<typename ElemType >
void WriteImageTensorImpl (const std::vector< ElemType > &imageData, std::ofstream &imageTensorFile)
 Write image tensor to ofstream. More...
 
template<>
void WriteImageTensorImpl< uint8_t > (const std::vector< uint8_t > &imageData, std::ofstream &imageTensorFile)
 
template<>
void WriteImageTensorImpl< int8_t > (const std::vector< int8_t > &imageData, std::ofstream &imageTensorFile)
 

Enumeration Type Documentation

◆ SupportedFrontend

enum SupportedFrontend
strong
Enumerator
TFLite 

Definition at line 26 of file ImageTensorGenerator.hpp.

27 {
28  TFLite = 0,
29 };

Function Documentation

◆ GetNormalizationParameters()

NormalizationParameters GetNormalizationParameters ( const SupportedFrontend modelFormat,
const armnn::DataType outputType 
)

Get normalization parameters.

Note that different flavours of models and different model data types have different normalization methods. This tool currently only supports TF and TFLite models

Parameters
[in]modelFormatOne of the supported frontends
[in]outputTypeOutput type of the image tensor, also the type of the intended model

Definition at line 38 of file ImageTensorGenerator.hpp.

References armnn::Float32, NormalizationParameters::mean, armnn::NHWC, PrepareImageTensor(), armnn::QAsymmS8, armnn::QAsymmU8, NormalizationParameters::scale, armnn::Signed32, NormalizationParameters::stddev, and TFLite.

Referenced by main().

40 {
41  NormalizationParameters normParams;
42  // Explicitly set default parameters
43  normParams.scale = 1.0;
44  normParams.mean = { 0.0, 0.0, 0.0 };
45  normParams.stddev = { 1.0, 1.0, 1.0 };
46  switch (modelFormat)
47  {
49  default:
50  switch (outputType)
51  {
53  normParams.scale = 127.5;
54  normParams.mean = { 1.0, 1.0, 1.0 };
55  break;
57  normParams.mean = { 128.0, 128.0, 128.0 };
58  break;
60  break;
62  normParams.mean = { 128.0, 128.0, 128.0 };
63  break;
64  default:
65  break;
66  }
67  break;
68  }
69  return normParams;
70 }
std::array< float, 3 > stddev
std::array< float, 3 > mean

◆ PrepareImageTensor()

std::vector<ElemType> PrepareImageTensor ( const std::string &  imagePath,
unsigned int  newWidth,
unsigned int  newHeight,
const NormalizationParameters normParams,
unsigned int  batchSize = 1,
const armnn::DataLayout outputLayout = armnn::DataLayout::NHWC 
)

Prepare raw image tensor data by loading the image from imagePath and preprocessing it.

Parameters
[in]imagePathPath to the image file
[in]newWidthThe new width of the output image tensor
[in]newHeightThe new height of the output image tensor
[in]normParamsNormalization parameters for the normalization of the image
[in]batchSizeBatch size
[in]outputLayoutData layout of the output image tensor

Referenced by GetNormalizationParameters().

◆ PrepareImageTensor< float >()

std::vector<float> PrepareImageTensor< float > ( const std::string &  imagePath,
unsigned int  newWidth,
unsigned int  newHeight,
const NormalizationParameters normParams,
unsigned int  batchSize,
const armnn::DataLayout outputLayout 
)

Definition at line 91 of file ImageTensorGenerator.hpp.

References InferenceTestImage::BilinearAndNormalized, CHECK_LOCATION, InferenceTestImage::GetWidth(), NormalizationParameters::mean, armnn::NCHW, armnnUtils::NHWCToArmNN, armnnUtils::Permute(), NormalizationParameters::scale, and NormalizationParameters::stddev.

Referenced by main(), PrepareImageTensor< int >(), PrepareImageTensor< int8_t >(), and PrepareImageTensor< uint8_t >().

97 {
98  // Generate image tensor
99  std::vector<float> imageData;
100  InferenceTestImage testImage(imagePath.c_str());
101  if (newWidth == 0)
102  {
103  newWidth = testImage.GetWidth();
104  }
105  if (newHeight == 0)
106  {
107  newHeight = testImage.GetHeight();
108  }
109  // Resize the image to new width and height or keep at original dimensions if the new width and height are specified
110  // as 0 Centre/Normalise the image.
111  imageData = testImage.Resize(newWidth, newHeight, CHECK_LOCATION(),
113  normParams.stddev, normParams.scale);
114  if (outputLayout == armnn::DataLayout::NCHW)
115  {
116  // Convert to NCHW format
117  const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
118  armnn::TensorShape dstShape({ batchSize, 3, newHeight, newWidth });
119  std::vector<float> tempImage(imageData.size());
120  armnnUtils::Permute(dstShape, NHWCToArmNN, imageData.data(), tempImage.data(), sizeof(float));
121  imageData.swap(tempImage);
122  }
123  return imageData;
124 }
std::array< float, 3 > stddev
const armnn::PermutationVector NHWCToArmNN
void Permute(const armnn::TensorShape &dstShape, const armnn::PermutationVector &mappings, const void *src, void *dst, size_t dataTypeSize)
Definition: Permute.cpp:131
unsigned int GetWidth() const
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
std::array< float, 3 > mean

◆ PrepareImageTensor< int >()

std::vector<int> PrepareImageTensor< int > ( const std::string &  imagePath,
unsigned int  newWidth,
unsigned int  newHeight,
const NormalizationParameters normParams,
unsigned int  batchSize,
const armnn::DataLayout outputLayout 
)

Definition at line 128 of file ImageTensorGenerator.hpp.

References PrepareImageTensor< float >().

Referenced by main().

134 {
135  // Get float32 image tensor
136  std::vector<float> imageDataFloat =
137  PrepareImageTensor<float>(imagePath, newWidth, newHeight, normParams, batchSize, outputLayout);
138  // Convert to int32 image tensor with static cast
139  std::vector<int> imageDataInt;
140  imageDataInt.reserve(imageDataFloat.size());
141  std::transform(imageDataFloat.begin(), imageDataFloat.end(), std::back_inserter(imageDataInt),
142  [](float val) { return static_cast<int>(val); });
143  return imageDataInt;
144 }
std::vector< float > PrepareImageTensor< float >(const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)

◆ PrepareImageTensor< int8_t >()

std::vector<int8_t> PrepareImageTensor< int8_t > ( const std::string &  imagePath,
unsigned int  newWidth,
unsigned int  newHeight,
const NormalizationParameters normParams,
unsigned int  batchSize,
const armnn::DataLayout outputLayout 
)

Definition at line 168 of file ImageTensorGenerator.hpp.

References PrepareImageTensor< float >().

Referenced by main().

174 {
175  // Get float32 image tensor
176  std::vector<float> imageDataFloat =
177  PrepareImageTensor<float>(imagePath, newWidth, newHeight, normParams, batchSize, outputLayout);
178  std::vector<int8_t> imageDataQasymms8;
179  imageDataQasymms8.reserve(imageDataFloat.size());
180  // Convert to uint8 image tensor with static cast
181  std::transform(imageDataFloat.begin(), imageDataFloat.end(), std::back_inserter(imageDataQasymms8),
182  [](float val) { return static_cast<uint8_t>(val); });
183  return imageDataQasymms8;
184 }
std::vector< float > PrepareImageTensor< float >(const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)

◆ PrepareImageTensor< uint8_t >()

std::vector<uint8_t> PrepareImageTensor< uint8_t > ( const std::string &  imagePath,
unsigned int  newWidth,
unsigned int  newHeight,
const NormalizationParameters normParams,
unsigned int  batchSize,
const armnn::DataLayout outputLayout 
)

Definition at line 148 of file ImageTensorGenerator.hpp.

References PrepareImageTensor< float >().

Referenced by main().

154 {
155  // Get float32 image tensor
156  std::vector<float> imageDataFloat =
157  PrepareImageTensor<float>(imagePath, newWidth, newHeight, normParams, batchSize, outputLayout);
158  std::vector<uint8_t> imageDataQasymm8;
159  imageDataQasymm8.reserve(imageDataFloat.size());
160  // Convert to uint8 image tensor with static cast
161  std::transform(imageDataFloat.begin(), imageDataFloat.end(), std::back_inserter(imageDataQasymm8),
162  [](float val) { return static_cast<uint8_t>(val); });
163  return imageDataQasymm8;
164 }
std::vector< float > PrepareImageTensor< float >(const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)

◆ WriteImageTensorImpl()

void WriteImageTensorImpl ( const std::vector< ElemType > &  imageData,
std::ofstream &  imageTensorFile 
)

Write image tensor to ofstream.

Parameters
[in]imageDataImage tensor data
[in]imageTensorFileOutput filestream (ofstream) to which the image tensor data is written

Definition at line 192 of file ImageTensorGenerator.hpp.

Referenced by main().

193 {
194  std::copy(imageData.begin(), imageData.end(), std::ostream_iterator<ElemType>(imageTensorFile, " "));
195 }

◆ WriteImageTensorImpl< int8_t >()

void WriteImageTensorImpl< int8_t > ( const std::vector< int8_t > &  imageData,
std::ofstream &  imageTensorFile 
)

Definition at line 208 of file ImageTensorGenerator.hpp.

209 {
210  std::copy(imageData.begin(), imageData.end(), std::ostream_iterator<int>(imageTensorFile, " "));
211 }

◆ WriteImageTensorImpl< uint8_t >()

void WriteImageTensorImpl< uint8_t > ( const std::vector< uint8_t > &  imageData,
std::ofstream &  imageTensorFile 
)

Definition at line 200 of file ImageTensorGenerator.hpp.

201 {
202  std::copy(imageData.begin(), imageData.end(), std::ostream_iterator<int>(imageTensorFile, " "));
203 }