20 #include <boost/filesystem/path.hpp> 21 #include <boost/lexical_cast.hpp> 22 #include "boost/date_time/posix_time/posix_time.hpp" 23 #include "boost/date_time/posix_time/posix_time_io.hpp" 24 #include "boost/algorithm/string.hpp" 38 using boost::filesystem::path;
45 if (!boost::filesystem::exists(
mDBPath))
48 ss <<
mDBPath <<
" does not exist!";
49 throw std::runtime_error(ss.str());
53 if(!boost::filesystem::is_directory(mOutputDataPath))
56 ss << mOutputDataPath <<
" does not exist or is not a directory!";
57 throw std::runtime_error(ss.str());
61 mOutputDataPath /= runName;
73 std::string logFileName =
"Log_" + runName +
".txt";
74 path logFilePath = mOutputDataPath /
"Logfile" / logFileName;
85 if (storeValidTestSets || storeInvalidTestSets)
88 if (storeValidTestSets)
93 if (storeInvalidTestSets)
116 std::map<std::string, std::pair<double, TreePtr> > bestPerPattern;
117 std::map<std::string, IsmPtr> bestISMPerPattern;
122 std::pair<double, TreePtr> currentBest;
125 double bestEvaluationResult = std::numeric_limits<double>::max();
127 for (
unsigned int i = 0; i < startTopologies.size(); ++i)
129 std::pair<double, TreePtr> result =
130 optimizationRunner->runOptimization(
patternName, startTopologies[i]);
132 if (result.first >= 0 && result.first < bestEvaluationResult)
134 currentBest = result;
135 bestEvaluationResult = result.first;
141 currentBest = optimizationRunner->runOptimization(
patternName);
144 if (currentBest.second)
146 IsmPtr bestISM = currentBest.second->getISM();
148 bestPerPattern.insert(std::make_pair(
patternName, currentBest));
149 bestISMPerPattern.insert(std::make_pair(
patternName, bestISM));
161 return bestPerPattern;
167 std::vector<ISM::ObjectSetPtr> objectsInCurrentPattern;
170 for (std::string& patternNameIt : patternNames)
172 objectsInCurrentPattern =
tableHelper->getRecordedPattern(patternNameIt)->objectSets;
174 objectTracksPerPattern.insert(std::make_pair(patternNameIt, tracksInCurrentPattern));
175 for (
size_t it = 0; it < tracksInCurrentPattern->tracks.size(); ++it)
177 if (tracksInCurrentPattern->tracks[it]->objects.size() > 0)
179 bool foundObjectModel =
false;
180 for(
ObjectPtr& o : tracksInCurrentPattern->tracks[it]->objects)
185 std::make_pair(o->type, o->ressourcePath));
186 foundObjectModel =
true;
190 if(!foundObjectModel)
192 std::cerr <<
"CombinatorialTrainer::getRecordedObjectsTracks: " 193 <<
" Not one Object in track." << std::endl;
196 if (it < tracksInCurrentPattern->tracks.size() - 1 &&
197 tracksInCurrentPattern->tracks[it]->objects.size() !=
198 tracksInCurrentPattern->tracks[it + 1]->objects.size())
200 std::cerr<<
"Corrupt database\n";
215 std::vector<ISM::ObjectRelationPtr> selfiesInCurrentPattern;
216 unsigned relationId = 0;
219 for (
ISM::TrackPtr& otherTracksIt : patternIt.second->tracks)
221 if (otherTracksIt->observedId != tracksIt->observedId || otherTracksIt->type != tracksIt->type)
223 bool alreadyThere =
false;
224 for (
const std::pair<unsigned int, ISM::ObjectRelationPtr>& inCurrentPatternIt : inCurrentPattern)
226 if ((inCurrentPatternIt.second->getObjectIdA() == tracksIt->observedId &&
227 inCurrentPatternIt.second->getObjectIdB() == otherTracksIt->observedId &&
228 inCurrentPatternIt.second->getObjectTypeA() == tracksIt->type &&
229 inCurrentPatternIt.second->getObjectTypeB() == otherTracksIt->type)
230 || (inCurrentPatternIt.second->getObjectIdA() == otherTracksIt->observedId &&
231 inCurrentPatternIt.second->getObjectIdB() == tracksIt->observedId &&
232 inCurrentPatternIt.second->getObjectTypeA() == otherTracksIt->type &&
233 inCurrentPatternIt.second->getObjectTypeB() == tracksIt->type))
243 inCurrentPattern.insert(std::make_pair(relationId++, objectRelation));
250 selfiesInCurrentPattern.push_back(objectRelation);
255 allObjectRelationsPerPattern.insert(std::make_pair(patternIt.first, inCurrentPattern));
265 for (
const std::pair<std::string, ISM::ObjectRelations>& pattern : allObjectRelationsPerPattern)
267 for (
const std::pair<unsigned int, ISM::ObjectRelationPtr>& relation : pattern.second)
269 bool appeared =
false;
270 bool disappeared =
false;
271 bool reappeared =
false;
272 for (
const std::pair<unsigned int, ISM::ObjectRelationPtr>& otherRelation : pattern.second)
274 if (otherRelation.second->getObjectTypeA() == relation.second->getObjectTypeA() &&
275 otherRelation.second->getObjectIdA() == relation.second->getObjectIdA())
289 for (
const std::pair<unsigned int, ISM::ObjectRelationPtr>& it : pattern.second)
291 std::cout<<it.second<<std::endl;
312 bool hasAppearance =
false;
313 for (
const std::pair<unsigned int, ISM::ObjectRelationPtr>& relation : topology)
315 if (relation.second->containsObject(object->type, object->observedId))
317 hasAppearance =
true;
320 if (hasAppearance ==
false)
331 std::map<std::string, IsmPtr> fullyMeshedTopologyPerPattern;
337 fullyMeshedTopologyPerPattern[patternNameIt] = fullyMeshedTopology->getISM();;
343 return fullyMeshedTopologyPerPattern;
347 const path dbFilePath,
348 const std::string & type)
350 if(!boost::filesystem::exists(dbFilePath.parent_path()))
351 boost::filesystem::create_directories(dbFilePath.parent_path());
355 localTableHelper->dropTables();
356 localTableHelper->createTablesIfNecessary();
357 for (PatternNameToObjectSet::iterator testSetIt = testSet.begin();
358 testSetIt != testSet.end();
362 + testSetIt->first +
" to " + dbFilePath.string());
364 localTableHelper->insertRecordedPattern(testSetIt->first);
365 std::vector<ISM::ObjectSetPtr> objectSets = testSetIt->second;
366 for (
unsigned int i = 0; i < objectSets.size(); ++i) {
368 localTableHelper->insertRecordedObjectSet(objectSets[i], testSetIt->first);
371 std::cout << std::endl;
377 +
" used to store evaluation results in storeISMToDB in CombinatorialTrainer and" 378 +
" setTestSets in Tester does not exist on your system",
LOG_ERROR);
379 std::cerr <<
"soci error\n" << e.what() << std::endl;
386 path dbfilename = fileName;
391 for (
unsigned int i = 0; i < patternNames.size(); ++i)
393 testSet[patternNames[i]] = localTableHelper->getRecordedPattern(patternNames[i])->objectSets;
395 " test sets for pattern " + patternNames[i] +
" from DB " + fileName);
402 double binSize,
double maxAngleDeviation,
double confidenceThreshold,
unsigned int testSetCount)
419 std::pair<std::vector<ObjectSetPtr>, std::vector<ObjectSetPtr>> testSets =
420 testSetGenerator->generateTestSets(patternIt->first, patternIt->second,
423 validTestSetsPerPattern[patternIt->first] = testSets.first;
424 invalidTestSetsPerPattern[patternIt->first] = testSets.second;
427 return std::make_pair(validTestSetsPerPattern, invalidTestSetsPerPattern);
431 double confidenceThreshold, std::string loadValidTestSetsFrom,
432 std::string loadInvalidTestSetsFrom,
unsigned int testSetCount)
437 bool createInvalidTestSet =
true;
438 bool createValidTestSet =
true;
440 if (loadValidTestSetsFrom.compare(
"") != 0) {
441 createValidTestSet =
false;
445 if (loadInvalidTestSetsFrom.compare(
"") != 0) {
446 createInvalidTestSet =
false;
450 if (createInvalidTestSet || createValidTestSet)
452 std::pair<PatternNameToObjectSet, PatternNameToObjectSet> testSets =
453 createTestSets(binSize, maxAngleDeviation, confidenceThreshold, testSetCount);
455 if (createValidTestSet) validTestSetsPerPattern = testSets.first;
456 if (createInvalidTestSet) invalidTestSetsPerPattern = testSets.second;
461 mTestSets = std::make_pair(validTestSetsPerPattern, invalidTestSetsPerPattern);
467 struct tm * timeinfo;
471 timeinfo = localtime(&rawtime);
473 strftime(buffer, 80,
"%d-%b-%Y_%H:%M:%S", timeinfo);
474 return std::string(buffer);
479 if (loadStartTopologiesFrom.compare(
"") != 0)
481 path from = path(loadStartTopologiesFrom);
482 if (!boost::filesystem::exists(from))
484 std::stringstream ss;
485 ss << loadStartTopologiesFrom <<
" does not exist!";
486 throw std::runtime_error(ss.str());
491 file.open(from.string());
493 while (std::getline(file, line))
495 std::vector<std::string> tokens;
496 boost::split(tokens, line, boost::is_any_of(
":"));
502 std::vector<bool> bitvector(allObjectRelations.size(), 0) ;
504 for (
unsigned int i = 1; i < tokens.size(); ++i)
506 unsigned int index = boost::lexical_cast<
unsigned int>(tokens[i]);
507 objectRelations[index] = allObjectRelations[index];
508 bitvector[index] = 1;
511 std::string identifier =
"";
512 for (
unsigned int i = 0; i < bitvector.size(); ++i)
514 identifier += bitvector[i] ?
"1" :
"0";
522 topology->objectRelations = objectRelations;
523 topology->identifier = identifier;
boost::shared_ptr< Recognizer > RecognizerPtr
std::pair< PatternNameToObjectSet, PatternNameToObjectSet > mTestSets
unsigned int testSetCount
CombinatorialTrainerParameters mParams
std::string storeInvalidTestSetsTo
static void logLine(LogLevel logLevel=LOG_INFO)
std::map< std::string, ISM::ObjectRelations > allObjectRelationsPerPattern
std::string loadValidTestSetsFrom
struct TopologyGeneratorParameters topologyGenerator
std::string outputDataPath
TableHelperPtr tableHelper
boost::shared_ptr< ISM::ObjectRelation > ObjectRelationPtr
static void displayProgress(double progress)
struct OptimizationAlgorithmParameters optimizationAlgorithm
bool checkCorrectOrder(std::map< std::string, ISM::ObjectRelations > allObjectRelationsPerPattern)
struct ISM::CombinatorialTrainerParameters::@0 general
bool containsAllObjects(const ISM::ObjectRelations &topology, const std::string &patternName) const
boost::shared_ptr< TableHelper > TableHelperPtr
std::pair< PatternNameToObjectSet, PatternNameToObjectSet > createTestSets(double binSize, double maxAngleDeviation, double confidenceThreshold, unsigned int testSetCount)
std::map< std::string, std::map< std::string, boost::filesystem::path > > objectModelsPerPattern
boost::shared_ptr< Topology > TopologyPtr
std::map< std::string, std::vector< ObjectSetPtr > > PatternNameToObjectSet
void initStartTopologiesPerPattern(std::string loadStartTopologiesFrom)
void storeTestSetsToDB(PatternNameToObjectSet testSet, const path dbFilePath, const std::string &type)
static void logMessage(const std::string &message, LogLevel logLevel=LOG_INFO, const char *logColor=LOG_COLOR_DEFAULT)
boost::shared_ptr< ObjectSetValidator > ObjectSetValidatorPtr
const std::map< std::string, ISM::TracksPtr > getRecordedObjectsTracks()
std::map< unsigned int, ISM::ObjectRelationPtr, std::less< unsigned > > ObjectRelations
std::map< std::string, std::vector< ISM::ObjectRelationPtr > > allSelfRelationsPerPattern
struct TreeValidatorParameters treeValidator
boost::shared_ptr< ImplicitShapeModel > IsmPtr
struct CostFunctionParameters costFunction
boost::shared_ptr< DocumentationHelper > DocumentationHelperPtr
boost::shared_ptr< OptimizationRunner > OptimizationRunnerPtr
std::string loadStartTopologiesFrom
CombinatorialTrainer(CombinatorialTrainerParameters params)
boost::shared_ptr< Tree > TreePtr
static OptimizationRunnerPtr createOptimizationRunner(EvaluatorParameters evaluatorParams, TopologyGeneratorParameters topologyGeneratorParams, OptimizationAlgorithmParameters optimizationAlgorithmParameters, CostFunctionParameters costFunctionParameters, TreeValidatorParameters treeValidatorParams, std::pair< PatternNameToObjectSet, PatternNameToObjectSet > testSets, std::map< std::string, ISM::ObjectRelations > allObjectRelationsPerPattern, DocumentationHelperPtr documentationHelper, bool storeFullyMeshedISM, bool storeStartTopologyISM)
std::map< std::string, std::pair< double, TreePtr > > learn()
double confidenceThreshold
void initTestSets(double binSize, double maxAngleDeviation, double confidenceThreshold, std::string loadValidTestSetsFrom, std::string loadInvalidTestSetsFrom, unsigned int testSetCount)
static void init(path logFilePath, LogLevel level)
boost::shared_ptr< Tracks > TracksPtr
std::map< std::string, std::vector< TopologyPtr > > mStartTopologiesPerPattern
DocumentationHelperPtr mDocumentationHelper
PatternNameToObjectSet loadTestSetsFromDB(std::string fileName)
boost::shared_ptr< TestSetGenerator > TestSetGeneratorPtr
const std::map< std::string, ISM::ObjectRelations > calculateAllObjectRelations()
std::string genTimeString()
std::map< std::string, IsmPtr > mFullyMeshedTopologyPerPattern
static const char * LOG_COLOR_RED
std::vector< std::string > patternNames
boost::shared_ptr< Track > TrackPtr
std::map< std::string, ISM::TracksPtr > objectTracksPerPattern
this namespace contains all generally usable classes.
std::string loadInvalidTestSetsFrom
struct EvaluatorParameters evaluator
std::map< std::string, IsmPtr > learnFullyMeshedTopologyPerPattern(bool naive=false)
std::string storeValidTestSetsTo
bool storeStartTopologyISM
boost::shared_ptr< Object > ObjectPtr