1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

trace: initial support for code trace

This commit is contained in:
Alexander Alekhin
2017-05-25 18:59:01 +03:00
parent 07aff8e85f
commit 006966e629
58 changed files with 2963 additions and 185 deletions
+2
View File
@@ -1028,6 +1028,7 @@ Ptr<TrainData> TrainData::loadFromCSV(const String& filename,
const String& varTypeSpec,
char delimiter, char missch)
{
CV_TRACE_FUNCTION_SKIP_NESTED();
Ptr<TrainDataImpl> td = makePtr<TrainDataImpl>();
if(!td->loadCSV(filename, headerLines, responseStartIdx, responseEndIdx, varTypeSpec, delimiter, missch))
td.release();
@@ -1038,6 +1039,7 @@ Ptr<TrainData> TrainData::create(InputArray samples, int layout, InputArray resp
InputArray varIdx, InputArray sampleIdx, InputArray sampleWeights,
InputArray varType)
{
CV_TRACE_FUNCTION_SKIP_NESTED();
Ptr<TrainDataImpl> td = makePtr<TrainDataImpl>();
td->setData(samples, layout, responses, varIdx, sampleIdx, sampleWeights, varType, noArray());
return td;
+6
View File
@@ -45,6 +45,7 @@ namespace cv { namespace ml {
ParamGrid::ParamGrid() { minVal = maxVal = 0.; logStep = 1; }
ParamGrid::ParamGrid(double _minVal, double _maxVal, double _logStep)
{
CV_TRACE_FUNCTION();
minVal = std::min(_minVal, _maxVal);
maxVal = std::max(_minVal, _maxVal);
logStep = std::max(_logStep, 1.);
@@ -60,17 +61,20 @@ int StatModel::getVarCount() const { return 0; }
bool StatModel::train( const Ptr<TrainData>&, int )
{
CV_TRACE_FUNCTION();
CV_Error(CV_StsNotImplemented, "");
return false;
}
bool StatModel::train( InputArray samples, int layout, InputArray responses )
{
CV_TRACE_FUNCTION();
return train(TrainData::create(samples, layout, responses));
}
float StatModel::calcError( const Ptr<TrainData>& data, bool testerr, OutputArray _resp ) const
{
CV_TRACE_FUNCTION_SKIP_NESTED();
Mat samples = data->getSamples();
int layout = data->getLayout();
Mat sidx = testerr ? data->getTestSampleIdx() : data->getTrainSampleIdx();
@@ -119,6 +123,7 @@ float StatModel::calcError( const Ptr<TrainData>& data, bool testerr, OutputArra
/* Calculates upper triangular matrix S, where A is a symmetrical matrix A=S'*S */
static void Cholesky( const Mat& A, Mat& S )
{
CV_TRACE_FUNCTION();
CV_Assert(A.type() == CV_32F);
S = A.clone();
@@ -133,6 +138,7 @@ static void Cholesky( const Mat& A, Mat& S )
average row vector, <cov> - symmetric covariation matrix */
void randMVNormal( InputArray _mean, InputArray _cov, int nsamples, OutputArray _samples )
{
CV_TRACE_FUNCTION();
// check mean vector and covariance matrix
Mat mean = _mean.getMat(), cov = _cov.getMat();
int dim = (int)mean.total(); // dimensionality
+5
View File
@@ -135,6 +135,7 @@ Ptr<LogisticRegression> LogisticRegression::load(const String& filepath, const S
bool LogisticRegressionImpl::train(const Ptr<TrainData>& trainData, int)
{
CV_TRACE_FUNCTION_SKIP_NESTED();
// return value
bool ok = false;
@@ -313,6 +314,7 @@ float LogisticRegressionImpl::predict(InputArray samples, OutputArray results, i
Mat LogisticRegressionImpl::calc_sigmoid(const Mat& data) const
{
CV_TRACE_FUNCTION();
Mat dest;
exp(-data, dest);
return 1.0/(1.0+dest);
@@ -320,6 +322,7 @@ Mat LogisticRegressionImpl::calc_sigmoid(const Mat& data) const
double LogisticRegressionImpl::compute_cost(const Mat& _data, const Mat& _labels, const Mat& _init_theta)
{
CV_TRACE_FUNCTION();
float llambda = 0; /*changed llambda from int to float to solve issue #7924*/
int m;
int n;
@@ -410,6 +413,7 @@ struct LogisticRegressionImpl_ComputeDradient_Impl : ParallelLoopBody
void LogisticRegressionImpl::compute_gradient(const Mat& _data, const Mat& _labels, const Mat &_theta, const double _lambda, Mat & _gradient )
{
CV_TRACE_FUNCTION();
const int m = _data.rows;
Mat pcal_a, pcal_b, pcal_ab;
@@ -431,6 +435,7 @@ void LogisticRegressionImpl::compute_gradient(const Mat& _data, const Mat& _labe
Mat LogisticRegressionImpl::batch_gradient_descent(const Mat& _data, const Mat& _labels, const Mat& _init_theta)
{
CV_TRACE_FUNCTION();
// implements batch gradient descent
if(this->params.alpha<=0)
{
+22 -1
View File
@@ -49,6 +49,7 @@ namespace ml {
//////////////////////////////////////////////////////////////////////////////////////////
RTreeParams::RTreeParams()
{
CV_TRACE_FUNCTION();
calcVarImportance = false;
nactiveVars = 0;
termCrit = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 50, 0.1);
@@ -58,6 +59,7 @@ RTreeParams::RTreeParams(bool _calcVarImportance,
int _nactiveVars,
TermCriteria _termCrit )
{
CV_TRACE_FUNCTION();
calcVarImportance = _calcVarImportance;
nactiveVars = _nactiveVars;
termCrit = _termCrit;
@@ -69,6 +71,7 @@ class DTreesImplForRTrees : public DTreesImpl
public:
DTreesImplForRTrees()
{
CV_TRACE_FUNCTION();
params.setMaxDepth(5);
params.setMinSampleCount(10);
params.setRegressionAccuracy(0.f);
@@ -83,6 +86,7 @@ public:
void clear()
{
CV_TRACE_FUNCTION();
DTreesImpl::clear();
oobError = 0.;
rng = RNG((uint64)-1);
@@ -90,6 +94,7 @@ public:
const vector<int>& getActiveVars()
{
CV_TRACE_FUNCTION();
int i, nvars = (int)allVars.size(), m = (int)activeVars.size();
for( i = 0; i < nvars; i++ )
{
@@ -104,6 +109,7 @@ public:
void startTraining( const Ptr<TrainData>& trainData, int flags )
{
CV_TRACE_FUNCTION();
DTreesImpl::startTraining(trainData, flags);
int nvars = w->data->getNVars();
int i, m = rparams.nactiveVars > 0 ? rparams.nactiveVars : cvRound(std::sqrt((double)nvars));
@@ -116,6 +122,7 @@ public:
void endTraining()
{
CV_TRACE_FUNCTION();
DTreesImpl::endTraining();
vector<int> a, b;
std::swap(allVars, a);
@@ -124,6 +131,7 @@ public:
bool train( const Ptr<TrainData>& trainData, int flags )
{
CV_TRACE_FUNCTION();
startTraining(trainData, flags);
int treeidx, ntrees = (rparams.termCrit.type & TermCriteria::COUNT) != 0 ?
rparams.termCrit.maxCount : 10000;
@@ -286,12 +294,14 @@ public:
void writeTrainingParams( FileStorage& fs ) const
{
CV_TRACE_FUNCTION();
DTreesImpl::writeTrainingParams(fs);
fs << "nactive_vars" << rparams.nactiveVars;
}
void write( FileStorage& fs ) const
{
CV_TRACE_FUNCTION();
if( roots.empty() )
CV_Error( CV_StsBadArg, "RTrees have not been trained" );
@@ -319,6 +329,7 @@ public:
void readParams( const FileNode& fn )
{
CV_TRACE_FUNCTION();
DTreesImpl::readParams(fn);
FileNode tparams_node = fn["training_params"];
@@ -327,6 +338,7 @@ public:
void read( const FileNode& fn )
{
CV_TRACE_FUNCTION();
clear();
//int nclasses = (int)fn["nclasses"];
@@ -351,6 +363,7 @@ public:
void getVotes( InputArray input, OutputArray output, int flags ) const
{
CV_TRACE_FUNCTION();
CV_Assert( !roots.empty() );
int nclasses = (int)classLabels.size(), ntrees = (int)roots.size();
Mat samples = input.getMat(), results;
@@ -435,6 +448,7 @@ public:
bool train( const Ptr<TrainData>& trainData, int flags )
{
CV_TRACE_FUNCTION();
if (impl.getCVFolds() != 0)
CV_Error(Error::StsBadArg, "Cross validation for RTrees is not implemented");
return impl.train(trainData, flags);
@@ -442,22 +456,26 @@ public:
float predict( InputArray samples, OutputArray results, int flags ) const
{
CV_TRACE_FUNCTION();
return impl.predict(samples, results, flags);
}
void write( FileStorage& fs ) const
{
CV_TRACE_FUNCTION();
impl.write(fs);
}
void read( const FileNode& fn )
{
CV_TRACE_FUNCTION();
impl.read(fn);
}
void getVotes_( InputArray samples, OutputArray results, int flags ) const
{
impl.getVotes(samples, results, flags);
CV_TRACE_FUNCTION();
impl.getVotes(samples, results, flags);
}
Mat getVarImportance() const { return Mat_<float>(impl.varImportance, true); }
@@ -477,17 +495,20 @@ public:
Ptr<RTrees> RTrees::create()
{
CV_TRACE_FUNCTION();
return makePtr<RTreesImpl>();
}
//Function needed for Python and Java wrappers
Ptr<RTrees> RTrees::load(const String& filepath, const String& nodeName)
{
CV_TRACE_FUNCTION();
return Algorithm::load<RTrees>(filepath, nodeName);
}
void RTrees::getVotes(InputArray input, OutputArray output, int flags) const
{
CV_TRACE_FUNCTION();
const RTreesImpl* this_ = dynamic_cast<const RTreesImpl*>(this);
if(!this_)
CV_Error(Error::StsNotImplemented, "the class is not RTreesImpl");