2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's 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.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "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 Intel Corporation or contributors 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.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
namespace cv { namespace ml {
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
ParamGrid::ParamGrid() { minVal = maxVal = 0.; logStep = 1; }
|
|
|
|
ParamGrid::ParamGrid(double _minVal, double _maxVal, double _logStep)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
minVal = std::min(_minVal, _maxVal);
|
|
|
|
maxVal = std::max(_minVal, _maxVal);
|
|
|
|
logStep = std::max(_logStep, 1.);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void StatModel::clear() {}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int StatModel::getVarCount() const { return 0; }
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
bool StatModel::train( const Ptr<TrainData>&, int )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
CV_Error(CV_StsNotImplemented, "");
|
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-08-03 05:41:09 +08:00
|
|
|
bool StatModel::train( InputArray samples, int layout, InputArray responses )
|
|
|
|
{
|
|
|
|
return train(TrainData::create(samples, layout, responses));
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
float StatModel::calcError( const Ptr<TrainData>& data, bool testerr, OutputArray _resp ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
Mat samples = data->getSamples();
|
|
|
|
int layout = data->getLayout();
|
|
|
|
Mat sidx = testerr ? data->getTestSampleIdx() : data->getTrainSampleIdx();
|
|
|
|
const int* sidx_ptr = sidx.ptr<int>();
|
|
|
|
int i, n = (int)sidx.total();
|
|
|
|
bool isclassifier = isClassifier();
|
|
|
|
Mat responses = data->getResponses();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( n == 0 )
|
|
|
|
n = data->getNSamples();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( n == 0 )
|
|
|
|
return -FLT_MAX;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
Mat resp;
|
|
|
|
if( _resp.needed() )
|
|
|
|
resp.create(n, 1, CV_32F);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
double err = 0;
|
|
|
|
for( i = 0; i < n; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int si = sidx_ptr ? sidx_ptr[i] : i;
|
|
|
|
Mat sample = layout == ROW_SAMPLE ? samples.row(si) : samples.col(si);
|
|
|
|
float val = predict(sample);
|
|
|
|
float val0 = responses.at<float>(si);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( isclassifier )
|
|
|
|
err += fabs(val - val0) > FLT_EPSILON;
|
|
|
|
else
|
|
|
|
err += (val - val0)*(val - val0);
|
|
|
|
if( resp.data )
|
|
|
|
resp.at<float>(i) = val;
|
|
|
|
/*if( i < 100 )
|
|
|
|
{
|
|
|
|
printf("%d. ref %.1f vs pred %.1f\n", i, val0, val);
|
|
|
|
}*/
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( _resp.needed() )
|
|
|
|
resp.copyTo(_resp);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
return err / n * (isclassifier ? 100 : 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void StatModel::save(const String& filename) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
FileStorage fs(filename, FileStorage::WRITE);
|
|
|
|
fs << getDefaultModelName() << "{";
|
|
|
|
write(fs);
|
|
|
|
fs << "}";
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculates upper triangular matrix S, where A is a symmetrical matrix A=S'*S */
|
2014-07-30 03:54:23 +08:00
|
|
|
static void Cholesky( const Mat& A, Mat& S )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
CV_Assert(A.type() == CV_32F);
|
|
|
|
|
|
|
|
int dim = A.rows;
|
|
|
|
S.create(dim, dim, CV_32F);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
int i, j, k;
|
|
|
|
|
|
|
|
for( i = 0; i < dim; i++ )
|
|
|
|
{
|
|
|
|
for( j = 0; j < i; j++ )
|
2014-07-30 03:54:23 +08:00
|
|
|
S.at<float>(i,j) = 0.f;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
float sum = 0.f;
|
2010-05-12 01:44:00 +08:00
|
|
|
for( k = 0; k < i; k++ )
|
2014-07-30 03:54:23 +08:00
|
|
|
{
|
|
|
|
float val = S.at<float>(k,i);
|
|
|
|
sum += val*val;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
S.at<float>(i,i) = std::sqrt(std::max(A.at<float>(i,i) - sum, 0.f));
|
|
|
|
float ival = 1.f/S.at<float>(i, i);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
for( j = i + 1; j < dim; j++ )
|
|
|
|
{
|
|
|
|
sum = 0;
|
|
|
|
for( k = 0; k < i; k++ )
|
2014-07-30 03:54:23 +08:00
|
|
|
sum += S.at<float>(k, i) * S.at<float>(k, j);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
S.at<float>(i, j) = (A.at<float>(i, j) - sum)*ival;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generates <sample> from multivariate normal distribution, where <mean> - is an
|
|
|
|
average row vector, <cov> - symmetric covariation matrix */
|
2014-07-30 03:54:23 +08:00
|
|
|
void randMVNormal( InputArray _mean, InputArray _cov, int nsamples, OutputArray _samples )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
Mat mean = _mean.getMat(), cov = _cov.getMat();
|
|
|
|
int dim = (int)mean.total();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
_samples.create(nsamples, dim, CV_32F);
|
|
|
|
Mat samples = _samples.getMat();
|
|
|
|
randu(samples, 0., 1.);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
Mat utmat;
|
|
|
|
Cholesky(cov, utmat);
|
|
|
|
int flags = mean.cols == 1 ? 0 : GEMM_3_T;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( int i = 0; i < nsamples; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
Mat sample = samples.row(i);
|
|
|
|
gemm(sample, utmat, 1, mean, 1, sample, flags);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
}}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/* End of file */
|