[pktools] 273/375: ConfusionMatix: must find exact match in getClassIndex, pkfssvm and pkfsann: confusion matrix global for class names

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:21 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 d8c43962e6d4bd47ac3577c5facbc49d3aea8cd9
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Thu May 29 09:54:31 2014 +0200

    ConfusionMatix: must find exact match in getClassIndex, pkfssvm and pkfsann: confusion matrix global for class names
---
 ChangeLog                        |   2 +
 doc/mainpage.dox                 |  18 ++++---
 src/algorithms/ConfusionMatrix.h |   2 +-
 src/algorithms/Filter2d.cc       |   8 +--
 src/algorithms/Filter2d.h        |   1 +
 src/apps/pkdiff.cc               |  36 ++++++-------
 src/apps/pkfsann.cc              | 106 +++++++++++++++++++++++++--------------
 src/apps/pkfssvm.cc              | 106 +++++++++++++++++++++++----------------
 src/apps/pklas2img.cc            |   3 +-
 src/apps/pksvm.cc                |   2 +-
 10 files changed, 173 insertions(+), 111 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 73e020a..159086e 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -267,6 +267,8 @@ version 2.5.2
  - configure script: GDAL>=1.10.0 is required for pkdiff
  - programs ported to windows and GUI with Qt
 	removed underscore for QProcess in Windows
+ - algorithms
+	ConfusionMatrix.h: must find exact match in getClassIndex
  - pkinfo
 	distinct long options
  - pkclassify_svm -> pksvm
diff --git a/doc/mainpage.dox b/doc/mainpage.dox
index e2125a3..dba08e2 100644
--- a/doc/mainpage.dox
+++ b/doc/mainpage.dox
@@ -41,20 +41,26 @@ Please refer to pktools as: http://pktools.nongnu.org (Pieter Kempeneers)
 contact: kempenep at gmail.com
 
 \section pktools_installation Installation
-Download the latest release from http://download.savannah.gnu.org/releases/pktools/ (e.g., pktools-2.4.3.tar.gz)
+Download the latest release from http://download.savannah.gnu.org/releases/pktools/ (pktools-latest.tar.gz)
 
 Optionally: check the release md5sum to ensure originality.
 
 \code 
-md5sum pktools-2.5.2.tar.gz
+md5sum pktools-latest.tar.gz
 \endcode
 
-The result should be identical to the content of the corresponding md5 file
+The result should be identical to the content of the corresponding md5 file you can also download.
 
-Then unpack the release
+Ubuntu users can download and run the installation script install_pktools.sh:
+
+\code
+sudo bash install_pktools.sh
+\endcode
+
+For a manual installation, download and unpack the latest release
 \code
-tar xzvf pktools-2.5.2.tar.gz
-cd pktools-2.5.2
+tar xzvf pktools-latest.tar.gz
+cd pktools-*
 \endcode
 
 Basic installation (please refer to INSTALL file for more advanced configuration)
