-
Notifications
You must be signed in to change notification settings - Fork 3
/
draft1.tex
315 lines (265 loc) · 8.58 KB
/
draft1.tex
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
% This LaTeX was auto-generated from MATLAB code.
% To make changes, update the MATLAB code and export to LaTeX again.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{color}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{epstopdf}
\usepackage{matlab}
\sloppy
\epstopdfsetup{outdir=./}
\graphicspath{ {./draft1_images/} }
\begin{document}
\matlabtitle{RX Basic Implementation}
\matlabheading{Introduction}
\begin{par}
\begin{flushleft}
THis notebook is meant to help implement the RX (and later the SSRX) algorithm on the RIT Blind Test data set.
\end{flushleft}
\end{par}
\begin{par}
\begin{flushleft}
The RX algorithm is given by: $\textrm{RX}={\left(\overrightarrow{X} -\overrightarrow{m} \right)}^T \phi^{-1} \left(\overrightarrow{X} -\overrightarrow{m} \right)>\mathrm{Threshold}$
\end{flushleft}
\end{par}
\begin{matlabcode}
file = 'G:\My Drive\Project\self_test\HyMap\self_test_rad.img';
[mat,hdr] = auto_load_HS(file);
\end{matlabcode}
\begin{matlaboutput}
Loading ENVI header ...
Loading ENVI image ...
\end{matlaboutput}
\begin{matlabcode}
data=double(permute(mat,[2,1,3]));
[x_size,y_size, num_of_bands]=size(data);
[X_MINUS_M,phi]=HSI_MF_params(data);
phi_inv = pinv(phi);
RX = zeros(x_size,y_size);
for x = 1:x_size
for y = 1:y_size
x_minus_m5 = squeeze(X_MINUS_M(x,y,:));
RX(x,y) = x_minus_m5' * phi_inv * x_minus_m5;
end
end
RX= RX';
RX_filt = RX > mean(RX(:)) + 3*std(RX(:));
\end{matlabcode}
\begin{par}
\begin{flushleft}
visualising results:
\end{flushleft}
\end{par}
\begin{matlabcode}
[~, idxs] = maxk(RX(:), 10);
[row,col] = ind2sub(size(RX),idxs);
figure; imshow(data(:,:,1)',[]); hold on;
plot(col,row,'og')
title('RX Top 10 Anomalies')
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{95.53437029603613em}]{figure_0}
\end{center}
\matlabheading{SSRX Algorithm}
\begin{par}
\begin{flushleft}
The SSRX algorithm adds a preprocessing step to the RX algrotihm - performing PCA and keeping only low variance PCs.
\end{flushleft}
\end{par}
\begin{par}
\begin{flushleft}
the formula for PCA is given by:
\end{flushleft}
\end{par}
\begin{par}
$$X_{\mathrm{PCA}} =\left(X-m\right)\phi {\;}^{-1} V_q$$
\end{par}
\begin{par}
$$\begin{array}{l}\linebreak
m-\textrm{the}\;\textrm{per}\;\textrm{channel}\;\textrm{mean}\;\textrm{of}\;X\\\linebreak
\phi -\textrm{the}\;\textrm{covariance}\;\textrm{matrix}\\\linebreak
V_q -a\;\textrm{matrix}\;\textrm{where}\;\textrm{every}\;\textrm{column}\;\textrm{is}\;\textrm{an}\;\textrm{eigen}\;\textrm{vector}\;\textrm{of}\;\phi \;\mathrm{while}\;\mathrm{ommiting}\;\mathrm{the}\;q\;\mathrm{eigen}\;\mathrm{vectors}\;\mathrm{that}\;\mathrm{correspond}\;\mathrm{to}\;\mathrm{the}\;\mathrm{highest}\;\mathrm{variance}\;\mathrm{eigen}\;\mathrm{values}\linebreak
\end{array}$$
\end{par}
\begin{par}
\begin{flushleft}
Given $X_{\mathrm{PCA}}$ we can now run RX to obtain SSRX:
\end{flushleft}
\end{par}
\begin{par}
$$\mathrm{SS}\textrm{RX}={\left(\overrightarrow{X_{\mathrm{PCA}} } -\overrightarrow{m_{\mathrm{PCA}} } \right)}^T {\phi_{\mathrm{PCA}} }^{-1} \left(\overrightarrow{X_{\mathrm{PCA}} } -\overrightarrow{m_{\mathrm{PCA}} } \right)>\textrm{Threshold}$$
\end{par}
\begin{matlabcode}
q = 120;
[V, D] = eigs(phi,126);
V5 = V(:,6:end); % deleting first 5 PC
V50 = V(:,51:end); % deleting first 50 PC
V120 = V(:,121:end);% deleting first 120 PC
data_pca2D5 = reshape(X_MINUS_M,x_size*y_size,num_of_bands) * V5;
data_pca2D50 = reshape(X_MINUS_M,x_size*y_size,num_of_bands) * V50;
data_pca2D120 = reshape(X_MINUS_M,x_size*y_size,num_of_bands) * V120;
data_pca5 = hyperConvert3d(data_pca2D5', x_size,y_size,num_of_bands);
data_pca50 = hyperConvert3d(data_pca2D50', x_size,y_size,num_of_bands);
data_pca120 = hyperConvert3d(data_pca2D120', x_size,y_size,num_of_bands);
\end{matlabcode}
\begin{par}
\begin{flushleft}
Now that we have the preprocessed data, we can run the RX algorithm on it to get SSRX:
\end{flushleft}
\end{par}
\begin{matlabcode}
%calc phi_inv
[X_MINUS_M_PCA5,phi_pca5]=HSI_MF_params(data_pca5);
[X_MINUS_M_PCA50,phi_pca50]=HSI_MF_params(data_pca50);
[X_MINUS_M_PCA120,phi_pca120]=HSI_MF_params(data_pca120);
phi_pca_inv5 = pinv(phi_pca5);
phi_pca_inv50 = pinv(phi_pca50);
phi_pca_inv120 = pinv(phi_pca120);
%init empty results matrices
SSRX5 = zeros(x_size,y_size);
SSRX50 = zeros(x_size,y_size);
SSRX120 = zeros(x_size,y_size);
for x = 1:x_size
for y = 1:y_size
x_minus_m5 = squeeze(X_MINUS_M_PCA5(x,y,:));
x_minus_m50 = squeeze(X_MINUS_M_PCA50(x,y,:));
x_minus_m120 = squeeze(X_MINUS_M_PCA120(x,y,:));
SSRX5(x,y) = x_minus_m5' * phi_pca_inv5 * x_minus_m5;
SSRX50(x,y) = x_minus_m50' * phi_pca_inv50 * x_minus_m50;
SSRX120(x,y) = x_minus_m120' * phi_pca_inv120 * x_minus_m120;
end
end
SSRX5= SSRX5';
SSRX50= SSRX50';
SSRX120= SSRX120';
SSRX5_filt = SSRX5 > mean(SSRX5(:)) + 3*std(SSRX5(:));
SSRX50_filt = SSRX50 > mean(SSRX50(:)) + 3*std(SSRX50(:));
SSRX120_filt = SSRX120 > mean(SSRX120(:)) + 3*std(SSRX120(:));
\end{matlabcode}
\vspace{1em}
\matlabheading{Top 10 Anomalies}
\begin{par}
\begin{flushleft}
Let's look at the top 10 results of the RX and the SSRX. Firstly, let's compare the different flavours of SSRX to see if there's any change in top anomalies:
\end{flushleft}
\end{par}
\begin{matlabcode}
[~, idxs_pca5] = maxk(SSRX5(:), 10);
[~, idxs_pca50] = maxk(SSRX50(:), 10);
[~, idxs_pca120] = maxk(SSRX120(:), 10);
[row_pca5,col_pca5] = ind2sub(size(SSRX5),idxs);
[row_pca50,col_pca50] = ind2sub(size(SSRX50),idxs);
[row_pca120,col_pca120] = ind2sub(size(SSRX120),idxs);
figure; imshow(data(:,:,1)',[]); hold on;
plot(col_pca5,row_pca5,'or')
plot(col_pca50,row_pca50,'xg')
plot(col_pca50,row_pca50,'sb')
legend('Delete 5 PC', 'Delete 50 PC', 'Delete 120 PC')
title('SSRX Top 10 Anomalies')
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{95.53437029603613em}]{figure_1}
\end{center}
\begin{par}
\begin{flushleft}
\textbf{Conclusion:} top 10 SSRX anomalies stay the same regardless of how many PCs we cut off.
\end{flushleft}
\end{par}
\vspace{1em}
\begin{par}
\begin{flushleft}
Now let's compare one the SSRX flavours to RX:
\end{flushleft}
\end{par}
\begin{matlabcode}
figure; imshow(data(:,:,1)',[]); hold on;
plot(col,row,'xr')
plot(col_pca5,row_pca5,'og')
title('SSRX5 vs RX Top 10 Anomalies')
legend('RX','SSRX5')
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{95.53437029603613em}]{figure_2}
\end{center}
\begin{par}
\begin{flushleft}
\textbf{Conclusion:} top 10 RX anomalies remain for SSRX as well.
\end{flushleft}
\end{par}
\matlabheading{Scatter Plot Comparison}
\begin{par}
\begin{flushleft}
Based on the results above, the difference between the 2 algorithms (if exists) is in the lower scored anomalies. Let's try to look at that:
\end{flushleft}
\end{par}
\begin{matlabcode}
h1=figure;
spy(RX_filt,'or');
hold on;
spy(SSRX5_filt,'xg');
legend('RX','SSRX');
title('RX vs SSRX (deleting 5 highest variance PCs');
set(h1, 'Position', [0 0 1200 600])
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{120.42147516307075em}]{figure_3}
\end{center}
\begin{matlabcode}
h2=figure;
spy(RX_filt,'or');
hold on;
spy(SSRX50_filt,'xg');
legend('RX','SSRX50');
title('RX vs SSRX (deleting 50 highest variance PCs');
set(h2, 'Position', [0 0 1200 600])
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{120.42147516307075em}]{figure_4}
\end{center}
\begin{matlabcode}
h2=figure;
spy(RX_filt,'or');
hold on;
spy(SSRX120_filt,'xg');
legend('RX','SSRX120');
title('RX vs SSRX (deleting 120 highest variance PCs');
set(h2, 'Position', [0 0 1200 600])
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{120.42147516307075em}]{figure_5}
\end{center}
\begin{par}
\begin{flushleft}
\textbf{Conclusion:} Seems like RX provides best results over all SSRX variants, and that the best SSRX variant is obtained by deleting only 5 hig variance PCs.
\end{flushleft}
\end{par}
\begin{par}
\begin{flushleft}
Histogram of all scores
\end{flushleft}
\end{par}
\begin{matlabcode}
[RX_val,RX_bins]=histcounts(RX,1000);
[SSRX5_val,SSRX5_bins]=histcounts(SSRX5,1000);
[SSRX50_val,SSRX50_bins]=histcounts(SSRX50,1000);
[SSRX120_val,SSRX120_bins]=histcounts(SSRX120,1000);
figure;
plot(RX_bins(1:1000),RX_val);
hold on;
plot(SSRX5_bins(1:1000),SSRX5_val);
plot(SSRX50_bins(1:1000),SSRX50_val);
plot(SSRX120_bins(1:1000),SSRX120_val);
xlim([-100 1000]);
legend('RX','SSRX5','SSRX50','SSRX120');
title('Histograms');
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{56.196688409433015em}]{figure_6}
\end{center}
\end{document}