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.java 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. import libsvm.*;
  2. import java.applet.*;
  3. import java.awt.*;
  4. import java.util.*;
  5. import java.awt.event.*;
  6. import java.io.*;
  7. public class svm_toy extends Applet {
  8. static final String DEFAULT_PARAM="-t 2 -c 100";
  9. int XLEN;
  10. int YLEN;
  11. // off-screen buffer
  12. Image buffer;
  13. Graphics buffer_gc;
  14. // pre-allocated colors
  15. final static Color colors[] =
  16. {
  17. new Color(0,0,0),
  18. new Color(0,120,120),
  19. new Color(120,120,0),
  20. new Color(120,0,120),
  21. new Color(0,200,200),
  22. new Color(200,200,0),
  23. new Color(200,0,200)
  24. };
  25. class point {
  26. point(double x, double y, byte value)
  27. {
  28. this.x = x;
  29. this.y = y;
  30. this.value = value;
  31. }
  32. double x, y;
  33. byte value;
  34. }
  35. Vector<point> point_list = new Vector<point>();
  36. byte current_value = 1;
  37. public void init()
  38. {
  39. setSize(getSize());
  40. final Button button_change = new Button("Change");
  41. Button button_run = new Button("Run");
  42. Button button_clear = new Button("Clear");
  43. Button button_save = new Button("Save");
  44. Button button_load = new Button("Load");
  45. final TextField input_line = new TextField(DEFAULT_PARAM);
  46. BorderLayout layout = new BorderLayout();
  47. this.setLayout(layout);
  48. Panel p = new Panel();
  49. GridBagLayout gridbag = new GridBagLayout();
  50. p.setLayout(gridbag);
  51. GridBagConstraints c = new GridBagConstraints();
  52. c.fill = GridBagConstraints.HORIZONTAL;
  53. c.weightx = 1;
  54. c.gridwidth = 1;
  55. gridbag.setConstraints(button_change,c);
  56. gridbag.setConstraints(button_run,c);
  57. gridbag.setConstraints(button_clear,c);
  58. gridbag.setConstraints(button_save,c);
  59. gridbag.setConstraints(button_load,c);
  60. c.weightx = 5;
  61. c.gridwidth = 5;
  62. gridbag.setConstraints(input_line,c);
  63. button_change.setBackground(colors[current_value]);
  64. p.add(button_change);
  65. p.add(button_run);
  66. p.add(button_clear);
  67. p.add(button_save);
  68. p.add(button_load);
  69. p.add(input_line);
  70. this.add(p,BorderLayout.SOUTH);
  71. button_change.addActionListener(new ActionListener()
  72. { public void actionPerformed (ActionEvent e)
  73. { button_change_clicked(); button_change.setBackground(colors[current_value]); }});
  74. button_run.addActionListener(new ActionListener()
  75. { public void actionPerformed (ActionEvent e)
  76. { button_run_clicked(input_line.getText()); }});
  77. button_clear.addActionListener(new ActionListener()
  78. { public void actionPerformed (ActionEvent e)
  79. { button_clear_clicked(); }});
  80. button_save.addActionListener(new ActionListener()
  81. { public void actionPerformed (ActionEvent e)
  82. { button_save_clicked(input_line.getText()); }});
  83. button_load.addActionListener(new ActionListener()
  84. { public void actionPerformed (ActionEvent e)
  85. { button_load_clicked(); }});
  86. input_line.addActionListener(new ActionListener()
  87. { public void actionPerformed (ActionEvent e)
  88. { button_run_clicked(input_line.getText()); }});
  89. this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  90. }
  91. void draw_point(point p)
  92. {
  93. Color c = colors[p.value+3];
  94. Graphics window_gc = getGraphics();
  95. buffer_gc.setColor(c);
  96. buffer_gc.fillRect((int)(p.x*XLEN),(int)(p.y*YLEN),4,4);
  97. window_gc.setColor(c);
  98. window_gc.fillRect((int)(p.x*XLEN),(int)(p.y*YLEN),4,4);
  99. }
  100. void clear_all()
  101. {
  102. point_list.removeAllElements();
  103. if(buffer != null)
  104. {
  105. buffer_gc.setColor(colors[0]);
  106. buffer_gc.fillRect(0,0,XLEN,YLEN);
  107. }
  108. repaint();
  109. }
  110. void draw_all_points()
  111. {
  112. int n = point_list.size();
  113. for(int i=0;i<n;i++)
  114. draw_point(point_list.elementAt(i));
  115. }
  116. void button_change_clicked()
  117. {
  118. ++current_value;
  119. if(current_value > 3) current_value = 1;
  120. }
  121. private static double atof(String s)
  122. {
  123. return Double.valueOf(s).doubleValue();
  124. }
  125. private static int atoi(String s)
  126. {
  127. return Integer.parseInt(s);
  128. }
  129. void button_run_clicked(String args)
  130. {
  131. // guard
  132. if(point_list.isEmpty()) return;
  133. svm_parameter param = new svm_parameter();
  134. // default values
  135. param.svm_type = svm_parameter.C_SVC;
  136. param.kernel_type = svm_parameter.RBF;
  137. param.degree = 3;
  138. param.gamma = 0;
  139. param.coef0 = 0;
  140. param.nu = 0.5;
  141. param.cache_size = 40;
  142. param.C = 1;
  143. param.eps = 1e-3;
  144. param.p = 0.1;
  145. param.shrinking = 1;
  146. param.probability = 0;
  147. param.nr_weight = 0;
  148. param.weight_label = new int[0];
  149. param.weight = new double[0];
  150. // parse options
  151. StringTokenizer st = new StringTokenizer(args);
  152. String[] argv = new String[st.countTokens()];
  153. for(int i=0;i<argv.length;i++)
  154. argv[i] = st.nextToken();
  155. for(int i=0;i<argv.length;i++)
  156. {
  157. if(argv[i].charAt(0) != '-') break;
  158. if(++i>=argv.length)
  159. {
  160. System.err.print("unknown option\n");
  161. break;
  162. }
  163. switch(argv[i-1].charAt(1))
  164. {
  165. case 's':
  166. param.svm_type = atoi(argv[i]);
  167. break;
  168. case 't':
  169. param.kernel_type = atoi(argv[i]);
  170. break;
  171. case 'd':
  172. param.degree = atoi(argv[i]);
  173. break;
  174. case 'g':
  175. param.gamma = atof(argv[i]);
  176. break;
  177. case 'r':
  178. param.coef0 = atof(argv[i]);
  179. break;
  180. case 'n':
  181. param.nu = atof(argv[i]);
  182. break;
  183. case 'm':
  184. param.cache_size = atof(argv[i]);
  185. break;
  186. case 'c':
  187. param.C = atof(argv[i]);
  188. break;
  189. case 'e':
  190. param.eps = atof(argv[i]);
  191. break;
  192. case 'p':
  193. param.p = atof(argv[i]);
  194. break;
  195. case 'h':
  196. param.shrinking = atoi(argv[i]);
  197. break;
  198. case 'b':
  199. param.probability = atoi(argv[i]);
  200. break;
  201. case 'w':
  202. ++param.nr_weight;
  203. {
  204. int[] old = param.weight_label;
  205. param.weight_label = new int[param.nr_weight];
  206. System.arraycopy(old,0,param.weight_label,0,param.nr_weight-1);
  207. }
  208. {
  209. double[] old = param.weight;
  210. param.weight = new double[param.nr_weight];
  211. System.arraycopy(old,0,param.weight,0,param.nr_weight-1);
  212. }
  213. param.weight_label[param.nr_weight-1] = atoi(argv[i-1].substring(2));
  214. param.weight[param.nr_weight-1] = atof(argv[i]);
  215. break;
  216. default:
  217. System.err.print("unknown option\n");
  218. }
  219. }
  220. // build problem
  221. svm_problem prob = new svm_problem();
  222. prob.l = point_list.size();
  223. prob.y = new double[prob.l];
  224. if(param.kernel_type == svm_parameter.PRECOMPUTED)
  225. {
  226. }
  227. else if(param.svm_type == svm_parameter.EPSILON_SVR ||
  228. param.svm_type == svm_parameter.NU_SVR)
  229. {
  230. if(param.gamma == 0) param.gamma = 1;
  231. prob.x = new svm_node[prob.l][1];
  232. for(int i=0;i<prob.l;i++)
  233. {
  234. point p = point_list.elementAt(i);
  235. prob.x[i][0] = new svm_node();
  236. prob.x[i][0].index = 1;
  237. prob.x[i][0].value = p.x;
  238. prob.y[i] = p.y;
  239. }
  240. // build model & classify
  241. svm_model model = svm.svm_train(prob, param);
  242. svm_node[] x = new svm_node[1];
  243. x[0] = new svm_node();
  244. x[0].index = 1;
  245. int[] j = new int[XLEN];
  246. Graphics window_gc = getGraphics();
  247. for (int i = 0; i < XLEN; i++)
  248. {
  249. x[0].value = (double) i / XLEN;
  250. j[i] = (int)(YLEN*svm.svm_predict(model, x));
  251. }
  252. buffer_gc.setColor(colors[0]);
  253. buffer_gc.drawLine(0,0,0,YLEN-1);
  254. window_gc.setColor(colors[0]);
  255. window_gc.drawLine(0,0,0,YLEN-1);
  256. int p = (int)(param.p * YLEN);
  257. for(int i=1;i<XLEN;i++)
  258. {
  259. buffer_gc.setColor(colors[0]);
  260. buffer_gc.drawLine(i,0,i,YLEN-1);
  261. window_gc.setColor(colors[0]);
  262. window_gc.drawLine(i,0,i,YLEN-1);
  263. buffer_gc.setColor(colors[5]);
  264. window_gc.setColor(colors[5]);
  265. buffer_gc.drawLine(i-1,j[i-1],i,j[i]);
  266. window_gc.drawLine(i-1,j[i-1],i,j[i]);
  267. if(param.svm_type == svm_parameter.EPSILON_SVR)
  268. {
  269. buffer_gc.setColor(colors[2]);
  270. window_gc.setColor(colors[2]);
  271. buffer_gc.drawLine(i-1,j[i-1]+p,i,j[i]+p);
  272. window_gc.drawLine(i-1,j[i-1]+p,i,j[i]+p);
  273. buffer_gc.setColor(colors[2]);
  274. window_gc.setColor(colors[2]);
  275. buffer_gc.drawLine(i-1,j[i-1]-p,i,j[i]-p);
  276. window_gc.drawLine(i-1,j[i-1]-p,i,j[i]-p);
  277. }
  278. }
  279. }
  280. else
  281. {
  282. if(param.gamma == 0) param.gamma = 0.5;
  283. prob.x = new svm_node [prob.l][2];
  284. for(int i=0;i<prob.l;i++)
  285. {
  286. point p = point_list.elementAt(i);
  287. prob.x[i][0] = new svm_node();
  288. prob.x[i][0].index = 1;
  289. prob.x[i][0].value = p.x;
  290. prob.x[i][1] = new svm_node();
  291. prob.x[i][1].index = 2;
  292. prob.x[i][1].value = p.y;
  293. prob.y[i] = p.value;
  294. }
  295. // build model & classify
  296. svm_model model = svm.svm_train(prob, param);
  297. svm_node[] x = new svm_node[2];
  298. x[0] = new svm_node();
  299. x[1] = new svm_node();
  300. x[0].index = 1;
  301. x[1].index = 2;
  302. Graphics window_gc = getGraphics();
  303. for (int i = 0; i < XLEN; i++)
  304. for (int j = 0; j < YLEN ; j++) {
  305. x[0].value = (double) i / XLEN;
  306. x[1].value = (double) j / YLEN;
  307. double d = svm.svm_predict(model, x);
  308. if (param.svm_type == svm_parameter.ONE_CLASS && d<0) d=2;
  309. buffer_gc.setColor(colors[(int)d]);
  310. window_gc.setColor(colors[(int)d]);
  311. buffer_gc.drawLine(i,j,i,j);
  312. window_gc.drawLine(i,j,i,j);
  313. }
  314. }
  315. draw_all_points();
  316. }
  317. void button_clear_clicked()
  318. {
  319. clear_all();
  320. }
  321. void button_save_clicked(String args)
  322. {
  323. FileDialog dialog = new FileDialog(new Frame(),"Save",FileDialog.SAVE);
  324. dialog.setVisible(true);
  325. String filename = dialog.getDirectory() + dialog.getFile();
  326. if (filename == null) return;
  327. try {
  328. DataOutputStream fp = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
  329. int svm_type = svm_parameter.C_SVC;
  330. int svm_type_idx = args.indexOf("-s ");
  331. if(svm_type_idx != -1)
  332. {
  333. StringTokenizer svm_str_st = new StringTokenizer(args.substring(svm_type_idx+2).trim());
  334. svm_type = atoi(svm_str_st.nextToken());
  335. }
  336. int n = point_list.size();
  337. if(svm_type == svm_parameter.EPSILON_SVR || svm_type == svm_parameter.NU_SVR)
  338. {
  339. for(int i=0;i<n;i++)
  340. {
  341. point p = point_list.elementAt(i);
  342. fp.writeBytes(p.y+" 1:"+p.x+"\n");
  343. }
  344. }
  345. else
  346. {
  347. for(int i=0;i<n;i++)
  348. {
  349. point p = point_list.elementAt(i);
  350. fp.writeBytes(p.value+" 1:"+p.x+" 2:"+p.y+"\n");
  351. }
  352. }
  353. fp.close();
  354. } catch (IOException e) { System.err.print(e); }
  355. }
  356. void button_load_clicked()
  357. {
  358. FileDialog dialog = new FileDialog(new Frame(),"Load",FileDialog.LOAD);
  359. dialog.setVisible(true);
  360. String filename = dialog.getDirectory() + dialog.getFile();
  361. if (filename == null) return;
  362. clear_all();
  363. try {
  364. BufferedReader fp = new BufferedReader(new FileReader(filename));
  365. String line;
  366. while((line = fp.readLine()) != null)
  367. {
  368. StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");
  369. if(st.countTokens() == 5)
  370. {
  371. byte value = (byte)atoi(st.nextToken());
  372. st.nextToken();
  373. double x = atof(st.nextToken());
  374. st.nextToken();
  375. double y = atof(st.nextToken());
  376. point_list.addElement(new point(x,y,value));
  377. }
  378. else if(st.countTokens() == 3)
  379. {
  380. double y = atof(st.nextToken());
  381. st.nextToken();
  382. double x = atof(st.nextToken());
  383. point_list.addElement(new point(x,y,current_value));
  384. }else
  385. break;
  386. }
  387. fp.close();
  388. } catch (IOException e) { System.err.print(e); }
  389. draw_all_points();
  390. }
  391. protected void processMouseEvent(MouseEvent e)
  392. {
  393. if(e.getID() == MouseEvent.MOUSE_PRESSED)
  394. {
  395. if(e.getX() >= XLEN || e.getY() >= YLEN) return;
  396. point p = new point((double)e.getX()/XLEN,
  397. (double)e.getY()/YLEN,
  398. current_value);
  399. point_list.addElement(p);
  400. draw_point(p);
  401. }
  402. }
  403. public void paint(Graphics g)
  404. {
  405. // create buffer first time
  406. if(buffer == null) {
  407. buffer = this.createImage(XLEN,YLEN);
  408. buffer_gc = buffer.getGraphics();
  409. buffer_gc.setColor(colors[0]);
  410. buffer_gc.fillRect(0,0,XLEN,YLEN);
  411. }
  412. g.drawImage(buffer,0,0,this);
  413. }
  414. public Dimension getPreferredSize() { return new Dimension(XLEN,YLEN+50); }
  415. public void setSize(Dimension d) { setSize(d.width,d.height); }
  416. public void setSize(int w,int h) {
  417. super.setSize(w,h);
  418. XLEN = w;
  419. YLEN = h-50;
  420. clear_all();
  421. }
  422. public static void main(String[] argv)
  423. {
  424. new AppletFrame("svm_toy",new svm_toy(),500,500+50);
  425. }
  426. }
  427. class AppletFrame extends Frame {
  428. AppletFrame(String title, Applet applet, int width, int height)
  429. {
  430. super(title);
  431. this.addWindowListener(new WindowAdapter() {
  432. public void windowClosing(WindowEvent e) {
  433. System.exit(0);
  434. }
  435. });
  436. applet.init();
  437. applet.setSize(width,height);
  438. applet.start();
  439. this.add(applet);
  440. this.pack();
  441. this.setVisible(true);
  442. }
  443. }

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