1. Input/OutputArray optimizations;
2. Algorithm::load/save added (moved from StatModel) 3. copyrights updated; added copyright/licensing info for ffmpeg 4. some warnings from Xcode 6.x are fixed
This commit is contained in:
@@ -297,11 +297,12 @@ public:
|
||||
COMPRESSED_INPUT=2,
|
||||
PREPROCESSED_INPUT=4
|
||||
};
|
||||
CV_WRAP virtual void clear();
|
||||
|
||||
/** @brief Returns the number of variables in training samples */
|
||||
CV_WRAP virtual int getVarCount() const = 0;
|
||||
|
||||
CV_WRAP virtual bool empty() const;
|
||||
|
||||
/** @brief Returns true if the model is trained */
|
||||
CV_WRAP virtual bool isTrained() const = 0;
|
||||
/** @brief Returns true if the model is classifier */
|
||||
@@ -347,40 +348,6 @@ public:
|
||||
*/
|
||||
CV_WRAP virtual float predict( InputArray samples, OutputArray results=noArray(), int flags=0 ) const = 0;
|
||||
|
||||
/** @brief Loads model from the file
|
||||
|
||||
This is static template method of StatModel. It's usage is following (in the case of SVM):
|
||||
@code
|
||||
Ptr<SVM> svm = StatModel::load<SVM>("my_svm_model.xml");
|
||||
@endcode
|
||||
In order to make this method work, the derived class must overwrite Algorithm::read(const
|
||||
FileNode& fn).
|
||||
*/
|
||||
template<typename _Tp> static Ptr<_Tp> load(const String& filename)
|
||||
{
|
||||
FileStorage fs(filename, FileStorage::READ);
|
||||
Ptr<_Tp> model = _Tp::create();
|
||||
model->read(fs.getFirstTopLevelNode());
|
||||
return model->isTrained() ? model : Ptr<_Tp>();
|
||||
}
|
||||
|
||||
/** @brief Loads model from a String
|
||||
|
||||
@param strModel The string variable containing the model you want to load.
|
||||
|
||||
This is static template method of StatModel. It's usage is following (in the case of SVM):
|
||||
@code
|
||||
Ptr<SVM> svm = StatModel::loadFromString<SVM>(myStringModel);
|
||||
@endcode
|
||||
*/
|
||||
template<typename _Tp> static Ptr<_Tp> loadFromString(const String& strModel)
|
||||
{
|
||||
FileStorage fs(strModel, FileStorage::READ + FileStorage::MEMORY);
|
||||
Ptr<_Tp> model = _Tp::create();
|
||||
model->read(fs.getFirstTopLevelNode());
|
||||
return model->isTrained() ? model : Ptr<_Tp>();
|
||||
}
|
||||
|
||||
/** @brief Create and train model with default parameters
|
||||
|
||||
The class must implement static `create()` method with no parameters or with all default parameter values
|
||||
@@ -390,14 +357,6 @@ public:
|
||||
Ptr<_Tp> model = _Tp::create();
|
||||
return !model.empty() && model->train(data, flags) ? model : Ptr<_Tp>();
|
||||
}
|
||||
|
||||
/** Saves the model to a file.
|
||||
In order to make this method work, the derived class must implement Algorithm::write(FileStorage& fs). */
|
||||
CV_WRAP virtual void save(const String& filename) const;
|
||||
|
||||
/** Returns model string identifier.
|
||||
This string is used as top level xml/yml node tag when model is saved to a file or string. */
|
||||
CV_WRAP virtual String getDefaultModelName() const = 0;
|
||||
};
|
||||
|
||||
/****************************************************************************************\
|
||||
@@ -939,7 +898,7 @@ public:
|
||||
|
||||
/** Creates empty %EM model.
|
||||
The model should be trained then using StatModel::train(traindata, flags) method. Alternatively, you
|
||||
can use one of the EM::train\* methods or load it from file using StatModel::load\<EM\>(filename).
|
||||
can use one of the EM::train\* methods or load it from file using Algorithm::load\<EM\>(filename).
|
||||
*/
|
||||
CV_WRAP static Ptr<EM> create();
|
||||
};
|
||||
@@ -1127,7 +1086,7 @@ public:
|
||||
|
||||
The static method creates empty decision tree with the specified parameters. It should be then
|
||||
trained using train method (see StatModel::train). Alternatively, you can load the model from
|
||||
file using StatModel::load\<DTrees\>(filename).
|
||||
file using Algorithm::load\<DTrees\>(filename).
|
||||
*/
|
||||
CV_WRAP static Ptr<DTrees> create();
|
||||
};
|
||||
@@ -1181,7 +1140,7 @@ public:
|
||||
|
||||
/** Creates the empty model.
|
||||
Use StatModel::train to train the model, StatModel::train to create and train the model,
|
||||
StatModel::load to load the pre-trained model.
|
||||
Algorithm::load to load the pre-trained model.
|
||||
*/
|
||||
CV_WRAP static Ptr<RTrees> create();
|
||||
};
|
||||
@@ -1231,7 +1190,7 @@ public:
|
||||
};
|
||||
|
||||
/** Creates the empty model.
|
||||
Use StatModel::train to train the model, StatModel::load\<Boost\>(filename) to load the pre-trained model. */
|
||||
Use StatModel::train to train the model, Algorithm::load\<Boost\>(filename) to load the pre-trained model. */
|
||||
CV_WRAP static Ptr<Boost> create();
|
||||
};
|
||||
|
||||
@@ -1416,7 +1375,7 @@ public:
|
||||
|
||||
/** @brief Creates empty model
|
||||
|
||||
Use StatModel::train to train the model, StatModel::load\<ANN_MLP\>(filename) to load the pre-trained model.
|
||||
Use StatModel::train to train the model, Algorithm::load\<ANN_MLP\>(filename) to load the pre-trained model.
|
||||
Note that the train method has optional flags: ANN_MLP::TrainFlags.
|
||||
*/
|
||||
static Ptr<ANN_MLP> create();
|
||||
|
@@ -1294,7 +1294,7 @@ public:
|
||||
return layer_sizes.empty() ? 0 : layer_sizes[0];
|
||||
}
|
||||
|
||||
String getDefaultModelName() const
|
||||
String getDefaultName() const
|
||||
{
|
||||
return "opencv_ml_ann_mlp";
|
||||
}
|
||||
|
@@ -465,7 +465,7 @@ public:
|
||||
CV_WRAP_SAME_PROPERTY(float, RegressionAccuracy, impl.params)
|
||||
CV_WRAP_SAME_PROPERTY_S(cv::Mat, Priors, impl.params)
|
||||
|
||||
String getDefaultModelName() const { return "opencv_ml_boost"; }
|
||||
String getDefaultName() const { return "opencv_ml_boost"; }
|
||||
|
||||
bool train( const Ptr<TrainData>& trainData, int flags )
|
||||
{
|
||||
|
@@ -227,7 +227,7 @@ public:
|
||||
return means.cols;
|
||||
}
|
||||
|
||||
String getDefaultModelName() const
|
||||
String getDefaultName() const
|
||||
{
|
||||
return "opencv_ml_em";
|
||||
}
|
||||
|
@@ -50,7 +50,7 @@ ParamGrid::ParamGrid(double _minVal, double _maxVal, double _logStep)
|
||||
logStep = std::max(_logStep, 1.);
|
||||
}
|
||||
|
||||
void StatModel::clear() {}
|
||||
bool StatModel::empty() const { return !isTrained(); }
|
||||
|
||||
int StatModel::getVarCount() const { return 0; }
|
||||
|
||||
@@ -111,15 +111,6 @@ float StatModel::calcError( const Ptr<TrainData>& data, bool testerr, OutputArra
|
||||
return (float)(err / n * (isclassifier ? 100 : 1));
|
||||
}
|
||||
|
||||
void StatModel::save(const String& filename) const
|
||||
{
|
||||
FileStorage fs(filename, FileStorage::WRITE);
|
||||
fs << getDefaultModelName() << "{";
|
||||
fs << "format" << (int)3;
|
||||
write(fs);
|
||||
fs << "}";
|
||||
}
|
||||
|
||||
/* Calculates upper triangular matrix S, where A is a symmetrical matrix A=S'*S */
|
||||
static void Cholesky( const Mat& A, Mat& S )
|
||||
{
|
||||
|
@@ -496,7 +496,7 @@ public:
|
||||
return impl->train(data, flags);
|
||||
}
|
||||
|
||||
String getDefaultModelName() const { return impl->getModelName(); }
|
||||
String getDefaultName() const { return impl->getModelName(); }
|
||||
|
||||
protected:
|
||||
void initImpl(int algorithmType)
|
||||
|
@@ -104,7 +104,7 @@ public:
|
||||
virtual int getVarCount() const { return learnt_thetas.cols; }
|
||||
virtual bool isTrained() const { return !learnt_thetas.empty(); }
|
||||
virtual bool isClassifier() const { return true; }
|
||||
virtual String getDefaultModelName() const { return "opencv_ml_lr"; }
|
||||
virtual String getDefaultName() const { return "opencv_ml_lr"; }
|
||||
protected:
|
||||
Mat calc_sigmoid(const Mat& data) const;
|
||||
double compute_cost(const Mat& _data, const Mat& _labels, const Mat& _init_theta);
|
||||
|
@@ -443,7 +443,7 @@ public:
|
||||
bool isTrained() const { return !avg.empty(); }
|
||||
bool isClassifier() const { return true; }
|
||||
int getVarCount() const { return nallvars; }
|
||||
String getDefaultModelName() const { return "opencv_ml_nbayes"; }
|
||||
String getDefaultName() const { return "opencv_ml_nbayes"; }
|
||||
|
||||
int nallvars;
|
||||
Mat var_idx, cls_labels, c;
|
||||
|
@@ -290,7 +290,7 @@ namespace ml
|
||||
virtual ~DTreesImpl();
|
||||
virtual void clear();
|
||||
|
||||
String getDefaultModelName() const { return "opencv_ml_dtree"; }
|
||||
String getDefaultName() const { return "opencv_ml_dtree"; }
|
||||
bool isTrained() const { return !roots.empty(); }
|
||||
bool isClassifier() const { return _isClassifier; }
|
||||
int getVarCount() const { return varType.empty() ? 0 : (int)(varType.size() - 1); }
|
||||
|
@@ -375,7 +375,7 @@ public:
|
||||
RTreesImpl() {}
|
||||
virtual ~RTreesImpl() {}
|
||||
|
||||
String getDefaultModelName() const { return "opencv_ml_rtrees"; }
|
||||
String getDefaultName() const { return "opencv_ml_rtrees"; }
|
||||
|
||||
bool train( const Ptr<TrainData>& trainData, int flags )
|
||||
{
|
||||
|
@@ -2008,7 +2008,7 @@ public:
|
||||
return var_count;
|
||||
}
|
||||
|
||||
String getDefaultModelName() const
|
||||
String getDefaultName() const
|
||||
{
|
||||
return "opencv_ml_svm";
|
||||
}
|
||||
|
@@ -576,7 +576,7 @@ protected:
|
||||
// Read in
|
||||
try
|
||||
{
|
||||
em = StatModel::load<EM>(filename);
|
||||
em = Algorithm::load<EM>(filename);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
|
@@ -179,7 +179,7 @@ void CV_LRTest_SaveLoad::run( int /*start_from*/ )
|
||||
// and load to another
|
||||
try
|
||||
{
|
||||
Ptr<LogisticRegression> lr2 = StatModel::load<LogisticRegression>(filename);
|
||||
Ptr<LogisticRegression> lr2 = Algorithm::load<LogisticRegression>(filename);
|
||||
lr2->predict(tdata->getSamples(), responses2);
|
||||
learnt_mat2 = lr2->get_learnt_thetas();
|
||||
}
|
||||
|
@@ -472,19 +472,19 @@ void CV_MLBaseTest::save( const char* filename )
|
||||
void CV_MLBaseTest::load( const char* filename )
|
||||
{
|
||||
if( modelName == CV_NBAYES )
|
||||
model = StatModel::load<NormalBayesClassifier>( filename );
|
||||
model = Algorithm::load<NormalBayesClassifier>( filename );
|
||||
else if( modelName == CV_KNEAREST )
|
||||
model = StatModel::load<KNearest>( filename );
|
||||
model = Algorithm::load<KNearest>( filename );
|
||||
else if( modelName == CV_SVM )
|
||||
model = StatModel::load<SVM>( filename );
|
||||
model = Algorithm::load<SVM>( filename );
|
||||
else if( modelName == CV_ANN )
|
||||
model = StatModel::load<ANN_MLP>( filename );
|
||||
model = Algorithm::load<ANN_MLP>( filename );
|
||||
else if( modelName == CV_DTREE )
|
||||
model = StatModel::load<DTrees>( filename );
|
||||
model = Algorithm::load<DTrees>( filename );
|
||||
else if( modelName == CV_BOOST )
|
||||
model = StatModel::load<Boost>( filename );
|
||||
model = Algorithm::load<Boost>( filename );
|
||||
else if( modelName == CV_RTREES )
|
||||
model = StatModel::load<RTrees>( filename );
|
||||
model = Algorithm::load<RTrees>( filename );
|
||||
else
|
||||
CV_Error( CV_StsNotImplemented, "invalid stat model name");
|
||||
}
|
||||
|
@@ -190,17 +190,17 @@ protected:
|
||||
bool isTree = modelName == CV_BOOST || modelName == CV_DTREE || modelName == CV_RTREES;
|
||||
Ptr<StatModel> model;
|
||||
if (modelName == CV_BOOST)
|
||||
model = StatModel::load<Boost>(filename);
|
||||
model = Algorithm::load<Boost>(filename);
|
||||
else if (modelName == CV_ANN)
|
||||
model = StatModel::load<ANN_MLP>(filename);
|
||||
model = Algorithm::load<ANN_MLP>(filename);
|
||||
else if (modelName == CV_DTREE)
|
||||
model = StatModel::load<DTrees>(filename);
|
||||
model = Algorithm::load<DTrees>(filename);
|
||||
else if (modelName == CV_NBAYES)
|
||||
model = StatModel::load<NormalBayesClassifier>(filename);
|
||||
model = Algorithm::load<NormalBayesClassifier>(filename);
|
||||
else if (modelName == CV_SVM)
|
||||
model = StatModel::load<SVM>(filename);
|
||||
model = Algorithm::load<SVM>(filename);
|
||||
else if (modelName == CV_RTREES)
|
||||
model = StatModel::load<RTrees>(filename);
|
||||
model = Algorithm::load<RTrees>(filename);
|
||||
if (!model)
|
||||
{
|
||||
code = cvtest::TS::FAIL_INVALID_TEST_DATA;
|
||||
@@ -273,11 +273,11 @@ TEST(DISABLED_ML_SVM, linear_save_load)
|
||||
{
|
||||
Ptr<cv::ml::SVM> svm1, svm2, svm3;
|
||||
|
||||
svm1 = StatModel::load<SVM>("SVM45_X_38-1.xml");
|
||||
svm2 = StatModel::load<SVM>("SVM45_X_38-2.xml");
|
||||
svm1 = Algorithm::load<SVM>("SVM45_X_38-1.xml");
|
||||
svm2 = Algorithm::load<SVM>("SVM45_X_38-2.xml");
|
||||
string tname = tempfile("a.xml");
|
||||
svm2->save(tname);
|
||||
svm3 = StatModel::load<SVM>(tname);
|
||||
svm3 = Algorithm::load<SVM>(tname);
|
||||
|
||||
ASSERT_EQ(svm1->getVarCount(), svm2->getVarCount());
|
||||
ASSERT_EQ(svm1->getVarCount(), svm3->getVarCount());
|
||||
|
Reference in New Issue
Block a user