forked from ManifoldFR/AlphaExpansion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlphaExpansion.cpp
218 lines (173 loc) · 6.74 KB
/
AlphaExpansion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <limits>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/range/numeric.hpp>
#include "graph.h"
#include "pushrelabel.h"
#include "AlphaExpansion.h"
using namespace std;
vector<int> buildLabels(const vector<vector<int>>& unaryPotential) {
//Graph Constructor to use at the begining
vector<int> labels;
//Computations of the initial labels
for (int i=0; i<unaryPotential.size(); i++){
labels.push_back(std::distance(unaryPotential[i].begin(), min_element(unaryPotential[i].begin(), unaryPotential[i].end())));
}
return labels;
}
/// Reparametrization for binary graph cuts.
/// This algorithm ensures all capacities are non-negative.
/// Book URL: http://www0.cs.ucl.ac.uk/external/s.prince/book/Algorithms.pdf
void reparametrize(Graph& G)
{
}
/// Build the graph for alpha expansion.
/// See Simon J.D. Prince, "Computer Vision: Models, Learning, and Inference", p. 41
/// Book URL: http://www0.cs.ucl.ac.uk/external/s.prince/book/Algorithms.pdf
Graph buildGraph(int label, const vector<int>& labels, const vector<vector<int>>& unaryPotential, const vector<vector<int>>& edges){
//Initialize graph with approriate edges information
int numberNodes = labels.size();
Graph G(numberNodes + 2);
int sink = numberNodes;
int source = sink + 1;
int new_node = source + 1; // denoted z in the book
for (int i = 0; i<edges.size(); i++){
int idxS = edges[i].at(0);
int idxT = edges[i].at(1);
if (labels.at(idxS) == labels.at(idxT) && labels.at(idxS) == label ){
// Case 1
;
}
else if (labels.at(idxS) == labels.at(idxT) && labels.at(idxS) != label ){
// Case 3
add_edge_clean(idxS, idxT, G, 100, 100); // CAUTION
/*
boost::add_edge(idxS, idxT, EdgeProperties{100,0}, G);
boost::add_edge(idxT, idxS, EdgeProperties{100,0}, G);
*/
}
else if (labels.at(idxS) == label){
// Case 2b
add_edge_clean(idxT, idxS, G, 100);
// boost::add_edge(idxT, idxS, EdgeProperties{100,0}, G);
}
else if (labels.at(idxT) == label){
// Case 2a
add_edge_clean(idxS, idxT, G, 100);
// boost::add_edge(idxS, idxT, EdgeProperties{100,0}, G);
}
else{
// Case 4
new_node = boost::num_vertices(G); // CAUTION
boost::add_vertex(G);
add_edge_clean(idxS, new_node, G, 100, BIG_INTEGER); // CAUTION
add_edge_clean(idxT, new_node, G, 100, BIG_INTEGER); // CAUTION
/*
boost::add_edge(idxS, new_node, EdgeProperties{100,0}, G);
boost::add_edge(new_node, idxS, EdgeProperties{BIG_INTEGER,0}, G);
boost::add_edge(new_node, idxT, EdgeProperties{BIG_INTEGER,0}, G);
boost::add_edge(idxT, new_node, EdgeProperties{100,0}, G);
*/
boost::add_edge(new_node, sink, EdgeProperties{100,0}, G);
/*
new_node++;
*/
}
}
for (int i=0; i<unaryPotential.size(); i++){
if (labels.at(i) == label){
boost::add_edge(source, i, EdgeProperties{0,0}, G);
boost::add_edge(i, sink, EdgeProperties{BIG_INTEGER, 0}, G);
}
else{
int smallest = min(unaryPotential[i].at(label),unaryPotential[i].at(labels[i]));
boost::add_edge(source, i, EdgeProperties{unaryPotential[i].at(label) - smallest,0}, G);
boost::add_edge(i, sink, EdgeProperties{unaryPotential[i].at(labels[i]) - smallest, 0}, G);
}
}
return G;
}
vector<int> getLabel(const Graph& G, vector<int> labels, int label, const Graph::vertex_descriptor& src, const Graph::vertex_descriptor& sk){
vector<int> new_labels;
auto vs = vertices(G);
for (auto &it = vs.first; it != vs.second; it++) {
if (*it < labels.size() && (*it != src) && (*it != sk)){
new_labels.push_back(G[*it].cut_class * label + (1-G[*it].cut_class) * labels.at(*it));
}
}
return new_labels;
}
void setLabel(vector<int>& oldLabels, const vector<int>& newLabels){
for (int i=0; i<oldLabels.size(); i++){
oldLabels[i] = newLabels[i];
}
}
int computeEnergy(const vector<int>& labels, const vector<vector<int>>& unaryPotential, const vector<vector<int>>& edges){
int energy = 0;
for (int i =0; i<labels.size(); i++){
energy = energy + unaryPotential[i].at(labels[i]);
}
for (int j=0; j < edges.size(); j++){
int idxS = edges[j].at(0);
int idxT = edges[j].at(1);
if (labels[idxS] != labels[idxT]){
energy = energy + 100;
}
}
return energy;
}
bool expansion(vector<int>& labels, vector<vector<int>> unaryPotential, vector<vector<int>> edges){
int minEnergy = computeEnergy(labels, unaryPotential, edges);
std::cout << minEnergy << endl;
int bestLabel = -1;
vector<int> bestLabels;
vector<int> localLabels;
Graph G;
size_t numberNodes = unaryPotential.size();
Graph::vertex_descriptor sk = numberNodes;
Graph::vertex_descriptor src = numberNodes + 1;
for (int i = 0; i<unaryPotential[0].size(); i++){
int localLabel = i;
std::cout << "Building graph to expand " << i << std::endl;
G = buildGraph(localLabel, labels, unaryPotential, edges);
symmetrize_graph(G);
std::cout << "Computing min cut" << std::endl;
compute_min_cut(G, src, sk);
// compute_min_cut_boost(G, src, sk);
std::cout << "Computing local labels" << std::endl;
localLabels = getLabel(G, labels, localLabel, src, sk);
int graphEnergy = computeEnergy(localLabels, unaryPotential, edges);
std::cout << graphEnergy << endl;
if (graphEnergy < minEnergy){
minEnergy = graphEnergy;
bestLabels = localLabels;
bestLabel = i;
}
}
if (bestLabel > -1){
setLabel(labels, bestLabels);
return true;
}
else{
return false;
}
}
pair<int,vector<int>> applyAlphaExpansion(vector<vector<int>> unaryPotential, vector<vector<int>> edges){
pair <int,vector<int>> result;
vector<int> labels = buildLabels(unaryPotential);
cout << "Pre label computed" <<endl;
bool modified = true;
while(modified){
cout << "Computing expansion" << endl;
modified = expansion(labels, unaryPotential, edges);
}
int finalEnergy = computeEnergy(labels, unaryPotential, edges);
result = make_pair (finalEnergy, labels);
return result;
}