7 #include "../InferenceTestImage.hpp" 12 #include <cxxopts/cxxopts.hpp> 26 class CommandLineProcessor
32 if (result.count(
"infile"))
34 if (!ValidateInputFile(result[
"infile"].as<std::string>()))
41 std::cerr <<
"-i/--infile parameter is mandatory." << std::endl;
46 if (!result.count(
"model-format"))
48 std::cerr <<
"-f/--model-format parameter is mandatory." << std::endl;
53 if (result.count(
"outfile"))
55 if (!ValidateOutputFile(result[
"outfile"].as<std::string>()))
62 std::cerr <<
"-o/--outfile parameter is mandatory." << std::endl;
66 if (result.count(
"layout"))
68 if(!ValidateLayout(result[
"layout"].as<std::string>()))
77 bool ValidateInputFile(
const std::string& inputFileName)
79 if (inputFileName.empty())
81 std::cerr <<
"No input file name specified" << std::endl;
85 if (!fs::exists(inputFileName))
87 std::cerr <<
"Input file [" << inputFileName <<
"] does not exist" << std::endl;
91 if (fs::is_directory(inputFileName))
93 std::cerr <<
"Input file [" << inputFileName <<
"] is a directory" << std::endl;
100 bool ValidateLayout(
const std::string& layout)
104 std::cerr <<
"No layout specified" << std::endl;
108 std::vector<std::string> supportedLayouts = {
"NHWC",
"NCHW" };
110 auto iterator = std::find(supportedLayouts.begin(), supportedLayouts.end(), layout);
111 if (iterator == supportedLayouts.end())
113 std::cerr <<
"Layout [" << layout <<
"] is not supported" << std::endl;
120 bool ValidateOutputFile(
const std::string& outputFileName)
122 if (outputFileName.empty())
124 std::cerr <<
"No output file name specified" << std::endl;
128 if (fs::exists(outputFileName))
130 std::cerr <<
"Output file [" << outputFileName <<
"] already exists" << std::endl;
134 if (fs::is_directory(outputFileName))
136 std::cerr <<
"Output file [" << outputFileName <<
"] is a directory" << std::endl;
140 fs::path outputPath(outputFileName);
141 if (!fs::exists(outputPath.parent_path()))
143 std::cerr <<
"Output directory [" << outputPath.parent_path().c_str() <<
"] does not exist" << std::endl;
150 bool ProcessCommandLine(
int argc,
char* argv[])
152 cxxopts::Options options(
"ImageTensorGenerator",
153 "Program for pre-processing a .jpg image " 154 "before generating a .raw tensor file from it.");
158 options.add_options()
159 (
"h,help",
"Display help messages")
161 "Input image file to generate tensor from",
162 cxxopts::value<std::string>(m_InputFileName))
164 "Format of the intended model file that uses the images." 165 "Different formats have different image normalization styles." 166 "If unset, defaults to tflite." 167 "Accepted value (tflite)",
168 cxxopts::value<std::string>(m_ModelFormat)->default_value(
"tflite"))
170 "Output raw tensor file path",
171 cxxopts::value<std::string>(m_OutputFileName))
173 "The data type of the output tensors." 174 "If unset, defaults to \"float\" for all defined inputs. " 175 "Accepted values (float, int, qasymms8 or qasymmu8)",
176 cxxopts::value<std::string>(m_OutputType)->default_value(
"float"))
178 "Resize image to new width. Keep original width if unspecified",
179 cxxopts::value<std::string>(m_NewWidth)->default_value(
"0"))
181 "Resize image to new height. Keep original height if unspecified",
182 cxxopts::value<std::string>(m_NewHeight)->default_value(
"0"))
184 "Output data layout, \"NHWC\" or \"NCHW\", default value NHWC",
185 cxxopts::value<std::string>(m_Layout)->default_value(
"NHWC"));
187 catch (
const std::exception& e)
189 std::cerr << options.help() << std::endl;
195 auto result = options.parse(argc, argv);
197 if (result.count(
"help"))
199 std::cout << options.help() << std::endl;
208 catch (
const cxxopts::OptionException& e)
210 std::cerr << e.what() << std::endl << std::endl;
217 std::string GetInputFileName() {
return m_InputFileName;}
220 if (m_Layout ==
"NHWC")
224 else if (m_Layout ==
"NCHW")
233 std::string GetOutputFileName() {
return m_OutputFileName;}
234 unsigned int GetNewWidth() {
return static_cast<unsigned int>(std::stoi(m_NewWidth));}
235 unsigned int GetNewHeight() {
return static_cast<unsigned int>(std::stoi(m_NewHeight));}
238 if (m_ModelFormat ==
"tflite")
249 if (m_OutputType ==
"float")
253 else if (m_OutputType ==
"int")
257 else if (m_OutputType ==
"qasymm8" || m_OutputType ==
"qasymmu8")
261 else if (m_OutputType ==
"qasymms8")
272 std::string m_InputFileName;
273 std::string m_Layout;
274 std::string m_OutputFileName;
275 std::string m_NewWidth;
276 std::string m_NewHeight;
277 std::string m_ModelFormat;
278 std::string m_OutputType;
283 int main(
int argc,
char* argv[])
285 CommandLineProcessor cmdline;
286 if (!cmdline.ProcessCommandLine(argc, argv))
290 const std::string imagePath(cmdline.GetInputFileName());
291 const std::string outputPath(cmdline.GetOutputFileName());
294 const unsigned int newWidth = cmdline.GetNewWidth();
295 const unsigned int newHeight = cmdline.GetNewHeight();
296 const unsigned int batchSize = 1;
299 std::vector<armnnUtils::TContainer> imageDataContainers;
307 imagePath, newWidth, newHeight, normParams, batchSize, outputLayout));
311 imagePath, newWidth, newHeight, normParams, batchSize, outputLayout));
315 imagePath, newWidth, newHeight, normParams, batchSize, outputLayout));
320 imagePath, newWidth, newHeight, normParams, batchSize, outputLayout));
326 ARMNN_LOG(fatal) <<
"Failed to load image file " << imagePath <<
" with error: " << e.
what();
330 std::ofstream imageTensorFile;
331 imageTensorFile.open(outputPath, std::ofstream::out);
332 if (imageTensorFile.is_open())
334 mapbox::util::apply_visitor(
336 imageDataContainers[0]
339 if (!imageTensorFile)
341 ARMNN_LOG(fatal) <<
"Failed to write to output file" << outputPath;
342 imageTensorFile.close();
345 imageTensorFile.close();
349 ARMNN_LOG(fatal) <<
"Failed to open output file" << outputPath;
NormalizationParameters GetNormalizationParameters(const SupportedFrontend &modelFormat, const armnn::DataType &outputType)
Get normalization parameters.
virtual const char * what() const noexcept override
#define ARMNN_LOG(severity)
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)
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)
Base class for all ArmNN exceptions so that users can filter to just those.
int main(int argc, char *argv[])
BenchmarkOptions ParseOptions(int argc, char *argv[])
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)