easy.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys
4 import os
5 from subprocess import *
6 
7 if len(sys.argv) <= 1:
8  print('Usage: {0} training_file [testing_file]'.format(sys.argv[0]))
9  raise SystemExit
10 
11 # svm, grid, and gnuplot executable files
12 
13 is_win32 = (sys.platform == 'win32')
14 if not is_win32:
15  svmscale_exe = "../svm-scale"
16  svmtrain_exe = "../svm-train"
17  svmpredict_exe = "../svm-predict"
18  grid_py = "./grid.py"
19  gnuplot_exe = "/usr/bin/gnuplot"
20 else:
21  # example for windows
22  svmscale_exe = r"..\windows\svm-scale.exe"
23  svmtrain_exe = r"..\windows\svm-train.exe"
24  svmpredict_exe = r"..\windows\svm-predict.exe"
25  gnuplot_exe = r"c:\tmp\gnuplot\binary\pgnuplot.exe"
26  grid_py = r".\grid.py"
27 
28 assert os.path.exists(svmscale_exe),"svm-scale executable not found"
29 assert os.path.exists(svmtrain_exe),"svm-train executable not found"
30 assert os.path.exists(svmpredict_exe),"svm-predict executable not found"
31 assert os.path.exists(gnuplot_exe),"gnuplot executable not found"
32 assert os.path.exists(grid_py),"grid.py not found"
33 
34 train_pathname = sys.argv[1]
35 assert os.path.exists(train_pathname),"training file not found"
36 file_name = os.path.split(train_pathname)[1]
37 scaled_file = file_name + ".scale"
38 model_file = file_name + ".model"
39 range_file = file_name + ".range"
40 
41 if len(sys.argv) > 2:
42  test_pathname = sys.argv[2]
43  file_name = os.path.split(test_pathname)[1]
44  assert os.path.exists(test_pathname),"testing file not found"
45  scaled_test_file = file_name + ".scale"
46  predict_test_file = file_name + ".predict"
47 
48 cmd = '{0} -s "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, train_pathname, scaled_file)
49 print('Scaling training data...')
50 Popen(cmd, shell = True, stdout = PIPE).communicate()
51 
52 cmd = '{0} -svmtrain "{1}" -gnuplot "{2}" "{3}"'.format(grid_py, svmtrain_exe, gnuplot_exe, scaled_file)
53 print('Cross validation...')
54 f = Popen(cmd, shell = True, stdout = PIPE).stdout
55 
56 line = ''
57 while True:
58  last_line = line
59  line = f.readline()
60  if not line: break
61 c,g,rate = map(float,last_line.split())
62 
63 print('Best c={0}, g={1} CV rate={2}'.format(c,g,rate))
64 
65 cmd = '{0} -c {1} -g {2} "{3}" "{4}"'.format(svmtrain_exe,c,g,scaled_file,model_file)
66 print('Training...')
67 Popen(cmd, shell = True, stdout = PIPE).communicate()
68 
69 print('Output model: {0}'.format(model_file))
70 if len(sys.argv) > 2:
71  cmd = '{0} -r "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, test_pathname, scaled_test_file)
72  print('Scaling testing data...')
73  Popen(cmd, shell = True, stdout = PIPE).communicate()
74 
75  cmd = '{0} "{1}" "{2}" "{3}"'.format(svmpredict_exe, scaled_test_file, model_file, predict_test_file)
76  print('Testing...')
77  Popen(cmd, shell = True).communicate()
78 
79  print('Output prediction: {0}'.format(predict_test_file))


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