-
Notifications
You must be signed in to change notification settings - Fork 328
/
shapes_7_isomap.m
360 lines (277 loc) · 7.57 KB
/
shapes_7_isomap.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
%% Manifold Learning with Isomap
% This tour explores the Isomap algorithm for manifold learning.
%%
% The <http://waldron.stanford.edu/~isomap/ Isomap> algorithm is introduced in
%%
% _A Global Geometric Framework for Nonlinear Dimensionality Reduction,_
% J. B. Tenenbaum, V. de Silva and J. C. Langford,
% Science 290 (5500): 2319-2323, 22 December 2000.
%CMT
rep = 'results/shapes_isomap/';
if not(exist(rep))
mkdir(rep);
end
%CMT
perform_toolbox_installation('signal', 'general', 'graph');
%CMT
rep = 'results/fastmarching_bendinginv_2d/';
if not(exist(rep))
mkdir(rep);
end
%CMT
%% Graph Approximation of Manifolds
% Manifold learning consist in approximating the parameterization of a
% manifold represented as a point cloud.
%%
% First we load a simple 3D point cloud, the famous Swiss Roll.
%%
% Number of points.
n = 1000;
%%
% Random position on the parameteric domain.
x = rand(2,n);
%%
% Mapping on the manifold.
v = 3*pi/2 * (.1 + 2*x(1,:));
X = zeros(3,n);
X(2,:) = 20 * x(2,:);
X(1,:) = - cos( v ) .* v;
X(3,:) = sin( v ) .* v;
%%
% Parameter for display.
ms = 50;
lw = 1.5;
v1 = -15; v2 = 20;
%%
% Display the point cloud.
clf;
scatter3(X(1,:),X(2,:),X(3,:),ms,v, 'filled');
colormap jet(256);
view(v1,v2); axis('equal'); axis('off');
%CMT
saveas(gcf, [rep 'isomap-cloud.eps'], 'epsc');
%CMT
%%
% Compute the pairwise Euclidean distance matrix.
D1 = repmat(sum(X.^2,1),n,1);
D1 = sqrt(D1 + D1' - 2*X'*X);
%%
% Number of NN for the graph.
k = 6;
%%
% Compute the k-NN connectivity.
[DNN,NN] = sort(D1);
NN = NN(2:k+1,:);
DNN = DNN(2:k+1,:);
%%
% Adjacency matrix, and weighted adjacency.
B = repmat(1:n, [k 1]);
A = sparse(B(:), NN(:), ones(k*n,1));
%%
% Weighted adjacency (the metric on the graph).
W = sparse(B(:), NN(:), DNN(:));
%%
% Display the graph.
options.lw = lw;
options.ps = 0.01;
clf; hold on;
scatter3(X(1,:),X(2,:),X(3,:),ms,v, 'filled');
plot_graph(A, X, options);
colormap jet(256);
view(v1,v2); axis('equal'); axis('off');
zoom(.8);
%CMT
saveas(gcf, [rep 'isomap-graph.eps'], 'epsc');
%CMT
%% Floyd Algorithm to Compute Pairwise Geodesic Distances
% A simple algorithm to compute the geodesic distances between all pairs of
% points on a graph is Floyd iterative algorithm. Its complexity is
% |O(n^3)| where |n| is the number of points. It is thus quite slow for
% sparse graph, where Dijkstra runs in |O(log(n)*n^2)|.
%%
% Floyd algorithm iterates the following update rule, for |k=1,...,n|
%%
% |D(i,j) <- min(D(i,j), D(i,k)+D(k,j)|,
%%
% with the initialization |D(i,j)=W(i,j)| if |W(i,j)>0|, and
% |D(i,j)=Inf| if |W(i,j)=0|.
%%
% Make the graph symmetric.
D = full(W);
D = (D+D')/2;
%%
% Initialize the matrix.
D(D==0) = Inf;
%%
% Add connexion between a point and itself.
D = D - diag(diag(D));
%EXO
%% Implement the Floyd algorithm to compute the full distance matrix
%% |D|, where |D(i,j)| is the geodesic distance between
for i=1:n
% progressbar(i,n);
D = min(D,repmat(D(:,i),[1 n])+repmat(D(i,:),[n 1]));
end
%EXO
%%
% Find index of vertices that are not connected to the main manifold.
Iremove = find(D(:,1)==Inf);
%%
% Remove Inf remaining values (disconnected comonents).
D(D==Inf) = 0;
%% Isomap with Classical Multidimensional Scaling
% Isomap perform the dimensionality reduction by applying multidimensional
% scaling.
%%
% Please refers to the tours on Bending Invariant for detail on
% Classical MDS (strain minimization).
%EXO
%% Perform classical MDS to compute the 2D flattening.
% centered kernel
J = eye(n) - ones(n)/n;
K = -1/2 * J*(D.^2)*J;
% diagonalization
opt.disp = 0;
[Xstrain, val] = eigs(K, 2, 'LR', opt);
Xstrain = Xstrain .* repmat(sqrt(diag(val))', [n 1]);
Xstrain = Xstrain';
% plot graph
clf; hold on;
scatter(Xstrain(1,:),Xstrain(2,:),ms,v, 'filled');
plot_graph(A, Xstrain, options);
colormap jet(256);
axis('equal'); axis('off');
%EXO
%%
% Redess the points using the two leading eigenvectors of the covariance
% matrix (PCA correction).
[U,L] = eig(Xstrain*Xstrain' / n);
Xstrain1 = U'*Xstrain;
%%
% Remove problematic points.
Xstrain1(:,Iremove) = Inf;
%%
% Display the final result of the dimensionality reduction.
clf; hold on;
scatter(Xstrain1(1,:),Xstrain1(2,:),ms,v, 'filled');
plot_graph(A, Xstrain1, options);
colormap jet(256);
axis('equal'); axis('off');
%CMT
saveas(gcf, [rep 'isomap-strain.eps'], 'epsc');
%CMT
%%
% For comparison, the ideal locations on the parameter domain.
Y = cat(1, v, X(2,:));
Y(1,:) = rescale(Y(1,:), min(Xstrain(1,:)), max(Xstrain(1,:)));
Y(2,:) = rescale(Y(2,:), min(Xstrain(2,:)), max(Xstrain(2,:)));
%%
% Display the ideal graph on the reduced parameter domain.
clf; hold on;
scatter(Y(1,:),Y(2,:),ms,v, 'filled');
plot_graph(A, Y, options);
colormap jet(256);
axis('equal'); axis('off');
camroll(90);
%CMT
saveas(gcf, [rep 'isomap-ideal.eps'], 'epsc');
%CMT
%% Isomap with SMACOF Multidimensional Scaling
% It is possible to use SMACOF instead of classical scaling.
%%
% Please refers to the tours on Bending Invariant for detail on both
% Classical MDS (strain minimization) and SMACOF MDS (stress minimization).
%EXO
%% Perform stress minimization MDS using SMACOF to compute the 2D flattening.
niter = 150;
stress = [];
Xstress = X;
ndisp = [1 5 10 min(niter,100) Inf];
k = 1;
clf;
for i=1:niter
if ndisp(k)==i
subplot(2,2,k);
hold on;
scatter3(Xstress(1,:),Xstress(2,:),Xstress(3,:),ms,v, 'filled');
plot_graph(A, Xstress, options);
colormap jet(256);
view(v1,v2); axis('equal'); axis('off');
k = k+1;
end
% Compute the distance matrix.
D1 = repmat(sum(Xstress.^2,1),n,1);
D1 = sqrt(D1 + D1' - 2*Xstress'*Xstress);
% Compute the scaling matrix.
B = -D./max(D1,1e-10);
B = B - diag(sum(B));
% update
Xstress = (B*Xstress')' / n;
% Xstress = Xstress-repmat(mean(Xstress,2), [1 n]);
% record stress
stress(end+1) = sqrt( sum( abs(D(:)-D1(:)).^2 ) / n^2 );
end
%EXO
%%
% Plot stress evolution during minimization.
clf;
plot(stress(1:end), '.-');
axis('tight');
%CMT
%% Perform stress minimization MDS using SMACOF to compute the 2D flattening.
niter = 100;
Xstress = X;
ndisp = [1 5 10 niter Inf];
k = 1;
clf;
for i=1:niter
if ndisp(k)==i
clf; hold on;
scatter3(Xstress(1,:),Xstress(2,:),Xstress(3,:),ms,v, 'filled');
plot_graph(A, Xstress, options);
colormap jet(256);
view(v1,v2); axis('equal'); axis('off');
zoom(.8);
saveas(gcf, [rep 'isomap-stress-' num2str(k) '.eps'], 'epsc');
k = k+1;
end
% Compute the distance matrix.
D1 = repmat(sum(Xstress.^2,1),n,1);
D1 = sqrt(D1 + D1' - 2*Xstress'*Xstress);
% Compute the scaling matrix.
B = -D./max(D1,1e-10);
B = B - diag(sum(B));
% update
Xstress = (B*Xstress')' / n;
end
%CMT
%%
% Compute the main direction of the point clouds.
[U,L] = eig(Xstress*Xstress' / n);
[L,I] = sort(diag(L));
U = U(:,I(2:3));
%%
% Project the points on the two leading eigenvectors of the covariance
% matrix (PCA final projection).
Xstress1 = U'*Xstress;
%%
% Remove problematic points.
Xstress1(:,Iremove) = Inf;
%%
% Display the final result of the dimensionality reduction.
clf; hold on;
scatter(Xstress1(1,:),Xstress1(2,:),ms,v, 'filled');
plot_graph(A, Xstress1, options);
colormap jet(256);
axis('equal'); axis('off');
%CMT
saveas(gcf, [rep 'isomap-stress.eps'], 'epsc');
%CMT
%% Learning Manifold of Patches
% Isomap algorithm can be used to analyze the structure of a high
% dimensional library of images.
%EXO
%% Apply Isomap to a library of small images, for instance binary digits or
%% faces with a rotating camera.
% No correction for this exercise.
%EXO