summaryrefslogtreecommitdiffstats
path: root/SD-VBS/common/toolbox/toolbox_basic/textons
diff options
context:
space:
mode:
authorLeo Chan <leochanj@live.unc.edu>2020-10-22 01:53:21 -0400
committerJoshua Bakita <jbakita@cs.unc.edu>2020-10-22 01:56:35 -0400
commitd17b33131c14864bd1eae275f49a3f148e21cf29 (patch)
tree0d8f77922e8d193cb0f6edab83018f057aad64a0 /SD-VBS/common/toolbox/toolbox_basic/textons
parent601ed25a4c5b66cb75315832c15613a727db2c26 (diff)
Squashed commit of the sb-vbs branch.
Includes the SD-VBS benchmarks modified to: - Use libextra to loop as realtime jobs - Preallocate memory before starting their main computation - Accept input via stdin instead of via argc Does not include the SD-VBS matlab code. Fixes libextra execution in LITMUS^RT.
Diffstat (limited to 'SD-VBS/common/toolbox/toolbox_basic/textons')
-rwxr-xr-xSD-VBS/common/toolbox/toolbox_basic/textons/dist2.m27
-rwxr-xr-xSD-VBS/common/toolbox/toolbox_basic/textons/find_textons.m46
-rwxr-xr-xSD-VBS/common/toolbox/toolbox_basic/textons/find_textons1.m37
-rwxr-xr-xSD-VBS/common/toolbox/toolbox_basic/textons/kmeans2.m127
4 files changed, 237 insertions, 0 deletions
diff --git a/SD-VBS/common/toolbox/toolbox_basic/textons/dist2.m b/SD-VBS/common/toolbox/toolbox_basic/textons/dist2.m
new file mode 100755
index 0000000..f2d93e1
--- /dev/null
+++ b/SD-VBS/common/toolbox/toolbox_basic/textons/dist2.m
@@ -0,0 +1,27 @@
1function n2 = dist2(x, c)
2%DIST2 Calculates squared distance between two sets of points.
3%
4% Description
5% D = DIST2(X, C) takes two matrices of vectors and calculates the
6% squared Euclidean distance between them. Both matrices must be of
7% the same column dimension. If X has M rows and N columns, and C has
8% L rows and N columns, then the result has M rows and L columns. The
9% I, Jth entry is the squared distance from the Ith row of X to the
10% Jth row of C.
11%
12% See also
13% GMMACTIV, KMEANS, RBFFWD
14%
15
16% Copyright (c) Christopher M Bishop, Ian T Nabney (1996, 1997)
17
18[ndata, dimx] = size(x);
19[ncentres, dimc] = size(c);
20if dimx ~= dimc
21 error('Data dimension does not match dimension of centres')
22end
23
24n2 = (ones(ncentres, 1) * sum((x.^2)', 1))' + ...
25 ones(ndata, 1) * sum((c.^2)',1) - ...
26 2.*(x*(c'));
27
diff --git a/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons.m b/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons.m
new file mode 100755
index 0000000..e7fa4b0
--- /dev/null
+++ b/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons.m
@@ -0,0 +1,46 @@
1function [centers,label,post,d2]=find_textons(FIw,ncenters,centers_in,n_iter);
2% [centers,label,post,d2]=find_textons(FIw,ncenters,centers_in,n_iter);
3%
4% find textons using kmeans for windowed portion FIw of filtered image
5%
6% to start with centers pulled randomly from image, set centers_in=[]
7
8% define number of textons
9%ncenters=25;
10
11[N1,N2,N3]=size(FIw);
12% reshape filtered image stack into a long array of feature vectors
13fv=reshape(FIw,N1*N2,N3);
14% (each row is a feature vector)
15
16%centers=.01^2*randn(ncenters,N3);
17% take centers randomly from within image
18if isempty(centers_in)
19 rndnum=1+floor(N1*N2*rand(1,ncenters));
20 centers_in=fv(rndnum,:);
21end
22
23options = foptions;
24options(1)=1; % Prints out error values.
25options(5) = 0;
26if nargin<4
27 n_iter=15;
28end
29options(14) = n_iter; % Number of iterations.
30
31[centers,options,d2,post]=kmeans2(centers_in,fv,options);
32
33% reshuffle the centers so that the one closest to the origin
34% (featureless) comes last
35norms=sum(centers.^2,2);
36[sortval,sortind]=sort(-norms);
37centers=centers(sortind,:);
38d2=reshape(d2,N1,N2,ncenters);
39post=reshape(post,N1,N2,ncenters);
40d2=d2(:,:,sortind);
41post=post(:,:,sortind);
42
43
44% retrieve cluster number assigned to each feature vector
45[minval,label]=min(d2,[],3);
46
diff --git a/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons1.m b/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons1.m
new file mode 100755
index 0000000..b192015
--- /dev/null
+++ b/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons1.m
@@ -0,0 +1,37 @@
1function [centers,label,post,d2]=find_textons(fv,ncenters,centers_in,n_iter);
2% [centers,label,post,d2]=find_textons(FIw,ncenters,centers_in,n_iter);
3%
4% find textons using kmeans for windowed portion FIw of filtered image
5%
6% to start with centers pulled randomly from image, set centers_in=[]
7
8[N1,N2] =size(fv);
9
10% take centers randomly from within image
11if isempty(centers_in)
12 rndnum=1+floor(N1*rand(1,ncenters));
13 centers_in=fv(rndnum,:);
14end
15
16options = foptions;
17options(1)=1; % Prints out error values.
18options(5) = 0;
19if nargin<4
20 n_iter=15;
21end
22options(14) = n_iter; % Number of iterations.
23
24[centers,options,d2,post]=kmeans2(centers_in,fv,options);
25
26
27% retrieve cluster number assigned to each feature vector
28[minval,label]=min(d2,[],2);
29
30
31h = hist(label(:),[1:max(label(:))]);
32a = h>0;
33a = cumsum(a);
34
35[nr,nc] = size(label);
36label = reshape(a(label(:)),nr,nc);
37
diff --git a/SD-VBS/common/toolbox/toolbox_basic/textons/kmeans2.m b/SD-VBS/common/toolbox/toolbox_basic/textons/kmeans2.m
new file mode 100755
index 0000000..0bd87b2
--- /dev/null
+++ b/SD-VBS/common/toolbox/toolbox_basic/textons/kmeans2.m
@@ -0,0 +1,127 @@
1function [centres, options, d2, post, errlog] = kmeans2(centres, data, options)
2%KMEANS Trains a k means cluster model.
3%
4% Description
5% CENTRES = KMEANS(CENTRES, DATA, OPTIONS) uses the batch K-means
6% algorithm to set the centres of a cluster model. The matrix DATA
7% represents the data which is being clustered, with each row
8% corresponding to a vector. The sum of squares error function is used.
9% The point at which a local minimum is achieved is returned as
10% CENTRES. The error value at that point is returned in OPTIONS(8).
11%
12% [CENTRES, OPTIONS, POST, ERRLOG] = KMEANS(CENTRES, DATA, OPTIONS)
13% also returns the cluster number (in a one-of-N encoding) for each
14% data point in POST and a log of the error values after each cycle in
15% ERRLOG. The optional parameters have the following
16% interpretations.
17%
18% OPTIONS(1) is set to 1 to display error values; also logs error
19% values in the return argument ERRLOG. If OPTIONS(1) is set to 0, then
20% only warning messages are displayed. If OPTIONS(1) is -1, then
21% nothing is displayed.
22%
23% OPTIONS(2) is a measure of the absolute precision required for the
24% value of CENTRES at the solution. If the absolute difference between
25% the values of CENTRES between two successive steps is less than
26% OPTIONS(2), then this condition is satisfied.
27%
28% OPTIONS(3) is a measure of the precision required of the error
29% function at the solution. If the absolute difference between the
30% error functions between two successive steps is less than OPTIONS(3),
31% then this condition is satisfied. Both this and the previous
32% condition must be satisfied for termination.
33%
34% OPTIONS(14) is the maximum number of iterations; default 100.
35%
36% See also
37% GMMINIT, GMMEM
38%
39
40% Copyright (c) Christopher M Bishop, Ian T Nabney (1996, 1997)
41
42[ndata, data_dim] = size(data);
43[ncentres, dim] = size(centres);
44
45if dim ~= data_dim
46 error('Data dimension does not match dimension of centres')
47end
48
49if (ncentres > ndata)
50 error('More centres than data')
51end
52
53% Sort out the options
54if (options(14))
55 niters = options(14);
56else
57 niters = 100;
58end
59
60store = 0;
61if (nargout > 3)
62 store = 1;
63 errlog = zeros(1, niters);
64end
65
66% Check if centres and posteriors need to be initialised from data
67if (options(5) == 1)
68 % Do the initialisation
69 perm = randperm(ndata);
70 perm = perm(1:ncentres);
71
72 % Assign first ncentres (permuted) data points as centres
73 centres = data(perm, :);
74end
75% Matrix to make unit vectors easy to construct
76id = eye(ncentres);
77
78% Main loop of algorithm
79for n = 1:niters
80
81 % Save old centres to check for termination
82 old_centres = centres;
83
84 % Calculate posteriors based on existing centres
85 d2 = dist2(data, centres);
86 % Assign each point to nearest centre
87 [minvals, index] = min(d2', [], 1);
88 post = id(index,:);
89
90 num_points = sum(post, 1);
91 % Adjust the centres based on new posteriors
92 for j = 1:ncentres
93 if (num_points(j) > 0)
94 centres(j,:) = sum(data(find(post(:,j)),:), 1)/num_points(j);
95 end
96 end
97
98 % Error value is total squared distance from cluster centres
99 e = sum(minvals);
100 tmp = sort(minvals);
101 e95 = sqrt(tmp(round(length(tmp) * 0.95)));
102 erms = sqrt(e/ndata);
103 if store
104 errlog(n) = e;
105 end
106 if options(1) > 0
107 fprintf(1, ' Cycle %4d RMS Error %11.6f 95-tier Error %11.6f\n', n, erms,e95);
108 end
109
110 if n > 1
111 % Test for termination
112 if max(max(abs(centres - old_centres))) < options(2) & ...
113 abs(old_e - e) < options(3)
114 options(8) = e;
115 return;
116 end
117 end
118 old_e = e;
119end
120
121% If we get here, then we haven't terminated in the given number of
122% iterations.
123options(8) = e;
124%if (options(1) >= 0)
125% disp('Warning: Maximum number of iterations has been exceeded');
126%end
127