[go: up one dir, main page]

Menu

[r999]: / libmona / src / cmeans.cc  Maximize  Restore  History

Download this file

455 lines (366 with data), 12.1 kB

  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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* -*- mona-c++ -*-
* Copyright (c) Leipzig, Madrid 2004 - 2008
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdexcept>
#include <cmath>
#include <cassert>
#include <numeric>
#ifdef HAVE_BLAS
extern "C" {
#include <cblas.h>
}
#endif
#include <boost/lambda/lambda.hpp>
#include <libmona/monaIOStream.hh>
#include <libmona/probmapio.hh>
using namespace mona;
using namespace std;
using namespace boost::lambda;
typedef pair<int, int> CClassRange;
typedef map<double, CClassRange> CClassMap;
struct CForwardTransform {
CForwardTransform(float minh, float maxh):
_M_minh(minh),
_M_div(maxh - minh + 1.0)
{
}
float operator() (float x) const
{
return (x - _M_minh) / _M_div;
}
private:
float _M_minh;
float _M_div;
};
struct CBackTransform {
CBackTransform(float minh, float maxh):
_M_minh(minh),
_M_div(maxh - minh + 1.0)
{
}
float operator() (float x) const
{
return x * _M_div + _M_minh;
}
private:
float _M_minh;
float _M_div;
};
typedef vector<double> CDoubleVector;
typedef vector<CDoubleVector> CProbabilityVector;
class CCMeans {
public:
CCMeans(double k, double epsilon, bool start_even);
CProbabilityVector operator()(CDoubleVector const& histogram,
CDoubleVector& class_centers, bool initialise, bool auto_k) const;
private:
void initialise_cc(CDoubleVector& class_centers, CDoubleVector const& histogram) const;
void evaluate_probabilities(CDoubleVector const& classes, size_t nvalues , CProbabilityVector& pv, double k)const;
double update_class_centers(CDoubleVector& center, CDoubleVector const& histo, CDoubleVector const& shisto,
CProbabilityVector const& pv)const;
double adjust_k(CDoubleVector const& class_centers, CDoubleVector const& histogram, CProbabilityVector const & pv)const;
double _M_k;
double _M_epsilon;
bool _M_even_start;
};
CCMeans::CCMeans(double k, double epsilon, bool start_even):
_M_k(k),
_M_epsilon(epsilon),
_M_even_start(start_even)
{
}
void CCMeans::evaluate_probabilities(CDoubleVector const& classes, size_t nvalues , CProbabilityVector& pv, double k)const
{
// cvmsg() << "evaluate_probabilities:" << k << "\n";
for (size_t i = 0; i < nvalues; ++i) {
double const x = double(i)/ nvalues;
double sum = 0.0;
for (size_t j = 0; j < classes.size(); ++j) {
double val = x - classes[j];
val = exp(- (val * val) / k);
pv[j][i] = val;
sum += val;
}
assert(sum != 0.0);
for (size_t j = 0; j < classes.size(); ++j)
pv[j][i] /= sum;
}
}
double CCMeans::update_class_centers(CDoubleVector& class_center, CDoubleVector const& histo,
CDoubleVector const& shisto,CProbabilityVector const& pv)const
{
double residuum = 0.0;
for (size_t i = 0; i < class_center.size(); ++i) {
float cc = class_center[i];
//float loc_res = 100;
//while (loc_res > _M_epsilon) {
// float cc_old = cc;
#ifdef HAVE_BLAS
double sum_prob = cblas_ddot(histo.size(), &histo[0], 1, &pv[i][0], 1);
double sum_weight = cblas_ddot(histo.size(), &shisto[0], 1, &pv[i][0], 1);
#else
CDoubleVector::const_iterator ihelp = histo.begin();
CDoubleVector::const_iterator shelp = shisto.begin();
CDoubleVector::const_iterator iprob = pv[i].begin();
CDoubleVector::const_iterator eprob = pv[i].end();
double sum_prob = 0.0;
double sum_weight = 0.0;
for (size_t ix = 0; ix < histo.size(); ++ix, ++iprob, ++ihelp, ++shelp) {
if (*iprob > 0.0f) {
sum_prob += *iprob * *ihelp;
sum_weight += *iprob * *shelp;
}
}
#endif
if (sum_prob != 0.0) // move slowly in the direction of new center
cc += 0.5 * (sum_weight / sum_prob - cc);
else {
cvwarn() << "class[" << i << "] has no probable members, keeping old value:" <<
sum_prob << ":" <<sum_weight <<endl;
}
// loc_res = cc > cc_old ? cc - cc_old : cc_old - cc;
// cvdebug() << "res = " << loc_res << " : cc = "<< cc <<"\n";
//}
double delta = cc - class_center[i];
residuum += delta * delta;
class_center[i] = cc;
}// end update class centers
return sqrt(residuum);
}
double CCMeans::adjust_k(CDoubleVector const& class_centers, CDoubleVector const& histogram, CProbabilityVector const & pv)const
{
CDoubleVector cc(class_centers.size());
transform(class_centers.begin(), class_centers.end(), cc.begin(), _1 * histogram.size());
// evaluate best mapping of classes based on maximum probability
vector<int> classmap(histogram.size(), 0);
vector<double> classprob(pv[0]);
for (size_t i = 1; i < cc.size(); ++i) {
CDoubleVector::const_iterator iprob = pv[i].begin();
CDoubleVector::const_iterator eprob = pv[i].end();
vector<int>::iterator cmi = classmap.begin();
vector<double>::iterator cpi = classprob.begin();
while (iprob != eprob) {
if (*cpi < *iprob) {
*cpi = *iprob;
*cmi = i;
}
++iprob;
++cpi;
++cmi;
}
}
double sum = 0.0;
double sum2 = 0.0;
double n = 0.0;
for (size_t i = 1; i < histogram.size(); ++i) {
double delta = i - cc[classmap[i]];
sum += delta * histogram[i];
sum2 += delta * delta * histogram[i];
n += histogram[i];
}
const double avg = sum / n;
float hsize = histogram.size() - 1;
double new_k = 2 * ((sum2 - avg * sum) / (n-1)) / (hsize * hsize);
return new_k > 0.001 ? new_k : 0.001;
}
CProbabilityVector CCMeans::operator()(CDoubleVector const& histogram, CDoubleVector& class_centers, bool initialise, bool auto_k) const
{
if (initialise)
initialise_cc(class_centers, histogram);
CProbabilityVector pv(class_centers.size(), histogram);
CDoubleVector scale_histo(histogram);
const double dx = 1.0 / histogram.size();
double x = 0.0;
for (size_t i = 0; i < histogram.size(); ++i, x+=dx)
scale_histo[i] *= x;
for (size_t i = 0; i < class_centers.size(); ++i)
if (class_centers[i] > 1.0)
class_centers[i] /= histogram.size();
const size_t size = histogram.size();
double k = _M_k;
bool cont = true;
while (cont) {
evaluate_probabilities(class_centers, size, pv, k);
cvmsg() << "Class centers: ";
for (CDoubleVector::const_iterator i = class_centers.begin(), e = class_centers.end();
i != e; ++i) {
cverb << *i << ", ";
}
cont = update_class_centers(class_centers, histogram, scale_histo, pv) > _M_epsilon;
if (auto_k) {
k = adjust_k(class_centers, histogram, pv);
cvmsg() << "k = " << k;
}
cvmsg() << '\n';
};
for (CDoubleVector::iterator i = class_centers.begin(), e = class_centers.end(); i != e; ++i)
*i *= size;
return pv;
}
void CCMeans::initialise_cc(CDoubleVector& class_centers, CDoubleVector const& histogram)const
{
double const size = histogram.size();
double const step = size / double(class_centers.size() + 1);
if (_M_even_start) {
for (size_t i = 0; i < class_centers.size(); ++i)
class_centers[i] = i * step;
}else{
class_centers[0] = 0.0;
CDoubleVector::const_iterator hi = histogram.begin();
CDoubleVector::const_iterator const he = histogram.end();
++hi;
double const thresh = accumulate(hi, he, 0.0) / size;
float hit = 0.0;
size_t i = 1;
float val = step;
while (i < class_centers.size() && hi != he) {
hit += *hi;
if (hit > thresh) {
class_centers[i++] = val;
hit -= thresh;
}
++hi;
val += step;
}
}
}
void test(double k, bool auto_k)
{
const size_t Nh = 1024;
const size_t Nc = 3;
cvdebug() << "k = " << k << "\n";
CDoubleVector class_centers(Nc);
float cstep = 1.0 / float(Nc);
cvdebug() << "class centers: ";
for (size_t i =0; i < Nc; ++i) {
class_centers[i] = i * cstep + cstep / 4.0;
cverb << class_centers[i] << ", ";
}
cverb << "\n";
CDoubleVector histogram(Nh);
CProbabilityVector pv(class_centers.size(), histogram);
for (size_t i = 0; i < Nh; ++i) {
float h = 0;
float x = float(i)/Nh;
for (size_t c = 0; c < Nc; ++c) {
float delta = class_centers[c] - x;
delta *= delta;
delta /= k;
float val = exp(-delta);
pv[c][i] = val;
cout << pv[c][i] << " ";
h += val;
histogram[i] += 1024 * val;
}
cout << histogram[i] << " ";
for (size_t c = 0; c < Nc; ++c) {
pv[c][i] /= h;
}
cout << "\n";
}
CDoubleVector eval_class_centers(Nc);
for (size_t i = 0; i < Nc; ++i)
eval_class_centers[i] = i * cstep;
CCMeans cmeans(k, false, 0.00001);
CProbabilityVector eval_pv = cmeans(histogram, eval_class_centers, false, auto_k);
for (size_t i = 0; i < Nc; ++i) {
if (fabs(eval_class_centers[i] - Nh * class_centers[i]) > 0.5) {
cverr() << i << ": " << eval_class_centers[i] << " vs." << Nh * class_centers[i] << "\n";
}
}
}
int main(int argc, const char *args[])
{
int nclasses;
int max_iter;
bool even_start = false;
bool auto_k = false;
bool cut_histo = false;
double epsilon = 0.00001;
double k = 1.0;
bool self_test = false;
CDoubleVector class_centers;
popt::COptions options;
options.push_back(popt::option( nclasses, "nclasses", 'n', "number of classes to partition into","3"));
options.push_back(popt::option( max_iter, "max-iter", 'm', "maximum number of iterations", "100"));
options.push_back(popt::option( even_start, "even-start", 'e', "start with centers evenly distributed over the histogram"));
options.push_back(popt::option( class_centers, "class-centers", 'c', "initial class centers", ""));
options.push_back(popt::option( auto_k, "auto", 'a', "atomatic adaption of variance (experimental)"));
options.push_back(popt::option( cut_histo,"cut-histo", 't', "cut empty histogram at the end "));
options.push_back(popt::option( k, "variance", 'k', "variance parameter", "256"));
options.push_back(popt::option( self_test, "self-test", 0, "run self test"));
try {
vector<string> non_args;
popt::parse_options(argc, args, options, non_args);
if (!non_args.empty()) {
cverr() << "some arguments where not recogniced\n";
cverr() << "try 'eva-kmeans --help' for help\n";
return -1;
}
if (self_test) {
test(k, auto_k);
return 0;
}
vector<double> histo;
// read input data
size_t sig_size_c = 0;
size_t sig_size = 0;
while (cin.good()) {
float val;
cin >> val;
histo.push_back(val);
++sig_size_c;
if (val > 0)
sig_size = sig_size_c;
}
if (sig_size < sig_size_c && cut_histo)
histo.resize(sig_size);
cvmsg() << "got a histogram with " << histo.size() << " values\n";
bool initialise = false;
if (class_centers.empty()) {
class_centers.resize(nclasses);
initialise = true;
}
CCMeans cmeans(k, even_start, epsilon);
CProbabilityVector pv = cmeans(histo, class_centers, initialise, auto_k);
if (!save_probability_map("-", pv)) {
cverr() << "runtime: unable to save probability map\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
catch (const runtime_error &e){
cerr << args[0] << " runtime: " << e.what() << endl;
}
catch (const invalid_argument &e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (const exception& e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (...){
cerr << args[0] << " unknown exception" << endl;
}
return EXIT_FAILURE;
}