-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tomo_cvx.m
143 lines (106 loc) · 3.32 KB
/
test_tomo_cvx.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
% Tomography test (with CVX package)
% intended for small-scale problems
%
% 2D Tomgoraphy example
% min_{x} | A*x - b |_2^2 subject to x \in {-1, 1}^n
%
% b - projected data
% A - tomography matrix
% x - (binary) image
%
% Note: - CVX must be installed!
% - If Gurobi is not installed, use sdpt3 solver instead.
%
% Created by:
% - Ajinkya Kadu, Utrecht University
% Feb 18, 2020
clc; clearvars; close all;
s = RandStream('mt19937ar','Seed',1);
RandStream.setGlobalStream(s);
addpath(genpath([pwd '/bin']));
%% generate true image, projection matrix and data
fprintf('------------- Setting up ---------------- \n')
n = 4; % size of image: total number of pixels - n^2
% create (random) binary image
xt = -1*ones(n^2,1);
xt(randperm(n^2,floor(0.5*n^2))) = 1;
xt = reshape(xt,n,n);
u = unique(xt(:));
% generate a tomography matrix (2: row + col sum, 3: row + col + diag sum)
A = getA(n,2);
if rank(A) < size(A,1)
[~,idx] = licols(A');
end
A = A(idx,:);
A = A/normest(A);
bt = A*xt(:);
% add noise to data (additive white Gaussian noise)
noiseLevel = 0.0;
noiseB = randn(size(bt));
noiseB = noiseLevel*(noiseB/norm(noiseB)*norm(bt));
b = bt + noiseB;
fprintf('matrix A and data b generated \n');
fprintf('rank of A: %d , m: %d, n: %d \n',rank(full(A)),size(A));
% measure the noise level (sigma value)
sigma = 0.5*norm(A*xt(:)-b)^2;
fprintf(['The noise is ' num2str(sigma) '\n']);
%% count solutions
if n <= 4
[totSol,Xint] = count_solutions_script(A,b,u,sigma);
else
totSol = 1;
Xint = xt;
end
%% PINV
fprintf('------- Pseudo-inverse solution --------- \n')
xP = pinv(full(A'*A))*(A'*b);
% threshold
xPt = xP;
xPt(xP<0) = -1;
xPt(xP>0) = 1;
xPt = reshape(xPt,n,n);
misfitP = 0.5*norm(A*xPt(:)-b)^2;
jacIdP = sum(max(xPt(:).*xt(:),0))/nnz(xt(:));
incIdP = nnz(min(xPt(:).*Xint(:),0)) + nnz(xPt(Xint==0));
fprintf('misfit = %.4f \n',misfitP);
fprintf('jaccard index = %.4f \n',jacIdP);
fprintf('Incorrect pixels = %d \n',incIdP);
%% dual - CVX
%
% solve: min_{p} |A'*p|_1 + 0.5*|p - b|_2^2
%
% We use Gurobi solver in conjunction with CVX
% In case Gurobi is not installed, use standard solver sdpt3
fprintf('------------- Dual solution ------------- \n')
cvx_solver gurobi % sdpt3
cvx_precision high
[rA,cA] = size(A);
cvx_begin quiet
variables p(rA)
minimize (norm(A'*p,1) + 0.5*sum_square(p - b))
cvx_end
q = A'*p;
qt = q;
qt(abs(q) < 1e-10) = 0; % thresholding of 1e-8
xD = sign(qt); % solution is signum function applied to dual variable
xD = reshape(xD,n,n);
misfitD = 0.5*norm(A*xD(:)-b)^2;
jacIdD = sum(max(xD(:).*xt(:),0))/nnz(xt(:));
jacIdDi = sum(max(xD(:).*Xint(:),0))/nnz(Xint(:));
incIdD = nnz(min(xD(:).*Xint(:),0)) + nnz(xD(Xint==0));
fprintf('misfit = %.4f \n',misfitD);
fprintf('jaccard index = %.4f \n',jacIdD);
fprintf('jaccard index (int) = %.4f \n',jacIdDi);
fprintf('Incorrect pixels = %d \n',incIdD);
%% compare
figure;
subplot(1,4,1); imagesc(xt,[-1 1]);axis image;
axis off; colormap gray; title('true');
subplot(1,4,2); imagesc(xPt,[-1 1]);axis image;
axis off; colormap gray; title('PINV');
subplot(1,4,3); imagesc(xD,[-1 1]);axis image;
axis off; colormap gray; title('Dual');
if totSol > 1
subplot(1,4,4); imagesc(Xint,[-1 1]);axis image;
axis off; colormap gray; title('Int');
end