From cedc430ef0b991e148ac7cf1bb44ce95e8dd363a Mon Sep 17 00:00:00 2001 From: Tadas Baltrusaitis Date: Sat, 30 Sep 2017 21:11:37 +0100 Subject: [PATCH] Simplifying the code a bit with moving to floats instead of doubles for likelihoods --- exe/FaceLandmarkVid/FaceLandmarkVid.cpp | 2 +- .../include/LandmarkDetectorFunc.h | 4 ++-- .../include/LandmarkDetectorModel.h | 8 ++++---- .../src/LandmarkDetectorFunc.cpp | 20 +++++++++---------- .../src/LandmarkDetectorModel.cpp | 10 +++++----- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/exe/FaceLandmarkVid/FaceLandmarkVid.cpp b/exe/FaceLandmarkVid/FaceLandmarkVid.cpp index fcb405b0..e7260414 100644 --- a/exe/FaceLandmarkVid/FaceLandmarkVid.cpp +++ b/exe/FaceLandmarkVid/FaceLandmarkVid.cpp @@ -87,7 +87,7 @@ double fps_tracker = -1.0; int64 t0 = 0; // Visualising the results -void visualise_tracking(cv::Mat& captured_image, const LandmarkDetector::CLNF& face_model, const LandmarkDetector::FaceModelParameters& det_parameters, cv::Point3f gazeDirection0, cv::Point3f gazeDirection1, int frame_count, double fx, double fy, double cx, double cy) +void visualise_tracking(cv::Mat& captured_image, const LandmarkDetector::CLNF& face_model, const LandmarkDetector::FaceModelParameters& det_parameters, cv::Point3f gazeDirection0, cv::Point3f gazeDirection1, int frame_count, float fx, float fy, float cx, float cy) { // Drawing the facial landmarks on the face and the bounding box around it if tracking is successful and initialised diff --git a/lib/local/LandmarkDetector/include/LandmarkDetectorFunc.h b/lib/local/LandmarkDetector/include/LandmarkDetectorFunc.h index 9c1aa8c0..4ba381d5 100644 --- a/lib/local/LandmarkDetector/include/LandmarkDetectorFunc.h +++ b/lib/local/LandmarkDetector/include/LandmarkDetectorFunc.h @@ -70,10 +70,10 @@ namespace LandmarkDetector // Return the current estimate of the head pose in world coordinates with camera at origin (0,0,0) // The format returned is [Tx, Ty, Tz, Eul_x, Eul_y, Eul_z] - cv::Vec6d GetPose(const CLNF& clnf_model, double fx, double fy, double cx, double cy); + cv::Vec6d GetPose(const CLNF& clnf_model, float fx, float fy, float cx, float cy); // Return the current estimate of the head pose in world coordinates with camera at origin (0,0,0), but with rotation representing if the head is looking at the camera // The format returned is [Tx, Ty, Tz, Eul_x, Eul_y, Eul_z] - cv::Vec6d GetPoseWRTCamera(const CLNF& clnf_model, double fx, double fy, double cx, double cy); + cv::Vec6d GetPoseWRTCamera(const CLNF& clnf_model, float fx, float fy, float cx, float cy); } #endif diff --git a/lib/local/LandmarkDetector/include/LandmarkDetectorModel.h b/lib/local/LandmarkDetector/include/LandmarkDetectorModel.h index 5d1a8a54..33647309 100644 --- a/lib/local/LandmarkDetector/include/LandmarkDetectorModel.h +++ b/lib/local/LandmarkDetector/include/LandmarkDetectorModel.h @@ -123,8 +123,8 @@ public: cv::Mat_ detected_landmarks; // The landmark detection likelihoods (combined and per patch expert) - double model_likelihood; - cv::Mat_ landmark_likelihoods; + float model_likelihood; + cv::Mat_ landmark_likelihoods; // Keeping track of how many frames the tracker has failed in so far when tracking in videos // This is useful for knowing when to initialise and reinitialise tracking @@ -194,8 +194,8 @@ private: void NonVectorisedMeanShift_precalc_kde(cv::Mat_& out_mean_shifts, const vector >& patch_expert_responses, const cv::Mat_ &dxs, const cv::Mat_ &dys, int resp_size, float a, int scale, int view_id, map >& mean_shifts); // The actual model optimisation (update step), returns the model likelihood - double NU_RLMS(cv::Vec6d& final_global, cv::Mat_& final_local, const vector >& patch_expert_responses, const cv::Vec6d& initial_global, const cv::Mat_& initial_local, - const cv::Mat_& base_shape, const cv::Matx22d& sim_img_to_ref, const cv::Matx22f& sim_ref_to_img, int resp_size, int view_idx, bool rigid, int scale, cv::Mat_& landmark_lhoods, const FaceModelParameters& parameters, bool compute_lhood); + float NU_RLMS(cv::Vec6d& final_global, cv::Mat_& final_local, const vector >& patch_expert_responses, const cv::Vec6d& initial_global, const cv::Mat_& initial_local, + const cv::Mat_& base_shape, const cv::Matx22d& sim_img_to_ref, const cv::Matx22f& sim_ref_to_img, int resp_size, int view_idx, bool rigid, int scale, cv::Mat_& landmark_lhoods, const FaceModelParameters& parameters, bool compute_lhood); // Generating the weight matrix for the Weighted least squares void GetWeightMatrix(cv::Mat_& WeightMatrix, int scale, int view_id, const FaceModelParameters& parameters); diff --git a/lib/local/LandmarkDetector/src/LandmarkDetectorFunc.cpp b/lib/local/LandmarkDetector/src/LandmarkDetectorFunc.cpp index 6dff28cf..cd5027a2 100644 --- a/lib/local/LandmarkDetector/src/LandmarkDetectorFunc.cpp +++ b/lib/local/LandmarkDetector/src/LandmarkDetectorFunc.cpp @@ -51,7 +51,7 @@ using namespace LandmarkDetector; // which is only correct close to the centre of the image // This method returns a corrected pose estimate with respect to world coordinates with camera at origin (0,0,0) // The format returned is [Tx, Ty, Tz, Eul_x, Eul_y, Eul_z] -cv::Vec6d LandmarkDetector::GetPose(const CLNF& clnf_model, double fx, double fy, double cx, double cy) +cv::Vec6d LandmarkDetector::GetPose(const CLNF& clnf_model, float fx, float fy, float cx, float cy) { if (!clnf_model.detected_landmarks.empty() && clnf_model.params_global[0] != 0) { @@ -97,7 +97,7 @@ cv::Vec6d LandmarkDetector::GetPose(const CLNF& clnf_model, double fx, double fy // Getting a head pose estimate from the currently detected landmarks, with appropriate correction due to perspective projection // This method returns a corrected pose estimate with respect to a point camera (NOTE not the world coordinates), which is useful to find out if the person is looking at a camera // The format returned is [Tx, Ty, Tz, Eul_x, Eul_y, Eul_z] -cv::Vec6d LandmarkDetector::GetPoseWRTCamera(const CLNF& clnf_model, double fx, double fy, double cx, double cy) +cv::Vec6d LandmarkDetector::GetPoseWRTCamera(const CLNF& clnf_model, float fx, float fy, float cx, float cy) { if (!clnf_model.detected_landmarks.empty() && clnf_model.params_global[0] != 0) { @@ -310,7 +310,7 @@ bool LandmarkDetector::DetectLandmarksInVideo(const cv::Mat &image, CLNF& clnf_m cv::Mat_ params_local_init = clnf_model.params_local.clone(); double likelihood_init = clnf_model.model_likelihood; cv::Mat_ detected_landmarks_init = clnf_model.detected_landmarks.clone(); - cv::Mat_ landmark_likelihoods_init = clnf_model.landmark_likelihoods.clone(); + cv::Mat_ landmark_likelihoods_init = clnf_model.landmark_likelihoods.clone(); // Use the detected bounding box and empty local parameters clnf_model.params_local.setTo(0); @@ -402,7 +402,7 @@ bool DetectLandmarksInImageMultiHypBasic(const cv::Mat_ &grayscale_image, cv::Vec6d best_global_parameters; cv::Mat_ best_local_parameters; cv::Mat_ best_detected_landmarks; - cv::Mat_ best_landmark_likelihoods; + cv::Mat_ best_landmark_likelihoods; bool best_success; // The hierarchical model parameters @@ -410,7 +410,7 @@ bool DetectLandmarksInImageMultiHypBasic(const cv::Mat_ &grayscale_image, vector best_global_parameters_h(clnf_model.hierarchical_models.size()); vector> best_local_parameters_h(clnf_model.hierarchical_models.size()); vector> best_detected_landmarks_h(clnf_model.hierarchical_models.size()); - vector> best_landmark_likelihoods_h(clnf_model.hierarchical_models.size()); + vector> best_landmark_likelihoods_h(clnf_model.hierarchical_models.size()); for (size_t hypothesis = 0; hypothesis < rotation_hypotheses.size(); ++hypothesis) { @@ -504,7 +504,7 @@ bool DetectLandmarksInImageMultiHypEarlyTerm(const cv::Mat_ &grayscale_im bool success = false; // Keeping track of converges - vector likelihoods; + vector likelihoods; vector global_parameters; vector> local_parameters; @@ -524,7 +524,7 @@ bool DetectLandmarksInImageMultiHypEarlyTerm(const cv::Mat_ &grayscale_im // Perform landmark detection in first scale clnf_model.DetectLandmarks(grayscale_image, params); - double lhood = clnf_model.model_likelihood * clnf_model.patch_experts.early_term_weights[clnf_model.view_used] + clnf_model.patch_experts.early_term_biases[clnf_model.view_used]; + float lhood = clnf_model.model_likelihood * clnf_model.patch_experts.early_term_weights[clnf_model.view_used] + clnf_model.patch_experts.early_term_biases[clnf_model.view_used]; // If likelihood higher than cutoff continue on this model if (lhood > clnf_model.patch_experts.early_term_cutoffs[clnf_model.view_used]) @@ -550,11 +550,11 @@ bool DetectLandmarksInImageMultiHypEarlyTerm(const cv::Mat_ &grayscale_im { // Store the current best estimate - double best_likelihood; + float best_likelihood; cv::Vec6d best_global_parameters; cv::Mat_ best_local_parameters; cv::Mat_ best_detected_landmarks; - cv::Mat_ best_landmark_likelihoods; + cv::Mat_ best_landmark_likelihoods; bool best_success; // The hierarchical model parameters @@ -562,7 +562,7 @@ bool DetectLandmarksInImageMultiHypEarlyTerm(const cv::Mat_ &grayscale_im vector best_global_parameters_h(clnf_model.hierarchical_models.size()); vector> best_local_parameters_h(clnf_model.hierarchical_models.size()); vector> best_detected_landmarks_h(clnf_model.hierarchical_models.size()); - vector> best_landmark_likelihoods_h(clnf_model.hierarchical_models.size()); + vector> best_landmark_likelihoods_h(clnf_model.hierarchical_models.size()); // Sort the likelihoods and pick the best top 3 models vector indices = sort_indexes(likelihoods); diff --git a/lib/local/LandmarkDetector/src/LandmarkDetectorModel.cpp b/lib/local/LandmarkDetector/src/LandmarkDetectorModel.cpp index 35be4199..f1385da9 100644 --- a/lib/local/LandmarkDetector/src/LandmarkDetectorModel.cpp +++ b/lib/local/LandmarkDetector/src/LandmarkDetectorModel.cpp @@ -938,8 +938,8 @@ void CLNF::GetWeightMatrix(cv::Mat_& WeightMatrix, int scale, int view_id } //============================================================================= -double CLNF::NU_RLMS(cv::Vec6d& final_global, cv::Mat_& final_local, const vector >& patch_expert_responses, const cv::Vec6d& initial_global, const cv::Mat_& initial_local, - const cv::Mat_& base_shape, const cv::Matx22d& sim_img_to_ref, const cv::Matx22f& sim_ref_to_img, int resp_size, int view_id, bool rigid, int scale, cv::Mat_& landmark_lhoods, +float CLNF::NU_RLMS(cv::Vec6d& final_global, cv::Mat_& final_local, const vector >& patch_expert_responses, const cv::Vec6d& initial_global, const cv::Mat_& initial_local, + const cv::Mat_& base_shape, const cv::Matx22d& sim_img_to_ref, const cv::Matx22f& sim_ref_to_img, int resp_size, int view_id, bool rigid, int scale, cv::Mat_& landmark_lhoods, const FaceModelParameters& parameters, bool compute_lhood) { @@ -1088,11 +1088,11 @@ double CLNF::NU_RLMS(cv::Vec6d& final_global, cv::Mat_& final_local, con } // compute the log likelihood - double loglhood = 0; + float loglhood = 0; if(compute_lhood) { - landmark_lhoods = cv::Mat_(n, 1, -1e8); + landmark_lhoods = cv::Mat_(n, 1, -1e8); for(int i = 0; i < n; i++) { @@ -1126,7 +1126,7 @@ double CLNF::NU_RLMS(cv::Vec6d& final_global, cv::Mat_& final_local, con sum += v; } } - landmark_lhoods.at(i,0) = (double)sum; + landmark_lhoods.at(i,0) = sum; // the offset is there for numerical stability loglhood += log(sum + 1e-8);