From c38d7e5544f6673de48064dcbc6f55afc9052bb1 Mon Sep 17 00:00:00 2001 From: Davis King Date: Tue, 13 Feb 2018 07:42:16 -0500 Subject: [PATCH] Refactored the random feature selection function so that it always has bounded runtime, even with very bad parameter selections. --- dlib/image_processing/shape_predictor_trainer.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/dlib/image_processing/shape_predictor_trainer.h b/dlib/image_processing/shape_predictor_trainer.h index e48ed379a..ddf0d2b04 100644 --- a/dlib/image_processing/shape_predictor_trainer.h +++ b/dlib/image_processing/shape_predictor_trainer.h @@ -523,15 +523,18 @@ namespace dlib { const double lambda = get_lambda(); impl::split_feature feat; - double accept_prob; - do + const size_t max_iters = get_feature_pool_size()*get_feature_pool_size(); + for (size_t i = 0; i < max_iters; ++i) { - feat.idx1 = rnd.get_random_32bit_number()%get_feature_pool_size(); - feat.idx2 = rnd.get_random_32bit_number()%get_feature_pool_size(); + feat.idx1 = rnd.get_integer(get_feature_pool_size()); + feat.idx2 = rnd.get_integer(get_feature_pool_size()); + while (feat.idx1 == feat.idx2) + feat.idx2 = rnd.get_integer(get_feature_pool_size()); const double dist = length(pixel_coordinates[feat.idx1]-pixel_coordinates[feat.idx2]); - accept_prob = std::exp(-dist/lambda); + const double accept_prob = std::exp(-dist/lambda); + if (accept_prob > rnd.get_random_double()) + break; } - while(feat.idx1 == feat.idx2 || !(accept_prob > rnd.get_random_double())); feat.thresh = (rnd.get_random_double()*256 - 128)/2.0;