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
26#include <pcl/point_types.h>
27
29
31
32#include "Common.h"
34#include <densecrf.h>
35#include <objective.h>
36#include <pairwise.h>
37#include <permutohedral.h>
38
39namespace armarx
40{
41 template <class GraphT>
42 class DenseGraphCRF : private DenseCRF
43 {
44 private:
45 // int N_ from base class == Number of Vertices in the Graph
46 // int M_ from base class == Number of Classes
47 typedef boost::shared_ptr<GraphT> GraphPtrT;
48 typedef typename boost::graph_traits<GraphT>::vertex_descriptor VtxId;
49 GraphPtrT _graph;
50
51 public:
52 DenseGraphCRF(GraphPtrT Graph, int M);
53
54 template <typename FeatureT>
55 inline void
56 addPairwiseGaussianFeature(std::vector<float> sigma,
57 LabelCompatibility* function,
58 KernelType kernel_type = DIAG_KERNEL,
59 NormalizationType normalization_type = NORMALIZE_SYMMETRIC)
60 {
62 FeatureT functor(*_graph);
63 Eigen::MatrixXf features = functor.computeFeatures(sigma);
64 addPairwiseEnergy(features, function, kernel_type, normalization_type);
65 }
66
67 inline void
68 addCombinedGaussianFeature(std::vector<float> sigma,
69 bool use_rgb,
70 bool use_norm,
71 bool use_xyz,
72 bool use_curv,
73 bool use_time,
74 LabelCompatibility* function,
75 KernelType kernel_type = DIAG_KERNEL,
76 NormalizationType normalization_type = NORMALIZE_SYMMETRIC)
77 {
80 *_graph, use_rgb, use_norm, use_xyz, use_curv, use_time);
81 Eigen::MatrixXf features = functor.computeFeatures(sigma);
82 addPairwiseEnergy(features, function, kernel_type, normalization_type);
83 }
84
85 void computeUnaryEnergyFromGraph(float gt_prob);
86
87 void map(int n_iterations);
88
89 Eigen::VectorXf map_and_confidence(int n_iterations);
90 // void addPairwiseGaussianVector();
91 };
92
93 template <class GraphT>
95 DenseCRF(static_cast<int>(boost::num_vertices(*Graph)), M), _graph(Graph)
96 {
97 }
98
99 template <class GraphT>
100 void
102 {
104 Eigen::MatrixXf energy(M_, N_);
105 ARMARX_CHECK_EXPRESSION(static_cast<int>(boost::num_vertices(*_graph)) == N_);
106 const float u_energy = -log(1.0 / M_);
107 const float n_energy = -log((1.0 - gt_prob) / (M_ - 1));
108 const float p_energy = -log(gt_prob);
109 energy.fill(u_energy);
110 for (int k = 0; k < N_; k++)
111 {
112 // Set the energy
113 int r = (*_graph)[static_cast<VtxId>(k)].label;
114 if (r >= 0)
115 {
117 energy.col(k).fill(n_energy);
118 energy(r, k) = p_energy;
119 }
120 }
122 setUnaryEnergy(energy);
123 }
124
125 template <class GraphT>
126 void
128 {
130 VectorXs map_result = DenseCRF::map(n_iterations);
131 const int res_size = map_result.size();
132 ARMARX_CHECK_EXPRESSION(res_size == static_cast<int>(boost::num_vertices(*_graph)));
133 for (int i = 0; i < res_size; i++)
134 {
135 (*_graph)[static_cast<VertexId>(i)].label = map_result[i];
136 }
137 }
138
139 template <class GraphT>
140 Eigen::VectorXf
142 {
144 Eigen::MatrixXf Q = inference(n_iterations);
145 // Find the map - VectorXs
146 Eigen::Matrix<short, Eigen::Dynamic, 1> map_result = currentMap(Q);
147 const int res_size = map_result.size();
148 ARMARX_CHECK_EXPRESSION(res_size == static_cast<int>(boost::num_vertices(*_graph)));
149 Eigen::VectorXf confidence_score;
150 ConfidenceMap cm = boost::get(boost::vertex_confidence_t(), _graph->m_graph);
151
152 confidence_score.resize(res_size);
153 for (int i = 0; i < res_size; i++)
154 {
155 (*_graph)[static_cast<VtxId>(i)].label = map_result[i];
156 boost::put(cm, static_cast<VtxId>(i), Q(map_result[i], i));
157 confidence_score[i] = Q(map_result[i], i);
158 }
159 return confidence_score;
160 }
161} // namespace armarx
162
163
164#endif //ROBDEKON_DENSEGRAPHCRF_HPP
Eigen::VectorXf map_and_confidence(int n_iterations)
void computeUnaryEnergyFromGraph(float gt_prob)
void map(int n_iterations)
void addPairwiseGaussianFeature(std::vector< float > sigma, LabelCompatibility *function, KernelType kernel_type=DIAG_KERNEL, NormalizationType normalization_type=NORMALIZE_SYMMETRIC)
DenseGraphCRF(GraphPtrT Graph, int M)
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)
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
This file offers overloads of toIce() and fromIce() functions for STL container types.
boost::property_map< CloudGraphWithTimestamp, boost::vertex_confidence_t >::type ConfidenceMap
Definition Common.h:77
boost::graph_traits< Graph >::vertex_descriptor VertexId
Definition Common.h:68
boost::subgraph< CloudGraph > Graph
Definition Common.h:58
Eigen::MatrixXf computeFeatures(std::vector< float > sigma)
#define ARMARX_TRACE
Definition trace.h:77