ArmNN
 21.08
ImageTensorGenerator.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "../InferenceTestImage.hpp"
7 
8 #include <armnn/TypesUtils.hpp>
9 
10 #include <armnnUtils/Permute.hpp>
11 
12 #include <algorithm>
13 #include <fstream>
14 #include <iterator>
15 #include <string>
16 
17 // Parameters used in normalizing images
19 {
20  float scale{ 1.0 };
21  std::array<float, 3> mean{ { 0.0, 0.0, 0.0 } };
22  std::array<float, 3> stddev{ { 1.0, 1.0, 1.0 } };
23 };
24 
26 {
27  TFLite = 0,
28 };
29 
30 /** Get normalization parameters.
31  * Note that different flavours of models and different model data types have different normalization methods.
32  * This tool currently only supports TF and TFLite models
33  *
34  * @param[in] modelFormat One of the supported frontends
35  * @param[in] outputType Output type of the image tensor, also the type of the intended model
36  */
38  const armnn::DataType& outputType)
39 {
40  NormalizationParameters normParams;
41  // Explicitly set default parameters
42  normParams.scale = 1.0;
43  normParams.mean = { 0.0, 0.0, 0.0 };
44  normParams.stddev = { 1.0, 1.0, 1.0 };
45  switch (modelFormat)
46  {
48  default:
49  switch (outputType)
50  {
52  normParams.scale = 127.5;
53  normParams.mean = { 1.0, 1.0, 1.0 };
54  break;
56  normParams.mean = { 128.0, 128.0, 128.0 };
57  break;
59  break;
61  normParams.mean = { 128.0, 128.0, 128.0 };
62  break;
63  default:
64  break;
65  }
66  break;
67  }
68  return normParams;
69 }
70 
71 /** Prepare raw image tensor data by loading the image from imagePath and preprocessing it.
72  *
73  * @param[in] imagePath Path to the image file
74  * @param[in] newWidth The new width of the output image tensor
75  * @param[in] newHeight The new height of the output image tensor
76  * @param[in] normParams Normalization parameters for the normalization of the image
77  * @param[in] batchSize Batch size
78  * @param[in] outputLayout Data layout of the output image tensor
79  */
80 template <typename ElemType>
81 std::vector<ElemType> PrepareImageTensor(const std::string& imagePath,
82  unsigned int newWidth,
83  unsigned int newHeight,
84  const NormalizationParameters& normParams,
85  unsigned int batchSize = 1,
86  const armnn::DataLayout& outputLayout = armnn::DataLayout::NHWC);
87 
88 // Prepare float32 image tensor
89 template <>
90 std::vector<float> PrepareImageTensor<float>(const std::string& imagePath,
91  unsigned int newWidth,
92  unsigned int newHeight,
93  const NormalizationParameters& normParams,
94  unsigned int batchSize,
95  const armnn::DataLayout& outputLayout)
96 {
97  // Generate image tensor
98  std::vector<float> imageData;
99  InferenceTestImage testImage(imagePath.c_str());
100  if (newWidth == 0)
101  {
102  newWidth = testImage.GetWidth();
103  }
104  if (newHeight == 0)
105  {
106  newHeight = testImage.GetHeight();
107  }
108  // Resize the image to new width and height or keep at original dimensions if the new width and height are specified
109  // as 0 Centre/Normalise the image.
110  imageData = testImage.Resize(newWidth, newHeight, CHECK_LOCATION(),
112  normParams.stddev, normParams.scale);
113  if (outputLayout == armnn::DataLayout::NCHW)
114  {
115  // Convert to NCHW format
116  const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
117  armnn::TensorShape dstShape({ batchSize, 3, newHeight, newWidth });
118  std::vector<float> tempImage(imageData.size());
119  armnnUtils::Permute(dstShape, NHWCToArmNN, imageData.data(), tempImage.data(), sizeof(float));
120  imageData.swap(tempImage);
121  }
122  return imageData;
123 }
124 
125 // Prepare int32 image tensor
126 template <>
127 std::vector<int> PrepareImageTensor<int>(const std::string& imagePath,
128  unsigned int newWidth,
129  unsigned int newHeight,
130  const NormalizationParameters& normParams,
131  unsigned int batchSize,
132  const armnn::DataLayout& outputLayout)
133 {
134  // Get float32 image tensor
135  std::vector<float> imageDataFloat =
136  PrepareImageTensor<float>(imagePath, newWidth, newHeight, normParams, batchSize, outputLayout);
137  // Convert to int32 image tensor with static cast
138  std::vector<int> imageDataInt;
139  imageDataInt.reserve(imageDataFloat.size());
140  std::transform(imageDataFloat.begin(), imageDataFloat.end(), std::back_inserter(imageDataInt),
141  [](float val) { return static_cast<int>(val); });
142  return imageDataInt;
143 }
144 
145 // Prepare qasymmu8 image tensor
146 template <>
147 std::vector<uint8_t> PrepareImageTensor<uint8_t>(const std::string& imagePath,
148  unsigned int newWidth,
149  unsigned int newHeight,
150  const NormalizationParameters& normParams,
151  unsigned int batchSize,
152  const armnn::DataLayout& outputLayout)
153 {
154  // Get float32 image tensor
155  std::vector<float> imageDataFloat =
156  PrepareImageTensor<float>(imagePath, newWidth, newHeight, normParams, batchSize, outputLayout);
157  std::vector<uint8_t> imageDataQasymm8;
158  imageDataQasymm8.reserve(imageDataFloat.size());
159  // Convert to uint8 image tensor with static cast
160  std::transform(imageDataFloat.begin(), imageDataFloat.end(), std::back_inserter(imageDataQasymm8),
161  [](float val) { return static_cast<uint8_t>(val); });
162  return imageDataQasymm8;
163 }
164 
165 // Prepare qasymms8 image tensor
166 template <>
167 std::vector<int8_t> PrepareImageTensor<int8_t>(const std::string& imagePath,
168  unsigned int newWidth,
169  unsigned int newHeight,
170  const NormalizationParameters& normParams,
171  unsigned int batchSize,
172  const armnn::DataLayout& outputLayout)
173 {
174  // Get float32 image tensor
175  std::vector<float> imageDataFloat =
176  PrepareImageTensor<float>(imagePath, newWidth, newHeight, normParams, batchSize, outputLayout);
177  std::vector<int8_t> imageDataQasymms8;
178  imageDataQasymms8.reserve(imageDataFloat.size());
179  // Convert to uint8 image tensor with static cast
180  std::transform(imageDataFloat.begin(), imageDataFloat.end(), std::back_inserter(imageDataQasymms8),
181  [](float val) { return static_cast<uint8_t>(val); });
182  return imageDataQasymms8;
183 }
184 
185 /** Write image tensor to ofstream
186  *
187  * @param[in] imageData Image tensor data
188  * @param[in] imageTensorFile Output filestream (ofstream) to which the image tensor data is written
189  */
190 template <typename ElemType>
191 void WriteImageTensorImpl(const std::vector<ElemType>& imageData, std::ofstream& imageTensorFile)
192 {
193  std::copy(imageData.begin(), imageData.end(), std::ostream_iterator<ElemType>(imageTensorFile, " "));
194 }
195 
196 // For uint8_t image tensor, cast it to int before writing it to prevent writing data as characters instead of
197 // numerical values
198 template <>
199 void WriteImageTensorImpl<uint8_t>(const std::vector<uint8_t>& imageData, std::ofstream& imageTensorFile)
200 {
201  std::copy(imageData.begin(), imageData.end(), std::ostream_iterator<int>(imageTensorFile, " "));
202 }
203 
204 // For int8_t image tensor, cast it to int before writing it to prevent writing data as characters instead of
205 // numerical values
206 template <>
207 void WriteImageTensorImpl<int8_t>(const std::vector<int8_t>& imageData, std::ofstream& imageTensorFile)
208 {
209  std::copy(imageData.begin(), imageData.end(), std::ostream_iterator<int>(imageTensorFile, " "));
210 }
DataLayout
Definition: Types.hpp:53
std::array< float, 3 > stddev
NormalizationParameters GetNormalizationParameters(const SupportedFrontend &modelFormat, const armnn::DataType &outputType)
Get normalization parameters.
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...
void WriteImageTensorImpl(const std::vector< ElemType > &imageData, std::ofstream &imageTensorFile)
Write image tensor to ofstream.
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)
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
DataType
Definition: Types.hpp:35
unsigned int GetWidth() const
void WriteImageTensorImpl< uint8_t >(const std::vector< uint8_t > &imageData, std::ofstream &imageTensorFile)
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)
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
std::array< float, 3 > mean
void WriteImageTensorImpl< int8_t >(const std::vector< int8_t > &imageData, std::ofstream &imageTensorFile)
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)
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)