icf_classify_subset.cpp
Go to the documentation of this file.
00001 #include <icf_core/client/Client.h>
00002 #include <icf_core/base/EvaluationResult.hpp>
00003 #include <icf_core/base/ConfusionMatrix.hpp>
00004 #include <icf_core/base/ClassificationResult.hpp>
00005 
00006 ...
00007 
00008   std::map<int, std::string> classnames;
00009   std::string classifier;
00010   std::string parameters;
00011 
00012   Eigen::MatrixXf conf_mat;
00013 
00014   // TODO: use boost_shared_ptr
00015   ClassifierManager* g_manager;
00016   ServerSideRepo* data_store;  
00017   ClassifierClient* client_test;
00018   
00019   ~Destructor ()
00020   {
00021     // delete pointers
00022     delete g_manager; 
00023     delete data_store;
00024     delete client_test;
00025   }
00026 
00027   Constructor()
00028   {
00029     // use here the same things as in "icf_train_subset.sh" of course, but with KNN you can change some stuff as well (-k, -w for sure)
00030     classifier = "knn";
00031     parameters = "-m chisquared -k 10 -w";
00032     
00033     // maybe this needs ~ expanded, just locate it and pass it in as a parameter
00034     std::string model_file_base = "~/.ros/vfh_subsets1to3_with_evaluation_knn"; // NOTE: there are multiple files with this base name
00035     
00036     classnames[1]="Armchair";
00037     classnames[2]="Chair1";
00038     classnames[3]="Chair2";
00039     classnames[4]="Chair3";
00040     classnames[5]="Sideboard";
00041     classnames[6]="Table1";
00042     classnames[7]="Table2";
00043     classnames[8]="Table3";
00044     // etc.
00045 
00046     // init data uploading and client
00047     std::string manager_name = "kinect_uima_bridge";
00048     data_store = new ServerSideRepo(n, manager_name);
00049     client_test = new ClassifierClient(n, manager_name, classifier, parameters);
00050     client_test->load(model_file_base);
00051     
00052     // optional, use confusion matrix to weight we resulting scores (i.e. accuracy weighting)
00053     conf_mat = client_test->getConfusionMatrix()->getCM().cast<float>();
00054     Eigen::ArrayXf col_sums = conf_mat.colwise().sum();
00055     //for (int i=0; i<col_sums.size(); ++i)
00056     //  conf_mat.row(i) = conf_mat.row(i).array() / col_sums;
00057     for (int i=0; i<conf_mat.rows(); ++i)
00058       for (int j=0; j<conf_mat.cols(); ++j)
00059         conf_mat(i,j) /= col_sums(j);
00060     //std::cerr << conf_mat << std::endl;
00061   }
00062 
00063   void classify (std::vector<float> feature) // or any other way to input the feature vector to be classified
00064   {
00065     try
00066     {  
00067       // set up dataset
00068       DS ds;
00069       DS::Matrix feature_matrix(1, feature.size());
00070       feature_matrix.row(0) = Eigen::VectorXf::Map(&feature[0], feature.size()).cast<double>(); // or any other way to get the feature
00071       ds.setFeatureMatrix(feature_matrix, "/x");
00072       //ds.setFeatureMatrix(&feature[0], 1, feature.size(), "/test"); needs double!
00073       data_store->uploadData(ds, "test");
00074       
00075       // classify data
00076       client_test->assignData("test", icf::Classify);
00077       ClassificationResult classificationResult = client_test->classify();
00078       
00079       // best result
00080       int result = classificationResult.results->at(0); // here we classify a single feaure vector, but we could do multiple at once
00081       
00082       // confidences and accuracies for all classes
00083       for(std::map<int,std::string>::iterator mit = classnames.begin(); mit != classnames.end(); ++mit)
00084       {
00085         // example use assuming a numbering of classes from 1
00086         float accuracy = conf_mat(mit->first-1,result-1); // TODO add pseudocounts to conf matrix or a minimum probability
00087         float confidence = classificationResult.confidenceFor(0,mit->first); // if k=1 this will be 1 for mit->first==result and 0 for all others classes
00088         float score = confidence * accuracy; // or one of them left out :)
00089       }
00090     }
00091     catch (ICFException& e)
00092     {
00093       std::cerr << boost::diagnostic_information(e);
00094       std::vector<service_unavailable_error>* err = boost::get_error_info<service_unavailable_collection>(e);
00095       if (err != NULL)
00096       {
00097         for (std::vector<service_unavailable_error>::iterator iter = err->begin(); iter != err->end(); iter++)
00098           std::cerr << "Error: " << iter->value() << std::endl;
00099       }
00100       else
00101         std::cerr << "No service availability related errors" << std::endl;
00102     }
00103   }
00104 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


furniture_classification
Author(s): Vladyslav Usenko
autogenerated on Sun Oct 6 2013 12:12:31