train.cpp
Go to the documentation of this file.
00001 #include <agile_grasp/learning.h>
00002 #include <agile_grasp/localization.h>
00003 
00004 #include <boost/filesystem.hpp>
00005 
00006 #include <fstream>
00007 #include <string>
00008 #include <vector>
00009 
00010 int main(int argc, char** argv)
00011 {
00012         if (argc > 3)
00013         {
00014                 // get list of PCD-files for training
00015                 int num_files = atoi(argv[1]);
00016     std::string pcd_dir = argv[2];
00017     std::vector<std::string> files;
00018     if (num_files == 0)
00019     {    
00020       std::string file_name = pcd_dir + "files.txt";
00021       std::ifstream file(file_name.c_str());
00022       std::string str;      
00023       while (std::getline(file, str))
00024       {
00025         files.push_back(pcd_dir + str);
00026         std::cout << files.at(files.size()-1) << "\n";
00027       }
00028     }
00029     else
00030     {
00031       for (int i=0; i < num_files; i++)
00032         files.push_back(pcd_dir + boost::lexical_cast<std::string>(i));
00033     }
00034     
00035     // get list of workspace dimensions if available
00036                 std::string file_name = pcd_dir + "workspace.txt";    
00037     boost::filesystem::path file_test(file_name);
00038     Eigen::MatrixXd workspace_mat(files.size(), 6);
00039     if( !boost::filesystem::exists(file_test) )
00040     {
00041       std::cout << "No workspace.txt file found in pcd directory\n";
00042       std::cout << " Using standard workspace limits\n";
00043       Eigen::VectorXd ws(6);
00044       ws << 0.65, 0.9, -0.1, 0.1, -0.2, 1.0;
00045       for (int i=0; i < files.size(); i++)
00046         workspace_mat.row(i) = ws;
00047     }
00048     else
00049     {
00050       std::ifstream file_ws(file_name.c_str());
00051       std::string str;
00052       int t = 0;
00053       while (std::getline(file_ws, str))
00054       {
00055         for (int i = 0; i < 6; ++i)
00056         {
00057           int idx = str.find(" ");
00058           workspace_mat(t,i) = atof(str.substr(0, idx).c_str());
00059           str = str.substr(idx + 1);
00060         }
00061         t++;
00062       }
00063                 }
00064     std::cout << "workspace_mat:\n" << workspace_mat << "\n";
00065     
00066     std::string svm_file_name = argv[3];
00067     
00068     bool plots_hands = false;
00069     if (argc > 4)
00070       plots_hands = atoi(argv[4]);
00071 
00072                 int num_samples = 1000;
00073                 if (argc > 5)
00074       num_samples = atoi(argv[5]);
00075                 
00076                 int num_threads = 4;
00077                 if (argc > 6)
00078       num_threads = atoi(argv[6]);
00079 
00080                 // camera poses for 2-camera Baxter setup
00081                 Eigen::Matrix4d base_tf, sqrt_tf;
00082 
00083                 base_tf <<      0, 0.445417, 0.895323, 0.21, 
00084                                                                 1, 0, 0, -0.02, 
00085                                                                 0, 0.895323, -0.445417, 0.24, 
00086                                                                 0, 0, 0, 1;
00087 
00088                 sqrt_tf <<      0.9366, -0.0162, 0.3500, -0.2863, 
00089                                                                 0.0151, 0.9999, 0.0058, 0.0058, 
00090                                                                 -0.3501, -0.0002, 0.9367, 0.0554, 
00091                                                                 0, 0, 0, 1;
00092 
00093                 // set-up parameters for the hand search
00094                 Localization loc(num_threads, false, plots_hands);
00095                 loc.setCameraTransforms(base_tf * sqrt_tf.inverse(), base_tf * sqrt_tf);
00096                 loc.setNumSamples(num_samples);
00097                 loc.setNeighborhoodRadiusTaubin(0.03);
00098                 loc.setNeighborhoodRadiusHands(0.08);
00099                 loc.setFingerWidth(0.01);
00100                 loc.setHandOuterDiameter(0.09);
00101                 loc.setHandDepth(0.06);
00102                 loc.setInitBite(0.015);
00103                 loc.setHandHeight(0.02);
00104 
00105                 // collect training data for the SVM from each PCD file
00106                 std::cout << "Acquiring training data ...\n";
00107                 std::vector<GraspHypothesis> hand_list;
00108     std::vector<int> hand_list_sizes(files.size());
00109                 for (int i = 0; i < files.size(); i++)
00110                 {
00111                         std::cout << " Creating training data from file " << files[i] << " ...\n";
00112                         std::string file_left = files[i] + "l_reg.pcd";
00113                         std::string file_right = files[i] + "r_reg.pcd";
00114                         loc.setWorkspace(workspace_mat.row(i));
00115                         std::vector<GraspHypothesis> hands = loc.localizeHands(file_left, file_right, true, true);
00116                         hand_list.insert(hand_list.end(), hands.begin(), hands.end());
00117       hand_list_sizes[i] = hand_list.size();
00118       std::cout << i << ") # hands: " << hands.size() << std::endl;
00119                 }
00120                 
00121                 // use the collected data for training the SVM
00122                 std::cout << "Training the SVM ...\n";
00123                 Learning learn;
00124                 Eigen::Matrix<double,3,2> cam_pos;
00125                 cam_pos.col(0) = loc.getCameraTransform(true).block<3,1>(0,3);
00126                 cam_pos.col(1) = loc.getCameraTransform(false).block<3,1>(0,3);
00127     int max_positives = 20;
00128     // learn.trainBalanced(hand_list, hand_list_sizes, svm_file_name, cam_pos, max_positives);
00129     learn.train(hand_list, hand_list_sizes, svm_file_name, cam_pos, max_positives);
00130                 // learn.train(hand_list, svm_file_name, cam_pos, false);
00131 
00132                 return 0;
00133         }
00134 
00135         std::cout << "No PCD filenames given!\n";
00136         std::cout << "Usage: train_svm num_files pcd_directory svm_filename [plots_hands] [num_samples] [num_threads]\n";
00137         std::cout << "Train an SVM to localize grasps in point clouds.\n\n";
00138         std::cout << "The standard way is to create a directory that contains *.pcd files for " <<
00139                 "training. Each file needs to be called obji.pcd where i goes from 0 to <num_files>. " <<
00140                 "<pcd_directory> is the location and the root name of the files, for example, " <<
00141                 "/home/userA/data/obj.\n";
00142         std::cout << "Alternatively, if <num_files> is set to 0, there needs to be a files.txt " << 
00143                 "within pcd_directory that lists all filenames that should be used for training, and " <<
00144                 "a workspace.txt that lists the workspace dimensions for each file.\n";
00145         
00146         return (-1);
00147 }
00148 


agile_grasp
Author(s):
autogenerated on Thu Aug 27 2015 12:01:28