-
Notifications
You must be signed in to change notification settings - Fork 1
/
runExperiments.m
111 lines (89 loc) · 3.69 KB
/
runExperiments.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
gamma = 10;
numLevels = 4;
iterations = 100;
t = 1:iterations+1;
graymap = zeros(256,3);
for i=1:256
graymap(i,:) = (i-1)*ones(1,3)/255;
end
for imnum=2:27
fprintf(1,'==Experiment %.2d==\n',imnum);
imname = sprintf('experiments/%.2d.png',imnum);
scribname = sprintf('experiments/dbscribs/%.2d_scribs.png',imnum);
gtname = sprintf('experiments/%.2d_gt.png',imnum);
labelname = sprintf('experiments/%.2d_labels.mat',imnum);
% Load shit.
I = double(imread(imname))./255;
% Size shit.
rows = size(I,1);
cols = size(I,2);
N = rows*cols;
gt = double(imread(gtname))./255;
gt = gt(:,:,1);
scribs = double(imread(scribname))./255;
scribs = scribs(:,:,1);
% DANGEROUS!
scribs = imresize(scribs, [rows,cols]);
%labels = load(labelname);
load(labelname);
% Scribbles and shit.
[c,b] = scribData(scribs);
c = reshape(c,[N,1]);
b = gamma*reshape(b,[N,1]);
[up,down] = mergeTransferOperators(I, labels, numLevels);
% Huge sparse matrix shit.
A = getLevinLap(I, 1e-4, 1) + gamma*spdiags(c,[0], N, N);
% Initial point.
x0 = 0.5*ones([N,1]);
% Gradient descent for various levels.
rgrad = zeros(iterations+1,numLevels+1);
rcg = zeros(iterations+1,numLevels+1);
for ell = numLevels+1:-1:1
upsmall = {};
downsmall = {};
for i = ell:numLevels
upsmall{end+1} = up{i};
downsmall{end+1} = down{i};
end
% =============Gradient Descent================
[xgrad, rgrad(:,numLevels+1-ell+1), xk] = multiGridRoughGradient(x0,A,b,upsmall,downsmall,iterations,gt);
myImwrite(reshape(xgrad,[rows,cols]), sprintf('results/%.2d_grad%d.png',imnum,numLevels+1-ell+1), [0,1]);
% Make a movie.
aviobj = avifile(sprintf('results/%.2d_grad%d.avi',imnum,numLevels+1-ell+1),'COLORMAP',graymap);
for framenum=1:iterations+1
aviobj = addframe(aviobj,myIm2Frame(reshape(xk(:,framenum),[rows,cols]),graymap));
end
aviobj = close(aviobj);
% ================CG Descent===================
[xcg, rcg(:,numLevels+1-ell+1), xk] = multiGridRoughCG(x0,A,b,upsmall,downsmall,iterations,gt);
myImwrite(reshape(xcg,[rows,cols]), sprintf('results/%.2d_cg%d.png',imnum,numLevels+1-ell+1), [0,1]);
% Make a movie.
aviobj = avifile(sprintf('results/%.2d_cg%d.avi',imnum,numLevels+1-ell+1),'COLORMAP',graymap);
for framenum=1:iterations+1
aviobj = addframe(aviobj,myIm2Frame(reshape(xk(:,framenum),[rows,cols]),graymap));
end
aviobj = close(aviobj);
end
% ================Combined Descent===================
[xcomb, rcombined, xk] = multiGridRoughCG(x0,A,b,upsmall,downsmall,iterations,gt,19);
myImwrite(reshape(xcomb,[rows,cols]), sprintf('results/%.2d_comb.png',imnum), [0,1]);
% Make a movie.
aviobj = avifile(sprintf('results/%.2d_comb.avi',imnum),'COLORMAP',graymap);
for framenum=1:iterations+1
aviobj = addframe(aviobj,myIm2Frame(reshape(xk(:,framenum),[rows,cols]),graymap));
end
aviobj = close(aviobj);
% Save raw data.
save(sprintf('results/%.2d_data.mat', imnum), 'rgrad', 'rcg', 'rcombined');
% Make a figure.
fig = figure(1);
plot(t,rgrad(:,1),'b-', t,rgrad(:,end),'c-', t,rcg(:,1),'r-', t,rcg(:,end),'m-', t,rcombined,'g-');
ylim([0,0.1]);
xlabel('Iterations');
ylabel('Ground Truth Error');
legend('Gradient', 'MG Gradient', 'CG', 'MG CG', 'Combined');
print(fig,sprintf('results/%.2d_plot.png',imnum),'-dpng');
%close all;
% Clean up shit.
clear A c b M xk;
end