Commit ef60be8e authored by Davis King's avatar Davis King

Simplified the potts model interface a little.

parent 9be84fe4
...@@ -140,34 +140,22 @@ namespace dlib ...@@ -140,34 +140,22 @@ namespace dlib
sink_label = FREE_NODE; sink_label = FREE_NODE;
// setup flows based on factor potentials // setup flows based on factor potentials
edge_type min_value = std::numeric_limits<edge_type>::max();
for (unsigned long i = 0; i < g.number_of_nodes(); ++i) for (unsigned long i = 0; i < g.number_of_nodes(); ++i)
{ {
source_flows(i,0) = -g.factor_value(i, true); const edge_type temp = g.factor_value(i);
sink_flows(i,1) = -g.factor_value(i, false); if (temp < 0)
source_flows(i,0) = -temp;
else
sink_flows(i,1) = temp;
source_flows(i,1) = 0; source_flows(i,1) = 0;
sink_flows(i,0) = 0; sink_flows(i,0) = 0;
if (source_flows(i,0) < min_value)
min_value = source_flows(i,0);
if (sink_flows(i,1) < min_value)
min_value = sink_flows(i,1);
for (unsigned long j = 0; j < g.number_of_neighbors(i); ++j) for (unsigned long j = 0; j < g.number_of_neighbors(i); ++j)
{ {
flows(i,j) = g.factor_value_disagreement(i, g.get_neighbor(i,j)); flows(i,j) = g.factor_value_disagreement(i, g.get_neighbor(i,j));
} }
} }
// The flows can't be negative, so if they are then just adjust them
// so they are positive. Note that doing this doesn't change the optimal
// labeling assignment.
if (min_value < 0)
{
source_flows -= min_value;
sink_flows -= min_value;
}
} }
class out_edge_iterator class out_edge_iterator
...@@ -439,7 +427,8 @@ namespace dlib ...@@ -439,7 +427,8 @@ namespace dlib
for (unsigned long i = 0; i < g.number_of_nodes(); ++i) for (unsigned long i = 0; i < g.number_of_nodes(); ++i)
{ {
const bool label = (g.get_label(i)!=0); const bool label = (g.get_label(i)!=0);
score += g.factor_value(i, label); if (label)
score += g.factor_value(i);
} }
for (unsigned long i = 0; i < g.number_of_nodes(); ++i) for (unsigned long i = 0; i < g.number_of_nodes(); ++i)
......
...@@ -126,16 +126,16 @@ namespace dlib ...@@ -126,16 +126,16 @@ namespace dlib
typedef an_integer_or_real_type value_type; typedef an_integer_or_real_type value_type;
value_type factor_value ( value_type factor_value (
unsigned long idx, unsigned long idx
bool lab
) const; ) const;
/*! /*!
requires requires
- idx < number_of_nodes() - idx < number_of_nodes()
ensures ensures
- returns a value which indicates how "good" it is to assign the idx-node - returns a value which indicates how "good" it is to assign the idx-node
a label equal to lab. The larger the value, the more desirable the label of true. The larger the value, the more desirable it is to
the label contained by lab. give it this label. Similarly, a negative value indicates that it is
better to give the node a label of false.
!*/ !*/
value_type factor_value_disagreement ( value_type factor_value_disagreement (
...@@ -177,7 +177,7 @@ namespace dlib ...@@ -177,7 +177,7 @@ namespace dlib
precisely: precisely:
- let L(i) == the boolean label of the ith variable in prob. Or in other - let L(i) == the boolean label of the ith variable in prob. Or in other
words, L(i) == (prob.get_label(i) != 0). words, L(i) == (prob.get_label(i) != 0).
- let F == the sum over valid i of prob.factor_value(i, L(i)). - let F == the sum of all values of prob.factor_value(i) where whenever L(i) == true.
- Let D == the sum of all values of prob.factor_value_disagreement(i,j) - Let D == the sum of all values of prob.factor_value_disagreement(i,j)
whenever the following conditions are true about i and j: whenever the following conditions are true about i and j:
- i and j are neighbors in the graph defined by prob, that is, - i and j are neighbors in the graph defined by prob, that is,
...@@ -209,10 +209,6 @@ namespace dlib ...@@ -209,10 +209,6 @@ namespace dlib
model. In particular, this means that this function finds the assignments model. In particular, this means that this function finds the assignments
to all the labels in prob which maximizes potts_model_score(#prob). to all the labels in prob which maximizes potts_model_score(#prob).
- The optimal labels are stored in #prob. - The optimal labels are stored in #prob.
- Note that this routine is a little bit faster if all the values
returned by prob.factor_value() are negative. So if you can arrange for that
to be true without spending any extra CPU cycles then it might be a good idea
to do so.
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -29,7 +29,7 @@ namespace ...@@ -29,7 +29,7 @@ namespace
typedef double value_type; typedef double value_type;
private: private:
matrix<value_type,0,2> factors1; matrix<value_type,0,1> factors1;
matrix<value_type> factors2; matrix<value_type> factors2;
matrix<node_label,0,1> labels; matrix<node_label,0,1> labels;
public: public:
...@@ -39,7 +39,7 @@ namespace ...@@ -39,7 +39,7 @@ namespace
dlib::rand& rnd dlib::rand& rnd
) )
{ {
factors1 = -7*(randm(num_nodes, 2, rnd)-0.5); factors1 = -7*(randm(num_nodes, 1, rnd)-0.5);
factors2 = make_symmetric(randm(num_nodes, num_nodes, rnd) > 0.5); factors2 = make_symmetric(randm(num_nodes, num_nodes, rnd) > 0.5);
labels.set_size(num_nodes); labels.set_size(num_nodes);
labels = FREE_NODE; labels = FREE_NODE;
...@@ -92,14 +92,11 @@ namespace ...@@ -92,14 +92,11 @@ namespace
} }
value_type factor_value (unsigned long idx, bool value) const value_type factor_value (unsigned long idx) const
{ {
DLIB_TEST(idx < number_of_nodes()); DLIB_TEST(idx < number_of_nodes());
if (value) return factors1(idx);
return factors1(idx,0);
else
return factors1(idx,1);
} }
value_type factor_value_disagreement (unsigned long idx1, unsigned long idx2) const value_type factor_value_disagreement (unsigned long idx1, unsigned long idx2) const
...@@ -124,7 +121,7 @@ namespace ...@@ -124,7 +121,7 @@ namespace
const static unsigned long max_number_of_neighbors = 4; const static unsigned long max_number_of_neighbors = 4;
private: private:
matrix<value_type,0,2> factors1; matrix<value_type,0,1> factors1;
matrix<value_type> factors2; matrix<value_type> factors2;
matrix<node_label,0,1> labels; matrix<node_label,0,1> labels;
long nr; long nr;
...@@ -142,7 +139,7 @@ namespace ...@@ -142,7 +139,7 @@ namespace
rect = rectangle(0,0,nc-1,nr-1); rect = rectangle(0,0,nc-1,nr-1);
inner_rect = shrink_rect(rect,1); inner_rect = shrink_rect(rect,1);
const unsigned long num_nodes = nr*nc; const unsigned long num_nodes = nr*nc;
factors1 = -7*(randm(num_nodes, 2, rnd)); factors1 = -7*(randm(num_nodes, 1, rnd));
factors2 = randm(num_nodes, 4, rnd) > 0.5; factors2 = randm(num_nodes, 4, rnd) > 0.5;
//factors1 = 0; //factors1 = 0;
...@@ -260,15 +257,12 @@ namespace ...@@ -260,15 +257,12 @@ namespace
return labels(idx); return labels(idx);
} }
value_type factor_value (unsigned long idx, bool value) const value_type factor_value (unsigned long idx) const
{ {
++count; ++count;
DLIB_TEST(idx < (unsigned long)number_of_nodes()); DLIB_TEST(idx < (unsigned long)number_of_nodes());
if (value) return factors1(idx);
return factors1(idx,0);
else
return factors1(idx,1);
} }
value_type factor_value_disagreement (unsigned long idx1, unsigned long idx2) const value_type factor_value_disagreement (unsigned long idx1, unsigned long idx2) const
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment