[pktools] 115/375: adapted string options in svm related tools

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:05 UTC 2014


This is an automated email from the git hooks/post-receive script.

sebastic-guest pushed a commit to branch upstream-master
in repository pktools.

commit d53a463d6e533369e43b537e4005fb70dfa808b6
Author: user <user at osgeolive.(none)>
Date:   Fri May 31 01:03:34 2013 +0200

    adapted string options in svm related tools
---
 doc/createappsdox.sh       |  2 ++
 src/apps/pkclassify_svm.cc | 40 ++++++++++++++++++++++++++++++++--------
 src/apps/pkfilter.cc       | 12 +++++++-----
 src/apps/pkfs_nn.cc        |  2 +-
 src/apps/pkfs_svm.cc       | 33 +++++++++++++++++++++++++++------
 src/apps/pkmosaic.cc       |  6 +++---
 src/apps/pkopt_svm.cc      | 33 +++++++++++++++++++++++++++++----
 src/apps/pksetmask.cc      |  4 ++--
 8 files changed, 103 insertions(+), 29 deletions(-)

diff --git a/doc/createappsdox.sh b/doc/createappsdox.sh
index 57b1942..bd0a2f7 100755
--- a/doc/createappsdox.sh
+++ b/doc/createappsdox.sh
@@ -61,3 +61,5 @@ echo "Savannah repository for homepage can only be maintained via cvs"
 #cvs add *
 #cvs commit -m "update of repository homepage"
 #rm -r ~/tmp/pktools
+echo "ftp to downloads"
+#sftp kempenep at download.savannah.gnu.org:/releases/pktools
diff --git a/src/apps/pkclassify_svm.cc b/src/apps/pkclassify_svm.cc
index 88fe43e..6be5b8e 100644
--- a/src/apps/pkclassify_svm.cc
+++ b/src/apps/pkclassify_svm.cc
@@ -30,6 +30,15 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include "algorithms/svm.h"
 #include "pkclassify_nn.h"
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+namespace svm{
+  enum SVM_TYPE {C_SVC=0, nu_SVC=1,one_class=2, epsilon_SVR=3, nu_SVR=4};
+  enum KERNEL_TYPE {linear=0,polynomial=1,radial=2,sigmoid=3};
+}
+
 #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
 
 int main(int argc, char *argv[])
@@ -50,19 +59,19 @@ int main(int argc, char *argv[])
   Optionpk<double> priors_opt("p", "prior", "prior probabilities for each class (e.g., -p 0.3 -p 0.3 -p 0.2 )", 0.0); 
   Optionpk<string> priorimg_opt("pim", "priorimg", "prior probability image (multi-band img with band for each class"); 
   Optionpk<unsigned short> cv_opt("cv", "cv", "n-fold cross validation mode",0);
-  Optionpk<unsigned short> svm_type_opt("svmt", "svmtype", "type of SVM (0: C-SVC, 1: nu-SVC, 2: one-class SVM, 3: epsilon-SVR,	4: nu-SVR)",0);
-  Optionpk<unsigned short> kernel_type_opt("kt", "kerneltype", "type of kernel function (0: linear: u'*v, 1: polynomial: (gamma*u'*v + coef0)^degree, 2: radial basis function: exp(-gamma*(u-v)^2), 3: sigmoid: tanh(gamma*u'*v + coef0), 4: precomputed kernel (kernel values in training_set_file)",2);
+  Optionpk<std::string> svm_type_opt("svmt", "svmtype", "type of SVM (C_SVC, nu_SVC,one_class, epsilon_SVR, nu_SVR)","C_SVC");
+  Optionpk<std::string> kernel_type_opt("kt", "kerneltype", "type of kernel function (linear,polynomial,radial,sigmoid) ","radial");
   Optionpk<unsigned short> kernel_degree_opt("kd", "kd", "degree in kernel function",3);
   Optionpk<float> gamma_opt("g", "gamma", "gamma in kernel function",0);
   Optionpk<float> coef0_opt("c0", "coef0", "coef0 in kernel function",0);
-  Optionpk<float> ccost_opt("cc", "ccost", "the parameter C of C-SVC, epsilon-SVR, and nu-SVR",1);
-  Optionpk<float> nu_opt("nu", "nu", "the parameter nu of nu-SVC, one-class SVM, and nu-SVR",0.5);
-  Optionpk<float> epsilon_loss_opt("eloss", "eloss", "the epsilon in loss function of epsilon-SVR",0.1);
+  Optionpk<float> ccost_opt("cc", "ccost", "the parameter C of C_SVC, epsilon_SVR, and nu_SVR",1);
+  Optionpk<float> nu_opt("nu", "nu", "the parameter nu of nu_SVC, one_class SVM, and nu_SVR",0.5);
+  Optionpk<float> epsilon_loss_opt("eloss", "eloss", "the epsilon in loss function of epsilon_SVR",0.1);
   Optionpk<int> cache_opt("cache", "cache", "cache memory size in MB",100);
   Optionpk<float> epsilon_tol_opt("etol", "etol", "the tolerance of termination criterion",0.001);
   Optionpk<bool> shrinking_opt("shrink", "shrink", "whether to use the shrinking heuristics",false);
   Optionpk<bool> prob_est_opt("pe", "probest", "whether to train a SVC or SVR model for probability estimates",false);
-  // Optionpk<bool> weight_opt("wi", "wi", "set the parameter C of class i to weight*C, for C-SVC",true);
+  // Optionpk<bool> weight_opt("wi", "wi", "set the parameter C of class i to weight*C, for C_SVC",true);
   Optionpk<unsigned short> comb_opt("comb", "comb", "how to combine bootstrap aggregation classifiers (0: sum rule, 1: product rule, 2: max rule). Also used to aggregate classes with rc option.",0); 
   Optionpk<unsigned short> bag_opt("bag", "bag", "Number of bootstrap aggregations", 1);
   Optionpk<int> bagSize_opt("bs", "bsize", "Percentage of features used from available training features for each bootstrap aggregation (one size for all classes, or a different size for each class respectively", 100);
@@ -137,6 +146,21 @@ int main(int argc, char *argv[])
     exit(0);//help was invoked, stop processing
   }
 
