-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcvx_quadprog_comparison.m
181 lines (149 loc) · 4.18 KB
/
cvx_quadprog_comparison.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
clc
clear
close all
%% Prepare the reference image
im = imread('exampleImage.png');
im = rgb2gray(im);
im = imresize(im, [ 128 128 ]);
im = 1 - im2double(im);
%% Simulate the low-resolution images
numImages = 10;
blurSigma = 2;
scaleFactor = 2;
[ images, offsets, croppedOriginal ] = SynthDataset(im, numImages, blurSigma, scaleFactor);
%%
[A , b, G] = formulateProblemCOPY(images, offsets, scaleFactor, blurSigma );
%%
figure
subplot(1,2,1)
imagesc(A, [0, 0.15])
colorbar
subplot(1,2,2)
imagesc(A2, [0, 0.15])
colorbar
%% solve with CVX
tic
[highResL2CVX, residualsL2CVX] = solveCVX(A, b, G, 5e-3, 2, size(images{1}));
cvxL2runtime = toc;
tic
[highResL1CVX, residualsL1CVX] = solveCVX(A, b, G, 5e-3, 1, size(images{1}));
cvxL1runtime = toc;
%% solve with quadprog
tic
[highResL2QP, residualsL2QP] = solveQuadprog(A, b, G, 5e-3, 2, size(images{1}));
qpL2runtime = toc;
tic
[highResL1QP, residualsL1QP] = solveQuadprog(A, b, G, 5e-3, 1, size(images{1}));
qpL1runtime = toc;
%% print runtimes
fprintf('CVX Runtimes\n')
fprintf('\t L2: %f\n\t L1: %f\n', cvxL2runtime, cvxL1runtime);
fprintf('CVX Runtimes\n')
fprintf('\t L2: %f\n\t L1: %f\n', qpL2runtime, qpL1runtime);
%% visualize solutions
figure
subplot(2,3,1)
imagesc(highResL2CVX, [0, 1])
title('CVX L2 Image')
axis image
subplot(2,3,2)
imagesc(highResL2QP, [0, 1])
title('Quadprog L2 Image')
axis image
subplot(2,3,3)
imagesc(highResL2CVX - highResL2QP)
title('CVX L2 - Quadprog L2')
axis image
colorbar
subplot(2,3,4)
imagesc(highResL1CVX, [0, 1])
title('CVX L2 Image')
axis image
subplot(2,3,5)
imagesc(highResL1QP, [0, 1])
title('Quadprog L2 Image')
axis image
subplot(2,3,6)
imagesc(highResL1CVX - highResL1QP)
title('CVX L2 - Quadprog L2')
axis image
colorbar
%%
figure
subplot(2,2,1)
imagesc(croppedOriginal, [0, 1])
title('Original Image')
axis image
% subplot(2,2,2)
% imagesc(highResGradDesc, [0,1])
% title(sprintf('Gradient Descent (mse = %.1d)', mean((highResGradDesc(:) - croppedOriginal(:)).^2)))
% axis image
subplot(2,2,3)
imagesc(highResL2CVX, [0, 1])
title(sprintf('L-2 gradient regularization (mse = %.1d)', mean((highResL2CVX(:) - croppedOriginal(:)).^2)))
axis image
subplot(2,2,4)
imagesc(highResL1CVX, [0, 1])
title(sprintf('L-1 gradient regularization (mse = %.1d)', mean((highResL1CVX(:) - croppedOriginal(:)).^2)))
axis image
%% run at multiple scale factors to create plot with curves
scaleFactors = [1,2,3,4];
timesL1cvx = zeros(size(scaleFactors));
timesL2cvx = zeros(size(scaleFactors));
timesL1qp = zeros(size(scaleFactors));
timesL2qp = zeros(size(scaleFactors));
for ii = 1:length(scaleFactors)
fprintf('Running with scale factor %d\n\n\n\n', scaleFactors(ii));
im = imread('exampleImage.png');
im = rgb2gray(im);
im = imresize(im, [ 64 64 ] * scaleFactors(ii));
im = 1 - im2double(im);
[ images, offsets, croppedOriginal ] = SynthDataset(im, numImages, blurSigma, scaleFactors(ii));
[A , b, G] = formulateProblemCOPY(images, offsets, scaleFactors(ii), blurSigma );
disp(size(A))
disp(size(b))
disp(size(G))
% solve with CVX
tic
[highResL2CVX, residualsL2CVX] = solveCVX(A, b, G, 5e-3, 2, size(images{1}));
timesL2cvx(ii) = toc;
tic
[highResL1CVX, residualsL1CVX] = solveCVX(A, b, G, 5e-3, 1, size(images{1}));
timesL1cvx(ii) = toc;
% solve with quadprog
tic
[highResL2QP, residualsL2QP] = solveQuadprog(A, b, G, 5e-3, 2, size(images{1}));
timesL2qp(ii) = toc;
tic
[highResL1QP, residualsL1QP] = solveQuadprog(A, b, G, 5e-3, 1, size(images{1}));
timesL1qp(ii) = toc;
% visual inspection for each run
figure
subplot(2,2,1)
imagesc(highResL1CVX)
axis image
subplot(2,2,2)
imagesc(highResL2CVX)
axis image
subplot(2,2,3)
imagesc(highResL1QP)
axis image
subplot(2,2,4)
imagesc(highResL2QP)
end
%%
figure
subplot(1,2,1)
hold on
plot(scaleFactors, timesL1cvx,'o-','linewidth',2)
plot(scaleFactors, timesL1qp,'s-','linewidth',2)
xlabel('Scale Factor')
ylabel('Runtime')
title('L-1 Regularization Runtimes')
subplot(1,2,2)
hold on
plot(scaleFactors, timesL2cvx,'o-','linewidth',2)
plot(scaleFactors, timesL2qp,'s-','linewidth',2)
xlabel('Scale Factor')
ylabel('Runtime')
title('L-2 Regularization Runtimes')