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 LanguageModel::recalc_bow_for_node(GramNode &node, uint32_t lvl) { //prob_t sum_next = 0; //prob_t sum = 0; if (child_num_buff[node.gram_id] == 0) { bow_buff[node.gram_id] = 0; //当前bow==1,log bow == 0 } word_id_t context[MAX_ORDER]; GramNode *tmp = &node; for (int32_t i = lvl; i >= 0; i--) { context[i] = tmp->word_id; tmp = tmp->prefix; } prob_t PA, PB; computeBOW(node.gram_id, context, lvl + 1, PA, PB); /*if (lvl == 0) { if (PA < Prob_Epsilon) { PA = 0.0; } } else */ if (PA < Prob_Epsilon && PB < Prob_Epsilon) { bow_buff[node.gram_id] = LogP_One; } else { bow_buff[node.gram_id] = log10(PA) - log10(PB); } if (is_debug) { cerr << "[RECALC_BOW]"; puts_gram(cerr, &node); cerr << " " << bow_buff[node.gram_id] << endl; } //assert(sum_next >= 0.0 && sum_next < 1.0); //assert(sum >= 0.0 && sum < 1.0); //prob_t new_bow = log10((1.0 - sum_next) / (1.0 - sum)); //bow_buff[node.gram_id] = new_bow; }
/** * \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 LanguageModel::calc_distance(uint32_t lvl, GramNode *n) { GramNode *prefix = n->prefix; prob_t log10_PH = 0.0; while (prefix != NULL) { log10_PH += prob_buff[prefix->gram_id]; prefix = prefix->prefix; } //感觉此处sunpinyin错了? prefix = n->prefix; prob_t log10_BOW = bow_buff[prefix->gram_id]; prob_t BOW = exp10(log10_BOW); //bow(h) word_id_t context[MAX_ORDER]; //0 1 lvl-1 lvl //w_0 ......... w_{n-1} w_n GramNode *tmp = n; for (int32_t i = lvl; i >= 0; i--) { context[i] = tmp->word_id; tmp = tmp->prefix; } //double numerator, denominator; prob_t PA, PB; computeBOW(n->prefix->gram_id, context, lvl, PA, PB); //srilm中如果算出来不对了,那么不删除,有可能因为精度关系概率<0,sunpinyin设了一个较小的值 prob_t PH = exp10(log10_PH); // PH是当前gram的history出现的概率。P(h) if (!(PH <= 1.0 && PH > 0)) { cerr << "[ERR] history prob not in (0, 1.0], gram_id = " << prefix->gram_id << ", P("; puts_gram(cerr, prefix); cerr << ") = " << PH << endl; while (prefix != NULL) { cerr << "gram_id = " << prefix->gram_id << "\t, conditional prob = " << prob_buff[prefix->gram_id] << endl; prefix = prefix->prefix; } assert(PH <= 1.0 && PH > 0); } prob_t log10_PHW = prob_buff[n->gram_id]; prob_t PHW = exp10(log10_PHW); // P(w|h) // h'是h的suffix. context[lvl] = n->word_id; prob_t log10_PH_W = wordProbBO(context + 1, lvl - 1); // P(w|h') //在sunpinyin中, log10_PH_W 不是直接获得,而是需要计算。 // language model中不一定有这个gram prob_t PH_W = exp10(log10_PH_W); // P(head(h)|tail(h)) assert(PHW < 1.0 && PHW > 0.0); assert(PH_W < 1.0 && PH_W > 0.0); assert(BOW > 0.0); assert(PA + PHW < 1.01); // %1 error rate assert(PB + PH_W < 1.01); // %1 error rate prob_t log10__BOW = log10(PA + PHW) - log10(PB + PH_W); prob_t _BOW = exp10(log10__BOW); assert(_BOW > 0.0); double deltaEntropy = -(PH * (PHW * (log10_PH_W + log10__BOW - log10_PHW) + PA * (log10__BOW - log10_BOW))); if(is_debug){ cerr << "GRAM "; puts_gram(cerr, n); cerr << " CONTEXTPROB " << log10_PH << " OLDPROB " << log10_PHW << " NEWPROB " << (log10_PH_W + log10__BOW) << " OLDBOW " << log10_BOW << " NEWBOW " << log10__BOW << " DELTA-H " << deltaEntropy << " DELTA-LOGP " << (log10_PH_W + log10__BOW - prob_buff[n->gram_id]) << " PA " << PA << " PB " << PB << " PH_W " << log10_PH_W << endl; } return deltaEntropy; }