wolffd@0: function engine = belprop_mrf2_inf_engine(mrf2, varargin) wolffd@0: % BELPROP_MRF2_INF_ENGINE Belief propagation for MRFs with discrete pairwise potentials wolffd@0: % engine = belprop_mrf2_inf_engine(mrf2, ...) wolffd@0: % wolffd@0: % This is like belprop_inf_engine, except it is designed for mrf2, so is much faster. wolffd@0: % wolffd@0: % [ ... ] = belprop_mrf2_inf_engine(..., 'param1',val1, 'param2',val2, ...) wolffd@0: % allows you to specify optional parameters as name/value pairs. wolffd@0: % Parameters modifying behavior of enter_evidence are below [default value in brackets] wolffd@0: % wolffd@0: % max_iter - max. num. iterations [ 5*nnodes] wolffd@0: % momentum - weight assigned to old message in convex combination wolffd@0: % (useful for damping oscillations) [0] wolffd@0: % tol - tolerance used to assess convergence [1e-3] wolffd@0: % verbose - 1 means print error at every iteration [0] wolffd@0: % wolffd@0: % Parameters can be changed later using set_params wolffd@0: wolffd@0: wolffd@0: % The advantages of pairwise potentials are wolffd@0: % (1) we can compute messages using vector-matrix multiplication wolffd@0: % (2) we can easily specify the parameters: one potential per edge wolffd@0: % In contrast, potentials on larger cliques are more complicated to deal with. wolffd@0: wolffd@0: wolffd@0: nnodes = length(mrf2.adj_mat); wolffd@0: wolffd@0: [engine.max_iter, engine.momentum, engine.tol, engine.verbose] = ... wolffd@0: process_options(varargin, 'max_iter', [], 'momentum', 0, 'tol', 1e-3, ... wolffd@0: 'verbose', 0); wolffd@0: wolffd@0: if isempty(engine.max_iter) % no user supplied value, so compute default wolffd@0: engine.max_iter = 5*nnodes; wolffd@0: %if acyclic(mrf2.adj_mat, 0) --- can be very slow! wolffd@0: % engine.max_iter = nnodes; wolffd@0: %else wolffd@0: % engine.max_iter = 5*nnodes; wolffd@0: %end wolffd@0: end wolffd@0: wolffd@0: engine.bel = cell(1, nnodes); % store results of enter_evidence here wolffd@0: engine.mrf2 = mrf2; wolffd@0: wolffd@0: engine = class(engine, 'belprop_mrf2_inf_engine'); wolffd@0: wolffd@0: