ArmNN  NotReleased
CommandLineProcessor.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #define BOOST_FILESYSTEM_NO_DEPRECATED
9 
10 #include <boost/program_options.hpp>
11 #include <boost/filesystem/operations.hpp>
12 #include <boost/filesystem/path.hpp>
13 
14 namespace armnnQuantizer
15 {
16 
17 bool ValidateOutputDirectory(std::string& dir)
18 {
19  if (dir.empty())
20  {
21  std::cerr << "No output directory specified" << std::endl;
22  return false;
23  }
24 
25  if (dir[dir.length() - 1] != '/')
26  {
27  dir += "/";
28  }
29 
30  if (!boost::filesystem::exists(dir))
31  {
32  std::cerr << "Output directory [" << dir << "] does not exist" << std::endl;
33  return false;
34  }
35 
36  if (!boost::filesystem::is_directory(dir))
37  {
38  std::cerr << "Given output directory [" << dir << "] is not a directory" << std::endl;
39  return false;
40  }
41 
42  return true;
43 }
44 
45 bool ValidateProvidedFile(const std::string& inputFileName)
46 {
47  if (!boost::filesystem::exists(inputFileName))
48  {
49  std::cerr << "Provided file [" << inputFileName << "] does not exist" << std::endl;
50  return false;
51  }
52 
53  if (boost::filesystem::is_directory(inputFileName))
54  {
55  std::cerr << "Given file [" << inputFileName << "] is a directory" << std::endl;
56  return false;
57  }
58 
59  return true;
60 }
61 
62 bool ValidateQuantizationScheme(const std::string& scheme)
63 {
64  if (scheme.empty())
65  {
66  std::cerr << "No Quantization Scheme specified" << std::endl;
67  return false;
68  }
69 
70  std::vector<std::string> supportedSchemes = {
71  "QAsymm8",
72  "QSymm16"
73  };
74 
75  auto iterator = std::find(supportedSchemes.begin(), supportedSchemes.end(), scheme);
76  if (iterator == supportedSchemes.end())
77  {
78  std::cerr << "Quantization Scheme [" << scheme << "] is not supported" << std::endl;
79  return false;
80  }
81 
82  return true;
83 }
84 
85 bool CommandLineProcessor::ProcessCommandLine(int argc, char* argv[])
86 {
87  namespace po = boost::program_options;
88 
89  po::options_description desc("Options");
90  try
91  {
92  desc.add_options()
93  ("help,h", "Display help messages")
94  ("infile,f", po::value<std::string>(&m_InputFileName)->required(),
95  "Input file containing float 32 ArmNN Input Graph")
96  ("scheme,s", po::value<std::string>(&m_QuantizationScheme)->default_value("QAsymm8"),
97  "Quantization scheme, \"QAsymm8\" or \"QSymm16\", default value QAsymm8")
98  ("csvfile,c", po::value<std::string>(&m_CsvFileName)->default_value(""),
99  "CSV file containing paths for RAW input tensors")
100  ("preserve-data-type,p", po::bool_switch(&m_PreserveDataType)->default_value(false),
101  "Preserve the input and output data types")
102  ("outdir,d", po::value<std::string>(&m_OutputDirectory)->required(),
103  "Directory that output file will be written to")
104  ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(), "ArmNN output file name");
105  }
106  catch (const std::exception& e)
107  {
108  std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
109  return false;
110  }
111 
112  po::variables_map vm;
113 
114  try
115  {
116  po::store(po::parse_command_line(argc, argv, desc), vm);
117 
118  if (vm.count("help") || argc <= 1)
119  {
120  std::cout << "Convert a Fp32 ArmNN model to a quantized ArmNN model." << std::endl;
121  std::cout << std::endl;
122  std::cout << desc << std::endl;
123  return false;
124  }
125 
126  po::notify(vm);
127  }
128  catch (const po::error& e)
129  {
130  std::cerr << e.what() << std::endl << std::endl;
131  std::cerr << desc << std::endl;
132  return false;
133  }
134 
136  {
137  return false;
138  }
139 
141  {
142  return false;
143  }
144 
145  if (m_CsvFileName != "")
146  {
148  {
149  return false;
150  }
151  else
152  {
153  boost::filesystem::path csvFilePath(m_CsvFileName);
154  m_CsvFileDirectory = csvFilePath.parent_path().c_str();
155  }
156 
157  // If CSV file is defined, create a QuantizationDataSet for specified CSV file.
159  }
160 
162  {
163  return false;
164  }
165 
166  std::string output(m_OutputDirectory);
167  output.append(m_OutputFileName);
168 
169  if (boost::filesystem::exists(output))
170  {
171  std::cerr << "Output file [" << output << "] already exists" << std::endl;
172  return false;
173  }
174 
175  return true;
176 }
177 
178 } // namespace armnnQuantizer
bool ProcessCommandLine(int argc, char *argv[])
bool ValidateQuantizationScheme(const std::string &scheme)
bool ValidateProvidedFile(const std::string &inputFileName)
bool ValidateOutputDirectory(std::string &dir)