You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

svm-toy.cpp 9.7 kB

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

A Python package for graph kernels, graph edit distances and graph pre-image problem.