SVMSGD class added

This commit is contained in:
joao.faro
2015-09-02 17:14:40 +01:00
committed by Marina Noskova
parent b529b37d06
commit a2f0963d66
3 changed files with 330 additions and 10 deletions

View File

@@ -1513,6 +1513,127 @@ CV_EXPORTS void randMVNormal( InputArray mean, InputArray cov, int nsamples, Out
CV_EXPORTS void createConcentricSpheresTestSet( int nsamples, int nfeatures, int nclasses,
OutputArray samples, OutputArray responses);
/****************************************************************************************\
* Stochastic Gradient Descent SVM Classifier *
\****************************************************************************************/
/*!
@brief Stochastic Gradient Descent SVM classifier
SVMSGD provides a fast and easy-to-use implementation of the SVM classifier using the Stochastic Gradient Descent approach, as presented in @cite bottou2010large.
The gradient descent show amazing performance for large-scale problems, reducing the computing time. This allows a fast and reliable online update of the classifier for each new feature which
is fundamental when dealing with variations of data over time (like weather and illumination changes in videosurveillance, for example).
First, create the SVMSGD object. To enable the online update, a value for updateFrequency should be defined.
Then the SVM model can be trained using the train features and the correspondent labels.
After that, the label of a new feature vector can be predicted using the predict function. If the updateFrequency was defined in the constructor, the predict function will update the weights automatically.
@code
// Initialize object
SVMSGD SvmSgd;
// Train the Stochastic Gradient Descent SVM
SvmSgd.train(trainFeatures, labels);
// Predict label for the new feature vector (1xM)
predictedLabel = SvmSgd.predict(newFeatureVector);
@endcode
*/
class CV_EXPORTS_W SVMSGD {
public:
/** @brief SGDSVM constructor.
@param lambda regularization
@param learnRate learning rate
@param nIterations number of training iterations
*/
SVMSGD(float lambda = 0.000001, float learnRate = 2, uint nIterations = 100000);
/** @brief SGDSVM constructor.
@param updateFrequency online update frequency
@param learnRateDecay learn rate decay over time: learnRate = learnRate * learnDecay
@param lambda regularization
@param learnRate learning rate
@param nIterations number of training iterations
*/
SVMSGD(uint updateFrequency, float learnRateDecay = 1, float lambda = 0.000001, float learnRate = 2, uint nIterations = 100000);
virtual ~SVMSGD();
virtual SVMSGD* clone() const;
/** @brief Train the SGDSVM classifier.
The function trains the SGDSVM classifier using the train features and the correspondent labels (-1 or 1).
@param trainFeatures features used for training. Each row is a new sample.
@param labels mat (size Nx1 with N = number of features) with the label of each training feature.
*/
virtual void train(cv::Mat trainFeatures, cv::Mat labels);
/** @brief Predict the label of a new feature vector.
The function predicts and returns the label of a new feature vector, using the previously trained SVM model.
@param newFeature new feature vector used for prediction
*/
virtual float predict(cv::Mat newFeature);
/** @brief Returns the weights of the trained model.
*/
virtual std::vector<float> getWeights(){ return _weights; };
/** @brief Sets the weights of the trained model.
@param weights weights used to predict the label of a new feature vector.
*/
virtual void setWeights(std::vector<float> weights){ _weights = weights; };
private:
void updateWeights();
void generateRandomIndex();
float calcInnerProduct(float *rowDataPointer);
void updateWeights(float innerProduct, float *rowDataPointer, int label);
// Vector with SVM weights
std::vector<float> _weights;
// Random index generation
long long int _randomNumber;
unsigned int _randomIndex;
// Number of features and samples
unsigned int _nFeatures;
unsigned int _nTrainSamples;
// Parameters for learning
float _lambda; //regularization
float _learnRate; //learning rate
unsigned int _nIterations; //number of training iterations
// Vars to control the features slider matrix
bool _onlineUpdate;
bool _initPredict;
uint _slidingWindowSize;
uint _predictSlidingWindowSize;
float* _labelSlider;
float _learnRateDecay;
// Mat with features slider and correspondent counter
unsigned int _sliderCounter;
cv::Mat _featuresSlider;
};
//! @} ml
}