qt/svm-toy.cpp
Go to the documentation of this file.
1 #include <QtGui>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <list>
7 #include "../../svm.h"
8 using namespace std;
9 
10 #define DEFAULT_PARAM "-t 2 -c 100"
11 #define XLEN 500
12 #define YLEN 500
13 
14 QRgb colors[] =
15 {
16  qRgb(0,0,0),
17  qRgb(0,120,120),
18  qRgb(120,120,0),
19  qRgb(120,0,120),
20  qRgb(0,200,200),
21  qRgb(200,200,0),
22  qRgb(200,0,200)
23 };
24 
25 class SvmToyWindow : public QWidget
26 {
27 
28 Q_OBJECT
29 
30 public:
31  SvmToyWindow();
32  ~SvmToyWindow();
33 protected:
34  virtual void mousePressEvent( QMouseEvent* );
35  virtual void paintEvent( QPaintEvent* );
36 
37 private:
38  QPixmap buffer;
39  QPixmap icon1;
40  QPixmap icon2;
41  QPixmap icon3;
42  QPushButton button_change_icon;
43  QPushButton button_run;
44  QPushButton button_clear;
45  QPushButton button_save;
46  QPushButton button_load;
47  QLineEdit input_line;
48  QPainter buffer_painter;
49  struct point {
50  double x, y;
51  signed char value;
52  };
53  list<point> point_list;
55  const QPixmap& choose_icon(int v)
56  {
57  if(v==1) return icon1;
58  else if(v==2) return icon2;
59  else return icon3;
60  }
61  void clear_all()
62  {
63  point_list.clear();
64  buffer.fill(Qt::black);
65  repaint();
66  }
67  void draw_point(const point& p)
68  {
69  const QPixmap& icon = choose_icon(p.value);
70  buffer_painter.drawPixmap((int)(p.x*XLEN),(int)(p.y*YLEN),icon);
71  repaint();
72  }
74  {
75  for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
76  draw_point(*p);
77  }
78 private slots:
80  {
81  ++current_value;
82  if(current_value > 3) current_value = 1;
83  button_change_icon.setIcon(choose_icon(current_value));
84  }
86  {
87  // guard
88  if(point_list.empty()) return;
89 
91  int i,j;
92 
93  // default values
94  param.svm_type = C_SVC;
95  param.kernel_type = RBF;
96  param.degree = 3;
97  param.gamma = 0;
98  param.coef0 = 0;
99  param.nu = 0.5;
100  param.cache_size = 100;
101  param.C = 1;
102  param.eps = 1e-3;
103  param.p = 0.1;
104  param.shrinking = 1;
105  param.probability = 0;
106  param.nr_weight = 0;
107  param.weight_label = NULL;
108  param.weight = NULL;
109 
110  // parse options
111  const char *p = input_line.text().toAscii().constData();
112 
113  while (1) {
114  while (*p && *p != '-')
115  p++;
116 
117  if (*p == '\0')
118  break;
119 
120  p++;
121  switch (*p++) {
122  case 's':
123  param.svm_type = atoi(p);
124  break;
125  case 't':
126  param.kernel_type = atoi(p);
127  break;
128  case 'd':
129  param.degree = atoi(p);
130  break;
131  case 'g':
132  param.gamma = atof(p);
133  break;
134  case 'r':
135  param.coef0 = atof(p);
136  break;
137  case 'n':
138  param.nu = atof(p);
139  break;
140  case 'm':
141  param.cache_size = atof(p);
142  break;
143  case 'c':
144  param.C = atof(p);
145  break;
146  case 'e':
147  param.eps = atof(p);
148  break;
149  case 'p':
150  param.p = atof(p);
151  break;
152  case 'h':
153  param.shrinking = atoi(p);
154  break;
155  case 'b':
156  param.probability = atoi(p);
157  break;
158  case 'w':
159  ++param.nr_weight;
160  param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight);
161  param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight);
162  param.weight_label[param.nr_weight-1] = atoi(p);
163  while(*p && !isspace(*p)) ++p;
164  param.weight[param.nr_weight-1] = atof(p);
165  break;
166  }
167  }
168 
169  // build problem
171 
172  prob.l = point_list.size();
173  prob.y = new double[prob.l];
174 
175  if(param.kernel_type == PRECOMPUTED)
176  {
177  }
178  else if(param.svm_type == EPSILON_SVR ||
179  param.svm_type == NU_SVR)
180  {
181  if(param.gamma == 0) param.gamma = 1;
182  svm_node *x_space = new svm_node[2 * prob.l];
183  prob.x = new svm_node *[prob.l];
184 
185  i = 0;
186  for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
187  {
188  x_space[2 * i].index = 1;
189  x_space[2 * i].value = q->x;
190  x_space[2 * i + 1].index = -1;
191  prob.x[i] = &x_space[2 * i];
192  prob.y[i] = q->y;
193  }
194 
195  // build model & classify
196  svm_model *model = svm_train(&prob, &param);
197  svm_node x[2];
198  x[0].index = 1;
199  x[1].index = -1;
200  int *j = new int[XLEN];
201 
202  for (i = 0; i < XLEN; i++)
203  {
204  x[0].value = (double) i / XLEN;
205  j[i] = (int)(YLEN*svm_predict(model, x));
206  }
207 
208  buffer_painter.setPen(colors[0]);
209  buffer_painter.drawLine(0,0,0,YLEN-1);
210 
211  int p = (int)(param.p * YLEN);
212  for(i = 1; i < XLEN; i++)
213  {
214  buffer_painter.setPen(colors[0]);
215  buffer_painter.drawLine(i,0,i,YLEN-1);
216 
217  buffer_painter.setPen(colors[5]);
218  buffer_painter.drawLine(i-1,j[i-1],i,j[i]);
219 
220  if(param.svm_type == EPSILON_SVR)
221  {
222  buffer_painter.setPen(colors[2]);
223  buffer_painter.drawLine(i-1,j[i-1]+p,i,j[i]+p);
224 
225  buffer_painter.setPen(colors[2]);
226  buffer_painter.drawLine(i-1,j[i-1]-p,i,j[i]-p);
227  }
228  }
229 
231  delete[] j;
232  delete[] x_space;
233  delete[] prob.x;
234  delete[] prob.y;
235  }
236  else
237  {
238  if(param.gamma == 0) param.gamma = 0.5;
239  svm_node *x_space = new svm_node[3 * prob.l];
240  prob.x = new svm_node *[prob.l];
241 
242  i = 0;
243  for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
244  {
245  x_space[3 * i].index = 1;
246  x_space[3 * i].value = q->x;
247  x_space[3 * i + 1].index = 2;
248  x_space[3 * i + 1].value = q->y;
249  x_space[3 * i + 2].index = -1;
250  prob.x[i] = &x_space[3 * i];
251  prob.y[i] = q->value;
252  }
253 
254  // build model & classify
255  svm_model *model = svm_train(&prob, &param);
256  svm_node x[3];
257  x[0].index = 1;
258  x[1].index = 2;
259  x[2].index = -1;
260 
261  for (i = 0; i < XLEN; i++)
262  for (j = 0; j < YLEN ; j++) {
263  x[0].value = (double) i / XLEN;
264  x[1].value = (double) j / YLEN;
265  double d = svm_predict(model, x);
266  if (param.svm_type == ONE_CLASS && d<0) d=2;
267  buffer_painter.setPen(colors[(int)d]);
268  buffer_painter.drawPoint(i,j);
269  }
270 
272  delete[] x_space;
273  delete[] prob.x;
274  delete[] prob.y;
275  }
276  free(param.weight_label);
277  free(param.weight);
278  draw_all_points();
279  }
281  {
282  clear_all();
283  }
285  {
286  QString filename = QFileDialog::getSaveFileName();
287  if(!filename.isNull())
288  {
289  FILE *fp = fopen(filename.toAscii().constData(),"w");
290 
291  const char *p = input_line.text().toAscii().constData();
292  const char* svm_type_str = strstr(p, "-s ");
293  int svm_type = C_SVC;
294  if(svm_type_str != NULL)
295  sscanf(svm_type_str, "-s %d", &svm_type);
296 
297  if(fp)
298  {
299  if(svm_type == EPSILON_SVR || svm_type == NU_SVR)
300  {
301  for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
302  fprintf(fp,"%f 1:%f\n", p->y, p->x);
303  }
304  else
305  {
306  for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
307  fprintf(fp,"%d 1:%f 2:%f\n", p->value, p->x, p->y);
308  }
309  fclose(fp);
310  }
311  }
312  }
314  {
315  QString filename = QFileDialog::getOpenFileName();
316  if(!filename.isNull())
317  {
318  FILE *fp = fopen(filename.toAscii().constData(),"r");
319  if(fp)
320  {
321  clear_all();
322  char buf[4096];
323  while(fgets(buf,sizeof(buf),fp))
324  {
325  int v;
326  double x,y;
327  if(sscanf(buf,"%d%*d:%lf%*d:%lf",&v,&x,&y)==3)
328  {
329  point p = {x,y,v};
330  point_list.push_back(p);
331  }
332  else if(sscanf(buf,"%lf%*d:%lf",&y,&x)==2)
333  {
334  point p = {x,y,current_value};
335  point_list.push_back(p);
336  }
337  else
338  break;
339  }
340  fclose(fp);
341  draw_all_points();
342  }
343  }
344 
345  }
346 };
347 
348 #include "svm-toy.moc"
349 
351 :button_change_icon(this)
352 ,button_run("Run",this)
353 ,button_clear("Clear",this)
354 ,button_save("Save",this)
355 ,button_load("Load",this)
356 ,input_line(this)
357 ,current_value(1)
358 {
359  buffer = QPixmap(XLEN,YLEN);
360  buffer.fill(Qt::black);
361 
362  buffer_painter.begin(&buffer);
363 
364  QObject::connect(&button_change_icon, SIGNAL(clicked()), this,
366  QObject::connect(&button_run, SIGNAL(clicked()), this,
367  SLOT(button_run_clicked()));
368  QObject::connect(&button_clear, SIGNAL(clicked()), this,
369  SLOT(button_clear_clicked()));
370  QObject::connect(&button_save, SIGNAL(clicked()), this,
371  SLOT(button_save_clicked()));
372  QObject::connect(&button_load, SIGNAL(clicked()), this,
373  SLOT(button_load_clicked()));
374  QObject::connect(&input_line, SIGNAL(returnPressed()), this,
375  SLOT(button_run_clicked()));
376 
377  // don't blank the window before repainting
378  setAttribute(Qt::WA_NoBackground);
379 
380  icon1 = QPixmap(4,4);
381  icon2 = QPixmap(4,4);
382  icon3 = QPixmap(4,4);
383 
384 
385  QPainter painter;
386  painter.begin(&icon1);
387  painter.fillRect(0,0,4,4,QBrush(colors[4]));
388  painter.end();
389 
390  painter.begin(&icon2);
391  painter.fillRect(0,0,4,4,QBrush(colors[5]));
392  painter.end();
393 
394  painter.begin(&icon3);
395  painter.fillRect(0,0,4,4,QBrush(colors[6]));
396  painter.end();
397 
398  button_change_icon.setGeometry( 0, YLEN, 50, 25 );
399  button_run.setGeometry( 50, YLEN, 50, 25 );
400  button_clear.setGeometry( 100, YLEN, 50, 25 );
401  button_save.setGeometry( 150, YLEN, 50, 25);
402  button_load.setGeometry( 200, YLEN, 50, 25);
403  input_line.setGeometry( 250, YLEN, 250, 25);
404 
405  input_line.setText(DEFAULT_PARAM);
406  button_change_icon.setIcon(icon1);
407 }
408 
410 {
411  buffer_painter.end();
412 }
413 
414 void SvmToyWindow::mousePressEvent( QMouseEvent* event )
415 {
416  point p = {(double)event->x()/XLEN, (double)event->y()/YLEN, current_value};
417  point_list.push_back(p);
418  draw_point(p);
419 }
420 
421 void SvmToyWindow::paintEvent( QPaintEvent* )
422 {
423  // copy the image from the buffer pixmap to the window
424  QPainter p(this);
425  p.drawPixmap(0, 0, buffer);
426 }
427 
428 int main( int argc, char* argv[] )
429 {
430  QApplication myapp( argc, argv );
431 
432  SvmToyWindow* mywidget = new SvmToyWindow();
433  mywidget->setGeometry( 100, 100, XLEN, YLEN+25 );
434 
435  mywidget->show();
436  return myapp.exec();
437 }
d
filename
QPainter buffer_painter
Definition: qt/svm-toy.cpp:48
struct svm_problem prob
Definition: svmtrain.c:57
const QPixmap & choose_icon(int v)
Definition: qt/svm-toy.cpp:55
virtual void mousePressEvent(QMouseEvent *)
Definition: qt/svm-toy.cpp:414
struct svm_parameter param
Definition: svmtrain.c:56
def svm_train(arg1, arg2=None, arg3=None)
Definition: svmutil.py:77
void button_save_clicked()
Definition: qt/svm-toy.cpp:284
QPushButton button_run
Definition: qt/svm-toy.cpp:43
Definition: svm.h:25
#define YLEN
Definition: qt/svm-toy.cpp:12
double value
Definition: svm.h:15
int nr_weight
Definition: svm.h:40
#define XLEN
Definition: qt/svm-toy.cpp:11
virtual void paintEvent(QPaintEvent *)
Definition: qt/svm-toy.cpp:421
QPushButton button_change_icon
Definition: qt/svm-toy.cpp:42
int * weight_label
Definition: svm.h:41
Definition: svm.h:25
TFSIMD_FORCE_INLINE const tfScalar & y() const
QPushButton button_load
Definition: qt/svm-toy.cpp:46
int main(int argc, char *argv[])
Definition: qt/svm-toy.cpp:428
void button_change_icon_clicked()
Definition: qt/svm-toy.cpp:79
void button_run_clicked()
Definition: qt/svm-toy.cpp:85
Definition: svm.h:52
QPixmap icon1
Definition: qt/svm-toy.cpp:39
double p
Definition: svm.h:44
void draw_all_points()
Definition: qt/svm-toy.cpp:73
def svm_predict(y, x, m, options="")
Definition: svmutil.py:164
struct svm_node * x
Definition: svm-predict.c:8
double cache_size
Definition: svm.h:37
QLineEdit input_line
Definition: qt/svm-toy.cpp:47
QPushButton button_save
Definition: qt/svm-toy.cpp:45
QRgb colors[]
Definition: qt/svm-toy.cpp:14
QPushButton button_clear
Definition: qt/svm-toy.cpp:44
void draw_point(const point &p)
Definition: qt/svm-toy.cpp:67
signed char value
Definition: qt/svm-toy.cpp:51
double eps
Definition: svm.h:38
int shrinking
Definition: svm.h:45
void clear_all()
Definition: callbacks.cpp:82
struct svm_node ** x
Definition: svm.h:22
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
Definition: svm.cpp:2961
QPixmap icon3
Definition: qt/svm-toy.cpp:41
#define DEFAULT_PARAM
Definition: qt/svm-toy.cpp:10
void draw_point(const point &p)
Definition: callbacks.cpp:69
Definition: svm.h:26
int index
Definition: svm.h:14
int current_value
Definition: callbacks.cpp:40
void button_clear_clicked()
Definition: qt/svm-toy.cpp:280
QPixmap icon2
Definition: qt/svm-toy.cpp:40
int probability
Definition: svm.h:46
int degree
Definition: svm.h:32
Definition: svm.h:25
Definition: svm.h:12
void button_load_clicked()
Definition: qt/svm-toy.cpp:313
double * y
Definition: svm.h:21
double gamma
Definition: svm.h:33
int l
Definition: svm.h:20
double * weight
Definition: svm.h:42
void draw_all_points()
Definition: callbacks.cpp:76
double C
Definition: svm.h:39
int svm_type
Definition: svm.h:30
double nu
Definition: svm.h:43
struct svm_model * model
Definition: svmtrain.c:58
double coef0
Definition: svm.h:34
QPixmap buffer
Definition: qt/svm-toy.cpp:38
list< point > point_list
Definition: qt/svm-toy.cpp:53
void clear_all()
Definition: qt/svm-toy.cpp:61
int kernel_type
Definition: svm.h:31
struct svm_node * x_space
Definition: svmtrain.c:59


haf_grasping
Author(s): David Fischinger
autogenerated on Mon Jun 10 2019 13:28:43