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
sink_label = FREE_NODE;
// 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)
{
source_flows(i,0) = -g.factor_value(i, true);
sink_flows(i,1) = -g.factor_value(i, false);
const edge_type temp = g.factor_value(i);
if (temp < 0)
source_flows(i,0) = -temp;
else
sink_flows(i,1) = temp;
source_flows(i,1) = 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)
{
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
......@@ -439,7 +427,8 @@ namespace dlib
for (unsigned long i = 0; i < g.number_of_nodes(); ++i)
{
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)
......
......@@ -126,16 +126,16 @@ namespace dlib
typedef an_integer_or_real_type value_type;
value_type factor_value (
unsigned long idx,
bool lab
unsigned long idx
) const;
/*!
requires
- idx < number_of_nodes()
ensures
- 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 contained by lab.
the label of true. The larger the value, the more desirable it is to
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 (
......@@ -177,7 +177,7 @@ namespace dlib
precisely:
- let L(i) == the boolean label of the ith variable in prob. Or in other
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)
whenever the following conditions are true about i and j:
- i and j are neighbors in the graph defined by prob, that is,
......@@ -209,10 +209,6 @@ namespace dlib
model. In particular, this means that this function finds the assignments
to all the labels in prob which maximizes potts_model_score(#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
typedef double value_type;
private:
matrix<value_type,0,2> factors1;
matrix<value_type,0,1> factors1;
matrix<value_type> factors2;
matrix<node_label,0,1> labels;
public:
......@@ -39,7 +39,7 @@ namespace
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);
labels.set_size(num_nodes);
labels = FREE_NODE;
......@@ -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());
if (value)
return factors1(idx,0);
else
return factors1(idx,1);
return factors1(idx);
}
value_type factor_value_disagreement (unsigned long idx1, unsigned long idx2) const
......@@ -124,7 +121,7 @@ namespace
const static unsigned long max_number_of_neighbors = 4;
private:
matrix<value_type,0,2> factors1;
matrix<value_type,0,1> factors1;
matrix<value_type> factors2;
matrix<node_label,0,1> labels;
long nr;
......@@ -142,7 +139,7 @@ namespace
rect = rectangle(0,0,nc-1,nr-1);
inner_rect = shrink_rect(rect,1);
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;
//factors1 = 0;
......@@ -260,15 +257,12 @@ namespace
return labels(idx);
}
value_type factor_value (unsigned long idx, bool value) const
value_type factor_value (unsigned long idx) const
{
++count;
DLIB_TEST(idx < (unsigned long)number_of_nodes());
if (value)
return factors1(idx,0);
else
return factors1(idx,1);
return factors1(idx);
}
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