ImFusion SDK 4.3
RANSAC< Dataset, Hypothesis > Class Template Reference

#include <ImFusion/Base/RANSAC.h>

This class implements a more generalized form of "Random Sample Consensus" algorithm. More...

Detailed Description

template<typename Dataset, typename Hypothesis>
class ImFusion::RANSAC< Dataset, Hypothesis >

This class implements a more generalized form of "Random Sample Consensus" algorithm.

It estimates parameters of a mathematical model from a set of observed data which contains outliers. Differences to regular RANSAC: Agreement of individual data points with the model is a continuous variable instead of boolean, allows assigning different importance to data points. Quality of a parameter set is measured by the sum of the agreement to individual points. This parameter quality measurement can be performed on a subset of the dataset to save computation time. Overall this version is more fuzzy/gray than the regular in-or-out RANSAC. For traditional RANSAC have the model return either 1.0 or 0.0 for the agreement of a data point to the model and set the size of the validation set to the entire dataset.

Template Parameters
Datasethas .size()
Hypothesis

Classes

struct  Result
 

Public Types

typedef std::function< bool(const Dataset &dataset, const std::vector< size_t > &indices, Hypothesis &hypothesisOut)> FitFunction
 Function that fits an hypothesis to a subset of the data.
 
typedef std::function< double(const Dataset &dataset, size_t index, const Hypothesis &hypothesis, double eps)> ScoreFunction
 A function that scores an hypothesis on a data point.
 
typedef std::function< Hypothesis(const Dataset &dataset, const Result &res, double eps)> RefineFunction
 A function that refines an hypothesis using the whole dataset.
 

Public Member Functions

 RANSAC (const Dataset &dataset, const FitFunction &fit, const ScoreFunction &score)
 
bool compute (int sampleSize, int keepBest=1, const std::vector< double > &weights={})
 Computes the RANSAC estimation.
 
void refine (double threshold=0.5)
 Refines the current results over the whole dataset, scores and sort them again.
 
void refine (const RefineFunction &computeRefinement)
 Refines the current results over the whole dataset, scores and sort them again.
 
std::vector< Hypothesis > hypotheses () const
 Vector of all the kept hypotheses ordered from best to worse.
 
Hypothesis best () const
 Best hypothesis computed by the algorithm.
 
std::vector< bool > inliers (double threshold=0.5, int which=0)
 Computes a vector of booleans where the i-th element is true if the i-th element of the dataset is an inlier.
 
size_t numInliers (double threshold=0.5, int which=0)
 Returns the number of inliers.
 
std::vector< Resultresults () const
 Vector of results ordered from best to worse. Each result contains an hypothesis, the score for each tested item and the total score.
 
void setMaxIterations (size_t iterations)
 Sets the maximum number of iterations (default: 100).
 
void setRequiredMatchingQuality (double q)
 Sets the required quality of the matching between parameters and testing set for an early acceptance (default: number of data points).
 
void setRandomSeed (uint32_t seed)
 Sets the seed that is used to initialize the random generator (default: 0).
 
void setEps (double eps)
 Set threshold for inliers (default: 0.1)
 
void setStopValidationEarly (bool v)
 If set to true, the algorithm will stop accumulating scores for the validation data as soon as it's clear that the total score will not be greater than the result with the worst score so far.
 
bool stopValidationEarly () const
 
void setValidationSetSize (size_t size)
 Controls how many of the data items are used to evaluate parameter quality (default: all available).
 
Hypothesis fitEntireDataset () const
 Runs fitFunction on the entire dataset.
 

Protected Member Functions

void computeScores (int first=0, int last=-1)
 Recomputes validation scores on all results that where not validated on the entire dataset.
 

Protected Attributes

std::vector< Resultm_results
 
const Datasetm_dataset
 
FitFunction m_fit
 
ScoreFunction m_score
 
size_t m_validationSetSize = std::numeric_limits<size_t>::max()
 
double m_eps = 0.1
 
size_t m_maxIterations = 100
 
uint32_t m_seed = 42
 
double m_requiredMatchingQuality = -1
 
bool m_stopValidationEarly = true
 
std::unique_ptr< Multinomialm_multinomial
 

Member Typedef Documentation

◆ FitFunction

template<typename Dataset, typename Hypothesis>
typedef std::function<bool(const Dataset& dataset, const std::vector<size_t>& indices, Hypothesis& hypothesisOut)> FitFunction

