30 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
57 testToRun = getProperty<std::string>(
"TestToRun").getValue();
63 std::cout <<
"Starting CommonPlacesTester" << std::endl;
68 memoryPrx = getProxy<WorkingMemoryInterfacePrx>(
"WorkingMemory");
69 longtermMemoryPrx = getProxy<LongtermMemoryInterfacePrx>(
"LongtermMemory");
70 cpLearnerPrx = getProxy<CommonPlacesLearnerInterfacePrx>(
"CommonPlacesLearner");
72 ltmSegmentName = getProperty<std::string>(
"LTMSegment").getValue();
73 cpLearnerPrx->setLTMSegmentName(ltmSegmentName);
75 objectToLoad = getProperty<std::string>(
"ObjectToLoad").getValue();
78 const std::string snapshotName = getProperty<std::string>(
"SnapshotToLoad").getValue();
80 if (!snapshotName.empty())
82 longtermMemoryPrx->loadWorkingMemorySnapshot(snapshotName, memoryPrx);
83 ARMARX_INFO <<
"Snapshot succcessfully loaded: " << snapshotName;
86 if (testToRun ==
"LearnFromSnapshot")
88 testLearnFromSnapshot();
90 else if (testToRun ==
"LearnFromSingleSnapshot")
92 testLearnFromSingleSnapshot();
94 else if (testToRun ==
"LoadLTM")
98 else if (testToRun ==
"Clustering")
102 else if (testToRun ==
"Merging")
106 else if (testToRun ==
"Aging")
110 else if (testToRun ==
"Aging2")
114 else if (testToRun ==
"ClusteringBatch")
116 testClusteringBatch();
118 else if (testToRun ==
"GetClusterLocations")
120 testGetClusterLocations();
125 std::cout <<
"Unknown test name: " << testToRun << std::endl;
130 CommonPlacesTester::testLearnFromSnapshot()
132 const std::string snapshotName =
133 getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
134 const bool loadToWM = getProperty<bool>(
"LoadToWM").getValue();
135 const int initDelay = getProperty<int>(
"InitialDelay").getValue();
136 const int delay = getProperty<int>(
"DelayBetweenSnapshots").getValue();
138 if (!snapshotName.empty())
140 std::this_thread::sleep_for(std::chrono::milliseconds(initDelay));
144 for (NameList::const_iterator it = snapNameList.begin(); it != snapNameList.end(); ++it)
146 cpLearnerPrx->learnFromSnapshot(*it);
147 ARMARX_INFO <<
"Snapshot succcessfully processed by CommonPlacesLearner: " << *it;
154 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
159 ARMARX_ERROR <<
"SnapshotsToLearnFrom parameter must be specified! ";
164 CommonPlacesTester::testLearnFromSingleSnapshot()
166 const std::string snapshotName =
167 getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
168 const bool loadToWM = getProperty<bool>(
"LoadToWM").getValue();
169 const int initDelay = getProperty<int>(
"InitialDelay").getValue();
170 const int delay = getProperty<int>(
"DelayBetweenSnapshots").getValue();
172 std::this_thread::sleep_for(std::chrono::milliseconds(initDelay));
174 WorkingMemorySnapshotInterfacePrx snapshot =
175 longtermMemoryPrx->openWorkingMemorySnapshot(snapshotName);
179 ARMARX_ERROR <<
"Snapshot not found in LTM: " << snapshotName;
183 const std::string segmentName =
"objectInstances";
184 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
188 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
192 EntityIdList ids = segInstances->getAllEntityIds();
195 struct EntityIdComparator
198 compare(
const std::string& i,
const std::string& j)
200 return (std::stoi(i) < std::stoi(j));
206 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
208 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
210 cpLearnerPrx->learnFromObject(snapInstance);
211 ARMARX_INFO <<
"Object processed by CommonPlacesLearner: " << *it;
220 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
225 CommonPlacesTester::testLoadLTM()
227 ObjectInstanceMemorySegmentBasePrx wmObjectsSeg = memoryPrx->getObjectInstancesSegment();
228 PersistentEntitySegmentBasePrx ltmObjectsSeg =
229 longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName,
false);
233 if (!objectToLoad.empty())
235 EntityBasePtr obj = ltmObjectsSeg->getEntityByName(objectToLoad);
236 EntityBasePtr oldWMObj = wmObjectsSeg->getEntityByName(objectToLoad);
242 wmObjectsSeg->updateEntity(oldWMObj->getId(), obj);
246 wmObjectsSeg->addEntity(obj);
249 std::cout <<
"Loaded from LTM: " << ObjectInstancePtr::dynamicCast(obj)
254 ARMARX_ERROR <<
"Entity not found in LTM: " << objectToLoad;
259 EntityIdList ids = ltmObjectsSeg->getAllEntityIds();
261 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
263 wmObjectsSeg->addEntity(ltmObjectsSeg->getEntityById(*it));
269 ARMARX_ERROR <<
"LTM segment not found: " << ltmSegmentName;
274 CommonPlacesTester::testClustering()
276 const bool loadToWM = getProperty<bool>(
"LoadToWM").getValue();
283 float maxDeviation = getProperty<float>(
"ClusterMaxDeviation").getValue();
284 std::string deviationTypeStr = getProperty<std::string>(
"ClusterDeviationType").getValue();
285 DeviationType devType = getDeviationTypeFromString(deviationTypeStr);
298 ObjectInstanceMemorySegmentBasePrx wmObjectsSeg = memoryPrx->getObjectInstancesSegment();
300 mstimer.time_start();
302 cpLearnerPrx->getPositionReducedByMaxDeviation(objectToLoad, maxDeviation, devType));
303 ARMARX_INFO <<
"Elapsed time for reduction: " << mstimer.time_stop() <<
" ms" << std::endl;
307 ARMARX_INFO <<
"Reduced to: " << posDistr->output() << std::endl;
309 EntityBasePtr oldWMObj = wmObjectsSeg->getEntityByName(objectToLoad +
"GaussPos");
313 wmObjectsSeg->removeEntity(oldWMObj->getId());
317 obj->setClass(objectToLoad, 1.f);
319 NormalDistributionPtr::dynamicCast(posDistr->getModalComponent().gaussian);
322 obj->setPosition(curPos);
323 obj->getPositionAttribute()->setValueWithUncertainty(
324 obj->getPositionAttribute()->getValue(), posDistr);
327 armarx::FramedOrientationBasePtr orient =
329 obj->setOrientation(orient);
331 obj->putAttribute(
"UncertaintyColorMap",
"eHot");
332 obj->putAttribute(
"UncertaintyTransparency", 0.7f);
334 wmObjectsSeg->addEntity(obj);
338 ARMARX_INFO <<
"Unable to get object pos gaussian! " << std::endl;
343 CommonPlacesTester::testMerging()
345 const std::string snapshotName =
346 getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
349 const int itCount = getProperty<int>(
"IterationCount").getValue();
351 const int THRESHOLD_COUNT = 11;
352 const float thresholds[THRESHOLD_COUNT] = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
353 float grandTotalDensity[THRESHOLD_COUNT];
354 float grandTotalKL[THRESHOLD_COUNT];
355 float grandTotalISD[THRESHOLD_COUNT];
356 float grandReductionRate[THRESHOLD_COUNT];
358 for (
int i = 0; i < THRESHOLD_COUNT; ++i)
360 grandTotalDensity[i] = 0;
362 grandTotalISD[i] = 0;
363 grandReductionRate[i] = 0;
367 ISDDistance isdDistance;
371 for (NameList::const_iterator it = snapNameList.begin(); it != snapNameList.end(); ++it)
373 WorkingMemorySnapshotInterfacePrx snapshot =
374 longtermMemoryPrx->openWorkingMemorySnapshot(*it);
378 ARMARX_ERROR <<
"Snapshot not found in LTM: " << snapshotName;
382 const std::string segmentName =
"objectInstances";
383 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
387 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
391 EntityIdList ids = segInstances->getAllEntityIds();
393 std::ofstream snfile;
394 const std::string fname =
"m_" + (*it) +
".csv";
395 snfile.open(fname.c_str());
397 for (
int t = 0; t < THRESHOLD_COUNT; ++t)
399 cpLearnerPrx->setMergingThreshold(thresholds[t]);
401 float totalDensity = 0., totalKL = 0., totalISD = 0.;
402 int origComp = 0, mergedComp = 0;
404 for (
int i = 0; i < itCount; ++i)
409 std::random_shuffle(ids.begin(), ids.end());
411 longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName,
false)->clear();
413 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
415 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
418 GaussianMixtureDistributionPtr::dynamicCast(
419 cpLearnerPrx->getPositionFull(
420 snapInstance->getMostProbableClass()));
424 totalDensity += gmm->getDensity(snapInstance->getPosition()->toEigen());
429 cpLearnerPrx->learnFromObject(snapInstance);
434 GaussianMixtureDistributionPtr::dynamicCast(
435 cpLearnerPrx->getPositionFull(objectToLoad));
438 cpLearnerPrx->setLTMSegmentName(ltmSegmentName +
"2");
439 longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName +
"2",
false)
441 cpLearnerPrx->setMergingThreshold(0.);
443 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
445 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
447 cpLearnerPrx->learnFromObject(snapInstance);
451 GaussianMixtureDistributionPtr::dynamicCast(
452 cpLearnerPrx->getPositionFull(objectToLoad));
453 origComp += origGMM->size();
454 mergedComp += mergedGMM->size();
455 totalISD += isdDistance.getDistance(mergedGMM, origGMM,
true);
456 cpLearnerPrx->setLTMSegmentName(ltmSegmentName);
460 const float redRate =
float(mergedComp) /
float(origComp);
463 snfile << thresholds[t] <<
"," << totalDensity <<
"," << totalISD <<
","
464 <<
float(origComp) / itCount <<
"," <<
float(mergedComp) / itCount <<
","
465 <<
float(mergedComp) /
float(origComp) << std::endl;
467 grandTotalDensity[t] += totalDensity;
468 grandTotalISD[t] += totalISD;
469 grandTotalKL[t] += totalKL;
470 grandReductionRate[t] += redRate;
478 std::ofstream totfile;
479 totfile.open(
"m_totals.csv");
481 for (
int t = 0; t < THRESHOLD_COUNT; ++t)
482 totfile << thresholds[t] <<
"," << (grandTotalDensity[t] / snapNameList.size()) <<
","
483 << (grandTotalISD[t] / snapNameList.size()) <<
","
484 << (grandReductionRate[t] / snapNameList.size()) << std::endl;
490 CommonPlacesTester::testAging()
492 const std::string snapshotName =
493 getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
495 const int itCount = getProperty<int>(
"IterationCount").getValue();
497 const int COEFF_COUNT = 10;
498 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};
499 float grandTotalMah[COEFF_COUNT];
500 int grandTotalCorrect[COEFF_COUNT];
501 int totalObjects = 0;
503 for (
int i = 0; i < COEFF_COUNT; ++i)
505 grandTotalMah[i] = 0;
506 grandTotalCorrect[i] = 0;
510 ISDDistance isdDistance;
511 MahalanobisDistance mahDistance;
515 for (NameList::const_iterator it = snapNameList.begin(); it != snapNameList.end(); ++it)
517 WorkingMemorySnapshotInterfacePrx snapshot =
518 longtermMemoryPrx->openWorkingMemorySnapshot(*it);
526 const std::string segmentName =
"objectInstances";
527 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
531 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
537 EntityIdList ids = segInstances->getAllEntityIds();
539 totalObjects += ids.size();
541 for (
int t = 0; t < COEFF_COUNT; ++t)
543 cpLearnerPrx->setAgingFactor(thresholds[t]);
546 int corrClusters = 0;
548 for (
int i = 0; i < itCount; ++i)
553 std::random_shuffle(ids.begin(), ids.end());
555 longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName,
false)->clear();
557 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
559 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
562 GaussianMixtureDistributionPtr::dynamicCast(
563 cpLearnerPrx->getPositionReducedByMaxDeviation(
564 objectToLoad, 150, eDevAABB));
568 totalMah += mahDistance.getWeigtedDistance(
569 gmm, snapInstance->getPositionUncertainty());
572 GaussianMixtureComponent
c = gmm->getComponent(0);
574 NormalDistributionPtr::dynamicCast(
c.gaussian);
575 const Eigen::VectorXf obsMean = snapInstance->getPosition()->toEigen();
576 Eigen::Vector3f sigma;
577 sigma << sqrtf(g->getCovariance(0, 0)), sqrtf(g->getCovariance(1, 1)),
578 sqrtf(g->getCovariance(2, 2));
579 const Eigen::VectorXf triSigmaPoint = g->toEigenMean() + 3 * sigma;
581 if (g->getDensity(obsMean) > g->getDensity(triSigmaPoint))
587 cpLearnerPrx->learnFromObject(snapInstance);
593 const float avgCorrect =
float(corrClusters) / itCount;
595 grandTotalMah[t] += totalMah;
596 grandTotalCorrect[t] += avgCorrect;
603 std::ofstream totfile;
604 totfile.open(
"a_totals.csv");
606 for (
int t = 0; t < COEFF_COUNT; ++t)
607 totfile << thresholds[t] <<
"," << grandTotalMah[t] <<
608 "," << grandTotalMah[t] / totalObjects <<
609 "," << grandTotalCorrect[t] <<
610 "," <<
float(grandTotalCorrect[t]) / totalObjects <<
617 CommonPlacesTester::testAging2()
619 const std::string snapshotName =
620 getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
622 const int itCount = getProperty<int>(
"IterationCount").getValue();
624 const int COEFF_COUNT = 10;
625 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};
626 float grandTotalMah[COEFF_COUNT];
627 int grandTotalCorrect[COEFF_COUNT];
628 int totalObjects = 0;
630 for (
int i = 0; i < COEFF_COUNT; ++i)
632 grandTotalMah[i] = 0;
633 grandTotalCorrect[i] = 0;
636 MahalanobisDistance mahDistance;
640 for (
int t = 0; t < COEFF_COUNT; ++t)
642 cpLearnerPrx->setAgingFactor(thresholds[t]);
645 int corrClusters = 0;
647 for (
int i = 0; i < itCount; ++i)
649 longtermMemoryPrx->getCustomInstancesSegment(ltmSegmentName,
false)->clear();
651 for (NameList::const_iterator it = snapNameList.begin(); it != snapNameList.end();
654 WorkingMemorySnapshotInterfacePrx snapshot =
655 longtermMemoryPrx->openWorkingMemorySnapshot(*it);
663 const std::string segmentName =
"objectInstances";
664 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
668 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
674 EntityIdList ids = segInstances->getAllEntityIds();
676 if (t == 0 && i == 0)
678 totalObjects += ids.size();
681 std::random_shuffle(ids.begin(), ids.end());
683 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
685 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
688 GaussianMixtureDistributionPtr::dynamicCast(
689 cpLearnerPrx->getPositionReducedByMaxDeviation(
690 objectToLoad, 150, eDevAABB));
694 totalMah += mahDistance.getWeigtedDistance(
695 gmm, snapInstance->getPositionUncertainty());
698 GaussianMixtureComponent
c = gmm->getModalComponent();
700 NormalDistributionPtr::dynamicCast(
c.gaussian);
701 const Eigen::VectorXf obsMean = snapInstance->getPosition()->toEigen();
702 Eigen::Vector3f sigma;
703 sigma << sqrtf(g->getCovariance(0, 0)), sqrtf(g->getCovariance(1, 1)),
704 sqrtf(g->getCovariance(2, 2));
705 const Eigen::VectorXf triSigmaPoint = g->toEigenMean() + 3 * sigma;
707 if (g->getDensity(obsMean) > g->getDensity(triSigmaPoint))
713 cpLearnerPrx->learnFromObject(snapInstance);
723 const float avgCorrect =
float(corrClusters) / itCount;
725 grandTotalMah[t] += totalMah;
726 grandTotalCorrect[t] += avgCorrect;
731 std::ofstream totfile;
732 totfile.open(
"a_totals.csv");
734 for (
int t = 0; t < COEFF_COUNT; ++t)
735 totfile << thresholds[t] <<
"," << grandTotalMah[t] <<
736 "," << grandTotalMah[t] / totalObjects <<
737 "," << grandTotalCorrect[t] <<
738 "," <<
float(grandTotalCorrect[t]) / totalObjects <<
745 CommonPlacesTester::testGetClusterLocations()
747 Cluster3DList clusterList =
748 cpLearnerPrx->getPositionClustersByComponentCount(objectToLoad, 3);
750 if (clusterList.size() > 0)
752 for (Cluster3DList::iterator it = clusterList.begin(); it != clusterList.end(); it++)
754 Cluster3D& entry = *it;
756 <<
" y: " << entry.center.y <<
" with weight: " << entry.weight;
766 CommonPlacesTester::testClusteringBatch()
768 const std::string snapshotName =
769 getProperty<std::string>(
"SnapshotsToLearnFrom").getValue();
771 const int DEV_COUNT = 11;
772 const float thresholds[DEV_COUNT] = {
773 10., 30., 50., 75., 100., 150., 200., 400., 800., 1000., 2000.};
778 std::ofstream snfile;
779 const std::string fname =
"c_" + ltmSegmentName +
"_" + objectToLoad +
".csv";
780 snfile.open(fname.c_str());
782 for (
int t = 0; t < DEV_COUNT; ++t)
784 ARMARX_INFO <<
"Processing dev: " << thresholds[t];
786 GaussianMixtureDistributionBasePtr gmmAABB =
787 cpLearnerPrx->getPositionReducedByMaxDeviation(
788 objectToLoad, thresholds[t], eDevAABB);
789 GaussianMixtureDistributionBasePtr gmmOBB =
790 cpLearnerPrx->getPositionReducedByMaxDeviation(
791 objectToLoad, thresholds[t], eDevOrientedBBox);
792 GaussianMixtureDistributionBasePtr gmmSphere =
793 cpLearnerPrx->getPositionReducedByMaxDeviation(
794 objectToLoad, thresholds[t], eDevEqualSphere);
797 snfile << thresholds[t] <<
"," << gmmAABB->size() <<
"," << gmmOBB->size() <<
","
798 << gmmSphere->size() <<
"," << std::endl;
808 CommonPlacesTester::getRandomIds(std::vector<std::string>& allIds,
809 std::vector<std::string>& ids,
812 if (sampleSize > (
int)(allIds.size()))
814 ids.assign(allIds.begin(), allIds.end());
818 std::set<std::string> idset;
820 while (idset.size() < (
size_t)sampleSize)
823 (size_t)truncf(((
float)rand()) /
float(RAND_MAX) *
float(allIds.size()));
824 std::cout <<
"index to add" << ind <<
std::flush;
825 idset.insert(allIds[ind]);
828 ids.assign(idset.begin(), idset.end());
832 CommonPlacesTester::getDeviationTypeFromString(
const std::string& devTypeStr)
835 if (devTypeStr ==
"AABB")
839 else if (devTypeStr ==
"OrientedBBox")
841 return eDevOrientedBBox;
843 else if (devTypeStr ==
"EqualSphere")
845 return eDevEqualSphere;