DenseGraphCRF.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ROBDEKON::ArmarXObjects::DenseCRFSegmentationProcessor
17  * @author Christoph Pohl ( christoph dot pohl at kit dot edu )
18  * @date 2019
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #ifndef ROBDEKON_DENSEGRAPHCRF_HPP
24 #define ROBDEKON_DENSEGRAPHCRF_HPP
25 
27 
28 #include <densecrf.h>
29 #include <pairwise.h>
30 #include <permutohedral.h>
31 #include <objective.h>
32 
33 
35 
36 
37 #include <pcl/point_types.h>
38 
39 #include "DenseCRFFeatureTerms.h"
40 #include "Common.h"
41 
42 
43 namespace armarx
44 {
45  template <class GraphT>
46  class DenseGraphCRF : private DenseCRF
47  {
48  private:
49  // int N_ from base class == Number of Vertices in the Graph
50  // int M_ from base class == Number of Classes
52  typedef typename boost::graph_traits<GraphT>::vertex_descriptor VtxId;
53  GraphPtrT _graph;
54 
55  public:
57 
58 
59  template <typename FeatureT>
60  inline void addPairwiseGaussianFeature(std::vector<float> sigma, LabelCompatibility* function,
61  KernelType kernel_type = DIAG_KERNEL,
62  NormalizationType normalization_type = NORMALIZE_SYMMETRIC)
63  {
65  FeatureT functor(*_graph);
66  Eigen::MatrixXf features = functor.computeFeatures(sigma);
67  addPairwiseEnergy(features, function, kernel_type, normalization_type);
68  }
69 
70  inline void
71  addCombinedGaussianFeature(std::vector<float> sigma, bool use_rgb, bool use_norm, bool use_xyz, bool use_curv,
72  bool use_time, LabelCompatibility* function, KernelType kernel_type = DIAG_KERNEL,
73  NormalizationType normalization_type = NORMALIZE_SYMMETRIC)
74  {
76  VariableCombinedNormalizedFeature functor(*_graph, use_rgb, use_norm, use_xyz, use_curv, use_time);
77  Eigen::MatrixXf features = functor.computeFeatures(sigma);
78  addPairwiseEnergy(features, function, kernel_type, normalization_type);
79  }
80 
81  void computeUnaryEnergyFromGraph(float gt_prob);
82 
83  void map(int n_iterations);
84 
85  Eigen::VectorXf map_and_confidence(int n_iterations);
86  // void addPairwiseGaussianVector();
87  };
88 
89  template <class GraphT>
91  static_cast<int>(boost::num_vertices(*Graph)), M), _graph(Graph)
92  {
93 
94  }
95 
96  template <class GraphT>
98  {
100  Eigen::MatrixXf energy(M_, N_);
101  ARMARX_CHECK_EXPRESSION(static_cast<int>(boost::num_vertices(*_graph)) == N_);
102  const float u_energy = -log(1.0 / M_);
103  const float n_energy = -log((1.0 - gt_prob) / (M_ - 1));
104  const float p_energy = -log(gt_prob);
105  energy.fill(u_energy);
106  for (int k = 0; k < N_; k++)
107  {
108  // Set the energy
109  int r = (*_graph)[static_cast<VtxId >(k)].label;
110  if (r >= 0)
111  {
112  ARMARX_TRACE;
113  energy.col(k).fill(n_energy);
114  energy(r, k) = p_energy;
115  }
116  }
117  ARMARX_TRACE;
118  setUnaryEnergy(energy);
119  }
120 
121  template <class GraphT>
122  void DenseGraphCRF<GraphT>::map(int n_iterations)
123  {
124  ARMARX_TRACE;
125  VectorXs map_result = DenseCRF::map(n_iterations);
126  const int res_size = map_result.size();
127  ARMARX_CHECK_EXPRESSION(res_size == static_cast<int>(boost::num_vertices(*_graph)));
128  for (int i = 0; i < res_size; i++)
129  {
130  (*_graph)[static_cast<VertexId>(i)].label = map_result[i];
131  }
132  }
133 
134  template <class GraphT>
135  Eigen::VectorXf DenseGraphCRF<GraphT>::map_and_confidence(int n_iterations)
136  {
137  ARMARX_TRACE;
138  Eigen::MatrixXf Q = inference(n_iterations);
139  // Find the map - VectorXs
140  Eigen::Matrix<short, Eigen::Dynamic, 1> map_result = currentMap(Q);
141  const int res_size = map_result.size();
142  ARMARX_CHECK_EXPRESSION(res_size == static_cast<int>(boost::num_vertices(*_graph)));
143  Eigen::VectorXf confidence_score;
144  ConfidenceMap cm = boost::get(boost::vertex_confidence_t(), _graph->m_graph);
145 
146  confidence_score.resize(res_size);
147  for (int i = 0; i < res_size; i++)
148  {
149  (*_graph)[static_cast<VtxId>(i)].label = map_result[i];
150  boost::put(cm, static_cast<VtxId>(i), Q(map_result[i], i));
151  confidence_score[i] = Q(map_result[i], i);
152  }
153  return confidence_score;
154  }
155 }
156 
157 
158 #endif //ROBDEKON_DENSEGRAPHCRF_HPP
armarx::DenseGraphCRF::computeUnaryEnergyFromGraph
void computeUnaryEnergyFromGraph(float gt_prob)
Definition: DenseGraphCRF.hpp:97
armarx::VariableCombinedNormalizedFeature
Definition: DenseCRFFeatureTerms.h:261
boost::shared_ptr< GraphT >
armarx::ConfidenceMap
boost::property_map< CloudGraphWithTimestamp, boost::vertex_confidence_t >::type ConfidenceMap
Definition: Common.h:71
DenseCRFFeatureTerms.h
Common.h
armarx::VariableCombinedNormalizedFeature::computeFeatures
Eigen::MatrixXf computeFeatures(std::vector< float > sigma)
Definition: DenseCRFFeatureTerms.h:275
armarx::VertexId
boost::graph_traits< Graph >::vertex_descriptor VertexId
Definition: Common.h:64
boost
Definition: ApplicationOptions.h:37
boost::vertex_confidence_t
Definition: Common.h:19
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::DenseGraphCRF::DenseGraphCRF
DenseGraphCRF(GraphPtrT Graph, int M)
Definition: DenseGraphCRF.hpp:90
armarx::DenseGraphCRF::addPairwiseGaussianFeature
void addPairwiseGaussianFeature(std::vector< float > sigma, LabelCompatibility *function, KernelType kernel_type=DIAG_KERNEL, NormalizationType normalization_type=NORMALIZE_SYMMETRIC)
Definition: DenseGraphCRF.hpp:60
armarx::DenseGraphCRF::map
void map(int n_iterations)
Definition: DenseGraphCRF.hpp:122
armarx::Graph
boost::subgraph< CloudGraph > Graph
Definition: Common.h:54
armarx::DenseGraphCRF
Definition: DenseGraphCRF.hpp:46
ExpressionException.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
Eigen::Matrix
Definition: EigenForwardDeclarations.h:27
armarx::DenseGraphCRF::addCombinedGaussianFeature
void addCombinedGaussianFeature(std::vector< float > sigma, bool use_rgb, bool use_norm, bool use_xyz, bool use_curv, bool use_time, LabelCompatibility *function, KernelType kernel_type=DIAG_KERNEL, NormalizationType normalization_type=NORMALIZE_SYMMETRIC)
Definition: DenseGraphCRF.hpp:71
PointXYZRGBLNormal.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::DenseGraphCRF::map_and_confidence
Eigen::VectorXf map_and_confidence(int n_iterations)
Definition: DenseGraphCRF.hpp:135