diff options
author | Leo Chan <leochanj@live.unc.edu> | 2020-10-22 01:53:21 -0400 |
---|---|---|
committer | Joshua Bakita <jbakita@cs.unc.edu> | 2020-10-22 01:56:35 -0400 |
commit | d17b33131c14864bd1eae275f49a3f148e21cf29 (patch) | |
tree | 0d8f77922e8d193cb0f6edab83018f057aad64a0 /SD-VBS/common/toolbox/toolbox_basic/textons/kmeans2.m | |
parent | 601ed25a4c5b66cb75315832c15613a727db2c26 (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/kmeans2.m')
-rwxr-xr-x | SD-VBS/common/toolbox/toolbox_basic/textons/kmeans2.m | 127 |
1 files changed, 127 insertions, 0 deletions
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 @@ | |||
1 | function [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 | |||
45 | if dim ~= data_dim | ||
46 | error('Data dimension does not match dimension of centres') | ||
47 | end | ||
48 | |||
49 | if (ncentres > ndata) | ||
50 | error('More centres than data') | ||
51 | end | ||
52 | |||
53 | % Sort out the options | ||
54 | if (options(14)) | ||
55 | niters = options(14); | ||
56 | else | ||
57 | niters = 100; | ||
58 | end | ||
59 | |||
60 | store = 0; | ||
61 | if (nargout > 3) | ||
62 | store = 1; | ||
63 | errlog = zeros(1, niters); | ||
64 | end | ||
65 | |||
66 | % Check if centres and posteriors need to be initialised from data | ||
67 | if (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, :); | ||
74 | end | ||
75 | % Matrix to make unit vectors easy to construct | ||
76 | id = eye(ncentres); | ||
77 | |||
78 | % Main loop of algorithm | ||
79 | for 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; | ||
119 | end | ||
120 | |||
121 | % If we get here, then we haven't terminated in the given number of | ||
122 | % iterations. | ||
123 | options(8) = e; | ||
124 | %if (options(1) >= 0) | ||
125 | % disp('Warning: Maximum number of iterations has been exceeded'); | ||
126 | %end | ||
127 | |||