/* * Gaussian Kernel Regression model class * * Author: Andrew Salamon * * Copyright (c) 2008 Machine Perception Laboratory * University of California San Diego. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #ifndef __GKR_MODEL_H__ #define __GKR_MODEL_H__ #include #include #include #include #ifdef XCODE #include #include #else #include "simplematrix.hpp" #include "simplematrix_ser.hpp" #endif #include #include //#include //#include //#include //#include //#include //#include //#include //#include #include namespace GKR { /** Gaussian Kernel Regression model class. * This class generates a model that can be used to predict AU baselines based on pose data. * It's a two step process: generate the model based on pose data and 'neutral' AU data, * then use the model to adjust runtime AU data based on the runtime pose data. */ class Model { public: typedef std::map< std::string, Model > Container; public: Model(); Model( const std::string &_ID ); Model( const Model &other ); ~Model() { } bool train( std::vector &au, Matrix2d &poses, int nGM = 8 ); bool predict( const Matrix2d &poses ); std::pair CKF( bool foundFace, float au ); ///< Conditional Kalman Filter. Returns filtered AU and confidence. Input au can be raw, or GKR baselined. float logistic( float au ); void setID( const std::string &_id ) { ID = _id; } const std::string &getID() const { return ID; } const Matrix2d &getGmean() const { return Gmean; } const Matrix2d &getGsigma() const { return Gsigma; } const Matrix2d &getC() const { return c; } const Matrix2d &getPrediction() const { return prediction; } void clear(); ///< Clears the GKR (but not CKF or logistic) values. bool operator==( const Model &other ) const; void operator=( const Model &other ); void initLogisticValues( float _alpha = -4.2, float _beta = 4.6 ); void initTemporalValues( float _S_x = 1.0, float _S_y = 1.0, float _S_0 = 25.0, float _Y_0 = -2.0 ); std::vector getCKFValues(); // Order is: alpha, beta, S_x, S_y, S_0, Y_0, private: Matrix2d Gmean; Matrix2d Gsigma; Matrix2d c; Matrix2d prediction; std::string ID; // CKF (i.e. temporal) related members float S_x; float S_y; float S_0; float Y_0; float S_t; // filter variance float K_t; // filter gain float Y_t; // filter output bool firstT; // logistic function related members float alpha; float beta; void updateVariance( bool foundFace ); void updateGain( bool foundFace ); private: std::vector Evec; Matrix2d Emat; const float sqrt2pi; void minAndMax( Matrix2d &poses, std::vector &min, std::vector &max ); void initE(); // Initialize Evec and Emat after un-serialization. // for serialization friend class boost::serialization::access; template void serialize(Archive &ar, const unsigned int version) { using boost::serialization::make_nvp; ar & make_nvp( "Gmean", Gmean ); ar & make_nvp( "Gsigma", Gsigma ); ar & make_nvp( "c", c ); ar & make_nvp( "ID", ID ); initE(); } }; void printMatrix( const Matrix2d &matrix, std::string title = std::string("") ); } // end namespace GKR #endif