ArmNN
 20.08
ImageCSVFileGenerator.cpp
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 <Filesystem.hpp>
7 #include <boost/program_options.hpp>
8 
9 #include <algorithm>
10 #include <fstream>
11 #include <iostream>
12 #include <string>
13 
14 namespace
15 {
16 
17 // parses the command line to extract
18 // * the directory -i to look through .raw files from (must exist)
19 // * the name of the file -o the output CSV file path (must not already exist)
20 class CommandLineProcessor
21 {
22 public:
23  bool ValidateDirectory(std::string& dir)
24  {
25  if (dir.empty())
26  {
27  std::cerr << "No directory specified" << std::endl;
28  return false;
29  }
30 
31  if (dir[dir.length() - 1] != '/')
32  {
33  dir += "/";
34  }
35 
36  if (!fs::exists(dir))
37  {
38  std::cerr << "Directory [" << dir << "] does not exist" << std::endl;
39  return false;
40  }
41 
42  if (!fs::is_directory(dir))
43  {
44  std::cerr << "Given directory [" << dir << "] is not a directory" << std::endl;
45  return false;
46  }
47 
48  return true;
49  }
50 
51  bool ValidateOutputFile(std::string& outputFileName)
52  {
53  if (outputFileName.empty())
54  {
55  std::cerr << "No output file name specified" << std::endl;
56  return false;
57  }
58 
59  if (fs::exists(outputFileName))
60  {
61  std::cerr << "Output file [" << outputFileName << "] already exists" << std::endl;
62  return false;
63  }
64 
65  if (fs::is_directory(outputFileName))
66  {
67  std::cerr << "Output file [" << outputFileName << "] is a directory" << std::endl;
68  return false;
69  }
70 
71  fs::path outputPath(outputFileName);
72  if (!fs::exists(outputPath.parent_path()))
73  {
74  std::cerr << "Directory [" << outputPath.parent_path().c_str() << "] does not exist" << std::endl;
75  return false;
76  }
77 
78  return true;
79  }
80 
81  bool ValidateBindingId(const std::string& id)
82  {
83  if (!std::all_of(id.begin(), id.end(), ::isdigit))
84  {
85  std::cerr << "Invalid input binding Id" << std::endl;
86  return false;
87  }
88 
89  return true;
90  }
91 
92  bool ProcessCommandLine(int argc, char* argv[])
93  {
94  namespace po = boost::program_options;
95 
96  po::options_description desc("Options");
97  try
98  {
99  desc.add_options()
100  ("help,h", "Display help messages")
101  ("indir,i", po::value<std::string>(&m_InputDirectory)->required(),
102  "Directory that .raw files are stored in")
103  ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(),
104  "Output CSV file path")
105  ("layer-binding-id,l", po::value<std::string>(&m_InputBindingId)->default_value("0"),
106  "Input layer binding Id, Defaults to 0");
107  }
108  catch (const std::exception& e)
109  {
110  std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
111  return false;
112  }
113 
114  po::variables_map vm;
115 
116  try
117  {
118  po::store(po::parse_command_line(argc, argv, desc), vm);
119 
120  if (vm.count("help"))
121  {
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 
135  if (!ValidateDirectory(m_InputDirectory))
136  {
137  return false;
138  }
139 
140  if (!ValidateOutputFile(m_OutputFileName))
141  {
142  return false;
143  }
144 
145  if(!ValidateBindingId(m_InputBindingId))
146  {
147  return false;
148  }
149 
150  return true;
151  }
152 
153  std::string GetInputDirectory() {return m_InputDirectory;}
154  std::string GetOutputFileName() {return m_OutputFileName;}
155  std::string GetInputBindingId() {return m_InputBindingId;}
156 
157 private:
158  std::string m_InputDirectory;
159  std::string m_OutputFileName;
160  std::string m_InputBindingId;
161 };
162 
163 } // namespace anonymous
164 
165 int main(int argc, char* argv[])
166 {
167  CommandLineProcessor cmdline;
168  if (!cmdline.ProcessCommandLine(argc, argv))
169  {
170  return -1;
171  }
172 
173  const std::string fileFormat(".raw");
174 
175  const std::string rawDirectory(cmdline.GetInputDirectory());
176  const std::string outputPath(cmdline.GetOutputFileName());
177  const std::string bindingId(cmdline.GetInputBindingId());
178 
179  std::vector<fs::path> rawFiles;
180  for (auto& entry : boost::make_iterator_range(fs::directory_iterator(rawDirectory), {}))
181  {
182  if (entry.path().extension().c_str() == fileFormat)
183  {
184  rawFiles.push_back(entry.path());
185  }
186  }
187 
188  if (!rawFiles.empty())
189  {
190  unsigned int pass = 0;
191  std::ofstream refinementData;
192  refinementData.open(outputPath, std::ofstream::out);
193  if (refinementData.is_open())
194  {
195  for (auto const& raw : rawFiles)
196  {
197  refinementData << pass << ", " << bindingId << ", " << raw.c_str() << "\n";
198  if (!refinementData)
199  {
200  std::cerr << "Failed to write to output file: " << outputPath << std::endl;
201  continue;
202  }
203  ++pass;
204  }
205  refinementData.close();
206  }
207  else
208  {
209  std::cerr << "Failed to open output file: " << outputPath << std::endl;
210  return -1;
211  }
212  }
213  else
214  {
215  std::cerr << "No matching files with the \".raw\" extension found in the directory: "
216  << rawDirectory << std::endl;
217  return -1;
218  }
219 
220  return 0;
221 }
int main(int argc, char *argv[])
bool ValidateDirectory(std::string &dir)