TopologyEvaluator.cpp
Go to the documentation of this file.
1 
19 
21 
23  std::vector<boost::shared_ptr<SceneObjectLearner>> pLearners, double pRecognitionThreshold):
24  AbstractTopologyEvaluator(), mExamplesList(pExamplesList), mLearners(pLearners), mRunNumber(0), mPrintHelper('-')
25 {
26  mRecognitionThreshold = pRecognitionThreshold;
27 
28  ros::NodeHandle nodeHandle("~");
29 
30  // Try to get the name of the inference algorithm.
31  if(!nodeHandle.getParam("inference_algorithm", mInferenceAlgorithm))
32  throw std::runtime_error("Please specify parameter inference_algorithm when starting this node.");
33 
34  // Try to get the target of the output of the learned model in xml form.
35  if(!nodeHandle.getParam("xml_output", mXmlOutput))
36  throw std::runtime_error("Please specify parameter xml_output when starting this node.");
37 
38  mXmlFilePath = "";
39  // If output to file, try to get the file path.
40  if (mXmlOutput == "file")
41  if(!nodeHandle.getParam("xml_file_path", mXmlFilePath))
42  throw std::runtime_error("Please specify parameter xml_file_path when starting this node.");
43 
44  // Try to get whether to create a log of the runtime in a file.
45  if(!nodeHandle.getParam("create_runtime_log", mCreateRuntimeLog))
46  throw std::runtime_error("Please specify parameter create_runtime_log when starting this node.");
47 
48  std::string runtimeLogFileName = "";
50  {
51  // Try to get the complete log file name.
52  if(!nodeHandle.getParam("log_file_name", runtimeLogFileName))
53  throw std::runtime_error("Please specify parameter log_file_name when starting this node.");
54 
55  mRuntimeLogger.open(runtimeLogFileName);
56  mRuntimeLogger << "sceneobject,runtime\n";
57  }
58 
59  // Try to get whether to visualize intermediate inference runs:
60  if(!nodeHandle.getParam("visualize_inference", mVisualize))
61  throw std::runtime_error("Please specify parameter visualize_inference when starting this node.");
62 
63  // Try to get whether to use targeting help in visualization:
64  if(!nodeHandle.getParam("targeting_help", mTargetingHelp))
65  throw std::runtime_error("Please specify parameter targeting_help when starting this node.");
66 
67  // Try to get the drawing parameters:
68  if(!nodeHandle.getParam("base_frame_id", mFrameId))
69  throw std::runtime_error("Please specify parameter base_frame_id when starting this node.");
70 
71  if(!nodeHandle.getParam("scale_factor", mScaleFactor))
72  throw std::runtime_error("Please specify parameter scale_factor when starting this node.");
73  if(!nodeHandle.getParam("sigma_multiplicator", mSigmaMultiplier))
74  throw std::runtime_error("Please specify parameter sigma_multiplicator when starting this node.");
75 }
76 
78  if (mCreateRuntimeLog && mRuntimeLogger.is_open())
79  {
80  mRuntimeLogger.close();
81  }
82 }
83 
85 {
86  if (!pTopology) throw std::runtime_error("In TopologyEvaluator::evaluate(): topology from argument is null pointer.");
87 
88  if (pTopology->isEvaluated()) return false; // no new evaluation needed
89 
90  mPrintHelper.printAsHeader("Evaluating topology " + pTopology->mIdentifier + ":");
91 
92  // learn partial model:
93  update(pTopology->getTree());
94 
95  // output partial model:
96  xmlOutput(pTopology);
97 
98  if (mValidTestSets.empty() && mInvalidTestSets.empty()) throw std::runtime_error("In TopologyEvaluator::evaluate(): no test sets found.");
99 
100  unsigned int falseNegatives = 0;
101  unsigned int falsePositives = 0;
102  double recognitionRuntimeSum = 0;
103 
104  for (unsigned int i = 0; i < mValidTestSets.size(); i++)
105  {
107  ROS_INFO_STREAM("Evaluating topology " << pTopology->mIdentifier << " against valid test set " << i << "/" << mValidTestSets.size());
108  std::pair<double, double> recognitionResult = recognize(valid, pFullyMeshed);
109  if (recognitionResult.first <= mRecognitionThreshold) falseNegatives++; // did not recognize a valid scene.
110  recognitionRuntimeSum += recognitionResult.second;
111  }
112 
113  for (unsigned int j = 0; j < mInvalidTestSets.size(); j++)
114  {
116  ROS_INFO_STREAM("Evaluating topology " << pTopology->mIdentifier << " against invalid test set " << j << "/" << mInvalidTestSets.size());
117  std::pair<double, double> recognitionResult = recognize(invalid, pFullyMeshed);
118  ROS_INFO_STREAM("Evaluated topology " << pTopology->mIdentifier << " against invalid test set " << j << "/" << mInvalidTestSets.size());
119  if (recognitionResult.first > mRecognitionThreshold) falsePositives++; // did recognize an invalid scene.
120  recognitionRuntimeSum += recognitionResult.second;
121  }
122 
123  double avgRuntime = recognitionRuntimeSum / (mValidTestSets.size() + mInvalidTestSets.size());
124 
125  ROS_INFO_STREAM("Evaluated topology " << pTopology->mIdentifier << " against " << mValidTestSets.size() << " valid and " << mInvalidTestSets.size() << " invalid test sets.");
126  ROS_INFO_STREAM("Evaluation result: " << falsePositives << " false positives, " << falseNegatives << " false negatives, " << avgRuntime << "s average recognition runtime.");
127 
128  pTopology->setEvaluationResult(avgRuntime, falsePositives, falseNegatives);
129 
130  return true; // was evaluated.
131 }
132 
133 std::pair<double, double> TopologyEvaluator::recognize(boost::shared_ptr<TestSet> pEvidence, bool pFullyMeshed)
134 {
135  struct timeval start;
136  struct timeval end;
137 
138  gettimeofday(&start, NULL); // get start time
139  double probability = getProbability(pEvidence);
140  gettimeofday(&end, NULL); // get the stop time;
141 
142  double recognitionRuntime, seconds, useconds;
143  seconds = end.tv_sec - start.tv_sec;
144  useconds = end.tv_usec - start.tv_usec;
145  recognitionRuntime = seconds + useconds / 1000000;
146 
147  if (pFullyMeshed)
148  pEvidence->setFullyMeshedTestResult(probability, recognitionRuntime);
149 
150  return std::pair<double, double>(probability, recognitionRuntime);
151 }
152 
154 {
155  std::vector<ISM::Object> evidence;
156  for (ISM::ObjectPtr objPtr: pEvidence->mObjectSet->objects)
157  evidence.push_back(*objPtr);
159  if (mVisualize)
160  {
161  if (mTargetingHelp)
162  mVisualizer->drawInTargetingMode();
163  else
164  mVisualizer->drawInInferenceMode();
165  }
166  return mForegroundSceneContent.getSceneProbability(); // Foreground scene probability for partial model;
167 }
168 
170 {
171  // reset mModel:
172  mModel = boost::property_tree::ptree();
173 
174  mPrintHelper.printAsHeader("Starting to learn OCM foreground model:");
175 
176  // Now just forward all examples for the scene to the scene object learners and save partial model to property tree.
178  {
179  learner->learn(mExamplesList, pTree);
180  learner->save(mModel);
181  }
182 
183  mPrintHelper.printAsHeader("Learning complete. Preparing inference.");
184 
185  // Reset and set up foreground scene content:
188  mForegroundSceneContent.load(mModel); // loads partial model.
189 
190  // Set up visualizer:
191  std::string sceneId = mExamplesList[0]->mIdentifier;
192  mVisualizer.reset(new Visualization::ProbabilisticSceneVisualization(sceneId));
193 
195  mVisualizer->setDrawingParameters(mScaleFactor, mSigmaMultiplier, mFrameId);
196 
197  // psm offers the possibility to add foreground learners other than OCM, which might use no or different relations.
198  // because of this and since the complete model is only assembled at the very end, only a partial model is used here.
199 }
200 
202 {
203  if (mXmlOutput != "none")
204  {
205  boost::property_tree::ptree extendedModel;
206 
207  mModel.add("<xmlattr>.run", mRunNumber);
208  mModel.add("<xmlattr>.topology_id", pTopology->mIdentifier);
209 
210  extendedModel.add_child("partial_model", mModel);
211 
212  if (mXmlOutput == "screen")
213  {
214  std::cout << "Partial model in TopologyEvaluator run " << mRunNumber << ":" << std::endl;
215  write_xml(std::cout, extendedModel);
216  std::cout << std::endl;
217  }
218  else if (mXmlOutput == "file")
219  {
220  std::string xmlFileName = mXmlFilePath + "partial_model_" + boost::lexical_cast<std::string>(mRunNumber) + ".xml";
221  write_xml(xmlFileName, extendedModel);
222  }
223  else throw std::runtime_error("Parameter xml_output has invalid value " + mXmlOutput);
224  }
225  // if value is "none": do nothing.
226  mRunNumber++;
227 }
228 
229 }
std::vector< boost::shared_ptr< TestSet > > mInvalidTestSets
std::vector< boost::shared_ptr< SceneObjectLearner > > mLearners
void xmlOutput(boost::shared_ptr< SceneModel::Topology > pTopology)
std::pair< double, double > recognize(boost::shared_ptr< TestSet > pEvidence, bool pFullyMeshed=false)
std::vector< boost::shared_ptr< ISM::ObjectSet > > mExamplesList
std::vector< boost::shared_ptr< TestSet > > mValidTestSets
void initializeVisualizer(boost::shared_ptr< Visualization::ProbabilisticSceneVisualization > mSuperior)
double getProbability(boost::shared_ptr< TestSet > pEvidence)
bool evaluate(boost::shared_ptr< SceneModel::Topology > pTopology, bool pFullyMeshed=false)
boost::shared_ptr< Visualization::ProbabilisticSceneVisualization > mVisualizer
void update(std::vector< ISM::Object > pEvidenceList, std::ofstream &pRuntimeLogger)
#define ROS_INFO_STREAM(args)
void update(boost::shared_ptr< SceneModel::TreeNode > pTree)
bool getParam(const std::string &key, std::string &s) const
TopologyEvaluator(std::vector< boost::shared_ptr< ISM::ObjectSet >> pExamplesList, std::vector< boost::shared_ptr< SceneObjectLearner >> pLearners, double pRecognitionThreshold)


asr_psm
Author(s): Braun Kai, Gehrung Joachim, Heizmann Heinrich, Meißner Pascal
autogenerated on Fri Nov 15 2019 04:00:08