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 60cd6657f6a781888a570e90edce88feb79d4522
Author: Pieter Kempeneers <kempe...@gmail.com>
Date:   Fri Jun 6 13:12:31 2014 +0200

    working on feature selection. todo: create class and pass instead of 
function
---
 src/algorithms/FeatureSelector.h | 10 +++--
 src/apps/pkextract.cc            |  2 +-
 src/apps/pkfssvm.cc              | 96 +++++++++++++++++++++++-----------------
 3 files changed, 63 insertions(+), 45 deletions(-)

diff --git a/src/algorithms/FeatureSelector.h b/src/algorithms/FeatureSelector.h
index b57771e..91bf2df 100644
--- a/src/algorithms/FeatureSelector.h
+++ b/src/algorithms/FeatureSelector.h
@@ -26,6 +26,7 @@ along with pktools.  If not, see 
<http://www.gnu.org/licenses/>.
 #include <algorithm>
 #include <iostream>
 #include <iomanip>
+#include "ConfusionMatrix.h"
 #include "base/IndexValue.h"
 #include "base/Vector2d.h"
 #include "gsl/gsl_combination.h"
@@ -157,11 +158,12 @@ template<class T> double 
FeatureSelector::floating(std::vector< Vector2d<T> >& v
   int k=subset.size();
   if(k>=maxFeatures)
     return -1;
-  cost.push_back(-1);//init 0 features as cost -1
+  while(cost.size()<subset.size())
+    cost.push_back(1);//init original features as cost 1
   cost.push_back(addFeature(v,*getCost,subset,verbose));
   ++k;
   if(verbose>1)
-    std::cout << "added " << subset.back() << ", " << cost.size()-1 << "/" << 
maxFeatures << " features selected with cost: " << cost.back() << std::endl;
+    std::cout << "added " << subset.back() << ", " << subset.size() << "/" << 
maxFeatures << " features selected with cost: " << cost.back() << std::endl;
   else if(verbose){
     for(std::list<int>::const_iterator 
lit=subset.begin();lit!=subset.end();++lit)
       std::cout << *lit << " ";
@@ -171,7 +173,7 @@ template<class T> double 
FeatureSelector::floating(std::vector< Vector2d<T> >& v
     cost.push_back(addFeature(v,*getCost,subset,verbose));
     ++k;
     if(verbose>1)
-    std::cout << "added " << subset.back() << ", " << cost.size()-1 << "/" << 
maxFeatures << " features selected with cost: " << cost.back() << std::endl;
+    std::cout << "added " << subset.back() << ", " << subset.size() << "/" << 
maxFeatures << " features selected with cost: " << cost.back() << std::endl;
     else if(verbose){
       for(std::list<int>::const_iterator 
lit=subset.begin();lit!=subset.end();++lit)
         std::cout << *lit << " ";
@@ -186,7 +188,7 @@ template<class T> double 
FeatureSelector::floating(std::vector< Vector2d<T> >& v
        cost[k]=cost_r;
        cost.pop_back();
        if(verbose>1)
-         std::cout << "removed " << x_r << ", " << cost.size()-1 << "/" << 
maxFeatures << " features remain with cost: " << cost_r << std::endl;
+         std::cout << "removed " << x_r << ", " << subset.size() << "/" << 
maxFeatures << " features remain with cost: " << cost_r << std::endl;
         else if(verbose){
           for(std::list<int>::const_iterator 
lit=subset.begin();lit!=subset.end();++lit)
             std::cout << *lit << " ";
diff --git a/src/apps/pkextract.cc b/src/apps/pkextract.cc
index 81f4be8..b13781c 100644
--- a/src/apps/pkextract.cc
+++ b/src/apps/pkextract.cc
@@ -51,7 +51,7 @@ int main(int argc, char *argv[])
   Optionpk<string> ogrformat_opt("f", "f", "Output sample file 
format","SQLite");
   Optionpk<string> ftype_opt("ft", "ftype", "Field type (only Real or 
Integer)", "Real");
   Optionpk<string> ltype_opt("lt", "ltype", "Label type: In16 or String", 
"Integer");
-  Optionpk<bool> polygon_opt("polygon", "polygon", "create OGRPolygon as 
geometry instead of OGRPoint. Only if sample option is also of polygon type.", 
false);
+  Optionpk<bool> polygon_opt("polygon", "polygon", "Create OGRPolygon as 
geometry instead of OGRPoint. Only valid if sample features are polygons.", 
false);
   Optionpk<int> band_opt("b", "band", "band index(es) to extract. Use -1 to 
use all bands)", -1);
   Optionpk<string> rule_opt("r", "rule", "rule how to report image information 
per feature (only for vector sample). point (value at each point or at centroid 
if polygon), centroid, mean (of polygon), median (of polygon), proportion, 
minimum (of polygon), maximum (of polygon), maxvote, sum.", "point");
   Optionpk<double> srcnodata_opt("srcnodata", "srcnodata", "invalid value(s) 
for input image");
diff --git a/src/apps/pkfssvm.cc b/src/apps/pkfssvm.cc
index 5dfccf6..36e3a16 100644
--- a/src/apps/pkfssvm.cc
+++ b/src/apps/pkfssvm.cc
@@ -178,6 +178,7 @@ double getCost(const vector<Vector2d<float> > 
&trainingFeatures)
        //todo: make distinction between svm_predict and 
svm_predict_probability?
        assert(svm_check_probability_model(svm));
        predict_label = svm_predict_probability(svm,x_test,&(result[0]));
+       // predict_label = svm_predict(svm,x_test);
        string refClassName=nameVector[iclass];
        string className=nameVector[static_cast<short>(predict_label)];
        if(classValueMap.size())
@@ -320,8 +321,6 @@ 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)
@@ -339,8 +338,8 @@ int main(int argc, char *argv[])
 
   struct svm_problem prob;
   //organize training data
-  // trainingPixels.clear();
-  // testPixels.clear();
+  trainingPixels.clear();
+  testPixels.clear();
   if(verbose_opt[0]>=1)
     std::cout << "reading training file " << training_opt[0] << std::endl;
   try{
@@ -400,6 +399,7 @@ int main(int argc, char *argv[])
       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;
@@ -412,6 +412,16 @@ 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)
@@ -500,44 +510,50 @@ 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));
-    }
+  //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]]));
   }
 
+  // // 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);

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

_______________________________________________
Pkg-grass-devel mailing list
Pkg-grass-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-grass-devel

Reply via email to