-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathgetClusters.m
94 lines (65 loc) · 3.11 KB
/
getClusters.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
function clsts= getClusters(net, opts, clstFn, k, dbTrain, trainDescFn)
if ~exist(clstFn, 'file')
if ~exist(trainDescFn, 'file')
simpleNnOpts= {'conserveMemory', true, 'mode', 'test'};
if opts.useGPU
net= relja_simplenn_move(net, 'gpu');
end
% ---------- extract training descriptors
relja_display('Computing training descriptors');
nTrain= 50000;
nPerImage= 100;
nIm= ceil(nTrain/nPerImage);
rng(43);
trainIDs= randsample(dbTrain.numImages, nIm);
nTotal= 0;
prog= tic;
for iIm= 1:nIm
relja_progress(iIm, nIm, 'extract train descs', prog);
% --- extract descriptors
% didn't want to complicate with batches here as it's only done once (per network and training set)
im= vl_imreadjpeg({[dbTrain.dbPath, dbTrain.dbImageFns{iIm}]});
im= im{1};
% fix non-colour images
if size(im,3)==1
im= cat(3,im,im,im);
end
im(:,:,1)= im(:,:,1) - net.meta.normalization.averageImage(1,1,1);
im(:,:,2)= im(:,:,2) - net.meta.normalization.averageImage(1,1,2);
im(:,:,3)= im(:,:,3) - net.meta.normalization.averageImage(1,1,3);
if opts.useGPU
im= gpuArray(im);
end
res= vl_simplenn(net, im, [], [], simpleNnOpts{:});
descs= gather(res(end).x);
descs= reshape( descs, [], size(descs,3) )';
% --- sample descriptors
nThis= min( min(nPerImage, size(descs,2)), nTrain - nTotal );
descs= descs(:, randsample( size(descs,2), nThis ) );
if iIm==1
trainDescs= zeros( size(descs,1), nTrain, 'single' );
end
trainDescs(:, nTotal+[1:nThis])= descs;
nTotal= nTotal+nThis;
end
trainDescs= trainDescs(:, 1:nTotal);
% move back to CPU addLayers() assumes it
if opts.useGPU
net= relja_simplenn_move(net, 'cpu');
end
save(trainDescFn, 'trainDescs');
else
relja_display('Loading training descriptors');
load(trainDescFn, 'trainDescs');
end
% ---------- Cluster descriptors
relja_display('Computing clusters');
clsts= yael_kmeans(trainDescs, k, 'niter', 100, 'verbose', 0, 'seed', 43);
clear trainDescs;
save(clstFn, 'clsts');
else
relja_display('Loading clusters');
load(clstFn, 'clsts');
assert(size(clsts, 2)==k);
end
end