ArmNN
 20.05
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  {
72  "QAsymmS8",
73  "QAsymmU8",
74  "QSymm16"
75  };
76 
77  auto iterator = std::find(supportedSchemes.begin(), supportedSchemes.end(), scheme);
78  if (iterator == supportedSchemes.end())
79  {
80  std::cerr << "Quantization Scheme [" << scheme << "] is not supported" << std::endl;
81  return false;
82  }
83 
84  return true;
85 }
86 
87 bool CommandLineProcessor::ProcessCommandLine(int argc, char* argv[])
88 {
89  namespace po = boost::program_options;
90 
91  po::options_description desc("Options");
92  try
93  {
94  desc.add_options()
95  ("help,h", "Display help messages")
96  ("infile,f", po::value<std::string>(&m_InputFileName)->required(),
97  "Input file containing float 32 ArmNN Input Graph")
98  ("scheme,s", po::value<std::string>(&m_QuantizationScheme)->default_value("QAsymmU8"),
99  "Quantization scheme,"
100  " \"QAsymmU8\" or \"QAsymmS8\" or \"QSymm16\","
101  " default value QAsymmU8")
102  ("csvfile,c", po::value<std::string>(&m_CsvFileName)->default_value(""),
103  "CSV file containing paths for RAW input tensors")
104  ("preserve-data-type,p", po::bool_switch(&m_PreserveDataType)->default_value(false),
105  "Preserve the input and output data types")
106  ("outdir,d", po::value<std::string>(&m_OutputDirectory)->required(),
107  "Directory that output file will be written to")
108  ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(), "ArmNN output file name");
109  }
110  catch (const std::exception& e)
111  {
112  std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
113  return false;
114  }
115 
116  po::variables_map vm;
117 
118  try
119  {
120  po::store(po::parse_command_line(argc, argv, desc), vm);
121 
122  if (vm.count("help") || argc <= 1)
123  {
124  std::cout << "Convert a Fp32 ArmNN model to a quantized ArmNN model." << std::endl;
125  std::cout << std::endl;
126  std::cout << desc << std::endl;
127  return false;
128  }
129 
130  po::notify(vm);
131  }
132  catch (const po::error& e)
133  {
134  std::cerr << e.what() << std::endl << std::endl;
135  std::cerr << desc << std::endl;
136  return false;
137  }
138 
140  {
141  return false;
142  }
143 
145  {
146  return false;
147  }
148 
149  if (m_CsvFileName != "")
150  {
152  {
153  return false;
154  }
155  else
156  {
157  boost::filesystem::path csvFilePath(m_CsvFileName);
158  m_CsvFileDirectory = csvFilePath.parent_path().c_str();
159  }
160 
161  // If CSV file is defined, create a QuantizationDataSet for specified CSV file.
163  }
164 
166  {
167  return false;
168  }
169 
170  std::string output(m_OutputDirectory);
171  output.append(m_OutputFileName);
172 
173  if (boost::filesystem::exists(output))
174  {
175  std::cerr << "Output file [" << output << "] already exists" << std::endl;
176  return false;
177  }
178 
179  return true;
180 }
181 
182 } // namespace armnnQuantizer
bool ValidateQuantizationScheme(const std::string &scheme)
QuantizationDataSet is a structure which is created after parsing a quantization CSV file...
bool ProcessCommandLine(int argc, char *argv[])
bool ValidateOutputDirectory(std::string &dir)
bool ValidateProvidedFile(const std::string &inputFileName)