51 int id = rhs[0].toInt();
52 string method(rhs[1].toString());
55 if (method ==
"new") {
67 if (method ==
"delete") {
72 else if (method ==
"clear") {
76 else if (method ==
"load") {
77 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
79 bool loadFromString =
false;
80 for (
int i=3; i<nrhs; i+=2) {
81 string key(rhs[i].toString());
83 objname = rhs[i+1].toString();
84 else if (key ==
"FromString")
85 loadFromString = rhs[i+1].toBool();
88 "Unrecognized option %s", key.
c_str());
91 obj_[id] = (loadFromString ?
92 Algorithm::loadFromString<Boost>(rhs[2].toString(), objname) :
93 Algorithm::load<Boost>(rhs[2].toString(), objname));
95 else if (method ==
"save") {
97 string fname(rhs[2].toString());
100 FileStorage fs(fname, FileStorage::WRITE + FileStorage::MEMORY);
112 else if (method ==
"empty") {
116 else if (method ==
"getDefaultName") {
120 else if (method ==
"getVarCount") {
124 else if (method ==
"isClassifier") {
128 else if (method ==
"isTrained") {
132 else if (method ==
"train") {
133 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
136 for (
int i=4; i<nrhs; i+=2) {
137 string key(rhs[i].toString());
139 dataOptions = rhs[i+1].toVector<
MxArray>();
140 else if (key ==
"Flags")
141 flags = rhs[i+1].toInt();
142 else if (key ==
"RawOutput")
143 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::RAW_OUTPUT);
144 else if (key ==
"CompressedInput")
145 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::COMPRESSED_INPUT);
146 else if (key ==
"PredictSum")
147 UPDATE_FLAG(flags, rhs[i+1].toBool(), DTrees::PREDICT_SUM);
148 else if (key ==
"PredictMaxVote")
149 UPDATE_FLAG(flags, rhs[i+1].toBool(), DTrees::PREDICT_MAX_VOTE);
152 "Unrecognized option %s", key.
c_str());
157 dataOptions.
begin(), dataOptions.
end());
162 dataOptions.
begin(), dataOptions.
end());
163 bool b = obj->
train(data, flags);
166 else if (method ==
"calcError") {
167 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
170 for (
int i=4; i<nrhs; i+=2) {
171 string key(rhs[i].toString());
173 dataOptions = rhs[i+1].toVector<
MxArray>();
174 else if (key ==
"TestError")
175 test = rhs[i+1].toBool();
178 "Unrecognized option %s", key.
c_str());
183 dataOptions.
begin(), dataOptions.
end());
188 dataOptions.
begin(), dataOptions.
end());
195 else if (method ==
"predict") {
196 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
198 for (
int i=3; i<nrhs; i+=2) {
199 string key(rhs[i].toString());
201 flags = rhs[i+1].toInt();
202 else if (key ==
"RawOutput")
203 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::RAW_OUTPUT);
204 else if (key ==
"CompressedInput")
205 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::COMPRESSED_INPUT);
206 else if (key ==
"PreprocessedInput")
207 UPDATE_FLAG(flags, rhs[i+1].toBool(), StatModel::PREPROCESSED_INPUT);
208 else if (key ==
"PredictAuto") {
210 UPDATE_FLAG(flags, !rhs[i+1].toBool(), DTrees::PREDICT_SUM);
211 UPDATE_FLAG(flags, !rhs[i+1].toBool(), DTrees::PREDICT_MAX_VOTE);
213 else if (key ==
"PredictSum")
214 UPDATE_FLAG(flags, rhs[i+1].toBool(), DTrees::PREDICT_SUM);
215 else if (key ==
"PredictMaxVote")
216 UPDATE_FLAG(flags, rhs[i+1].toBool(), DTrees::PREDICT_MAX_VOTE);
219 "Unrecognized option %s", key.
c_str());
223 float f = obj->
predict(samples, results, flags);
228 else if (method ==
"getNodes") {
232 else if (method ==
"getRoots") {
236 else if (method ==
"getSplits") {
240 else if (method ==
"getSubsets") {
244 else if (method ==
"get") {
246 string prop(rhs[2].toString());
247 if (prop ==
"CVFolds")
249 else if (prop ==
"MaxCategories")
251 else if (prop ==
"MaxDepth")
253 else if (prop ==
"MinSampleCount")
255 else if (prop ==
"Priors")
257 else if (prop ==
"RegressionAccuracy")
259 else if (prop ==
"TruncatePrunedTree")
261 else if (prop ==
"Use1SERule")
263 else if (prop ==
"UseSurrogates")
265 else if (prop ==
"BoostType")
267 else if (prop ==
"WeakCount")
269 else if (prop ==
"WeightTrimRate")
273 "Unrecognized property %s", prop.
c_str());
275 else if (method ==
"set") {
277 string prop(rhs[2].toString());
278 if (prop ==
"CVFolds")
280 else if (prop ==
"MaxCategories")
282 else if (prop ==
"MaxDepth")
284 else if (prop ==
"MinSampleCount")
286 else if (prop ==
"Priors")
288 else if (prop ==
"RegressionAccuracy")
290 else if (prop ==
"TruncatePrunedTree")
292 else if (prop ==
"Use1SERule")
294 else if (prop ==
"UseSurrogates")
296 else if (prop ==
"BoostType")
298 else if (prop ==
"WeakCount")
300 else if (prop ==
"WeightTrimRate")
304 "Unrecognized property %s", prop.
c_str());
308 "Unrecognized operation %s", method.
c_str());
virtual int getMaxDepth() const=0
virtual int getBoostType() const=0
virtual bool getUse1SERule() const=0
virtual bool isTrained() const=0
virtual void setMinSampleCount(int val)=0
const ConstMap< int, string > InvBoostType
Option values for Inverse boost types.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void setUse1SERule(bool val)=0
virtual void setRegressionAccuracy(float val)=0
virtual void setUseSurrogates(bool val)=0
virtual float getRegressionAccuracy() const=0
virtual bool isOpened() const
const ConstMap< string, int > BoostType
Option values for Boost types.
struct mxArray_tag mxArray
Forward declaration for mxArray.
int last_id
Last object id to allocate.
virtual double getWeightTrimRate() const=0
virtual bool getUseSurrogates() const=0
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
virtual bool train(const Ptr< TrainData > &trainData, int flags=0)
cv::Ptr< cv::ml::TrainData > loadTrainData(const std::string &filename, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Read a dataset from a CSV file.
virtual const std::vector< Split > & getSplits() const=0
virtual String releaseAndGetString()
virtual void setWeightTrimRate(double val)=0
virtual void setBoostType(int val)=0
virtual void write(FileStorage &fs) const
InputOutputArray noArray()
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
virtual const std::vector< Node > & getNodes() const=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/output arguments number check.
virtual int getMaxCategories() const=0
virtual bool getTruncatePrunedTree() const=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual int getWeakCount() const=0
virtual cv::Mat getPriors() const=0
virtual const std::vector< int > & getSubsets() const=0
virtual int getMinSampleCount() const=0
virtual String getDefaultName() const
Global constant definitions.
virtual float predict(InputArray samples, OutputArray results=noArray(), int flags=0) const=0
virtual int getCVFolds() const=0
virtual bool empty() const
map< int, Ptr< Boost > > obj_
Object container.
virtual void setTruncatePrunedTree(bool val)=0
virtual bool isClassifier() const=0
virtual float calcError(const Ptr< TrainData > &data, bool test, OutputArray resp) const
virtual void setWeakCount(int val)=0
virtual void save(const String &filename) const
cv::Ptr< cv::ml::TrainData > createTrainData(const cv::Mat &samples, const cv::Mat &responses, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of TrainData using options in arguments.
virtual void setMaxDepth(int val)=0
std::map wrapper with one-line initialization and lookup method.
virtual const std::vector< int > & getRoots() const=0
virtual void setPriors(const cv::Mat &val)=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
Common definitions for the ml module.
virtual void setCVFolds(int val)=0
virtual int getVarCount() const=0
virtual void setMaxCategories(int val)=0