30 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
56 testToRun = getProperty<std::string>(
"TestToRun").getValue();
62 std::cout <<
"Starting CommonPlacesTester" << std::endl;
67 memoryPrx = getProxy<WorkingMemoryInterfacePrx>(
"WorkingMemory");
68 longtermMemoryPrx = getProxy<LongtermMemoryInterfacePrx>(
"LongtermMemory");
69 cpLearnerPrx = getProxy<CommonPlacesLearnerInterfacePrx>(
"CommonPlacesLearner");
71 ltmSegmentName = getProperty<std::string>(
"LTMSegment").getValue();
72 cpLearnerPrx->setLTMSegmentName(ltmSegmentName);
74 objectToLoad = getProperty<std::string>(
"ObjectToLoad").getValue();
77 const std::string snapshotName = getProperty<std::string>(
"SnapshotToLoad").getValue();
79 if (!snapshotName.empty())
81 longtermMemoryPrx->loadWorkingMemorySnapshot(snapshotName, memoryPrx);
82 ARMARX_INFO <<
"Snapshot succcessfully loaded: " << snapshotName;
85 if (testToRun ==
"LearnFromSnapshot")
87 testLearnFromSnapshot();
89 else if (testToRun ==
"LearnFromSingleSnapshot")
91 testLearnFromSingleSnapshot();
93 else if (testToRun ==
"LoadLTM")
97 else if (testToRun ==
"Clustering")
101 else if (testToRun ==
"Merging")
105 else if (testToRun ==
"Aging")
109 else if (testToRun ==
"Aging2")
113 else if (testToRun ==
"ClusteringBatch")
115 testClusteringBatch();
117 else if (testToRun ==
"GetClusterLocations")
119 testGetClusterLocations();
124 std::cout <<
"Unknown test name: " << testToRun << std::endl;
129 void CommonPlacesTester::testLearnFromSnapshot()
131 const std::string snapshotName = getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
132 const bool loadToWM = getProperty<bool>(
"LoadToWM").getValue();
133 const int initDelay = getProperty<int>(
"InitialDelay").getValue();
134 const int delay = getProperty<int>(
"DelayBetweenSnapshots").getValue();
136 if (!snapshotName.empty())
138 std::this_thread::sleep_for(std::chrono::milliseconds(initDelay));
142 for (NameList::const_iterator it = snapNameList.begin(); it != snapNameList.end(); ++it)
144 cpLearnerPrx->learnFromSnapshot(*it);
145 ARMARX_INFO <<
"Snapshot succcessfully processed by CommonPlacesLearner: " << *it;
152 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
157 ARMARX_ERROR <<
"SnapshotsToLearnFrom parameter must be specified! ";
161 void CommonPlacesTester::testLearnFromSingleSnapshot()
163 const std::string snapshotName = getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
164 const bool loadToWM = getProperty<bool>(
"LoadToWM").getValue();
165 const int initDelay = getProperty<int>(
"InitialDelay").getValue();
166 const int delay = getProperty<int>(
"DelayBetweenSnapshots").getValue();
168 std::this_thread::sleep_for(std::chrono::milliseconds(initDelay));
170 WorkingMemorySnapshotInterfacePrx snapshot = longtermMemoryPrx->openWorkingMemorySnapshot(snapshotName);
174 ARMARX_ERROR <<
"Snapshot not found in LTM: " << snapshotName;
178 const std::string segmentName =
"objectInstances";
179 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
183 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
187 EntityIdList ids = segInstances->getAllEntityIds();
190 struct EntityIdComparator
192 static bool compare(
const std::string& i,
const std::string& j)
194 return (std::stoi(i) < std::stoi(j));
199 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
201 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
203 cpLearnerPrx->learnFromObject(snapInstance);
204 ARMARX_INFO <<
"Object processed by CommonPlacesLearner: " << *it;
213 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
217 void CommonPlacesTester::testLoadLTM()
219 ObjectInstanceMemorySegmentBasePrx wmObjectsSeg = memoryPrx->getObjectInstancesSegment();
220 PersistentEntitySegmentBasePrx ltmObjectsSeg = longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName,
false);
224 if (!objectToLoad.empty())
226 EntityBasePtr obj = ltmObjectsSeg->getEntityByName(objectToLoad);
227 EntityBasePtr oldWMObj = wmObjectsSeg->getEntityByName(objectToLoad);
233 wmObjectsSeg->updateEntity(oldWMObj->getId(), obj);
237 wmObjectsSeg->addEntity(obj);
240 std::cout <<
"Loaded from LTM: " << ObjectInstancePtr::dynamicCast(obj) << std::endl;
244 ARMARX_ERROR <<
"Entity not found in LTM: " << objectToLoad;
249 EntityIdList ids = ltmObjectsSeg->getAllEntityIds();
251 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
253 wmObjectsSeg->addEntity(ltmObjectsSeg->getEntityById(*it));
259 ARMARX_ERROR <<
"LTM segment not found: " << ltmSegmentName;
263 void CommonPlacesTester::testClustering()
265 const bool loadToWM = getProperty<bool>(
"LoadToWM").getValue();
272 float maxDeviation = getProperty<float>(
"ClusterMaxDeviation").getValue();
273 std::string deviationTypeStr = getProperty<std::string>(
"ClusterDeviationType").getValue();
274 DeviationType devType = getDeviationTypeFromString(deviationTypeStr);
287 ObjectInstanceMemorySegmentBasePrx wmObjectsSeg = memoryPrx->getObjectInstancesSegment();
289 mstimer.time_start();
290 GaussianMixtureDistributionPtr posDistr = GaussianMixtureDistributionPtr::dynamicCast(cpLearnerPrx->getPositionReducedByMaxDeviation(objectToLoad, maxDeviation, devType));
291 ARMARX_INFO <<
"Elapsed time for reduction: " << mstimer.time_stop() <<
" ms" << std::endl;
295 ARMARX_INFO <<
"Reduced to: " << posDistr->output() << std::endl;
297 EntityBasePtr oldWMObj = wmObjectsSeg->getEntityByName(objectToLoad +
"GaussPos");
301 wmObjectsSeg->removeEntity(oldWMObj->getId());
305 obj->setClass(objectToLoad, 1.f);
306 NormalDistributionPtr mode = NormalDistributionPtr::dynamicCast(posDistr->getModalComponent().gaussian);
308 obj->setPosition(curPos);
309 obj->getPositionAttribute()->setValueWithUncertainty(obj->getPositionAttribute()->getValue(), posDistr);
313 obj->setOrientation(orient);
315 obj->putAttribute(
"UncertaintyColorMap",
"eHot");
316 obj->putAttribute(
"UncertaintyTransparency", 0.7f);
318 wmObjectsSeg->addEntity(obj);
322 ARMARX_INFO <<
"Unable to get object pos gaussian! " << std::endl;
327 void CommonPlacesTester::testMerging()
329 const std::string snapshotName = getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
332 const int itCount = getProperty<int>(
"IterationCount").getValue();
334 const int THRESHOLD_COUNT = 11;
335 const float thresholds[THRESHOLD_COUNT] = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
336 float grandTotalDensity[THRESHOLD_COUNT];
337 float grandTotalKL[THRESHOLD_COUNT];
338 float grandTotalISD[THRESHOLD_COUNT];
339 float grandReductionRate[THRESHOLD_COUNT];
341 for (
int i = 0; i < THRESHOLD_COUNT; ++i)
343 grandTotalDensity[i] = 0;
345 grandTotalISD[i] = 0;
346 grandReductionRate[i] = 0;
350 ISDDistance isdDistance;
354 for (NameList::const_iterator it = snapNameList.begin(); it != snapNameList.end(); ++it)
356 WorkingMemorySnapshotInterfacePrx snapshot = longtermMemoryPrx->openWorkingMemorySnapshot(*it);
360 ARMARX_ERROR <<
"Snapshot not found in LTM: " << snapshotName;
364 const std::string segmentName =
"objectInstances";
365 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
369 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
373 EntityIdList ids = segInstances->getAllEntityIds();
375 std::ofstream snfile;
376 const std::string fname =
"m_" + (*it) +
".csv";
377 snfile.open(fname.c_str());
379 for (
int t = 0; t < THRESHOLD_COUNT; ++t)
381 cpLearnerPrx->setMergingThreshold(thresholds[t]);
383 float totalDensity = 0., totalKL = 0., totalISD = 0.;
384 int origComp = 0, mergedComp = 0;
386 for (
int i = 0; i < itCount; ++i)
391 std::random_shuffle(ids.begin(), ids.end());
393 longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName,
false)->clear();
395 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
397 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
399 GaussianMixtureDistributionPtr gmm = GaussianMixtureDistributionPtr::dynamicCast(cpLearnerPrx->getPositionFull(snapInstance->getMostProbableClass()));
403 totalDensity += gmm->getDensity(snapInstance->getPosition()->toEigen());
408 cpLearnerPrx->learnFromObject(snapInstance);
415 cpLearnerPrx->setLTMSegmentName(ltmSegmentName +
"2");
416 longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName +
"2",
false)->clear();
417 cpLearnerPrx->setMergingThreshold(0.);
419 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
421 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
423 cpLearnerPrx->learnFromObject(snapInstance);
427 origComp += origGMM->size();
428 mergedComp += mergedGMM->size();
429 totalISD += isdDistance.getDistance(mergedGMM, origGMM,
true);
430 cpLearnerPrx->setLTMSegmentName(ltmSegmentName);
434 const float redRate =
float(mergedComp) /
float(origComp);
437 snfile << thresholds[t] <<
"," << totalDensity <<
"," << totalISD <<
438 "," <<
float(origComp) / itCount <<
"," <<
float(mergedComp) / itCount
442 grandTotalDensity[t] += totalDensity;
443 grandTotalISD[t] += totalISD;
444 grandTotalKL[t] += totalKL;
445 grandReductionRate[t] += redRate;
453 std::ofstream totfile;
454 totfile.open(
"m_totals.csv");
456 for (
int t = 0; t < THRESHOLD_COUNT; ++t)
457 totfile << thresholds[t] <<
"," << (grandTotalDensity[t] / snapNameList.size()) <<
458 "," << (grandTotalISD[t] / snapNameList.size()) <<
459 "," << (grandReductionRate[t] / snapNameList.size())
466 void CommonPlacesTester::testAging()
468 const std::string snapshotName = getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
470 const int itCount = getProperty<int>(
"IterationCount").getValue();
472 const int COEFF_COUNT = 10;
473 const float thresholds[COEFF_COUNT] = {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1};
474 float grandTotalMah[COEFF_COUNT];
475 int grandTotalCorrect[COEFF_COUNT];
476 int totalObjects = 0;
478 for (
int i = 0; i < COEFF_COUNT; ++i)
480 grandTotalMah[i] = 0;
481 grandTotalCorrect[i] = 0;
485 ISDDistance isdDistance;
486 MahalanobisDistance mahDistance;
490 for (NameList::const_iterator it = snapNameList.begin(); it != snapNameList.end(); ++it)
492 WorkingMemorySnapshotInterfacePrx snapshot = longtermMemoryPrx->openWorkingMemorySnapshot(*it);
500 const std::string segmentName =
"objectInstances";
501 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
505 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
511 EntityIdList ids = segInstances->getAllEntityIds();
513 totalObjects += ids.size();
515 for (
int t = 0; t < COEFF_COUNT; ++t)
517 cpLearnerPrx->setAgingFactor(thresholds[t]);
520 int corrClusters = 0;
522 for (
int i = 0; i < itCount; ++i)
527 std::random_shuffle(ids.begin(), ids.end());
529 longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName,
false)->clear();
531 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
533 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
535 GaussianMixtureDistributionPtr gmm = GaussianMixtureDistributionPtr::dynamicCast(cpLearnerPrx->getPositionReducedByMaxDeviation(objectToLoad, 150, eDevAABB));
539 totalMah += mahDistance.getWeigtedDistance(gmm, snapInstance->getPositionUncertainty());
542 GaussianMixtureComponent
c = gmm->getComponent(0);
544 const Eigen::VectorXf obsMean = snapInstance->getPosition()->toEigen();
545 Eigen::Vector3f sigma;
546 sigma << sqrtf(g->getCovariance(0, 0)), sqrtf(g->getCovariance(1, 1)), sqrtf(g->getCovariance(2, 2));
547 const Eigen::VectorXf triSigmaPoint = g->toEigenMean() + 3 * sigma;
549 if (g->getDensity(obsMean) > g->getDensity(triSigmaPoint))
555 cpLearnerPrx->learnFromObject(snapInstance);
561 const float avgCorrect =
float(corrClusters) / itCount;
563 grandTotalMah[t] += totalMah;
564 grandTotalCorrect[t] += avgCorrect;
571 std::ofstream totfile;
572 totfile.open(
"a_totals.csv");
574 for (
int t = 0; t < COEFF_COUNT; ++t)
575 totfile << thresholds[t] <<
"," << grandTotalMah[t] <<
576 "," << grandTotalMah[t] / totalObjects <<
577 "," << grandTotalCorrect[t] <<
578 "," <<
float(grandTotalCorrect[t]) / totalObjects <<
585 void CommonPlacesTester::testAging2()
587 const std::string snapshotName = getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
589 const int itCount = getProperty<int>(
"IterationCount").getValue();
591 const int COEFF_COUNT = 10;
592 const float thresholds[COEFF_COUNT] = {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1};
593 float grandTotalMah[COEFF_COUNT];
594 int grandTotalCorrect[COEFF_COUNT];
595 int totalObjects = 0;
597 for (
int i = 0; i < COEFF_COUNT; ++i)
599 grandTotalMah[i] = 0;
600 grandTotalCorrect[i] = 0;
603 MahalanobisDistance mahDistance;
607 for (
int t = 0; t < COEFF_COUNT; ++t)
609 cpLearnerPrx->setAgingFactor(thresholds[t]);
612 int corrClusters = 0;
614 for (
int i = 0; i < itCount; ++i)
616 longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName,
false)->clear();
618 for (NameList::const_iterator it = snapNameList.begin(); it != snapNameList.end(); ++it)
620 WorkingMemorySnapshotInterfacePrx snapshot = longtermMemoryPrx->openWorkingMemorySnapshot(*it);
628 const std::string segmentName =
"objectInstances";
629 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
633 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
639 EntityIdList ids = segInstances->getAllEntityIds();
641 if (t == 0 && i == 0)
643 totalObjects += ids.size();
646 std::random_shuffle(ids.begin(), ids.end());
648 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
650 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
652 GaussianMixtureDistributionPtr gmm = GaussianMixtureDistributionPtr::dynamicCast(cpLearnerPrx->getPositionReducedByMaxDeviation(objectToLoad, 150, eDevAABB));
656 totalMah += mahDistance.getWeigtedDistance(gmm, snapInstance->getPositionUncertainty());
659 GaussianMixtureComponent
c = gmm->getModalComponent();
661 const Eigen::VectorXf obsMean = snapInstance->getPosition()->toEigen();
662 Eigen::Vector3f sigma;
663 sigma << sqrtf(g->getCovariance(0, 0)), sqrtf(g->getCovariance(1, 1)), sqrtf(g->getCovariance(2, 2));
664 const Eigen::VectorXf triSigmaPoint = g->toEigenMean() + 3 * sigma;
666 if (g->getDensity(obsMean) > g->getDensity(triSigmaPoint))
672 cpLearnerPrx->learnFromObject(snapInstance);
682 const float avgCorrect =
float(corrClusters) / itCount;
684 grandTotalMah[t] += totalMah;
685 grandTotalCorrect[t] += avgCorrect;
690 std::ofstream totfile;
691 totfile.open(
"a_totals.csv");
693 for (
int t = 0; t < COEFF_COUNT; ++t)
694 totfile << thresholds[t] <<
"," << grandTotalMah[t] <<
695 "," << grandTotalMah[t] / totalObjects <<
696 "," << grandTotalCorrect[t] <<
697 "," <<
float(grandTotalCorrect[t]) / totalObjects <<
704 void CommonPlacesTester::testGetClusterLocations()
706 Cluster3DList clusterList = cpLearnerPrx->getPositionClustersByComponentCount(objectToLoad, 3);
708 if (clusterList.size() > 0)
710 for (Cluster3DList::iterator it = clusterList.begin(); it != clusterList.end(); it++)
712 Cluster3D& entry = *it;
713 ARMARX_IMPORTANT <<
"object center: x:" << entry.center.x <<
" y: " << entry.center.y <<
" with weight: " << entry.weight;
723 void CommonPlacesTester::testClusteringBatch()
725 const std::string snapshotName = getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
727 const int DEV_COUNT = 11;
728 const float thresholds[DEV_COUNT] = {10., 30., 50., 75., 100., 150., 200., 400., 800., 1000., 2000.};
733 std::ofstream snfile;
734 const std::string fname =
"c_" + ltmSegmentName +
"_" + objectToLoad +
".csv";
735 snfile.open(fname.c_str());
737 for (
int t = 0; t < DEV_COUNT; ++t)
739 ARMARX_INFO <<
"Processing dev: " << thresholds[t];
741 GaussianMixtureDistributionBasePtr gmmAABB = cpLearnerPrx->getPositionReducedByMaxDeviation(objectToLoad, thresholds[t], eDevAABB);
742 GaussianMixtureDistributionBasePtr gmmOBB = cpLearnerPrx->getPositionReducedByMaxDeviation(objectToLoad, thresholds[t], eDevOrientedBBox);
743 GaussianMixtureDistributionBasePtr gmmSphere = cpLearnerPrx->getPositionReducedByMaxDeviation(objectToLoad, thresholds[t], eDevEqualSphere);
746 snfile << thresholds[t] <<
"," << gmmAABB->size() <<
"," <<
747 gmmOBB->size() <<
"," <<
748 gmmSphere->size() <<
"," << std::endl;
757 void CommonPlacesTester::getRandomIds(std::vector<std::string>& allIds, std::vector<std::string>& ids,
int sampleSize)
759 if (sampleSize > (
int)(allIds.size()))
761 ids.assign(allIds.begin(), allIds.end());
765 std::set<std::string> idset;
767 while (idset.size() < (
size_t) sampleSize)
769 const size_t ind = (size_t) truncf(((
float) rand()) /
float(RAND_MAX) *
float(allIds.size()));
770 std::cout <<
"index to add" << ind <<
std::flush;
771 idset.insert(allIds[ind]);
774 ids.assign(idset.begin(), idset.end());
778 DeviationType CommonPlacesTester::getDeviationTypeFromString(
const std::string& devTypeStr)
781 if (devTypeStr ==
"AABB")
785 else if (devTypeStr ==
"OrientedBBox")
787 return eDevOrientedBBox;
789 else if (devTypeStr ==
"EqualSphere")
791 return eDevEqualSphere;