void jpcnn_destroy_predictor(void* predictorHandle) { SPredictorInfo* predictorInfo = (SPredictorInfo*)(predictorHandle); svm_free_and_destroy_model(&predictorInfo->model); if (predictorInfo->problem != NULL) { destroy_svm_problem(predictorInfo->problem); } free(predictorInfo); }
void im_compute_bdd_bow(const IMbdd& bdd, std::map <std::string, struct svm_problem>& peopleBOW){ std::string path2bdd(bdd.getFolder()); std::vector<std::string> activities = bdd.getActivities(); std::vector<std::string> people = bdd.getPeople(); int dim = bdd.getDim(); int maxPts = bdd.getMaxPts(); int k = bdd.getK(); for(std::vector<std::string>::iterator person = people.begin(); person != people.end(); ++person){ int currentActivity = 1; struct svm_problem svmPeopleBOW; svmPeopleBOW.l = 0; svmPeopleBOW.x = NULL; svmPeopleBOW.y = NULL; std::cout << "Computing the svmProblem of " << *person << std::endl; for(std::vector<std::string>::iterator activity = activities.begin(); activity != activities.end(); ++activity){ string rep(path2bdd + "/" + *person + "/" + *activity + "/fp"); DIR * repertoire = opendir(rep.c_str()); if (!repertoire){ std::cerr << "Impossible to open the feature points directory!" << std::endl; exit(EXIT_FAILURE); } struct dirent * ent; while ( (ent = readdir(repertoire)) != NULL){ std::string file = ent->d_name; if(file.compare(".") != 0 && file.compare("..") != 0){ std::string path2FPs(rep + "/" + file); KMdata dataPts(dim,maxPts); int nPts = importSTIPs(path2FPs, dim, maxPts, &dataPts); if(nPts != 0){ dataPts.setNPts(nPts); dataPts.buildKcTree(); KMfilterCenters ctrs(k, dataPts); importCenters(path2bdd + "/" + "training.means", dim, k, &ctrs); // Only one BOW struct svm_problem svmBow = computeBOW(currentActivity, dataPts, ctrs); addBOW(svmBow.x[0], svmBow.y[0], svmPeopleBOW); destroy_svm_problem(svmBow); } } } closedir(repertoire); currentActivity++; } peopleBOW.insert(std::make_pair<std::string, struct svm_problem>((*person), svmPeopleBOW)); } }
void* jpcnn_create_predictor_from_trainer(void* trainerHandle) { SLibSvmTrainingInfo* trainer = (SLibSvmTrainingInfo*)(trainerHandle); SLibSvmProblem* problem = create_svm_problem_from_training_info(trainer); const char* parameterCheckError = svm_check_parameter(problem->svmProblem, problem->svmParameters); if (parameterCheckError != NULL) { fprintf(stderr, "libsvm parameter check error: %s\n", parameterCheckError); destroy_svm_problem(problem); return NULL; } struct svm_model* model = svm_train(problem->svmProblem, problem->svmParameters); SPredictorInfo* result = (SPredictorInfo*)(malloc(sizeof(SPredictorInfo))); result->model = model; result->problem = problem; return result; }
void im_normalize_bdd_bow(const IMbdd& bdd, const std::vector<std::string>& trainingPeople, std::map<std::string, struct svm_problem>& peopleBOW){ int k = bdd.getK(); // Extract and export gaussian parameters struct svm_problem svmProblem; svmProblem.l = 0; svmProblem.x = NULL; svmProblem.y = NULL; for(std::vector<std::string>::const_iterator person = trainingPeople.begin(); person != trainingPeople.end(); ++person){ for(int i=0 ; i<peopleBOW[*person].l ; i++){ svm_node *bow = peopleBOW[*person].x[i]; addBOW(bow, peopleBOW[*person].y[i], svmProblem); } } double *means=NULL, *stand_devia=NULL; means = new double[k]; stand_devia = new double[k]; std::cout << "Extracting gaussian parameters..." << std::endl; get_gaussian_parameters(k, svmProblem, means, stand_devia); destroy_svm_problem(svmProblem); std::cout<<"Exporting gaussian parameters..." << std::endl; save_gaussian_parameters(bdd, means, stand_devia); delete []means; delete []stand_devia; std::vector<std::string> people = bdd.getPeople(); std::string normalization(bdd.getNormalization()); if(normalization.compare("") !=0){ std::cout << "Doing the " << normalization << " normalization..." << std::endl; for(std::vector<std::string>::iterator person = people.begin(); person != people.end(); ++person){ bow_normalization(bdd,peopleBOW[*person]); } } }
double im_training_leave_one_out(const IMbdd& bdd, const std::vector<std::string>& trainingPeople, const std::map <std::string, struct svm_problem>& peopleBOW, int& minC, int& maxC, int& minG, int& maxG, struct svm_parameter& svmParameter){ if(trainingPeople.size() < 2){ std::cerr << "Impossible to make a leave-one-out-cross-validation with less than 2 people!" << std::endl; exit(EXIT_FAILURE); } int nrActivities = bdd.getActivities().size(); std::string path2bdd(bdd.getFolder()); int C, gamma; int bestC, bestG; double bestAccuracy = -1; for(C=minC; C<=maxC ; C++){ for(gamma = minG ; gamma <= maxG ; gamma++){ svmParameter.C = pow(2,C); svmParameter.gamma = pow(2,gamma); double XValidationAccuracy; int total_correct = 0; int nrBOW = 0; // For each couple (C,gamma) we do people.size() leave one outs for(std::vector<std::string>::const_iterator person = trainingPeople.begin(); person != trainingPeople.end(); ++person){ // This person will be the testing person // We do the training with the others struct svm_problem trainingProblem; trainingProblem.l = 0; trainingProblem.x = NULL; trainingProblem.y = NULL; for(std::vector<std::string>::const_iterator trainingPerson = trainingPeople.begin(); trainingPerson != trainingPeople.end(); ++ trainingPerson){ // Training with the other person if((*trainingPerson).compare(*person) != 0){ // For each person we train the rest and we test the person // Then compute the mean of the cross validation accuracy for(int i=0 ; i < peopleBOW.at(*trainingPerson).l ; i++){ struct svm_node* bow = peopleBOW.at(*trainingPerson).x[i]; addBOW(bow, peopleBOW.at(*trainingPerson).y[i],trainingProblem); } } } struct svm_model** svmModels = svm_train_ovr(&trainingProblem,&svmParameter); destroy_svm_problem(trainingProblem); // Making test for(int i=0 ; i<peopleBOW.at(*person).l ; i++){ double* probs = new double[nrActivities]; double lab_in = peopleBOW.at(*person).y[i]; double lab_out = svm_predict_ovr_probs(svmModels,peopleBOW.at(*person).x[i], nrActivities,probs,2); delete []probs; if(lab_in == lab_out) total_correct++; nrBOW++; } // Releasing svmModels memory for(int i=0 ; i<nrActivities ; i++){ svm_free_and_destroy_model(&svmModels[i]); } delete[] svmModels; } // leave one out if((XValidationAccuracy = total_correct * 1.0 / nrBOW) > bestAccuracy){ bestAccuracy = XValidationAccuracy; bestC = C; bestG = gamma; } } // gamma loop } // C loop svmParameter.C = pow(2,bestC); svmParameter.gamma = pow(2,bestG); return bestAccuracy; }
/** * \fn void predictActivity(std::string, std::string bddName, int maxPts) * \brief Predict the activity done in a video with an existant trained BDD. * * \param[in] The path to the video to predict. * \param[in] bddName The name of the BDD containing videos. * \param[in] maxPts The maximum number of STIPs we can extract. * \return The name of the activity. */ void predictActivity(std::string videoPath, std::string bddName ){ std::string path2bdd("bdd/" + bddName); // Loading parameters IMbdd bdd(bddName,path2bdd); bdd.load_bdd_configuration(path2bdd.c_str(),"imconfig.xml"); int scale_num = bdd.getScaleNum(); std::string descriptor = bdd.getDescriptor(); int dim = bdd.getDim(); int k = bdd.getK(); int maxPts = bdd.getMaxPts(); //double p = getTrainProbability(path2bdd); // Computing feature points KMdata dataPts(dim,maxPts); int nPts = 0; nPts = extract_feature_points(videoPath, scale_num, descriptor, dim, maxPts, dataPts); if(nPts == 0){ std::cerr << "No activity detected !" << std::endl; exit(EXIT_FAILURE); } std::cout << nPts << " vectors extracted..." << std::endl; dataPts.setNPts(nPts); dataPts.buildKcTree(); KMfilterCenters ctrs(k, dataPts); importCenters(bdd.getFolder() + "/" + bdd.getKMeansFile(), dim, k, &ctrs); std::cout << "KMeans centers imported..." << std::endl; activitiesMap *am; int nbActivities = mapActivities(path2bdd,&am); struct svm_problem svmProblem = computeBOW(0, dataPts, ctrs); double means[k], stand_devia[k]; load_gaussian_parameters(bdd, means, stand_devia); // simple, gaussian, both, nothing if(bdd.getNormalization().compare("") != 0) bow_normalization(bdd,svmProblem); std::cout << "Bag of words normalized..." << std::endl; struct svm_model** pSVMModels = new svm_model*[nbActivities]; std::vector<std::string> modelFiles(bdd.getModelFiles()); int i=0; for (std::vector<std::string>::iterator it = modelFiles.begin() ; it != modelFiles.end() ; ++it){ pSVMModels[i]= svm_load_model((*it).c_str()); i++; } std::cout << "SVM models imported..." << std::endl; double probs[nbActivities]; double label = svm_predict_ovr_probs(pSVMModels, svmProblem.x[0], nbActivities, probs, 2); std::cerr<<"Probs: "; for(int j=0 ; j<nbActivities ; j++){ std::cout << setw(2) << setiosflags(ios::fixed) << probs[j]*100<<" "; } destroy_svm_problem(svmProblem); int index = searchMapIndex(label, am, nbActivities); std::cout << "Activity predicted: "; std::cout << am[index].activity << "(" << am[index].label << ")"; std::cout << std::endl; for(int m=0 ; m<nbActivities ; m++){ svm_free_and_destroy_model(&pSVMModels[m]); } }
double im_svm_train(IMbdd& bdd, const std::vector<std::string>& trainingPeople, MatrixC& trainMC, const std::vector<std::string>& testingPeople, MatrixC& testMC){ std::string path2bdd(bdd.getFolder()); int k = bdd.getK(); // Computing Bag Of Words for each people std::cout << "Computing BOWs..." << std::endl; std::map<std::string, struct svm_problem> peopleBOW; std::map<std::string, int> activitiesLabel; std::cout << "Computing the SVM problem (ie. BOW) of all the people..." << std::endl; im_compute_bdd_bow(bdd,peopleBOW); // all the BOW are saved in peopleBOW // Normalization: do not forget to change the normalization before this step //bdd.changeNormalizationSettings(normalization, // "means.txt", //"stand_devia.txt"); for(std::vector<std::string>::const_iterator trainingPerson = trainingPeople.begin(); trainingPerson != trainingPeople.end(); ++ trainingPerson){ for(int i=0 ; i<peopleBOW[*trainingPerson].l ; i++) std::cout << peopleBOW[*trainingPerson].y[i] << std::endl; } std::cout << "Normalizing the BOW..." << std::endl; bdd.changeNormalizationSettings("simple", "means.txt", "stand_devia.txt"); im_normalize_bdd_bow(bdd,trainingPeople,peopleBOW); std::cout << "Not exporting the problem..." << std::endl; // Export ?? OR NOT export ???????? THAT IS THE QUESTION // Exporting problem //exportProblem(svmTrainProblem, path2bdd + "/concatenate.bow.train"); //if(testingPeople != {""}) //exportProblem(svmTestProblem, path2bdd + "/concatenate.bow.test"); // * SVM * // (One Versus the Rest (OVR)) // SVM parameters struct svm_parameter svmParameter; get_svm_parameter(k,svmParameter); std::cout << "Searching the best C and the best Gamma..." << std::endl; // We have to do a leave one out with the training data // For the moment we use the same training centers to find Gamma and C int minC = -5, maxC = 5; int minG = -10, maxG = 3; double crossValidationAccuracy = im_training_leave_one_out(bdd, trainingPeople, peopleBOW, minC, maxC, // ln(min/max C) minG, maxG, // ln(min/max G) svmParameter); // N.B: when we export the model to the robot, peopleBOW index = trainingPeople // Generating the SVM model std::cout << "Generating the SVM model..." << std::endl; struct svm_problem trainingProblem; trainingProblem.l = 0; trainingProblem.x = NULL; trainingProblem.y = NULL; for(std::vector<std::string>::const_iterator trainingPerson = trainingPeople.begin(); trainingPerson != trainingPeople.end(); ++ trainingPerson){ for(int i=0 ; i < peopleBOW.at(*trainingPerson).l ; i++){ struct svm_node* bow = peopleBOW.at(*trainingPerson).x[i]; addBOW(bow, peopleBOW.at(*trainingPerson).y[i],trainingProblem); } } struct svm_model** svmModels = svm_train_ovr(&trainingProblem,&svmParameter); // Exporting models std::cout << "Saving the SVM model..." << std::endl; std::vector <std::string> modelFiles; int nrActivities = bdd.getActivities().size(); for(int i=0 ; i< nrActivities ; i++){ std::string fileToSaveModel = path2bdd; std::stringstream ss; ss << i; fileToSaveModel = fileToSaveModel + "/svm_ovr_" + ss.str() + ".model"; svm_save_model(fileToSaveModel.c_str(),svmModels[i]); modelFiles.push_back(fileToSaveModel); } bdd.changeSVMSettings(nrActivities, modelFiles); // Calculate the confusion matrix and the probability estimation std::cout << "Filling the training confusion matrix..." << std::endl; im_fill_confusion_matrix(bdd,trainingProblem,svmModels, trainMC); destroy_svm_problem(trainingProblem); if(testingPeople.size() > 0){ std::cerr << "Entering in Hell..." << std::endl; struct svm_problem testingProblem; trainingProblem.l = 0; trainingProblem.x = NULL; trainingProblem.y = NULL; for(std::vector<std::string>::const_iterator testingPerson = testingPeople.begin(); testingPerson != testingPeople.end(); ++ testingPerson){ for(int i=0 ; i < peopleBOW.at(*testingPerson).l ; i++){ std::cerr << "HELlllll" << std::endl; struct svm_node* bow = peopleBOW.at(*testingPerson).x[i]; std::cerr << peopleBOW.at(*testingPerson).y[i] << std::endl; std::cerr << "YOOOO man" << std::endl; addBOW(bow, peopleBOW.at(*testingPerson).y[i],testingProblem); std::cerr << "Why ?!!!" << std::endl; } } std::cout << "Filling the testing confusion matrix..." << std::endl; im_fill_confusion_matrix(bdd,testingProblem,svmModels, testMC); destroy_svm_problem(testingProblem); } std::cout << "Releasing peopleBOW" << std::endl; // Releasing peopleBOW std::vector<std::string> people = bdd.getPeople(); for(std::vector<std::string>::iterator person = people.begin(); person != people.end(); ++ person){ destroy_svm_problem(peopleBOW[*person]); } // Releasing OVR models for(int i=0;i<nrActivities;i++){ svm_free_and_destroy_model(&svmModels[i]);} delete [] svmModels; svmModels = NULL; return crossValidationAccuracy; }