Function that fits an hypothesis to a subset of the data.

Parameters
datasetreference to the entire dataset
indicesvector containing the indices of the elements that should be used to fit the hypothesis
hypothesisOutreference that should be set to the fitted hypothesis
Returns
true if fitting successful, false otherwise. If false is returned the scoring phase is skipped for this hypothesis

◆ ScoreFunction

template<typename Dataset, typename Hypothesis>
typedef std::function<double(const Dataset& dataset, size_t index, const Hypothesis& hypothesis, double eps)> ScoreFunction

A function that scores an hypothesis on a data point.

Parameters
datasetreference to the entire dataset
indexthe index of the data point that should be used for scoring
hypothesisthe hypothesis that is being scored
epsthe current value of the parameter eps of RANSAC
Returns
the score in range [0,1] or other custom range for the current data point. The higher scores indicate inliers, the lower - outliers. If the range is not [0,1], stopValidationEarly must be set to false!

Constructor & Destructor Documentation

◆ RANSAC()

template<typename Dataset, typename Hypothesis>
RANSAC ( const Dataset & dataset,
const FitFunction & fit,
const ScoreFunction & score )
inline
Parameters
dataset
fitA function that fits a hypothesis to a subset of the data
scoreA function that scores a hypothesis on a data point

Member Function Documentation

◆ compute()

template<typename Dataset, typename Hypothesis>
bool compute ( int sampleSize,
int keepBest = 1,
const std::vector< double > & weights = {} )

Computes the RANSAC estimation.

Parameters
sampleSizeNumber of datapoints to sample at each iteration to fit an hypothesis
keepBestMaximum number of best hypotheses that should be kept
weightsOptional weights for each element in the dataset
Returns
true if fitting was possible, false if it failed. True doesn't mean that the RequiredMatchingQuality has been reached

◆ refine() [1/2]

template<typename Dataset, typename Hypothesis>
void refine ( double threshold = 0.5)

Refines the current results over the whole dataset, scores and sort them again.

By default calls FitFunction with indices of all the inliers

Parameters
thresholdAn element of the dataset is considered an inlier if its score is > threshold

◆ refine() [2/2]

template<typename Dataset, typename Hypothesis>
void refine ( const RefineFunction & computeRefinement)

Refines the current results over the whole dataset, scores and sort them again.

Parameters
computeRefinementfunction that takes the dataset, the result to be refined and eps and returns the refined hypothesis

◆ inliers()

template<typename Dataset, typename Hypothesis>
std::vector< bool > inliers ( double threshold = 0.5,
int which = 0 )

Computes a vector of booleans where the i-th element is true if the i-th element of the dataset is an inlier.

Parameters
thresholdAn element of the dataset is considered an inlier if its score is > threshold
whichSpecifies which hypothesis to use (0 = best)

◆ numInliers()

template<typename Dataset, typename Hypothesis>
size_t numInliers ( double threshold = 0.5,
int which = 0 )

Returns the number of inliers.

Parameters
thresholdAn element of the dataset is considered an inlier if its score is > threshold
whichSpecifies which hypothesis to use (0 = best)

◆ setRequiredMatchingQuality()

template<typename Dataset, typename Hypothesis>
void setRequiredMatchingQuality ( double q)
inline

Sets the required quality of the matching between parameters and testing set for an early acceptance (default: number of data points).

Once this quality is reached, the iteration is aborted.

◆ setRandomSeed()

template<typename Dataset, typename Hypothesis>
void setRandomSeed ( uint32_t seed)
inline

Sets the seed that is used to initialize the random generator (default: 0).

This can be used to reproduce results.

◆ setStopValidationEarly()

template<typename Dataset, typename Hypothesis>
void setStopValidationEarly ( bool v)
inline

If set to true, the algorithm will stop accumulating scores for the validation data as soon as it's clear that the total score will not be greater than the result with the worst score so far.

Might speed up the computation. Warning: only works as expected if the score per sample is in [0,1] range. Should be set to false otherwise.

◆ setValidationSetSize()

template<typename Dataset, typename Hypothesis>
void setValidationSetSize ( size_t size)
inline

Controls how many of the data items are used to evaluate parameter quality (default: all available).

Can be used to restrict the computational load when comparing large datasets to fitted parameters. Setting the validation set size to 0 will reset it to use all available data.


The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close