[pktools] 124/375: priors in classification of vector files

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:06 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 035120e777ea5efdc42c86cfc354797ba9518b6e
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Fri Jul 26 10:19:27 2013 +0200

    priors in classification of vector files
---
 ChangeLog                       |  9 +++++++++
 doc/examples_pkascii2ogr.dox    |  5 +++++
 doc/examples_pkclassify_svm.dox | 33 +++++++++++++++++++++++++++++++++
 doc/examples_pkdiff.dox         | 12 ++++++++++++
 src/apps/pkclassify_nn.cc       | 24 ++++++++++++------------
 src/apps/pkclassify_svm.cc      | 27 ++++++++++++++-------------
 6 files changed, 85 insertions(+), 25 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9748a4e..07dc87a 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -108,3 +108,12 @@ version 2.4.1
 	filter spectra in ASCII file (including wavelet transform)
  - pkfilter
 	discrete wavelet transform (dwtForward, dwtInverse, dwtQuantize)
+ - pkclassify_svm
+	priors for input vector file
+	todo: take priors into account for cross validation
+	ordering of labels before training
+ - pkclassify_nn
+	priors for input vector file
+	todo: take priors into account for cross validation
+	ordering of labels before training
+
diff --git a/doc/examples_pkascii2ogr.dox b/doc/examples_pkascii2ogr.dox
new file mode 100644
index 0000000..2c5231f
--- /dev/null
+++ b/doc/examples_pkascii2ogr.dox
@@ -0,0 +1,5 @@
+\section examples_pkascii2ogr Examples of pkascii2ogr
+\code
+pkascii2ogr -i input.txt -o output.shp -x 2 -y 1 -n id -ot Integer -n label -ot Integer -p epsg:4326
+\endcode
+create a vector shape file (output.shp) from input ASCII file (input.txt). The coordinates x (longitude) and y (latitude) can be found in input.txt as columns 3 and 2 respectively (columns start counting from 0). The remaining 2 columns in input.txt are used as fields (attributes) of type integer: id (column 0) and label (column 3). The projection is set to lat lon (epsg:4326). 
diff --git a/doc/examples_pkclassify_svm.dox b/doc/examples_pkclassify_svm.dox
new file mode 100644
index 0000000..1fc7149
--- /dev/null
+++ b/doc/examples_pkclassify_svm.dox
@@ -0,0 +1,33 @@
+\section examples_pkclassify_svm Examples of pkclassify_svm
+\code
+pkclassify_svm -i input.tif -t training.shp -o output.tif -cv 2 -ct colourtable.txt -cc 1000 -g 0.1
+\endcode
+
+Classify input image input.tif with a support vector machine. A training sample that is provided as a vector (shp) file. It contains all features (same dimensionality as input.tif) in its fields (please check \ref pkextract "pkextract" on how to obtain such a file from a "clean" vector file containing locations only). A two-fold cross validation (cv) is performed (output on screen). The parameters cost and gamma of the support vector machine are set to 1000 and 0.1 respectively. A colour [...]
+
+\code
+pkclassify_svm -i input.tif -t training.shp -o output.tif -bs 33 -bag 3
+\endcode
+Classification using bootstrap aggregation. The training sample is randomly split in three subsamples (33% of the original sample each).
+
+\code
+pkclassify_svm -i input.tif -t training.shp -o output.tif -p 1 -p 1 -p 1 -p 1 -p 1 -p 1 -p 1 -p 1 -p 1 -p 1 -p 1 -p 0.2 -p 1 -p 1 -p 1
+\endcode
+Classification using prior probabilities for each class. The priors are automatically normalized. The order in which the options -p are provide should respect the alphanumeric order of the class names (class 10 comes before 2...)
+
+
+
+replace pixel values 1 and 2 with value 0
+
+\code
+pkreclass -i vector.shp -o output.shp -c FROM -r TO -n INFIELD
+\endcode
+replace FROM with TO in field INFIELD (of type string) in vector file vector.shp and write to new vector output.shp
+
+for((i=0;i<256;++i));do if(($i<100));then echo "$i 1";else echo "$i 0";fi;done > code.txt; pkreclass -i input1.tif -o output.tif –code code.txt; # replace all values smaller than 100 with 1, all other values with 0
+
+\code
+pkreclass -i input1.tif -o output.tif $(for((i=0;i<256;++i));do if(($i<100));then echo -n " -c $i " " -r 1";else echo " -c $i " " -r 0";fi;done)
+\endcode
+same as previous but without temporary file 
+
diff --git a/doc/examples_pkdiff.dox b/doc/examples_pkdiff.dox
new file mode 100644
index 0000000..1ff9d06
--- /dev/null
+++ b/doc/examples_pkdiff.dox
@@ -0,0 +1,12 @@
+\section examples_pkdiff Examples of pkdiff
+\code
+pkdiff -i input.tif -r reference.tif
+\endcode
+
+Check if two raster images are different (reports only if images are different or not)
+
+\code
+pkdiff -i classificationMap.tif -r referencePoints.shp -cm	
+\endcode
+
+Validate a classification map using points from a reference sample and report confusion matrix
diff --git a/src/apps/pkclassify_nn.cc b/src/apps/pkclassify_nn.cc
index 4ebec2a..5922502 100644
--- a/src/apps/pkclassify_nn.cc
+++ b/src/apps/pkclassify_nn.cc
@@ -361,6 +361,14 @@ int main(int argc, char *argv[])
 	  cm.pushBackClassName(mapit->first,doSort);
 	++mapit;
       }
