37 EQUAL_RESULTS_NUMBER_(equal_results_number),
38 IMPORTANCE_RESAMPLED_SIZE_(importance_resampled_size),
39 shared_recognition_results_manager_ptr_(shared_recognition_results_ptr)
47 ROS_INFO(
"\n--------------------------------------------------\n");
48 ROS_INFO(
"Resampler::drawSamples() is called.");
51 ROS_INFO_STREAM(
"Recognition results in shared memory before sampling: " << SCENE_RECOGNITION_RESULT_NUMBER);
55 map<string, unsigned int>::iterator resultIt;
60 map<string, unsigned int> patternToResultsNumber;
62 for(RecognitionResultPtr result : allResults) {
64 resultIt = patternToResultsNumber.find(result->patternName);
66 if(resultIt != patternToResultsNumber.end())
69 patternToResultsNumber.insert(make_pair(result->patternName, 1));
73 for(pair<const string, unsigned int>& pair : patternToResultsNumber)
74 ROS_INFO_STREAM(pair.second <<
" recognition results in shared memory for scene: " << pair.first);
81 list<RecognitionResult> results_for_sampling;
82 list<RecognitionResult> surplus_results;
83 double sum_of_confidence = 0.0;
87 auto minIt = min_element(patternToResultsNumber.begin(), patternToResultsNumber.end(),
88 [](decltype(patternToResultsNumber)::value_type& l, decltype(patternToResultsNumber)::value_type& r)
89 ->
bool {
return l.second < r.second; });
91 ROS_INFO_STREAM(minIt->second <<
" results per scene are considered as input for importance sampling.");
92 ROS_INFO(
"The following results are considered as input for importance sampling: ");
94 for(resultIt = patternToResultsNumber.begin(); resultIt != patternToResultsNumber.end(); resultIt++)
95 resultIt->second = minIt->second;
97 unsigned int num_results_for_sampling = minIt->second * patternToResultsNumber.size();
99 while(results_for_sampling.size() < num_results_for_sampling) {
103 if(patternToResultsNumber.at(recognition_result.patternName)){
105 sum_of_confidence += recognition_result.confidence;
106 results_for_sampling.push_back(recognition_result);
107 patternToResultsNumber.at(recognition_result.patternName)--;
110 surplus_results.push_back(recognition_result);
119 for (
unsigned int i = 0; i < SCENE_RECOGNITION_RESULT_NUMBER; ++i)
123 sum_of_confidence += recognition_result.confidence;
124 results_for_sampling.push_back(recognition_result);
126 ROS_INFO(
"All results are considered as input for importance sampling.");
129 const int ACTUAL_SAMPLE_SIZE = results_for_sampling.size();
136 double accumulated_confidence_per_result[ACTUAL_SAMPLE_SIZE];
137 list<RecognitionResult>::iterator results_for_sampling_it = results_for_sampling.begin();
139 accumulated_confidence_per_result[0] = results_for_sampling_it->confidence / sum_of_confidence;
140 for (
int i = 1; i < ACTUAL_SAMPLE_SIZE; ++i)
142 results_for_sampling_it++;
143 RecognitionResult recognition_result = (*results_for_sampling_it);
144 accumulated_confidence_per_result[i] =
145 (recognition_result.confidence / sum_of_confidence) +
146 accumulated_confidence_per_result[i - 1];
148 if (accumulated_confidence_per_result[ACTUAL_SAMPLE_SIZE - 1] != 1.0)
149 accumulated_confidence_per_result[ACTUAL_SAMPLE_SIZE - 1] = 1.0;
151 ROS_INFO_STREAM(
"These are the recognition results from importance sampling.");
153 vector<RecognitionResult> resampled_results;
154 vector<list<RecognitionResult>::iterator> resultsToBeDeleted;
159 double slot = rand() / double(RAND_MAX);
161 results_for_sampling_it = results_for_sampling.begin();
162 while (slot > accumulated_confidence_per_result[index])
165 results_for_sampling_it++;
168 RecognitionResult recognition_result = *results_for_sampling_it;
170 resampled_results.push_back(recognition_result);
172 if (std::find(resultsToBeDeleted.begin(), resultsToBeDeleted.end(), results_for_sampling_it) == resultsToBeDeleted.end()) {
173 resultsToBeDeleted.push_back(results_for_sampling_it);
177 ROS_INFO_STREAM(
"We sampled " << resampled_results.size() <<
" results.");
182 for (list<RecognitionResult>::iterator delIt : resultsToBeDeleted)
183 results_for_sampling.erase(delIt);
185 results_for_sampling.insert(results_for_sampling.end(), surplus_results.begin(), surplus_results.end());
186 results_for_sampling.sort([](
const RecognitionResult & a,
const RecognitionResult & b) ->
bool 188 return a.confidence > b.confidence;
190 vector<RecognitionResultPtr> remainingResults;
193 list<RecognitionResult>::reverse_iterator reverseResultIt;
195 for(reverseResultIt = results_for_sampling.rbegin(); reverseResultIt != results_for_sampling.rend();
197 remainingResults.push_back(RecognitionResultPtr(
new RecognitionResult(*reverseResultIt)));
203 ROS_INFO(
"\n--------------------------------------------------\n");
204 return resampled_results;
const SharedRecognitionResultsManagerPtr shared_recognition_results_manager_ptr_
#define ROS_DEBUG_STREAM_NAMED(name, args)
const unsigned int IMPORTANCE_RESAMPLED_SIZE_
bool recognitionResultsAvailable()
#define ROS_DEBUG_STREAM(args)
std::vector< ISM::RecognitionResult > drawSamples()
#define ROS_INFO_STREAM(args)
unsigned int getImportanceResampledSize() const
const bool EQUAL_RESULTS_NUMBER_