-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBNF_multi_class_edge_affinity_demo.m
executable file
·239 lines (139 loc) · 5.22 KB
/
BNF_multi_class_edge_affinity_demo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
function BNF_multi_class_demo()
close all
addpath('Ncut_9');
lib_path='./libs/';
setup_path=[lib_path '/vlfeat-0.9.18/toolbox/vl_setup'];
run(setup_path);
%% INPUT PARAMS %%
type='aggressive'; % aggressive or non-agressive; agressive- slower,
% assigns more pixels to the background, which often
% leads to better accuracy
root_dir='multi_class_data/';
edge_dir=[root_dir 'edges/'];
img_dir=[root_dir 'images/'];
unary_dir=[root_dir 'unary_data/']; %unary potential files must be stored as HxWxC matrix
%H-height, W-width, C-number of classes where
%the matrix encodes probabilities for each
%class
if strcmp(type,'aggressive')
TH=1.5; %descides how aggressively to cut
%higher threshold meeans less cutting
end
sp_size=10;
% Globalization parameters
tol=10^-6;
max_it=200;
mu=0.025; %lower mu puts more weight on pairwise cost,
%higher mu puts more cost on unary cost
alpha=1/(1+mu);
beta=mu/(1+mu);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%% MAIN CODE
files=dir([img_dir '*.jpg']);
for file_no=1:numel(files)
cur_name=files(file_no).name;
[r,nm,ext]=fileparts(cur_name);
fprintf('Processing file %s %d / %d\n',nm,file_no,numel(files));
im_file=strcat(img_dir,nm,'.jpg');
edge_file=strcat(edge_dir,nm,'.png');
unary_file=strcat(unary_dir,nm,'.mat');
if exist(unary_file) && exist(edge_file)
fprintf('File no %d File nm %s\n',file_no,nm);
im=read_img_rgb(im_file);
sp_info=gen_supperpixel_info(im, sp_size);
sp_map=sp_info.sp_ind_map;
h=size(im,1);
w=size(im,2);
n=h*w;
edge_im=im2double(imread(edge_file));
load(unary_file);
%% ASSUMING THAT THE LOADED FILE WILL SHOW UP WITH THE FIELD 'data'
ch=size(data,3);
[V,I]=max(data,[],3);
fprintf('Constructing Affinity Matrix...\n');
[W,~]=get_my_W(im,edge_im);
[ii,jj,v_ic]=find(W);
v=ones(size(ii,1),1);
v(sp_map(ii)~=sp_map(jj))=v_ic(sp_map(ii)~=sp_map(jj));
%% Building single affinity matrix
v_unary=zeros(size(ii));
I_ii=I(ii);
I_jj=I(jj);
pos_ind=find(I_ii==I_jj);
pos_labels=I_ii(pos_ind);
v_unary(I_ii~=I_jj)=0.001;
y0_list=cell(ch,1);
count=0;
for c=1:ch
[rr,cc]=find(I==c);
if size(rr,1)>0
temp_ind=pos_ind(pos_labels==c);
class_fg=double(data(:,:,c));
y0_list{c}=class_fg(:);
count=count+1;
v_unary(temp_ind)=get_fc8_w(ii(temp_ind),jj(temp_ind),class_fg);
end
end
%% GLOBALIZATION STEP
%Combining Unary + Edge affinities;
%shouldnt be done if unaries are spatially disjoint !!!!
v=exp(v_unary).*v;
%v=v_ic;
W=sparse(ii,jj,v,n,n);
d = sum(abs(W),2);
D=spdiags(d,0,h*w,h*w);
X=zeros(n,ch);
XC=X;
for c=1:ch
[rr,cc]=find(I==c);
%if 0
if size(rr,1)>0
fprintf('Channel %d\n',c);
class_fg=double(data(:,:,c));
class_bg=imcomplement(class_fg);
A=D-alpha*W;
b=beta*class_fg(:);
x=pcg(A,b,tol,max_it);
X(:,c)=x;
if strcmp(type,'aggressive')
b=beta*class_bg(:);
xc=pcg(A,b,tol,max_it);
XC(:,c)=xc;
end
end
end
%% Extracting the Predictions
[~,S]=max(X,[],2);
if ~strcmp(type,'aggressive')
S=reshape(S,[h w]);
S=S-1;
else
bg_ind=find(S==1);
F=zeros(n,ch);
%% Aggressive Mode
for c=2:ch
temp=[XC(:,c) TH*X(:,c)];
[~,pred]=max(temp,[],2);
pred=pred-1;
pos_ind=find(pred==1);
F(pos_ind,c)=TH*X(pos_ind,c);
end
[~,S]=max(F,[],2);
S=reshape(S,[h w]);
S(bg_ind)=0;
S=max(S-1,0);
end
fprintf('Done\n');
temp=cat(2,I-1,S);
imshow(temp,colormap);
%pause(3)
%output_path=[output_dir nm '.png'];
%imwrite(temp,colormap,output_path);
end
end
end
function v=get_fc8_w(ii,jj,fc8)
v=max(abs(fc8(ii)-fc8(jj)),0.001);
sigma=0.12;
v=exp(-v/sigma);
end