blob: f391a27a4d3b08943b63b3880783bb3758ab049f [file] [log] [blame]
Sadik Armagan8271f812019-04-19 09:55:06 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
SiCong Li39f46392019-06-21 12:00:04 +01006#include "ImageTensorGenerator.hpp"
Sadik Armagan8271f812019-04-19 09:55:06 +01007#include "../InferenceTestImage.hpp"
SiCong Li39f46392019-06-21 12:00:04 +01008#include <armnn/TypesUtils.hpp>
Sadik Armagan8271f812019-04-19 09:55:06 +01009
10#include <boost/filesystem.hpp>
11#include <boost/filesystem/operations.hpp>
12#include <boost/filesystem/path.hpp>
13#include <boost/log/trivial.hpp>
14#include <boost/program_options.hpp>
SiCong Li39f46392019-06-21 12:00:04 +010015#include <boost/variant.hpp>
Sadik Armagan8271f812019-04-19 09:55:06 +010016
17#include <algorithm>
18#include <fstream>
19#include <iostream>
20#include <string>
21
22namespace
23{
24
25// parses the command line to extract
26// * the input image file -i the input image file path (must exist)
27// * the layout -l the data layout output generated with (optional - default value is NHWC)
28// * the output file -o the output raw tensor file path (must not already exist)
29class CommandLineProcessor
30{
31public:
32 bool ValidateInputFile(const std::string& inputFileName)
33 {
34 if (inputFileName.empty())
35 {
36 std::cerr << "No input file name specified" << std::endl;
37 return false;
38 }
39
40 if (!boost::filesystem::exists(inputFileName))
41 {
42 std::cerr << "Input file [" << inputFileName << "] does not exist" << std::endl;
43 return false;
44 }
45
46 if (boost::filesystem::is_directory(inputFileName))
47 {
48 std::cerr << "Input file [" << inputFileName << "] is a directory" << std::endl;
49 return false;
50 }
51
52 return true;
53 }
54
55 bool ValidateLayout(const std::string& layout)
56 {
57 if (layout.empty())
58 {
59 std::cerr << "No layout specified" << std::endl;
60 return false;
61 }
62
SiCong Li39f46392019-06-21 12:00:04 +010063 std::vector<std::string> supportedLayouts = { "NHWC", "NCHW" };
Sadik Armagan8271f812019-04-19 09:55:06 +010064
65 auto iterator = std::find(supportedLayouts.begin(), supportedLayouts.end(), layout);
66 if (iterator == supportedLayouts.end())
67 {
68 std::cerr << "Layout [" << layout << "] is not supported" << std::endl;
69 return false;
70 }
71
72 return true;
73 }
74
75 bool ValidateOutputFile(std::string& outputFileName)
76 {
77 if (outputFileName.empty())
78 {
79 std::cerr << "No output file name specified" << std::endl;
80 return false;
81 }
82
83 if (boost::filesystem::exists(outputFileName))
84 {
85 std::cerr << "Output file [" << outputFileName << "] already exists" << std::endl;
86 return false;
87 }
88
89 if (boost::filesystem::is_directory(outputFileName))
90 {
91 std::cerr << "Output file [" << outputFileName << "] is a directory" << std::endl;
92 return false;
93 }
94
95 boost::filesystem::path outputPath(outputFileName);
96 if (!boost::filesystem::exists(outputPath.parent_path()))
97 {
98 std::cerr << "Output directory [" << outputPath.parent_path().c_str() << "] does not exist" << std::endl;
99 return false;
100 }
101
102 return true;
103 }
104
105 bool ProcessCommandLine(int argc, char* argv[])
106 {
107 namespace po = boost::program_options;
108
109 po::options_description desc("Options");
110 try
111 {
112 desc.add_options()
113 ("help,h", "Display help messages")
114 ("infile,i", po::value<std::string>(&m_InputFileName)->required(),
115 "Input image file to generate tensor from")
SiCong Li39f46392019-06-21 12:00:04 +0100116 ("model-format,f", po::value<std::string>(&m_ModelFormat)->required(),
117 "Format of the model file, Accepted values (caffe, tensorflow, tflite)")
Sadik Armagan8271f812019-04-19 09:55:06 +0100118 ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(),
SiCong Li39f46392019-06-21 12:00:04 +0100119 "Output raw tensor file path")
120 ("output-type,z", po::value<std::string>(&m_OutputType)->default_value("float"),
121 "The data type of the output tensors."
122 "If unset, defaults to \"float\" for all defined inputs. "
123 "Accepted values (float, int or qasymm8)")
124 ("new-width,w", po::value<std::string>(&m_NewWidth)->default_value("0"),
125 "Resize image to new width. Keep original width if unspecified")
126 ("new-height,h", po::value<std::string>(&m_NewHeight)->default_value("0"),
127 "Resize image to new height. Keep original height if unspecified")
128 ("layout,l", po::value<std::string>(&m_Layout)->default_value("NHWC"),
129 "Output data layout, \"NHWC\" or \"NCHW\", default value NHWC");
Sadik Armagan8271f812019-04-19 09:55:06 +0100130 }
131 catch (const std::exception& e)
132 {
133 std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
134 return false;
135 }
136
137 po::variables_map vm;
138
139 try
140 {
141 po::store(po::parse_command_line(argc, argv, desc), vm);
142
143 if (vm.count("help"))
144 {
145 std::cout << desc << std::endl;
146 return false;
147 }
148
149 po::notify(vm);
150 }
151 catch (const po::error& e)
152 {
153 std::cerr << e.what() << std::endl << std::endl;
154 std::cerr << desc << std::endl;
155 return false;
156 }
157
158 if (!ValidateInputFile(m_InputFileName))
159 {
160 return false;
161 }
162
163 if (!ValidateLayout(m_Layout))
164 {
165 return false;
166 }
167
168 if (!ValidateOutputFile(m_OutputFileName))
169 {
170 return false;
171 }
172
173 return true;
174 }
175
176 std::string GetInputFileName() {return m_InputFileName;}
SiCong Li39f46392019-06-21 12:00:04 +0100177 armnn::DataLayout GetLayout()
178 {
179 if (m_Layout == "NHWC")
180 {
181 return armnn::DataLayout::NHWC;
182 }
183 else if (m_Layout == "NCHW")
184 {
185 return armnn::DataLayout::NCHW;
186 }
187 else
188 {
189 throw armnn::Exception("Unsupported data layout: " + m_Layout);
190 }
191 }
Sadik Armagan8271f812019-04-19 09:55:06 +0100192 std::string GetOutputFileName() {return m_OutputFileName;}
SiCong Li39f46392019-06-21 12:00:04 +0100193 unsigned int GetNewWidth() {return static_cast<unsigned int>(std::stoi(m_NewWidth));}
194 unsigned int GetNewHeight() {return static_cast<unsigned int>(std::stoi(m_NewHeight));}
195 SupportedFrontend GetModelFormat()
196 {
197 if (m_ModelFormat == "caffe")
198 {
199 return SupportedFrontend::Caffe;
200 }
201 else if (m_ModelFormat == "tensorflow")
202 {
203 return SupportedFrontend::TensorFlow;
204 }
205 else if (m_ModelFormat == "tflite")
206 {
207 return SupportedFrontend::TFLite;
208 }
209 else
210 {
211 throw armnn::Exception("Unsupported model format" + m_ModelFormat);
212 }
213 }
214 armnn::DataType GetOutputType()
215 {
216 if (m_OutputType == "float")
217 {
218 return armnn::DataType::Float32;
219 }
220 else if (m_OutputType == "int")
221 {
222 return armnn::DataType::Signed32;
223 }
224 else if (m_OutputType == "qasymm8")
225 {
226 return armnn::DataType::QuantisedAsymm8;
227 }
228 else
229 {
230 throw armnn::Exception("Unsupported input type" + m_OutputType);
231 }
232 }
Sadik Armagan8271f812019-04-19 09:55:06 +0100233
234private:
235 std::string m_InputFileName;
236 std::string m_Layout;
237 std::string m_OutputFileName;
SiCong Li39f46392019-06-21 12:00:04 +0100238 std::string m_NewWidth;
239 std::string m_NewHeight;
240 std::string m_ModelFormat;
241 std::string m_OutputType;
Sadik Armagan8271f812019-04-19 09:55:06 +0100242};
243
244} // namespace anonymous
245
246int main(int argc, char* argv[])
247{
248 CommandLineProcessor cmdline;
249 if (!cmdline.ProcessCommandLine(argc, argv))
250 {
251 return -1;
252 }
Sadik Armagan8271f812019-04-19 09:55:06 +0100253 const std::string imagePath(cmdline.GetInputFileName());
254 const std::string outputPath(cmdline.GetOutputFileName());
SiCong Li39f46392019-06-21 12:00:04 +0100255 const SupportedFrontend& modelFormat(cmdline.GetModelFormat());
256 const armnn::DataType outputType(cmdline.GetOutputType());
257 const unsigned int newWidth = cmdline.GetNewWidth();
258 const unsigned int newHeight = cmdline.GetNewHeight();
259 const unsigned int batchSize = 1;
260 const armnn::DataLayout outputLayout(cmdline.GetLayout());
Sadik Armagan8271f812019-04-19 09:55:06 +0100261
SiCong Li39f46392019-06-21 12:00:04 +0100262 using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<uint8_t>>;
263 std::vector<TContainer> imageDataContainers;
264 const NormalizationParameters& normParams = GetNormalizationParameters(modelFormat, outputType);
Sadik Armagan8271f812019-04-19 09:55:06 +0100265 try
266 {
SiCong Li39f46392019-06-21 12:00:04 +0100267 switch (outputType)
268 {
269 case armnn::DataType::Signed32:
270 imageDataContainers.push_back(PrepareImageTensor<int>(
271 imagePath, newWidth, newHeight, normParams, batchSize, outputLayout));
272 break;
273 case armnn::DataType::QuantisedAsymm8:
274 imageDataContainers.push_back(PrepareImageTensor<uint8_t>(
275 imagePath, newWidth, newHeight, normParams, batchSize, outputLayout));
276 break;
277 case armnn::DataType::Float32:
278 default:
279 imageDataContainers.push_back(PrepareImageTensor<float>(
280 imagePath, newWidth, newHeight, normParams, batchSize, outputLayout));
281 break;
282 }
Sadik Armagan8271f812019-04-19 09:55:06 +0100283 }
284 catch (const InferenceTestImageException& e)
285 {
286 BOOST_LOG_TRIVIAL(fatal) << "Failed to load image file " << imagePath << " with error: " << e.what();
287 return -1;
288 }
289
290 std::ofstream imageTensorFile;
291 imageTensorFile.open(outputPath, std::ofstream::out);
292 if (imageTensorFile.is_open())
293 {
SiCong Li39f46392019-06-21 12:00:04 +0100294 boost::apply_visitor([&imageTensorFile](auto&& imageData) { WriteImageTensorImpl(imageData, imageTensorFile); },
295 imageDataContainers[0]);
Sadik Armagan8271f812019-04-19 09:55:06 +0100296 if (!imageTensorFile)
297 {
298 BOOST_LOG_TRIVIAL(fatal) << "Failed to write to output file" << outputPath;
299 imageTensorFile.close();
300 return -1;
301 }
302 imageTensorFile.close();
303 }
304 else
305 {
306 BOOST_LOG_TRIVIAL(fatal) << "Failed to open output file" << outputPath;
307 return -1;
308 }
309
310 return 0;
SiCong Li39f46392019-06-21 12:00:04 +0100311}