+  std::map<std::string, svm::SVM_TYPE> svmMap;
+
+  svmMap["C_SVC"]=svm::C_SVC;
+  svmMap["nu_SVC"]=svm::nu_SVC;
+  svmMap["one_class"]=svm::one_class;
+  svmMap["epsilon_SVR"]=svm::epsilon_SVR;
+  svmMap["nu_SVR"]=svm::nu_SVR;
+
+  std::map<std::string, svm::KERNEL_TYPE> kernelMap;
+
+  kernelMap["linear"]=svm::linear;
+  kernelMap["polynomial"]=svm::polynomial;
+  kernelMap["radial"]=svm::radial;
+  kernelMap["sigmoid;"]=svm::sigmoid;
+
   assert(training_opt.size());
 
   if(verbose_opt[0]>=1){
@@ -448,8 +472,8 @@ int main(int argc, char *argv[])
     assert(lIndex==prob[ibag].l);
 
     //set SVM parameters through command line options
-    param[ibag].svm_type = svm_type_opt[0];
-    param[ibag].kernel_type = kernel_type_opt[0];
+    param[ibag].svm_type = svmMap[svm_type_opt[0]];
+    param[ibag].kernel_type = kernelMap[kernel_type_opt[0]];
     param[ibag].degree = kernel_degree_opt[0];
     param[ibag].gamma = (gamma_opt[0]>0)? gamma_opt[0] : 1.0/nFeatures;
     param[ibag].coef0 = coef0_opt[0];
diff --git a/src/apps/pkfilter.cc b/src/apps/pkfilter.cc
index 246d28c..1a58215 100644
--- a/src/apps/pkfilter.cc
+++ b/src/apps/pkfilter.cc
@@ -57,7 +57,7 @@ int main(int argc,char **argv) {
   Optionpk<std::string> interpolationType_opt("interp", "interp", "type of interpolation for spectral filtering (see http://www.gnu.org/software/gsl/manual/html_node/Interpolation-Types.html)","akima");
   Optionpk<std::string>  otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image","");
   Optionpk<string>  oformat_opt("of", "oformat", "Output image format (see also gdal_translate). Empty string: inherit from input image");
-  Optionpk<string>  colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
+  Optionpk<string>  colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid). Use none to ommit color table");
   Optionpk<std::string> option_opt("co", "co", "options: NAME=VALUE [-co COMPRESS=LZW] [-co INTERLEAVE=BAND]");
   Optionpk<short> down_opt("d", "down", "down sampling factor. Use value 1 for no downsampling)", 1);
   Optionpk<string> beta_opt("beta", "beta", "ASCII file with beta for each class transition in Markov Random Field");
@@ -158,10 +158,12 @@ int main(int argc,char **argv) {
     output.setGeoTransform(ulx,uly,deltaX*down_opt[0],deltaY*down_opt[0],rot1,rot2);
   }
   if(colorTable_opt.size()){
-    if(verbose_opt[0])
-      cout << "set colortable " << colorTable_opt[0] << endl;
-    assert(output.getDataType()==GDT_Byte);
-    output.setColorTable(colorTable_opt[0]);
+    if(colorTable_opt[0]!="none"){
+      if(verbose_opt[0])
+	cout << "set colortable " << colorTable_opt[0] << endl;
+      assert(output.getDataType()==GDT_Byte);
+      output.setColorTable(colorTable_opt[0]);
+    }
   }
   else if(input.getColorTable()!=NULL)
     output.setColorTable(input.getColorTable());
diff --git a/src/apps/pkfs_nn.cc b/src/apps/pkfs_nn.cc
index b73a6f2..e87a24c 100644
--- a/src/apps/pkfs_nn.cc
+++ b/src/apps/pkfs_nn.cc
@@ -183,7 +183,7 @@ int main(int argc, char *argv[])
   // vector<double> priors;
   
   //--------------------------- command line options ------------------------------------
-  Optionpk<string> input_opt("i", "input", "input image"); 
+  Optionpk<string> input_opt("i", "input", "input test set (leave empty to perform a cross validation based on training only)"); 
   Optionpk<string> training_opt("t", "training", "training shape file. A single shape file contains all training features (must be set as: B0, B1, B2,...) for all classes (class numbers identified by label option). Use multiple training files for bootstrap aggregation (alternative to the bag and bsize options, where a random subset is taken from a single training file)"); 
   Optionpk<string> label_opt("\0", "label", "identifier for class label in training shape file.","label"); 
   Optionpk<unsigned short> maxFeatures_opt("n", "nf", "number of features to select (0 to select optimal number, see also ecost option)", 0);
diff --git a/src/apps/pkfs_svm.cc b/src/apps/pkfs_svm.cc
index f88cb8e..ca094d6 100644
--- a/src/apps/pkfs_svm.cc
+++ b/src/apps/pkfs_svm.cc
@@ -31,17 +31,23 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include <config.h>
 #endif
 
-#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
+namespace svm{
+  enum SVM_TYPE {C_SVC=0, nu_SVC=1,one_class=2, epsilon_SVR=3, nu_SVR=4};
+  enum KERNEL_TYPE {linear=0,polynomial=1,radial=2,sigmoid=3};
+}
 
 enum SelectorValue  { NA=0, SFFS=1, SFS=2, SBS=3, BFS=4 };
 
+
+#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
+
 //global parameters used in cost function getCost
 map<string,short> classValueMap;
 vector<std::string> nameVector;
 vector<unsigned int> nctraining;
 vector<unsigned int> nctest;
-Optionpk<unsigned short> svm_type_opt("svmt", "svmtype", "type of SVM (0: C-SVC, 1: nu-SVC, 2: one-class SVM, 3: epsilon-SVR,	4: nu-SVR)",0);
-Optionpk<unsigned short> kernel_type_opt("kt", "kerneltype", "type of kernel function (0: linear: u'*v, 1: polynomial: (gamma*u'*v + coef0)^degree, 2: radial basis function: exp(-gamma*(u-v)^2), 3: sigmoid: tanh(gamma*u'*v + coef0), 4: precomputed kernel (kernel values in training_set_file)",2);
+Optionpk<std::string> svm_type_opt("svmt", "svmtype", "type of SVM (C_SVC, nu_SVC,one_class, epsilon_SVR, nu_SVR)","C_SVC");
+Optionpk<std::string> kernel_type_opt("kt", "kerneltype", "type of kernel function (linear,polynomial,radial,sigmoid) ","radial");
 Optionpk<unsigned short> kernel_degree_opt("kd", "kd", "degree in kernel function",3);
 Optionpk<float> gamma_opt("g", "gamma", "gamma in kernel function",0);
 Optionpk<float> coef0_opt("c0", "coef0", "coef0 in kernel function",0);
@@ -60,6 +66,21 @@ Optionpk<short> verbose_opt("v", "verbose", "set to: 0 (results only), 1 (confus
 
 double getCost(const vector<Vector2d<float> > &trainingFeatures)
 {
+  std::map<std::string, svm::SVM_TYPE> svmMap;
+
+  svmMap["C_SVC"]=svm::C_SVC;
+  svmMap["nu_SVC"]=svm::nu_SVC;
+  svmMap["one_class"]=svm::one_class;
+  svmMap["epsilon_SVR"]=svm::epsilon_SVR;
+  svmMap["nu_SVR"]=svm::nu_SVR;
+
+  std::map<std::string, svm::KERNEL_TYPE> kernelMap;
+
+  kernelMap["linear"]=svm::linear;
+  kernelMap["polynomial"]=svm::polynomial;
+  kernelMap["radial"]=svm::radial;
+  kernelMap["sigmoid;"]=svm::sigmoid;
+
   unsigned short nclass=trainingFeatures.size();
   unsigned int ntraining=0;
   unsigned int ntest=0;
@@ -74,8 +95,8 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
   unsigned short nFeatures=trainingFeatures[0][0].size();
 
   struct svm_parameter param;
-  param.svm_type = svm_type_opt[0];
-  param.kernel_type = kernel_type_opt[0];
+  param.svm_type = svmMap[svm_type_opt[0]];
+  param.kernel_type = kernelMap[kernel_type_opt[0]];
   param.degree = kernel_degree_opt[0];
   param.gamma = (gamma_opt[0]>0)? gamma_opt[0] : 1.0/nFeatures;
   param.coef0 = coef0_opt[0];
@@ -197,7 +218,7 @@ int main(int argc, char *argv[])
   // vector<double> priors;
   
   //--------------------------- command line options ------------------------------------
-  Optionpk<string> input_opt("i", "input", "input image"); 
+  Optionpk<string> input_opt("i", "input", "input test set (leave empty to perform a cross validation based on training only)"); 
   Optionpk<string> training_opt("t", "training", "training shape file. A single shape file contains all training features (must be set as: B0, B1, B2,...) for all classes (class numbers identified by label option)."); 
   Optionpk<string> label_opt("\0", "label", "identifier for class label in training shape file.","label"); 
   Optionpk<unsigned short> maxFeatures_opt("n", "nf", "number of features to select (0 to select optimal number, see also ecost option)", 0);
diff --git a/src/apps/pkmosaic.cc b/src/apps/pkmosaic.cc
index 0b7f100..b25c606 100644
--- a/src/apps/pkmosaic.cc
+++ b/src/apps/pkmosaic.cc
@@ -106,8 +106,8 @@ int main(int argc, char *argv[])
   }
 
   std::map<std::string, mrule::MRULE_TYPE> mruleMap;
-  //initialize mruleMap
-  enum MRULE_TYPE {overwrite=0, maxndvi=1, maxband=2, minband=3, validband=4, mean=5, maxvote=6, median=7,sum=8};
+  // //initialize mruleMap
+  // enum MRULE_TYPE {overwrite=0, maxndvi=1, maxband=2, minband=3, validband=4, mean=5, maxvote=6, median=7,sum=8};
 
   mruleMap["overwrite"]=mrule::overwrite;
   mruleMap["maxndvi"]=mrule::maxndvi;
@@ -402,7 +402,7 @@ int main(int argc, char *argv[])
   Vector2d<short> maxBuffer;//buffer used for maximum voting
   Vector2d<double> readBuffer(nband);
   statfactory::StatFactory stat;
-  if(mruleMap[mrule_opt[0]]==maxndvi)//ndvi
+  if(mruleMap[mrule_opt[0]]==mrule::maxndvi)//ndvi
     assert(ruleBand_opt.size()==2);
   if(mruleMap[mrule_opt[0]]==mrule::maxvote){//max voting
     maxBuffer.resize(imgWriter.nrOfCol(),256);//use only byte images for max voting
diff --git a/src/apps/pkopt_svm.cc b/src/apps/pkopt_svm.cc
index 5d9b6da..0c11f28 100644
--- a/src/apps/pkopt_svm.cc
+++ b/src/apps/pkopt_svm.cc
@@ -31,6 +31,15 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include "algorithms/svm.h"
 #include "pkclassify_nn.h"
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+namespace svm{
+  enum SVM_TYPE {C_SVC=0, nu_SVC=1,one_class=2, epsilon_SVR=3, nu_SVR=4};
+  enum KERNEL_TYPE {linear=0,polynomial=1,radial=2,sigmoid=3};
+}
+
 #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
                                     //declare objective function
 double objFunction(const std::vector<double> &x, std::vector<double> &grad, void *my_func_data);
@@ -40,8 +49,8 @@ map<string,short> classValueMap;
 vector<std::string> nameVector;
 vector<unsigned int> nctraining;
 vector<unsigned int> nctest;
-Optionpk<unsigned short> svm_type_opt("svmt", "svmtype", "type of SVM (0: C-SVC, 1: nu-SVC, 2: one-class SVM, 3: epsilon-SVR,	4: nu-SVR)",0);
-Optionpk<unsigned short> kernel_type_opt("kt", "kerneltype", "type of kernel function (0: linear: u'*v, 1: polynomial: (gamma*u'*v + coef0)^degree, 2: radial basis function: exp(-gamma*(u-v)^2), 3: sigmoid: tanh(gamma*u'*v + coef0), 4: precomputed kernel (kernel values in training_set_file)",2);
+Optionpk<std::string> svm_type_opt("svmt", "svmtype", "type of SVM (C_SVC, nu_SVC,one_class, epsilon_SVR, nu_SVR)","C_SVC");
+Optionpk<std::string> kernel_type_opt("kt", "kerneltype", "type of kernel function (linear,polynomial,radial,sigmoid) ","radial");
 Optionpk<unsigned short> kernel_degree_opt("kd", "kd", "degree in kernel function",3);
 Optionpk<float> coef0_opt("c0", "coef0", "coef0 in kernel function",0);
 Optionpk<float> nu_opt("nu", "nu", "the parameter nu of nu-SVC, one-class SVM, and nu-SVR",0.5);
@@ -58,6 +67,22 @@ Optionpk<short> classvalue_opt("r", "reclass", "list of class values (use same o
 Optionpk<short> verbose_opt("v", "verbose", "set to: 0 (results only), 1 (confusion matrix), 2 (debug)",0);
 
 double objFunction(const std::vector<double> &x, std::vector<double> &grad, void *my_func_data){
+
+  std::map<std::string, svm::SVM_TYPE> svmMap;
+
+  svmMap["C_SVC"]=svm::C_SVC;
+  svmMap["nu_SVC"]=svm::nu_SVC;
+  svmMap["one_class"]=svm::one_class;
+  svmMap["epsilon_SVR"]=svm::epsilon_SVR;
+  svmMap["nu_SVR"]=svm::nu_SVR;
+
+  std::map<std::string, svm::KERNEL_TYPE> kernelMap;
+
+  kernelMap["linear"]=svm::linear;
+  kernelMap["polynomial"]=svm::polynomial;
+  kernelMap["radial"]=svm::radial;
+  kernelMap["sigmoid;"]=svm::sigmoid;
+
   assert(grad.empty());
   vector<Vector2d<float> > *tf=reinterpret_cast<vector<Vector2d<float> >*> (my_func_data);
   float ccost=x[0];
@@ -80,8 +105,8 @@ double objFunction(const std::vector<double> &x, std::vector<double> &grad, void
     // ntraining+=(*tf)[iclass].size();
   unsigned short nFeatures=(*tf)[0][0].size();
   struct svm_parameter param;
-  param.svm_type = svm_type_opt[0];
-  param.kernel_type = kernel_type_opt[0];
+  param.svm_type = svmMap[svm_type_opt[0]];
+  param.kernel_type = kernelMap[kernel_type_opt[0]];
   param.degree = kernel_degree_opt[0];
   param.gamma = gamma;
   param.coef0 = coef0_opt[0];
diff --git a/src/apps/pksetmask.cc b/src/apps/pksetmask.cc
index f4cab7f..aa6e520 100644
--- a/src/apps/pksetmask.cc
+++ b/src/apps/pksetmask.cc
@@ -37,7 +37,7 @@ int main(int argc, char *argv[])
   Optionpk<int> invalid_opt("t", "invalid", "Mask value(s) where image is invalid. Use one value for each mask, or multiple values for a single mask.", 1);
   Optionpk<char> operator_opt("p", "operator", "Operator: < = > !. Use operator for each invalid option", '=');
   Optionpk<int> flag_opt("f", "flag", "Flag value to put in image if not valid", 0);
-  Optionpk<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)", "");
+  Optionpk<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
   Optionpk<short> verbose_opt("v", "verbose", "verbose", 0);
 
   bool doProcess;//stop process when program was invoked with help option (-h --help)
@@ -117,7 +117,7 @@ int main(int argc, char *argv[])
   // if(verbose_opt[0])
   //   cout << "opening output image file " << output_opt[0] << endl;
   // outputWriter.open(output_opt[0],inputReader);
-  if(colorTable_opt[0]!=""){
+  if(colorTable_opt.size()){
     if(colorTable_opt[0]!="none")
       outputWriter.setColorTable(colorTable_opt[0]);
   }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/pktools.git



More information about the Pkg-grass-devel mailing list