test_svm.cpp
Go to the documentation of this file.
00001 #include <libsvm/svm.h>
00002 #include <sstream>
00003 #include <string>
00004 #include <ros/ros.h>
00005 #include <tabletop_pushing/shape_features.h>
00006 
00007 using cpl_visual_features::ShapeDescriptors;
00008 using cpl_visual_features::ShapeDescriptor;
00009 
00010 int main(int argc, char** argv)
00011 {
00012   std::string param_path;
00013   param_path = "/home/thermans/Dropbox/Data/ichr2013-results/icdl_data/examples_line_dist/push_svm_1.model";
00014   ROS_INFO_STREAM("param_path " << param_path);
00015   svm_model* push_model;
00016   push_model = svm_load_model(param_path.c_str());
00017   ROS_INFO_STREAM("svm type: " << svm_get_svm_type(push_model));
00018   ROS_INFO_STREAM("nr classes: " << svm_get_nr_class(push_model));
00019   int num_sv = svm_get_nr_sv(push_model);
00020   ROS_INFO_STREAM("nr sv: " << num_sv);
00021   ROS_INFO_STREAM("rho: " << *push_model->rho);
00022   svm_parameter params = push_model->param;
00023   ROS_INFO_STREAM("kernel type: " << params.kernel_type);
00024   // ROS_INFO_STREAM("free_sv: " << push_model->free_sv);
00025   // std::stringstream sv_idx;
00026   // std::stringstream sv_coef;
00027   // for (int i = 0; i < num_sv; ++i)
00028   // {
00029   //   sv_idx << " " << push_model->SV[i]->value;
00030   //   sv_idx << ":" << push_model->sv_coef[push_model->SV[i]->index][i];
00031   // }
00032   // ROS_INFO_STREAM("SVs: " << sv_idx.str());
00033 
00034 
00035   int local_length = 36;
00036   int global_length = 60;
00037   std::string test_feat_path = "/home/thermans/Dropbox/Data/ichr2013-results/icdl_data/examples_line_dist/test_K_sds.txt";
00038   ShapeDescriptors sds = tabletop_pushing::loadSVRTrainingFeatures(test_feat_path, local_length+global_length);
00039 
00040   // Remove trailing .model
00041   param_path.erase(param_path.size()-6, 6);
00042   std::stringstream train_feat_path;
00043   train_feat_path << param_path << "-feats.txt";
00044   ROS_INFO_STREAM(train_feat_path.str());
00045 
00046   double gamma_local = 2.5;
00047   double gamma_global = 2.0;
00048   double mixture_weight = 0.7;
00049 
00050   cv::Mat K = tabletop_pushing::computeChi2Kernel(sds, train_feat_path.str(), local_length, global_length,
00051                                                   gamma_local, gamma_global, mixture_weight);
00052   // cv::imshow("K matrix", K);
00053   // cv::waitKey();
00054 
00055   ROS_INFO_STREAM("size(K): (" << K.rows << ", " << K.cols << ")");
00056 
00057   // TODO: Compare K here to MATLAB K
00058   std::stringstream scores_out;
00059   std::vector<double> pred_push_scores;
00060   for (int i = 0; i < K.cols; ++i)
00061   {
00062     svm_node* x = new svm_node[K.rows+1];
00063     x[0].value = 0;
00064     x[0].index = 0;
00065     for (int j = 0; j < K.rows; ++j)
00066     {
00067       // NOTE: Need to add 1 to index since support vectors are 1 based indices (thanks MATLAB!)
00068       x[j+1].value = K.at<double>(j, i);
00069       x[j+1].index = 0; // unused
00070     }
00071     // Perform prediction and convert out of log space
00072     // TODO: Collapse below once we get the bugs worked out
00073     double raw_pred_score = svm_predict(push_model, x);
00074     delete x;
00075     pred_push_scores.push_back(raw_pred_score);
00076     scores_out  << raw_pred_score << "    ";
00077   }
00078   ROS_INFO_STREAM("\n" << scores_out.str());
00079 
00080   return 0;
00081 }


tabletop_pushing
Author(s): Tucker Hermans
autogenerated on Wed Nov 27 2013 11:59:44