+      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));
+        }
+      }
       if(priors_opt.size()==nameVector.size()){
 	std::cerr << "Warning: please check if priors are provided in correct order!!!" << std::endl;
 	for(int iclass=0;iclass<nameVector.size();++iclass)
@@ -539,14 +547,6 @@ int main(int argc, char *argv[])
     float progress=0;
   //-------------------------------- open image file ------------------------------------
   if(input_opt[0].find(".shp")==string::npos){
-    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));
-      }
-    }
     ImgReaderGdal testImage;
     try{
       if(verbose_opt[0]>=1)
@@ -989,14 +989,14 @@ int main(int argc, char *argv[])
             switch(comb_opt[0]){
             default:
             case(0)://sum rule
-              probOut[iclass]+=result[iclass]+static_cast<float>(1.0-nbag)/nbag*priors[iclass];//add probabilities for each bag
+              probOut[iclass]+=result[iclass]*priors[iclass];//add probabilities for each bag
               break;
             case(1)://product rule
-              probOut[iclass]*=pow(priors[iclass],static_cast<float>(1.0-nbag)/nbag)*result[iclass];//add probabilities for each bag
+              probOut[iclass]*=pow(priors[iclass],static_cast<float>(1.0-nbag)/nbag)*result[iclass];//multiply probabilities for each bag
               break;
             case(2)://max rule
-              if(result[iclass]>probOut[iclass])
-                probOut[iclass]=result[iclass];
+              if(priors[iclass]*result[iclass]>probOut[iclass])
+                probOut[iclass]=priors[iclass]*result[iclass];
               break;
             }
           }
diff --git a/src/apps/pkclassify_svm.cc b/src/apps/pkclassify_svm.cc
index 0cdca37..d38a12e 100644
--- a/src/apps/pkclassify_svm.cc
+++ b/src/apps/pkclassify_svm.cc
@@ -411,6 +411,15 @@ int main(int argc, char *argv[])
 	  cm.pushBackClassName(mapit->first,doSort);
 	++mapit;
       }
+      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));
+        }
+      }
+
       if(priors_opt.size()==nameVector.size()){
 	std::cerr << "Warning: please check if priors are provided in correct order!!!" << std::endl;
 	for(int iclass=0;iclass<nameVector.size();++iclass)
@@ -548,14 +557,6 @@ int main(int argc, char *argv[])
     pfnProgress(progress,pszMessage,pProgressArg);
   //-------------------------------- open image file ------------------------------------
   if(input_opt[0].find(".shp")==string::npos){
-    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));
-      }
-    }
     ImgReaderGdal testImage;
     try{
       if(verbose_opt[0]>=1)
@@ -1031,14 +1032,14 @@ int main(int argc, char *argv[])
             switch(comb_opt[0]){
             default:
             case(0)://sum rule
-              probOut[iclass]+=result[iclass]+static_cast<float>(1.0-nbag)/nbag*priors[iclass];//add probabilities for each bag
-              break;
+              probOut[iclass]+=result[iclass]*priors[iclass];//add probabilities for each bag
+               break;
             case(1)://product rule
-              probOut[iclass]*=pow(priors[iclass],static_cast<float>(1.0-nbag)/nbag)*result[iclass];//add probabilities for each bag
+              probOut[iclass]*=pow(priors[iclass],static_cast<float>(1.0-nbag)/nbag)*result[iclass];//multiply probabilities for each bag
               break;
             case(2)://max rule
-              if(result[iclass]>probOut[iclass])
-                probOut[iclass]=result[iclass];
+              if(priors[iclass]*result[iclass]>probOut[iclass])
+                probOut[iclass]=priors[iclass]*result[iclass];
               break;
             }
           }

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