OcmSceneObjectContent.cpp
Go to the documentation of this file.
1 
19 
21 
24  , mProbability(0.0)
25  {
29  }
30 
32  {
33 
34  }
35 
36  void OcmSceneObjectContent::load(boost::property_tree::ptree& pPt)
37  {
38  // Load number of slots from XMl file.
39  mNumberOfSlots = pPt.get<unsigned int>("slots.<xmlattr>.number");
40 
41  // Check, if the number of slots is valid.
42  if(mNumberOfSlots <= 0)
43  throw std::invalid_argument("Unable to procees loading. OCM: number of slots must be > 0.");
44 
45  // Command the factories to load their data from the XML file.
46  BOOST_FOREACH(boost::shared_ptr<TermEvaluator> evaluator, mEvaluators)
47  {
48  // Load term evaluator data.
49  evaluator->load(pPt);
50 
51  // Check, if the model managed by the term evaluator has the right number of slots.
52  if(mNumberOfSlots != evaluator->getNumberOfSlots())
53  throw std::invalid_argument("Unable to procees loading. Divergent number of slots between the OCM distributions.");
54  }
55  }
56 
58  {
59  // Store a pointer to the visualizer.
60  mVisualizer = mSuperior;
61 
62  // Forward visualizer to the term evaluators.
63  for(unsigned int i = 0; i < mEvaluators.size(); i++)
64  mEvaluators[i]->initializeVisualizer(mSuperior);
65  }
66 
67  void OcmSceneObjectContent::update(std::vector<ISM::Object> pEvidenceList)
68  {
69  // Generate object list for debug message
70  std::stringstream objs;
71  for(unsigned int i = 0; i < pEvidenceList.size(); i++)
72  {
73  objs << pEvidenceList[i].type;
74 
75  if(i < pEvidenceList.size() - 1)
76  objs<< ", ";
77  }
78 
79  // Show debug message with the found objects.
80  ROS_INFO_STREAM(" > There are '" << pEvidenceList.size() << "' objects: " << objs.str() << ".");
81 
82  // Reset visualizer
83  mVisualizer->resetHypothesis();
84 
85  /**************************************************************************************
86  * Command the factories to calculate the probability for every hypothesis.
87  * Marginalize over all hypotheses to calculate the probability for this scene object.
88  ***************************************************************************************/
89 
90  // Reset probability. It is a marginalization and we add the probabilities
91  // of the hypotheses to it, so this must be ZERO.
92  mProbability = 0.0;
93 
94  // First of all we create all possible hypotheses (=assignments of parts to slots, parts are found objects).
95  // Therefore we need their number, which is the size of the hypothesis space '|h|' to the power
96  // of the number of slots 's': |h|^s.
97  unsigned int numberOfEvidence = pEvidenceList.size() + 1;
98  unsigned int numberOfHypotheses = pow(numberOfEvidence, mNumberOfSlots);
99  unsigned int numberOfValidHypotheses = 0;
100 
101  // The sum of all filled slots for normalizing the result.
102  unsigned int sumOfFilledSlots = 0;
103 
104  // Now we iterate over all combinations of hypotheses 'h'.
105  for(unsigned int h = 0; h < numberOfHypotheses; h++)
106  {
107  // A marker that becomes true, when hypothesis contains double assignments and should be dropped.
108  bool dropped = false;
109 
110  // A counter for the number of filled slots.
111  unsigned int numberOfFilledSlots = 0;
112 
113  // A vector holding the assignments of parts to slots.
114  std::vector<unsigned int> assignments;
115 
116  // Calculate the assignments of parts to slots. Drop hypotheses with double assignments of parts to slots.
117  for(unsigned int s = 0; s < mNumberOfSlots; s++)
118  {
119  // Calculates which part should be associated with the 's'th slot, based on the 'h'th hypothesis.
120  unsigned int part = (h / ((unsigned int) pow(numberOfEvidence, s)) ) % numberOfEvidence;
121 
122  if(part > 0)
123  {
124  numberOfFilledSlots++;
125 
126  // If part is not zero-object and is already assigned, we drop this hypothesis and continue with the next one.
127  if(std::find(assignments.begin(), assignments.end(), part) != assignments.end())
128  dropped = true;
129  }
130 
131  // Assign the calculated part to the next slot in the row.
132  assignments.push_back(part);
133  }
134 
135  // If the root node is occluded, we drop this hypothesis and continue with the next one.
136  if(assignments[0] == 0)
137  dropped = true;
138 
139  // Tell all factories to calculate the probability for the current hypothesis.
140  if(!dropped)
141  {
142  // Generate a debug message with the results.
143  std::stringstream msgResult;
144  msgResult << "Assignment (";
145 
146  // Generate a list of all assignments for the debug message.
147  for(unsigned int i = 0; i < assignments.size(); i++)
148  {
149  if(assignments[i] == 0)
150  msgResult << "NULL";
151  else
152  msgResult << pEvidenceList[assignments[i] - 1].type;
153 
154  // Add separator.
155  if(i < assignments.size() - 1)
156  msgResult<< ", ";
157  }
158 
159  // Add results to debug message
160  msgResult << ") generates results (";
161 
162  // We multiply the terms of a hypothesis, so this must be ONE!
163  double hValue = 1.0;
164 
165  // Iterate over all factories and multiply their output.
166  mVisualizer->resetTermScores();
167  for(unsigned int i = 0; i < mEvaluators.size(); i++)
168  {
169  // Get the probability from the evaluator.
170  double termScore = mEvaluators[i]->calculateProbabilityForHypothesis(pEvidenceList, assignments);
171 
172  // Add term score to visualizer.
173  mVisualizer->addTermScore(termScore);
174 
175  // Add it to the debug message.
176  msgResult << termScore;
177 
178  // Add separator.
179  if(i < mEvaluators.size() - 1)
180  msgResult<< ", ";
181 
182  // Multiply the probability from the evaluators to the hypothesis probability.
183  hValue *= termScore;
184  }
185 
186  // Finish debug message with hypothesis probability.
187  msgResult << "). Hypothesis probability is '" << hValue << "'.";
188 
189  // Show the debug message composed above.
190  if(hValue > 0.0)
191  ROS_DEBUG_STREAM(" > " << msgResult.str());
192 
193  // Multiply hypotheses with the numerator of the 'hypothesis length' normalization term.
194  hValue *= numberOfFilledSlots;
195 
196  // Update visualizer with current hypothesis score.
197  mVisualizer->setHypothesisScore(hValue);
198 
199  // Add the probability of the hypothesis to the sum.
200  mProbability += hValue;
201 
202  // +1 for this valid hypothesis. A hypothesis is valid, if it scores more than zero.
203  // Normalize probability with the denominator of the 'hypothesis length' normalization term.
204  if(hValue > std::numeric_limits<double>::epsilon())
205  {
206  numberOfValidHypotheses++;
207  sumOfFilledSlots += numberOfFilledSlots;
208  }
209  }
210  }
211  ROS_DEBUG_STREAM("THERE ARE " << numberOfValidHypotheses << " hypotheses larger than zero.");
212 
213  // Apply normalization term by dividing by the number of all VALID (!) hypotheses.
214  // Only apply term if there was one valid hypothesis, otherwise we'll have nan.
215  if(numberOfValidHypotheses > 0)
216  {
217  mProbability /= (/*numberOfValidHypotheses * */sumOfFilledSlots);
218  mVisualizer->normalizeHypothesisScore(sumOfFilledSlots);
219  }
220 
221  // The term evaluators shall update their visualizers.
223  evaluator->visualize(pEvidenceList);
224 
225  // Print scene object probability.
226  ROS_DEBUG_STREAM("Probability for scene object before applying priori is " << mProbability << ".");
227  }
228 
230  {
231  return mProbability;
232  }
233 
235  {
236  mVisualizer->setBestStatus(pStatus);
237  }
238 
239 }
boost::shared_ptr< Visualization::ProbabilisticPrimarySceneObjectVisualization > mVisualizer
XmlRpcServer s
bool find(const std::vector< double > &in, double toFind)
Definition: validator.cpp:128
std::vector< boost::shared_ptr< TermEvaluator > > mEvaluators
void update(std::vector< ISM::Object > pEvidenceList)
void initializeVisualizer(boost::shared_ptr< Visualization::ProbabilisticPrimarySceneObjectVisualization > mSuperior)
#define ROS_DEBUG_STREAM(args)
#define ROS_INFO_STREAM(args)


asr_psm
Author(s): Braun Kai, Gehrung Joachim, Heizmann Heinrich, Meißner Pascal
autogenerated on Fri Nov 15 2019 03:57:54