diff --git a/src/algorithms/ConfusionMatrix.h b/src/algorithms/ConfusionMatrix.h
index 4a2b22d..db11864 100644
--- a/src/algorithms/ConfusionMatrix.h
+++ b/src/algorithms/ConfusionMatrix.h
@@ -48,7 +48,7 @@ public:
   int getClassIndex(std::string className) const {
     int index=0;
     for(index=0;index<m_classes.size();++index){
-      if(m_classes[index].find(className)!=std::string::npos)
+      if(m_classes[index]==className)
 	break;
     }
     if(index>=m_classes.size())
diff --git a/src/algorithms/Filter2d.cc b/src/algorithms/Filter2d.cc
index e2e95eb..0fc643a 100644
--- a/src/algorithms/Filter2d.cc
+++ b/src/algorithms/Filter2d.cc
@@ -407,7 +407,7 @@ void filter2d::Filter2d::doit(const ImgReaderGdal& input, ImgWriterGdal& output,
           continue;
 	outBuffer[x/down]=0;
 	std::vector<double> windowBuffer;
-	std::map<int,int> occurrence;
+	std::map<long int,int> occurrence;
         int centre=dimX*(dimY-1)/2+(dimX-1)/2;
 	for(int j=-(dimY-1)/2;j<=dimY/2;++j){
 	  for(int i=-(dimX-1)/2;i<=dimX/2;++i){
@@ -576,15 +576,15 @@ void filter2d::Filter2d::doit(const ImgReaderGdal& input, ImgWriterGdal& output,
 	}
         case(filter2d::countid):{
 	  if(windowBuffer.size())
-	    outBuffer[x/down]+=occurrence.size();
+	    outBuffer[x/down]=occurrence.size();
 	  else
 	    outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0;
           break;
 	}
         case(filter2d::majority):{
 	  if(occurrence.size()){
-            std::map<int,int>::const_iterator maxit=occurrence.begin();
-            for(std::map<int,int>::const_iterator mit=occurrence.begin();mit!=occurrence.end();++mit){
+            std::map<long int,int>::const_iterator maxit=occurrence.begin();
+            for(std::map<long int,int>::const_iterator mit=occurrence.begin();mit!=occurrence.end();++mit){
               if(mit->second>maxit->second)
                 maxit=mit;
             }
diff --git a/src/algorithms/Filter2d.h b/src/algorithms/Filter2d.h
index 30ef9da..dec5a32 100644
--- a/src/algorithms/Filter2d.h
+++ b/src/algorithms/Filter2d.h
@@ -165,6 +165,7 @@ private:
     m_filterMap["scramble"]=filter2d::scramble;
     m_filterMap["shift"]=filter2d::shift;
     m_filterMap["linearfeature"]=filter2d::linearfeature;
+    m_filterMap["countid"]=filter2d::countid;
   }
 
   Vector2d<double> m_taps;
diff --git a/src/apps/pkdiff.cc b/src/apps/pkdiff.cc
index 84ff21c..f07d617 100644
--- a/src/apps/pkdiff.cc
+++ b/src/apps/pkdiff.cc
@@ -29,28 +29,28 @@ using namespace std;
 
 int main(int argc, char *argv[])
 {
-  Optionpk<string> input_opt("i", "input", "Input image file.");
-  Optionpk<string> reference_opt("ref", "reference", "Reference image file");
-  Optionpk<string> layer_opt("ln", "ln", "layer name(s) in sample (leave empty to select all)");
-  Optionpk<string> output_opt("o", "output", "Output image file. Default is empty: no output image, only report difference or identical.");
-  Optionpk<string> ogrformat_opt("f", "f", "Output sample file format","SQLite");
+  Optionpk<string> input_opt("i", "input", "Input raster dataset.");
+  Optionpk<string> reference_opt("ref", "reference", "Reference (raster or vector) dataset");
+  Optionpk<string> layer_opt("ln", "ln", "layer name(s) in sample. Leave empty to select all (for vector reference datasets only)");
+  Optionpk<string> output_opt("o", "output", "Output dataset (optional)");
+  Optionpk<string> ogrformat_opt("f", "f", "OGR format for output vector (for vector reference datasets only)","SQLite");
   Optionpk<string> mask_opt("m", "mask", "Mask image file. A single mask is supported only, but several mask values can be used. See also msknodata option. (default is empty)");
   Optionpk<int> masknodata_opt("msknodata", "msknodata", "Mask value(s) where image is invalid. Use negative value for valid data (example: use -t -1: if only -1 is valid value)", 0);
-  Optionpk<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
   Optionpk<short> valueE_opt("\0", "correct", "Value for correct pixels", 0,2);
   Optionpk<short> valueO_opt("\0", "omission", "Value for omission errors: input label > reference label", 1,2);
   Optionpk<short> valueC_opt("\0", "commission", "Value for commission errors: input label < reference label", 2,1);
   Optionpk<short> nodata_opt("nodata", "nodata", "No data value(s) in input or reference dataset are ignored");
-  Optionpk<short> band_opt("b", "band", "Input band", 0);
+  Optionpk<short> band_opt("b", "band", "Input raster band", 0);
   Optionpk<bool> confusion_opt("cm", "confusion", "create confusion matrix (to std out)", false);
-  Optionpk<string> labelref_opt("lr", "lref", "attribute name of the reference label in case reference is OGR vector file", "label");
-  Optionpk<string> labelclass_opt("lc", "lclass", "attribute name of the classified label in case output is OGR vector file", "class");
-  Optionpk<short> boundary_opt("bnd", "boundary", "boundary for selecting the sample", 1,1);
-  Optionpk<bool> disc_opt("circ", "circular", "use circular disc kernel boundary)", false,1);
-  Optionpk<bool> homogeneous_opt("hom", "homogeneous", "only take homogeneous regions into account", false,1);
-  Optionpk<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified.");
+  Optionpk<string> labelref_opt("lr", "lref", "attribute name of the reference label (for vector reference datasets only)", "label");
+  Optionpk<string> labelclass_opt("lc", "lclass", "attribute name of the classified label (for vector reference datasets only)", "class");
+  Optionpk<short> boundary_opt("bnd", "boundary", "boundary for selecting the sample (for vector reference datasets only)", 1,1);
+  Optionpk<bool> homogeneous_opt("hom", "homogeneous", "only take regions with homogeneous boundary into account (for reference datasets only)", false,1);
+  Optionpk<bool> disc_opt("circ", "circular", "use circular boundary (for vector reference datasets only)", false,1);
   Optionpk<string> classname_opt("c", "class", "list of class names."); 
   Optionpk<short> classvalue_opt("r", "reclass", "list of class values (use same order as in classname opt."); 
+  Optionpk<string> colorTable_opt("ct", "ct", "color table in ascii format having 5 columns: id R G B ALFA (0: transparent, 255: solid).");
+  Optionpk<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified.");
   Optionpk<short> verbose_opt("v", "verbose", "verbose", 0);
 
   bool doProcess;//stop process when program was invoked with help option (-h --help)
@@ -58,6 +58,7 @@ int main(int argc, char *argv[])
     doProcess=input_opt.retrieveOption(argc,argv);
     reference_opt.retrieveOption(argc,argv);
     layer_opt.retrieveOption(argc,argv);
+    band_opt.retrieveOption(argc,argv);
     confusion_opt.retrieveOption(argc,argv);
     labelref_opt.retrieveOption(argc,argv);
     classname_opt.retrieveOption(argc,argv);
@@ -68,16 +69,15 @@ int main(int argc, char *argv[])
     output_opt.retrieveOption(argc,argv);
     ogrformat_opt.retrieveOption(argc,argv);
     labelclass_opt.retrieveOption(argc,argv);
-    colorTable_opt.retrieveOption(argc,argv);
     valueE_opt.retrieveOption(argc,argv);
     valueO_opt.retrieveOption(argc,argv);
     valueC_opt.retrieveOption(argc,argv);
-    band_opt.retrieveOption(argc,argv);
-    option_opt.retrieveOption(argc,argv);
-    // class_opt.retrieveOption(argc,argv);
     boundary_opt.retrieveOption(argc,argv);
-    disc_opt.retrieveOption(argc,argv);
     homogeneous_opt.retrieveOption(argc,argv);
+    disc_opt.retrieveOption(argc,argv);
+    colorTable_opt.retrieveOption(argc,argv);
+    option_opt.retrieveOption(argc,argv);
+    // class_opt.retrieveOption(argc,argv);
     verbose_opt.retrieveOption(argc,argv);
   }
   catch(string predefinedString){
diff --git a/src/apps/pkfsann.cc b/src/apps/pkfsann.cc
index 94fbd3c..51d8b98 100644
--- a/src/apps/pkfsann.cc
+++ b/src/apps/pkfsann.cc
@@ -41,6 +41,7 @@ using namespace std;
 
 
 //global parameters used in cost function getCost
+ConfusionMatrix cm;
 map<string,short> classValueMap;
 vector<std::string> nameVector;
 vector<unsigned int> nctraining;
@@ -120,14 +121,14 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
   vector<unsigned short> referenceVector;
   vector<unsigned short> outputVector;
   float rmse=0;
-  ConfusionMatrix cm;
-  //set names in confusion matrix using nameVector
-  for(int iname=0;iname<nameVector.size();++iname){
-    if(classValueMap.empty())
-      cm.pushBackClassName(nameVector[iname]);
-    else if(cm.getClassIndex(type2string<short>(classValueMap[nameVector[iname]]))<0)
-      cm.pushBackClassName(type2string<short>(classValueMap[nameVector[iname]]));
-  }
+  // ConfusionMatrix cm;
+  // //set names in confusion matrix using nameVector
+  // for(int iname=0;iname<nameVector.size();++iname){
+  //   if(classValueMap.empty())
+  //     cm.pushBackClassName(nameVector[iname]);
+  //   else if(cm.getClassIndex(type2string<short>(classValueMap[nameVector[iname]]))<0)
+  //     cm.pushBackClassName(type2string<short>(classValueMap[nameVector[iname]]));
+  // }
   vector<Vector2d<float> > tmpFeatures(nclass);
   for(int iclass=0;iclass<nclass;++iclass){
     tmpFeatures[iclass].resize(trainingFeatures[iclass].size(),nFeatures);
@@ -272,11 +273,6 @@ int main(int argc, char *argv[])
   int nband=0;
   int startBand=2;//first two bands represent X and Y pos
 
-  vector<double> offset;
-  vector<double> scale;
-  vector< Vector2d<float> > trainingPixels;//[class][sample][band]
-  vector< Vector2d<float> > testPixels;//[class][sample][band]
-
   // if(priors_opt.size()>1){//priors from argument list
   //   priors.resize(priors_opt.size());
   //   double normPrior=0;
@@ -300,11 +296,16 @@ int main(int argc, char *argv[])
       classValueMap[classname_opt[iclass]]=classvalue_opt[iclass];
   }
   //----------------------------------- Training -------------------------------
+  vector<double> offset;
+  vector<double> scale;
+  vector< Vector2d<float> > trainingPixels;//[class][sample][band]
+  vector< Vector2d<float> > testPixels;//[class][sample][band]
+  map<string,Vector2d<float> > trainingMap;
+  map<string,Vector2d<float> > testMap;
   vector<string> fields;
+
   //organize training data
   trainingPixels.clear();
-  map<string,Vector2d<float> > trainingMap;
-  map<string,Vector2d<float> > testMap;
   if(verbose_opt[0]>=1)
     std::cout << "reading imageVector file " << training_opt[0] << std::endl;
   try{
@@ -339,6 +340,12 @@ int main(int argc, char *argv[])
     cerr << error << std::endl;
     exit(1);
   }
+  catch(std::exception& e){
+    std::cerr << "Error: ";
+    std::cerr << e.what() << std::endl;
+    std::cerr << CPLGetLastErrorMsg() << std::endl; 
+    exit(1);
+  }
   catch(...){
     cerr << "error catched" << std::endl;
     exit(1);
@@ -354,23 +361,22 @@ int main(int argc, char *argv[])
     std::cout << "training pixels: " << std::endl;
   map<string,Vector2d<float> >::iterator mapit=trainingMap.begin();
   while(mapit!=trainingMap.end()){
-    if(classValueMap.size()){
-      //check if name in training is covered by classname_opt (values can not be 0)
-      if(classValueMap[mapit->first]>0){
-	if(verbose_opt[0])
-	  std::cout << mapit->first << " -> " << classValueMap[mapit->first] << std::endl;
-      }
-      else{
-	std::cerr << "Error: names in classname option are not complete, please check names in training vector and make sure classvalue is > 0" << std::endl;
-	exit(1);
-      }
-    }    
+    // if(classValueMap.size()){
+    //   //check if name in training is covered by classname_opt (values can not be 0)
+    //   if(classValueMap[mapit->first]>0){
+    // 	if(verbose_opt[0])
+    // 	  std::cout << mapit->first << " -> " << classValueMap[mapit->first] << std::endl;
+    //   }
+    //   else{
+    // 	std::cerr << "Error: names in classname option are not complete, please check names in training vector and make sure classvalue is > 0" << std::endl;
+    // 	exit(1);
+    //   }
+    // }    
     //delete small classes
     if((mapit->second).size()<minSize_opt[0]){
       trainingMap.erase(mapit);
       continue;
     }
-    nameVector.push_back(mapit->first);
     trainingPixels.push_back(mapit->second);
     if(verbose_opt[0]>1)
       std::cout << mapit->first << ": " << (mapit->second).size() << " samples" << std::endl;
@@ -383,16 +389,6 @@ int main(int argc, char *argv[])
 
   mapit=testMap.begin();
   while(mapit!=testMap.end()){
-    if(classValueMap.size()){
-      //check if name in test is covered by classname_opt (values can not be 0)
-      if(classValueMap[mapit->first]>0){
-	;//ok, no need to print to std::cout 
-      }
-      else{
-	std::cerr << "Error: names in classname option are not complete, please check names in test vector and make sure classvalue is > 0" << std::endl;
-	exit(1);
-      }
-    }    
     //no need to delete small classes for test sample
     testPixels.push_back(mapit->second);
     if(verbose_opt[0]>1)
@@ -480,6 +476,42 @@ int main(int argc, char *argv[])
     // std::cout << std::endl;
   }
 
+  //new
+  mapit=trainingMap.begin();
+  bool doSort=true;
+  try{
+    while(mapit!=trainingMap.end()){
+      nameVector.push_back(mapit->first);
+      if(classValueMap.size()){
+	//check if name in training is covered by classname_opt (values can not be 0)
+	if(classValueMap[mapit->first]>0){
+	  if(cm.getClassIndex(type2string<short>(classValueMap[mapit->first]))<0){
+	    cm.pushBackClassName(type2string<short>(classValueMap[mapit->first]),doSort);
+	  }
+	}
+	else{
+	  std::cerr << "Error: names in classname option are not complete, please check names in training vector and make sure classvalue is > 0" << std::endl;
+	  exit(1);
+	}
+      }
+      else
+	cm.pushBackClassName(mapit->first,doSort);
+      ++mapit;
+    }
+  }
+  catch(BadConversion conversionString){
+    std::cerr << "Error: did you provide class pairs names (-c) and integer values (-r) for each class in training vector?" << std::endl;
+    exit(1);
+  }
+  if(classname_opt.empty()){
+    //std::cerr << "Warning: no class name and value pair provided for all " << nclass << " classes, using string2type<int> instead!" << std::endl;
+    for(int iclass=0;iclass<nclass;++iclass){
+      if(verbose_opt[0])
+	std::cout << iclass << " " << cm.getClass(iclass) << " -> " << string2type<short>(cm.getClass(iclass)) << std::endl;
+      classValueMap[cm.getClass(iclass)]=string2type<short>(cm.getClass(iclass));
+    }
+  }
+
   //Calculate features of trainig set
   nctraining.resize(nclass);
   nctest.resize(nclass);
diff --git a/src/apps/pkfssvm.cc b/src/apps/pkfssvm.cc
index a314c76..5dfccf6 100644
--- a/src/apps/pkfssvm.cc
+++ b/src/apps/pkfssvm.cc
@@ -44,6 +44,7 @@ using namespace std;
 #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
 
 //global parameters used in cost function getCost
+ConfusionMatrix cm;
 map<string,short> classValueMap;
 vector<std::string> nameVector;
 vector<unsigned int> nctraining;
@@ -51,9 +52,9 @@ vector<unsigned int> nctest;
 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> gamma_opt("g", "gamma", "gamma in kernel function",1.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> ccost_opt("cc", "ccost", "the parameter C of C-SVC, epsilon-SVR, and nu-SVR",1000);
 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);
@@ -148,14 +149,6 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
   if(verbose_opt[0]>2)
     std::cout << "SVM is now trained" << std::endl;
 
-  ConfusionMatrix cm;
-  //set names in confusion matrix using nameVector
-  for(int iname=0;iname<nameVector.size();++iname){
-    if(classValueMap.empty())
-      cm.pushBackClassName(nameVector[iname]);
-    else if(cm.getClassIndex(type2string<short>(classValueMap[nameVector[iname]]))<0)
-      cm.pushBackClassName(type2string<short>(classValueMap[nameVector[iname]]));
-  }
   if(cv_opt[0]>1){
     double *target = Malloc(double,prob.l);
     svm_cross_validation(&prob,&param,cv_opt[0],target);
@@ -172,6 +165,7 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
   }
   else{
     struct svm_node *x_test;
+    vector<double> result(nclass);
     x_test = Malloc(struct svm_node,(nFeatures+1));
     for(int iclass=0;iclass<nclass;++iclass){
       for(int isample=0;isample<nctest[iclass];++isample){
@@ -182,7 +176,8 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
 	x_test[nFeatures].index=-1;
 	double predict_label=0;
 	//todo: make distinction between svm_predict and svm_predict_probability?
-	predict_label = svm_predict(svm,x_test);
+	assert(svm_check_probability_model(svm));
+	predict_label = svm_predict_probability(svm,x_test,&(result[0]));
 	string refClassName=nameVector[iclass];
 	string className=nameVector[static_cast<short>(predict_label)];
 	if(classValueMap.size())
@@ -212,6 +207,7 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
   free(prob.x);
   free(x_space);
   svm_free_and_destroy_model(&(svm));
+
   return(cm.kappa());
 }
 
@@ -307,11 +303,6 @@ int main(int argc, char *argv[])
   int nband=0;
   int startBand=2;//first two bands represent X and Y pos
 
-  vector<double> offset;
-  vector<double> scale;
-  vector< Vector2d<float> > trainingPixels;//[class][sample][band]
-  vector< Vector2d<float> > testPixels;//[class][sample][band]
-
   // if(priors_opt.size()>1){//priors from argument list
   //   priors.resize(priors_opt.size());
   //   double normPrior=0;
@@ -329,6 +320,8 @@ int main(int argc, char *argv[])
     std::sort(band_opt.begin(),band_opt.end());
 
   // map<string,short> classValueMap;//global variable for now (due to getCost)
+  // vector<std::string> nameVector;//global variable for now (due to getCost)
+
   if(classname_opt.size()){
     assert(classname_opt.size()==classvalue_opt.size());
     for(int iclass=0;iclass<classname_opt.size();++iclass)
@@ -336,13 +329,18 @@ int main(int argc, char *argv[])
   }
 
   //----------------------------------- Training -------------------------------
-  struct svm_problem prob;
-  vector<string> fields;
-  //organize training data
-  trainingPixels.clear();
-  testPixels.clear();
+  vector<double> offset;
+  vector<double> scale;
+  vector< Vector2d<float> > trainingPixels;//[class][sample][band]
+  vector< Vector2d<float> > testPixels;//[class][sample][band]
   map<string,Vector2d<float> > trainingMap;
   map<string,Vector2d<float> > testMap;
+  vector<string> fields;
+
+  struct svm_problem prob;
+  //organize training data
+  // trainingPixels.clear();
+  // testPixels.clear();
   if(verbose_opt[0]>=1)
     std::cout << "reading training file " << training_opt[0] << std::endl;
   try{
@@ -377,6 +375,12 @@ int main(int argc, char *argv[])
     cerr << error << std::endl;
     exit(1);
   }
+  catch(std::exception& e){
+    std::cerr << "Error: ";
+    std::cerr << e.what() << std::endl;
+    std::cerr << CPLGetLastErrorMsg() << std::endl; 
+    exit(1);
+  }
   catch(...){
     cerr << "error catched" << std::endl;
     exit(1);
@@ -391,23 +395,11 @@ int main(int argc, char *argv[])
     std::cout << "training pixels: " << std::endl;
   map<string,Vector2d<float> >::iterator mapit=trainingMap.begin();
   while(mapit!=trainingMap.end()){
-    if(classValueMap.size()){
-      //check if name in training is covered by classname_opt (values can not be 0)
-      if(classValueMap[mapit->first]>0){
-	if(verbose_opt[0])
-	  std::cout << mapit->first << " -> " << classValueMap[mapit->first] << std::endl;
-      }
-      else{
-	std::cerr << "Error: names in classname option are not complete, please check names in training vector and make sure classvalue is > 0" << std::endl;
-	exit(1);
-      }
-    }    
     //delete small classes
     if((mapit->second).size()<minSize_opt[0]){
       trainingMap.erase(mapit);
       continue;
     }
-    nameVector.push_back(mapit->first);
     trainingPixels.push_back(mapit->second);
     if(verbose_opt[0]>1)
       std::cout << mapit->first << ": " << (mapit->second).size() << " samples" << std::endl;
@@ -420,16 +412,6 @@ int main(int argc, char *argv[])
 
   mapit=testMap.begin();
   while(mapit!=testMap.end()){
-    if(classValueMap.size()){
-      //check if name in test is covered by classname_opt (values can not be 0)
-      if(classValueMap[mapit->first]>0){
-	;//ok, no need to print to std::cout 
-      }
-      else{
-	std::cerr << "Error: names in classname option are not complete, please check names in test vector and make sure classvalue is > 0" << std::endl;
-	exit(1);
-      }
-    }    
     //no need to delete small classes for test sample
     testPixels.push_back(mapit->second);
     if(verbose_opt[0]>1)
@@ -518,6 +500,44 @@ int main(int argc, char *argv[])
     // std::cout << std::endl;
   }
 
+
+  //new
+  // map<string,Vector2d<float> >::iterator mapit=trainingMap.begin();
+  mapit=trainingMap.begin();
+  bool doSort=true;
+  try{
+    while(mapit!=trainingMap.end()){
+      nameVector.push_back(mapit->first);
+      if(classValueMap.size()){
+	//check if name in training is covered by classname_opt (values can not be 0)
+	if(classValueMap[mapit->first]>0){
+	  if(cm.getClassIndex(type2string<short>(classValueMap[mapit->first]))<0){
+	    cm.pushBackClassName(type2string<short>(classValueMap[mapit->first]),doSort);
+	  }
+	}
+	else{
+	  std::cerr << "Error: names in classname option are not complete, please check names in training vector and make sure classvalue is > 0" << std::endl;
+	  exit(1);
+	}
+      }
+      else
+	cm.pushBackClassName(mapit->first,doSort);
+      ++mapit;
+    }
+  }
+  catch(BadConversion conversionString){
+    std::cerr << "Error: did you provide class pairs names (-c) and integer values (-r) for each class in training vector?" << std::endl;
+    exit(1);
+  }
+  if(classname_opt.empty()){
+    //std::cerr << "Warning: no class name and value pair provided for all " << nclass << " classes, using string2type<int> instead!" << std::endl;
+    for(int iclass=0;iclass<nclass;++iclass){
+      if(verbose_opt[0])
+	std::cout << iclass << " " << cm.getClass(iclass) << " -> " << string2type<short>(cm.getClass(iclass)) << std::endl;
+      classValueMap[cm.getClass(iclass)]=string2type<short>(cm.getClass(iclass));
+    }
+  }
+
   //Calculate features of training (and test) set
   nctraining.resize(nclass);
   nctest.resize(nclass);
diff --git a/src/apps/pklas2img.cc b/src/apps/pklas2img.cc
index dad94b0..1f50a53 100644
--- a/src/apps/pklas2img.cc
+++ b/src/apps/pklas2img.cc
@@ -146,10 +146,11 @@ int main(int argc,char **argv) {
       lasReader.open(input_opt[iinput]);
     }
     catch(string errorString){
-      cout << errorString << endl;
+      cerr << errorString << endl;
       exit(1);
     }
     catch(...){
+      cerr << "Error opening input " << input_opt[iinput] << endl;
       exit(2);
     }
     nPoints=lasReader.getPointCount();
diff --git a/src/apps/pksvm.cc b/src/apps/pksvm.cc
index fa0918a..23a2e0c 100644
--- a/src/apps/pksvm.cc
+++ b/src/apps/pksvm.cc
@@ -86,7 +86,7 @@ int main(int argc, char *argv[])
   Optionpk<string> output_opt("o", "output", "output classification image"); 
   Optionpk<string>  oformat_opt("of", "oformat", "Output image format (see also gdal_translate). Empty string: inherit from input image");
   Optionpk<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified.");
-  Optionpk<string> colorTable_opt("ct", "ct", "colour table in ascii format having 5 columns: id R G B ALFA (0: transparent, 255: solid)"); 
+  Optionpk<string> colorTable_opt("ct", "ct", "color table in ascii format having 5 columns: id R G B ALFA (0: transparent, 255: solid)"); 
   Optionpk<string> prob_opt("prob", "prob", "probability image."); 
   Optionpk<string> entropy_opt("entropy", "entropy", "entropy image (measure for uncertainty of classifier output","",2); 
   Optionpk<string> active_opt("active", "active", "ogr output for active training sample.","",2); 

-- 
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