annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/fgraph/fg_mrf1.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 seed = 0;
wolffd@0 2 rand('state', seed);
wolffd@0 3 randn('state', seed);
wolffd@0 4
wolffd@0 5 nrows = 3;
wolffd@0 6 ncols = 3;
wolffd@0 7 npixels = nrows*ncols;
wolffd@0 8
wolffd@0 9 % we number pixels in transposed raster scan order (top to bottom, left to right)
wolffd@0 10
wolffd@0 11 % hidden var
wolffd@0 12 HV = reshape(1:npixels, nrows, ncols);
wolffd@0 13 % observed var
wolffd@0 14 OV = reshape(1:npixels, nrows, ncols) + length(HV(:));
wolffd@0 15
wolffd@0 16 % observed factor
wolffd@0 17 OF = reshape(1:npixels, nrows, ncols);
wolffd@0 18 % vertical edge factor VEF(i,j) is the factor for edge HV(i,j) - HV(i+1,j)
wolffd@0 19 VEF = reshape((1:(nrows-1)*ncols), nrows-1, ncols) + length(OF(:));
wolffd@0 20 % horizontal edge factor HEF(i,j) is the factor for edge HV(i,j) - HV(i,j+1)
wolffd@0 21 HEF = reshape((1:nrows*(ncols-1)), nrows, ncols-1) + length(OF(:)) + length(VEF(:));
wolffd@0 22
wolffd@0 23 nvars = length(HV(:))+length(OV(:));
wolffd@0 24 assert(nvars == 2*npixels);
wolffd@0 25 nfac = length(OF(:)) + length(VEF(:)) + length(HEF(:));
wolffd@0 26
wolffd@0 27 K = 2; % number of discrete values for the hidden vars
wolffd@0 28 %O = 1; % each observed pixel is a scalar
wolffd@0 29 O = 2; % each observed pixel is binary
wolffd@0 30
wolffd@0 31 factors = cell(1,3);
wolffd@0 32
wolffd@0 33 % hidden states generate observed 0 or 1 plus noise
wolffd@0 34 %factors{2} = cond_gauss1_kernel(K, O, 'mean', [0 1], 'cov', [0.1 0.1]);
wolffd@0 35 pnoise = 0.2;
wolffd@0 36 factors{1} = tabular_kernel([K O], [1-pnoise pnoise; pnoise 1-pnoise]);
wolffd@0 37 ofactor = 1;
wolffd@0 38
wolffd@0 39 % encourage compatibility between neighboring vertical pixels
wolffd@0 40 factors{2} = tabular_kernel([K K], [0.8 0.2; 0.2 0.8]);
wolffd@0 41 vedge_factor = 2;
wolffd@0 42
wolffd@0 43 %% no constraint between neighboring horizontal pixels
wolffd@0 44 %factors{3} = tabular_kernel([K K], [0.5 0.5; 0.5 0.5]);
wolffd@0 45
wolffd@0 46 factors{3} = tabular_kernel([K K], [0.8 0.2; 0.2 0.8]);
wolffd@0 47 hedge_factor = 3;
wolffd@0 48
wolffd@0 49
wolffd@0 50
wolffd@0 51 factor_ndx = zeros(1, 3);
wolffd@0 52 G = zeros(nvars, nfac);
wolffd@0 53 ns = [K*ones(1,length(HV(:))) O*ones(1,length(OV(:)))];
wolffd@0 54
wolffd@0 55 N = length(ns);
wolffd@0 56 %cnodes = OV(:);
wolffd@0 57 cnodes = [];
wolffd@0 58 dnodes = 1:N;
wolffd@0 59
wolffd@0 60 for i=1:nrows
wolffd@0 61 for j=1:ncols
wolffd@0 62 G([HV(i,j), OV(i,j)], OF(i,j)) = 1;
wolffd@0 63 factor_ndx(OF(i,j)) = ofactor;
wolffd@0 64
wolffd@0 65 if i < nrows
wolffd@0 66 G(HV(i:i+1,j), VEF(i,j)) = 1;
wolffd@0 67 factor_ndx(VEF(i,j)) = vedge_factor;
wolffd@0 68 end
wolffd@0 69
wolffd@0 70 if j < ncols
wolffd@0 71 G(HV(i,j:j+1), HEF(i,j)) = 1;
wolffd@0 72 factor_ndx(HEF(i,j)) = hedge_factor;
wolffd@0 73 end
wolffd@0 74
wolffd@0 75 end
wolffd@0 76 end
wolffd@0 77
wolffd@0 78
wolffd@0 79 fg = mk_fgraph(G, ns, factors, 'discrete', dnodes, 'equiv_class', factor_ndx);
wolffd@0 80
wolffd@0 81 if 1
wolffd@0 82 % make image with vertical stripes
wolffd@0 83 I = zeros(nrows, ncols);
wolffd@0 84 for j=1:2:ncols
wolffd@0 85 I(:,j) = 1;
wolffd@0 86 end
wolffd@0 87 else
wolffd@0 88 % make image with square in middle
wolffd@0 89 I = zeros(nrows, ncols);
wolffd@0 90 I(3:6,3:6) = 1;
wolffd@0 91 end
wolffd@0 92
wolffd@0 93
wolffd@0 94 % corrupt image
wolffd@0 95 O = mod(I + (rand(nrows,ncols)> (1-pnoise)), 2);
wolffd@0 96
wolffd@0 97 maximize = 1;
wolffd@0 98 engine = belprop_fg_inf_engine(fg, 'maximize', maximize, 'max_iter', npixels*5);
wolffd@0 99
wolffd@0 100 evidence = cell(1, nvars);
wolffd@0 101 onodes = OV(:);
wolffd@0 102 evidence(onodes) = num2cell(O+1); % values must be in range {1,2}
wolffd@0 103
wolffd@0 104 engine = enter_evidence(engine, evidence);
wolffd@0 105
wolffd@0 106 for i=1:nrows
wolffd@0 107 for j=1:ncols
wolffd@0 108 m = marginal_nodes(engine, HV(i,j));
wolffd@0 109 Ihat(i,j) = argmax(m.T)-1;
wolffd@0 110 end
wolffd@0 111 end
wolffd@0 112
wolffd@0 113 Ihat