65 int id = rhs[0].toInt();
66 string method(rhs[1].toString());
69 if (method ==
"new") {
81 if (method ==
"delete") {
86 else if (method ==
"clear") {
90 else if (method ==
"load") {
91 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
93 bool loadFromString =
false;
94 for (
int i=3; i<nrhs; i+=2) {
95 string key(rhs[i].toString());
97 objname = rhs[i+1].toString();
98 else if (key ==
"FromString")
99 loadFromString = rhs[i+1].toBool();
102 "Unrecognized option %s", key.
c_str());
105 obj_[id] = (loadFromString ?
106 Algorithm::loadFromString<ANN_MLP>(rhs[2].toString(), objname) :
107 Algorithm::load<ANN_MLP>(rhs[2].toString(), objname));
109 else if (method ==
"save") {
111 string fname(rhs[2].toString());
114 FileStorage fs(fname, FileStorage::WRITE + FileStorage::MEMORY);
126 else if (method ==
"empty") {
130 else if (method ==
"getDefaultName") {
134 else if (method ==
"getVarCount") {
138 else if (method ==
"isClassifier") {
142 else if (method ==
"isTrained") {
146 else if (method ==
"train") {
147 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
150 for (
int i=4; i<nrhs; i+=2) {
151 string key(rhs[i].toString());
153 dataOptions = rhs[i+1].toVector<
MxArray>();
154 else if (key ==
"Flags")
155 flags = rhs[i+1].toInt();
156 else if (key ==
"UpdateWeights")
157 UPDATE_FLAG(flags, rhs[i+1].toBool(), ANN_MLP::UPDATE_WEIGHTS);
158 else if (key ==
"NoInputScale")
159 UPDATE_FLAG(flags, rhs[i+1].toBool(), ANN_MLP::NO_INPUT_SCALE);
160 else if (key ==
"NoOutputScale")
161 UPDATE_FLAG(flags, rhs[i+1].toBool(), ANN_MLP::NO_OUTPUT_SCALE);
164 "Unrecognized option %s", key.
c_str());
169 dataOptions.
begin(), dataOptions.
end());
174 dataOptions.
begin(), dataOptions.
end());
175 bool b = obj->
train(data, flags);
178 else if (method ==
"calcError") {
179 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
182 for (
int i=4; i<nrhs; i+=2) {
183 string key(rhs[i].toString());
185 dataOptions = rhs[i+1].toVector<
MxArray>();
186 else if (key ==
"TestError")
187 test = rhs[i+1].toBool();
190 "Unrecognized option %s", key.
c_str());
195 dataOptions.
begin(), dataOptions.
end());
200 dataOptions.
begin(), dataOptions.
end());
207 else if (method ==
"predict") {
208 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=2);
210 for (
int i=3; i<nrhs; i+=2) {
211 string key(rhs[i].toString());
213 flags = rhs[i+1].toInt();
216 "Unrecognized option %s", key.
c_str());
220 float f = obj->
predict(samples, results, flags);
225 else if (method ==
"getWeights") {
227 int layerIdx = rhs[2].toInt();
230 else if (method ==
"setActivationFunction" || method ==
"setTrainMethod") {
231 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
234 for (
int i=3; i<nrhs; i+=2) {
235 string key(rhs[i].toString());
237 param1 = rhs[i+1].toDouble();
238 else if (key ==
"Param2")
239 param2 = rhs[i+1].toDouble();
242 "Unrecognized option %s", key.
c_str());
244 if (method ==
"setActivationFunction") {
253 else if (method ==
"get") {
255 string prop(rhs[2].toString());
256 if (prop ==
"TrainMethod")
258 else if (prop ==
"LayerSizes")
260 else if (prop ==
"TermCriteria")
262 else if (prop ==
"BackpropWeightScale")
264 else if (prop ==
"BackpropMomentumScale")
266 else if (prop ==
"RpropDW0")
268 else if (prop ==
"RpropDWPlus")
270 else if (prop ==
"RpropDWMinus")
272 else if (prop ==
"RpropDWMin")
274 else if (prop ==
"RpropDWMax")
276 else if (prop ==
"AnnealInitialT")
278 else if (prop ==
"AnnealFinalT")
280 else if (prop ==
"AnnealCoolingRatio")
282 else if (prop ==
"AnnealItePerStep")
286 "Unrecognized property %s", prop.
c_str());
288 else if (method ==
"set") {
290 string prop(rhs[2].toString());
291 if (prop ==
"TrainMethod")
293 else if (prop ==
"ActivationFunction")
295 else if (prop ==
"LayerSizes")
297 else if (prop ==
"TermCriteria")
299 else if (prop ==
"BackpropWeightScale")
301 else if (prop ==
"BackpropMomentumScale")
303 else if (prop ==
"RpropDW0")
305 else if (prop ==
"RpropDWPlus")
307 else if (prop ==
"RpropDWMinus")
309 else if (prop ==
"RpropDWMin")
311 else if (prop ==
"RpropDWMax")
313 else if (prop ==
"AnnealInitialT")
315 else if (prop ==
"AnnealFinalT")
317 else if (prop ==
"AnnealCoolingRatio")
319 else if (prop ==
"AnnealItePerStep")
323 "Unrecognized property %s", prop.
c_str());
327 "Unrecognized operation %s", method.
c_str());
virtual double getRpropDWMax() const=0
virtual bool isTrained() const=0
virtual double getRpropDWMinus() const=0
virtual double getRpropDWMin() const=0
virtual void setRpropDWMinus(double val)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void setAnnealItePerStep(int val)
const ConstMap< string, int > ANN_MLPTrain
Option values for ANN_MLP train types.
virtual void setRpropDWPlus(double val)=0
virtual void setTermCriteria(TermCriteria val)=0
double getAnnealInitialT() const
void setAnnealFinalT(double val)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
virtual void setRpropDWMax(double val)=0
virtual double getRpropDWPlus() const=0
virtual cv::Mat getLayerSizes() const=0
const ConstMap< int, string > InvANN_MLPTrain
Inverse option values for ANN_MLP train types.
const ConstMap< int, string > InvActivateFunc
Inverse option values for ANN_MLP activation function.
virtual void setBackpropWeightScale(double val)=0
map< int, Ptr< ANN_MLP > > obj_
Object container.
virtual double getBackpropMomentumScale() const=0
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 TermCriteria getTermCriteria() const=0
virtual String releaseAndGetString()
virtual double getBackpropWeightScale() const=0
virtual void write(FileStorage &fs) const
int getAnnealItePerStep() const
InputOutputArray noArray()
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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 void setRpropDWMin(double val)=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.
double getAnnealFinalT() const
void setAnnealCoolingRatio(double val)
virtual void setBackpropMomentumScale(double val)=0
virtual int getTrainMethod() const=0
virtual void setActivationFunction(int type, double param1=0, double param2=0)=0
virtual void setLayerSizes(InputArray _layer_sizes)=0
const ConstMap< string, int > ActivateFunc
Option values for ANN_MLP activation function.
virtual String getDefaultName() const
Global constant definitions.
virtual float predict(InputArray samples, OutputArray results=noArray(), int flags=0) const=0
virtual bool empty() const
int last_id
Last object id to allocate.
double getAnnealCoolingRatio() const
virtual void setTrainMethod(int method, double param1=0, double param2=0)=0
virtual void setRpropDW0(double val)=0
virtual bool isClassifier() const=0
void setAnnealInitialT(double val)
virtual float calcError(const Ptr< TrainData > &data, bool test, OutputArray resp) const
virtual double getRpropDW0() const=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.
std::map wrapper with one-line initialization and lookup method.
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
Common definitions for the ml module.
virtual int getVarCount() const=0
virtual Mat getWeights(int layerIdx) const=0