1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
14 //
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
17 //
18 //   * Redistribution's of source code must retain the above copyright notice,
19 //     this list of conditions and the following disclaimer.
20 //
21 //   * Redistribution's in binary form must reproduce the above copyright notice,
22 //     this list of conditions and the following disclaimer in the documentation
23 //     and/or other materials provided with the distribution.
24 //
25 //   * The name of Intel Corporation may not be used to endorse or promote products
26 //     derived from this software without specific prior written permission.
27 //
28 // This software is provided by the copyright holders and contributors "as is" and
29 // any express or implied warranties, including, but not limited to, the implied
30 // warranties of merchantability and fitness for a particular purpose are disclaimed.
31 // In no event shall the Intel Corporation or contributors be liable for any direct,
32 // indirect, incidental, special, exemplary, or consequential damages
33 // (including, but not limited to, procurement of substitute goods or services;
34 // loss of use, data, or profits; or business interruption) however caused
35 // and on any theory of liability, whether in contract, strict liability,
36 // or tort (including negligence or otherwise) arising in any way out of
37 // the use of this software, even if advised of the possibility of such damage.
38 //
39 //M*/
40 
41 #include "precomp.hpp"
42 
43 namespace cv { namespace ml {
44 
ParamGrid()45 ParamGrid::ParamGrid() { minVal = maxVal = 0.; logStep = 1; }
ParamGrid(double _minVal,double _maxVal,double _logStep)46 ParamGrid::ParamGrid(double _minVal, double _maxVal, double _logStep)
47 {
48     minVal = std::min(_minVal, _maxVal);
49     maxVal = std::max(_minVal, _maxVal);
50     logStep = std::max(_logStep, 1.);
51 }
52 
empty() const53 bool StatModel::empty() const { return !isTrained(); }
54 
getVarCount() const55 int StatModel::getVarCount() const { return 0; }
56 
train(const Ptr<TrainData> &,int)57 bool StatModel::train( const Ptr<TrainData>&, int )
58 {
59     CV_Error(CV_StsNotImplemented, "");
60     return false;
61 }
62 
train(InputArray samples,int layout,InputArray responses)63 bool StatModel::train( InputArray samples, int layout, InputArray responses )
64 {
65     return train(TrainData::create(samples, layout, responses));
66 }
67 
calcError(const Ptr<TrainData> & data,bool testerr,OutputArray _resp) const68 float StatModel::calcError( const Ptr<TrainData>& data, bool testerr, OutputArray _resp ) const
69 {
70     Mat samples = data->getSamples();
71     int layout = data->getLayout();
72     Mat sidx = testerr ? data->getTestSampleIdx() : data->getTrainSampleIdx();
73     const int* sidx_ptr = sidx.ptr<int>();
74     int i, n = (int)sidx.total();
75     bool isclassifier = isClassifier();
76     Mat responses = data->getResponses();
77 
78     if( n == 0 )
79         n = data->getNSamples();
80 
81     if( n == 0 )
82         return -FLT_MAX;
83 
84     Mat resp;
85     if( _resp.needed() )
86         resp.create(n, 1, CV_32F);
87 
88     double err = 0;
89     for( i = 0; i < n; i++ )
90     {
91         int si = sidx_ptr ? sidx_ptr[i] : i;
92         Mat sample = layout == ROW_SAMPLE ? samples.row(si) : samples.col(si);
93         float val = predict(sample);
94         float val0 = responses.at<float>(si);
95 
96         if( isclassifier )
97             err += fabs(val - val0) > FLT_EPSILON;
98         else
99             err += (val - val0)*(val - val0);
100         if( !resp.empty() )
101             resp.at<float>(i) = val;
102         /*if( i < 100 )
103         {
104             printf("%d. ref %.1f vs pred %.1f\n", i, val0, val);
105         }*/
106     }
107 
108     if( _resp.needed() )
109         resp.copyTo(_resp);
110 
111     return (float)(err / n * (isclassifier ? 100 : 1));
112 }
113 
114 /* Calculates upper triangular matrix S, where A is a symmetrical matrix A=S'*S */
Cholesky(const Mat & A,Mat & S)115 static void Cholesky( const Mat& A, Mat& S )
116 {
117     CV_Assert(A.type() == CV_32F);
118 
119     int dim = A.rows;
120     S.create(dim, dim, CV_32F);
121 
122     int i, j, k;
123 
124     for( i = 0; i < dim; i++ )
125     {
126         for( j = 0; j < i; j++ )
127             S.at<float>(i,j) = 0.f;
128 
129         float sum = 0.f;
130         for( k = 0; k < i; k++ )
131         {
132             float val = S.at<float>(k,i);
133             sum += val*val;
134         }
135 
136         S.at<float>(i,i) = std::sqrt(std::max(A.at<float>(i,i) - sum, 0.f));
137         float ival = 1.f/S.at<float>(i, i);
138 
139         for( j = i + 1; j < dim; j++ )
140         {
141             sum = 0;
142             for( k = 0; k < i; k++ )
143                 sum += S.at<float>(k, i) * S.at<float>(k, j);
144 
145             S.at<float>(i, j) = (A.at<float>(i, j) - sum)*ival;
146         }
147     }
148 }
149 
150 /* Generates <sample> from multivariate normal distribution, where <mean> - is an
151    average row vector, <cov> - symmetric covariation matrix */
randMVNormal(InputArray _mean,InputArray _cov,int nsamples,OutputArray _samples)152 void randMVNormal( InputArray _mean, InputArray _cov, int nsamples, OutputArray _samples )
153 {
154     Mat mean = _mean.getMat(), cov = _cov.getMat();
155     int dim = (int)mean.total();
156 
157     _samples.create(nsamples, dim, CV_32F);
158     Mat samples = _samples.getMat();
159     randu(samples, 0., 1.);
160 
161     Mat utmat;
162     Cholesky(cov, utmat);
163     int flags = mean.cols == 1 ? 0 : GEMM_3_T;
164 
165     for( int i = 0; i < nsamples; i++ )
166     {
167         Mat sample = samples.row(i);
168         gemm(sample, utmat, 1, mean, 1, sample, flags);
169     }
170 }
171 
172 }}
173 
174 /* End of file */
175