diff --git a/corelib/src/BayesFilter.cpp b/corelib/src/BayesFilter.cpp index a9142dc5..5d932b33 100644 --- a/corelib/src/BayesFilter.cpp +++ b/corelib/src/BayesFilter.cpp @@ -451,6 +451,7 @@ cv::Mat BayesFilter::updatePrediction(const cv::Mat & oldPrediction, UDEBUG("time getting removed ids = %fs", timer.restart()); int added = 0; + float epsilon = 0.00001f; // get ids to update std::set idsToUpdate; for(unsigned int i=0; i epsilon && j!=i && removedIds.find(oldIds[j]) == removedIds.end()) { //UDEBUG("to update id=%d from id=%d removed (value=%f)", oldIds[j], oldIds[i], ((const float *)oldPrediction.data)[i + j*cols]); idsToUpdate.insert(oldIds[j]); + ++count; } } + UDEBUG("From removed id %d, %d neighbors to update.", oldIds[i], count); } } if(iaddNeighborProb(prediction, i, neighbors, newIdToIndexMap); this->normalize(prediction, i, sum, newIds[0]<0); ++added; + int count = 0; for(std::map::iterator iter=neighbors.begin(); iter!=neighbors.end(); ++iter) { if(uContains(oldIdToIndexMap, iter->first) && removedIds.find(iter->first) == removedIds.end()) { idsToUpdate.insert(iter->first); + ++count; } } + UDEBUG("From added id %d, %d neighbors to update.", newIds[i], count); } } - UDEBUG("time getting ids to update = %fs", timer.restart()); + UDEBUG("time getting %d ids to update = %fs", idsToUpdate.size(), timer.restart()); // update modified/added ids int modified = 0; + std::set idsDone; for(std::set::iterator iter = idsToUpdate.begin(); iter!=idsToUpdate.end(); ++iter) { - std::map neighbors = memory->getNeighborsId(*iter, _predictionLC.size()-1, 0, false, false, true); - int index = newIdToIndexMap.at(*iter); - float sum = this->addNeighborProb(prediction, index, neighbors, newIdToIndexMap); - this->normalize(prediction, index, sum, newIds[0]<0); - ++modified; + if(idsDone.find(*iter) == idsDone.end() && *iter > 0) + { + std::map neighbors = memory->getNeighborsId(*iter, _predictionLC.size()-1, 0, false, false, true); + + std::list idsLoopMargin; + //filter neighbors in STM + for(std::map::iterator jter=neighbors.begin(); jter!=neighbors.end();) + { + if(memory->isInSTM(jter->first)) + { + neighbors.erase(jter++); + } + else + { + if(jter->second == 0) + { + idsLoopMargin.push_back(jter->first); + } + ++jter; + } + } + + // should at least have 1 id in idsMarginLoop + if(idsLoopMargin.size() == 0) + { + UFATAL("No 0 margin neighbor for signature %d !?!?", *iter); + } + + // same neighbor tree for loop signatures (margin = 0) + for(std::list::iterator iter = idsLoopMargin.begin(); iter!=idsLoopMargin.end(); ++iter) + { + int index = newIdToIndexMap.at(*iter); + float sum = this->addNeighborProb(prediction, index, neighbors, newIdToIndexMap); + idsDone.insert(*iter); + this->normalize(prediction, index, sum, newIds[0]<0); + ++modified; + } + } } - UDEBUG("time updating modified/added ids = %fs", timer.restart()); + UDEBUG("time updating modified/added %d ids = %fs", idsToUpdate.size(), timer.restart()); //UDEBUG("oldIds.size()=%d, oldPrediction.cols=%d, oldPrediction.rows=%d", oldIds.size(), oldPrediction.cols, oldPrediction.rows); //UDEBUG("newIdToIndexMap.size()=%d, prediction.cols=%d, prediction.rows=%d", newIdToIndexMap.size(), prediction.cols, prediction.rows); @@ -510,15 +551,22 @@ cv::Mat BayesFilter::updatePrediction(const cv::Mat & oldPrediction, { if(oldIds[i]>0 && removedIds.find(oldIds[i]) == removedIds.end() && idsToUpdate.find(oldIds[i]) == idsToUpdate.end()) { - for(int j=0; j epsilon) { //UDEBUG("i=%d, j=%d", i, j); //UDEBUG("oldIds[i]=%d, oldIds[j]=%d", oldIds[i], oldIds[j]); //UDEBUG("newIdToIndexMap.at(oldIds[i])=%d", newIdToIndexMap.at(oldIds[i])); //UDEBUG("newIdToIndexMap.at(oldIds[j])=%d", newIdToIndexMap.at(oldIds[j])); - ((float *)prediction.data)[newIdToIndexMap.at(oldIds[i]) + newIdToIndexMap.at(oldIds[j])*prediction.cols] = ((const float *)oldPrediction.data)[i + j*oldPrediction.cols]; + float v = ((const float *)oldPrediction.data)[i + j*oldPrediction.cols]; + int ii = newIdToIndexMap.at(oldIds[i]); + int jj = newIdToIndexMap.at(oldIds[j]); + ((float *)prediction.data)[ii + jj*prediction.cols] = v; + if(ii != jj) + { + ((float *)prediction.data)[jj + ii*prediction.cols] = v; + } } } ++copied; @@ -536,6 +584,7 @@ cv::Mat BayesFilter::updatePrediction(const cv::Mat & oldPrediction, for(int j=1; j0)