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