randomTestRunner.cpp
Go to the documentation of this file.
1 
18 #include "randomDemoRecorder.hpp"
19 
24 
25 #include <boost/program_options.hpp>
26 #include <string>
27 #include <set>
28 #include <stack>
29 #include <queue>
30 #include <utility>
31 #include <boost/filesystem.hpp>
32 #include <sys/time.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <fstream>
36 #include <iostream>
37 
38 using namespace ISM;
39 using namespace std;
40 namespace po = boost::program_options;
41 namespace fs = boost::filesystem;
42 
43 struct TestSpec {
44  int setCount;
47 
48  double bin_size;
51 
52  TestSpec(int setCount, int objectCount, bool useClustering, double bin_size, double maxAngleDeviation, int useAPORater)
53  : setCount(setCount), objectCount(objectCount), useClustering(useClustering), bin_size(bin_size),
54  maxAngleDeviation(maxAngleDeviation), useAPORater(useAPORater) {}
55 };
56 
57 struct TestResult {
59  double avgRuntime;
60  double avgConfidence;
61  bool timeout;
62 };
63 
64 void removeFileIfExists(const string& filename) {
65  if (fs::exists(filename)) {
66  fs::remove(filename);
67  }
68 }
69 
70 void printTest(const TestSpec& t) {
71  cerr << "setCount: " << t.setCount << ", objectCount: " << t.objectCount << ", useClustering:" << t.useClustering
72  << ",\nbin_size:" << t.bin_size << ", maxAngleDeviation:" << t.maxAngleDeviation
73  << ",\nuseAPORater:" << t.useAPORater
74  << endl;
75 }
76 
77 void printTestResult(const TestResult& r) {
78  cerr << "\nTestSpecs: \n\tsetCount: " << r.spec.setCount << ", objectCount: " << r.spec.objectCount << ", useClustering: " << r.spec.useClustering
79  << ",\n\tbin_size: " << r.spec.bin_size << ", maxAngleDeviation: " << r.spec.maxAngleDeviation
80  << ",\n\tuseAPORater: " << r.spec.useAPORater
81  << "\n\nResult: avgRuntime: " << r.avgRuntime << ", avgConfidence: " << r.avgConfidence << ", timeout: " << r.timeout << endl;
82 }
83 
84 void writeTestResult(const TestResult& r, ofstream& csvFile) {
85  csvFile << r.spec.setCount << "," << r.spec.objectCount << "," << r.spec.useClustering << ","
86  << r.spec.bin_size << "," << r.spec.maxAngleDeviation << ","
87  << r.spec.useAPORater << "," << r.avgRuntime << "," << r.avgConfidence << "," << r.timeout << endl;
88 }
89 
90 std::string patternName;
92 
93 void trainNewModel(const std::string& sourceFile, const string& testFile, const int& objectCount, const int& setCount, const bool& useClustering)
94 {
95  removeFileIfExists(testFile);
96  TableHelper source = TableHelper(sourceFile);
97  TableHelper* target = new TableHelper(testFile);
98  target->dropTables();
99  target->createTablesIfNecessary();
100 
101  set<pair<string, string> > typesAndIds;
102  patternName = source.getRecordedPatternNames()[0];
103 
105  for (ObjectSetPtr& set : pattern->objectSets)
106  {
107  for (ObjectPtr& obj : set->objects)
108  {
109  std::pair<std::string, std::string> p = make_pair(obj->type, obj->observedId);
110  if (typesAndIds.find(p) == typesAndIds.end())
111  {
112  typesAndIds.insert(p);
113  }
114  }
115  }
116 
117  set<pair<string, string> > typesAndIdsToUse;
118  while (typesAndIdsToUse.size() < (size_t)objectCount)
119  {
120  set<pair<string, string> >::iterator first = typesAndIds.begin();
121  typesAndIdsToUse.insert(*first);
122  typesAndIds.erase(first);
123  }
124 
125  cerr << "creating test database";
126  insertedSets = 0;
127  for (ObjectSetPtr& set : pattern->objectSets)
128  {
129  ObjectSetPtr newSet(new ObjectSet());
130  for (ObjectPtr& obj : set->objects)
131  {
132  std::pair<std::string, std::string> p = make_pair(obj->type, obj->observedId);
133  if (typesAndIdsToUse.find(p) != typesAndIdsToUse.end())
134  {
135  newSet->insert(obj);
136  }
137  }
138  target->insertRecordedObjectSet(newSet, patternName);
139  insertedSets++;
140  cerr << ".";
141  cerr.flush();
142  if (insertedSets >= setCount) {
143  break;
144  }
145  }
146  cerr << " done" << endl;
147 
148  delete target;
149 
150  Trainer* trainer = new Trainer(testFile);
151  if (!useClustering) {
152  trainer->setUseClustering(false);
153  }
154  trainer->trainPattern();
155  delete trainer;
156 
157  return;
158 }
159 
160 
161 
162 int main(int argc, char** argv) {
163  po::options_description desc("Allowed options");
164  desc.add_options()("help,h", "produce help message")
165  ("database-file,d", po::value<string>()->default_value("record.sqlite"), "database file to use")
166  ("test-database-file,t", po::value<string>()->default_value("test.sqlite"), "database file write tests to")
167  ("csv-file,c", po::value<string>()->default_value("results.csv"), "csv file to write results");
168 
169  po::variables_map vm;
170  po::store(po::parse_command_line(argc, argv, desc), vm);
171  po::notify(vm);
172 
173  if (vm.count("help")) {
174  cerr << desc << "\n";
175  return 0;
176  }
177 
178  vector<int> setCounts = { 1, 10, 50, 100, 500};
179  vector<int> objectCounts = { 1, 2, 3, 4, 5, 10 };
180  vector<bool> useClusterings = { true, false };
181 
182  vector<double> bin_sizes = { 0.01, 0.1, 0.5 };
183  vector<double> maxAngleDeviations = {1.0, 10.0, 45.0};
184 
185  //all new options
186  vector<int> useAPORaterVec = { 0, 1 };
187 
188  queue<TestSpec> tests;
189  stack<TestResult> testResults;
190 
191  for (int setCount : setCounts)
192  {
193  for (int objectCount : objectCounts)
194  {
195  for (bool useClustering : useClusterings)
196  {
197  for (double bin_size : bin_sizes)
198  {
199  for (double maxAngleDeviation : maxAngleDeviations)
200  {
201  for (int useAPORater : useAPORaterVec)
202  {
203  TestSpec t = TestSpec(setCount, objectCount, useClustering, bin_size, maxAngleDeviation, useAPORater);
204  tests.push(t);
205  }
206 
207  }
208  }
209  }
210  }
211  }
212 
213 
214  string sourceFile = vm["database-file"].as<string>();
215  string testFile = vm["test-database-file"].as<string>();
216 
217  string csvFilename = vm["csv-file"].as<string>();
218  removeFileIfExists(csvFilename);
219  ofstream csvFile;
220  csvFile.open(csvFilename.c_str());
221  csvFile << "setCount, objectCount,useClustering,bin_size,maxAngleDeviation,"
222  <<"useAPORater,avgRuntime,avgConfidence,timeout"
223  << endl;
224 
225  //Generate random record db for further use
226  RandomDemoRecorder demoRec = *(new RandomDemoRecorder());
227  TableHelper* recordingsTable = new TableHelper(sourceFile);
228  recordingsTable->dropTables();
229  cout<<"generate random record db"<<endl;
230  demoRec.generateDemoRecording(sourceFile, objectCounts.back(), setCounts.back(), true);
231  cout<<"finished generating"<<endl;
232  delete (recordingsTable);
233 
234  TestSpec tempTest = tests.front();
235  trainNewModel(sourceFile, testFile, tempTest.objectCount, tempTest.setCount, tempTest.useClustering);
236  bool usedClusteringBefore = tempTest.useClustering;
237 
238  while (!tests.empty()) {
239  TestSpec test = tests.front();
240  tests.pop();
241 
242  //printTest(test);
243  if(usedClusteringBefore != test.useClustering)
244  {
245  trainNewModel(sourceFile, testFile, test.objectCount, test.setCount, test.useClustering);
246  usedClusteringBefore = test.useClustering;
247  }
248 
249 
250  TestResult testResult = { test, 0, 0, false };
251  TableHelper* target = new TableHelper(testFile);
252  cerr <<"recognizing";
253  int i;
254  for (i = 0; ((i < insertedSets) && (i < 25)); i++) {
255  ObjectSetPtr testSet = target->getRecordedObjectSet(i+1);
256  Recognizer recognizer(testFile, test.bin_size, test.maxAngleDeviation,
257  test.useAPORater);
258 
259  struct timeval start, end;
260 
261  gettimeofday(&start, NULL);
262 
263  vector<RecognitionResultPtr> results = recognizer.recognizePattern(testSet, 0, 1);
264 
265  gettimeofday(&end, NULL);
266 
267  long mtime, seconds, useconds;
268  seconds = end.tv_sec - start.tv_sec;
269  useconds = end.tv_usec - start.tv_usec;
270 
271  mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
272 
273  testResult.avgRuntime += mtime;
274 
275  for (RecognitionResultPtr& result : results)
276  {
277  if (result->patternName == patternName)
278  {
279  testResult.avgConfidence += result->confidence;
280  }
281  }
282  cerr<<".";
283  cerr.flush();
284 
285  //if recognition-time longer than 10 sec -> stop further iterations
286  if(mtime > 10000)
287  {
288  testResult.timeout = true;
289  break;
290  }
291  }
292 
293  cerr<<"done"<<endl;
294  delete (target);
295 
296  testResult.avgConfidence /= (double) i;
297  testResult.avgRuntime /= (double) i;
298 
299  if(testResult.timeout)
300  {
301  testResult.avgConfidence = 0.0;
302  testResult.avgRuntime = 0.0;
303  }
304 
305  testResults.push(testResult);
306  printTestResult(testResult);
307  writeTestResult(testResult, csvFile);
308  }
309 
310  csvFile.close();
311 
312  return 0;
313 }
const ObjectSetPtr getRecordedObjectSet(int setId) const
void setUseClustering(const bool useClustering)
Definition: Trainer.cpp:56
double avgConfidence
int insertRecordedObjectSet(const boost::shared_ptr< ObjectSet > &os, const std::string &patternName)
std::string patternName
boost::shared_ptr< RecordedPattern > RecordedPatternPtr
TestSpec(int setCount, int objectCount, bool useClustering, double bin_size, double maxAngleDeviation, int useAPORater)
void trainPattern()
Definition: Trainer.cpp:60
void trainNewModel(const std::string &sourceFile, const string &testFile, const int &objectCount, const int &setCount, const bool &useClustering)
void printTest(const TestSpec &t)
int insertedSets
void removeFileIfExists(const string &filename)
void generateDemoRecording(std::string dbfile, unsigned objects, unsigned timesteps, bool genRanOrientsAdditionally)
double maxAngleDeviation
boost::shared_ptr< ObjectSet > ObjectSetPtr
Definition: ObjectSet.hpp:53
boost::shared_ptr< RecognitionResult > RecognitionResultPtr
void dropTables() const
void writeTestResult(const TestResult &r, ofstream &csvFile)
const std::vector< RecognitionResultPtr > recognizePattern(const ObjectSetPtr &objectSet, const double filterThreshold=0.0, const int resultsPerPattern=-1, const std::string targetPatternName="")
Definition: Recognizer.cpp:47
std::vector< unsigned > objectCounts
std::vector< std::string > getRecordedPatternNames() const
void createTablesIfNecessary() const
Definition: TableHelper.cpp:80
this namespace contains all generally usable classes.
void printTestResult(const TestResult &r)
boost::shared_ptr< Object > ObjectPtr
Definition: Object.hpp:82
int main(int argc, char **argv)
const RecordedPatternPtr getRecordedPattern(const std::string &patternName) const


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40