Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

opencv 3.1 ANN MLP

I have data of 300 unique object. Each object have 11 unique feature. I will feed this data in to ANN_MLP in teach .

After teaching i will get single object data. Using ANN_MLP predict i would like to know the best match out of 300 taught object.

My code is as follows:

define UNIQUE_OBJECT 15

define UNIQUE_FEATURE 11

void Teach() { Mat train_data; Mat train_labels = Mat::zeros(UNIQUE_OBJECT, UNIQUE_OBJECT, CV_32F); for(int index=0;index<unique_object ;index++)="" {="" train_labels.at<float="">(index, index) = 1.f; } for(int index=0;index<unique_object ;index++)="" {="" mat="" singlerowimage="Mat::zeros(1,UNIQUE_FEATURE,CV_32FC1);" float="" floatindex="(float)(index);" singlerowimage.setto(floatindex);="" singlerowimage.convertto(singlerowimage,cv_32f);="" train_data.push_back(singlerowimage.reshape(1,1));="" }="" ptr<ml::ann_mlp=""> ann = ml::ANN_MLP::create(); Mat layersSize = Mat(4, 1, CV_16U); layersSize.row(0) = Scalar(train_data.cols); layersSize.row(1) = Scalar(15); layersSize.row(2) = Scalar(15); layersSize.row(3) = Scalar(train_labels.cols); ann->setLayerSizes(layersSize); ann->setActivationFunction(ml::ANN_MLP::ActivationFunctions::SIGMOID_SYM); TermCriteria termCrit = TermCriteria( TermCriteria::Type::COUNT + TermCriteria::Type::EPS, 100000, 0.0000001 ); ann->setTermCriteria(termCrit); ann->setTrainMethod(ml::ANN_MLP::TrainingMethods::BACKPROP); Ptr<ml::traindata> trainingData = ml::TrainData::create( train_data, ml::SampleTypes::ROW_SAMPLE, train_labels ); ann->train(trainingData ); cv::FileStorage fs("mlp_NEW.yml", cv::FileStorage::WRITE); // or xml ann->write(fs); } void Inspect() { //Current sample FileStorage ffs("mlp_NEW.yml", FileStorage::READ); Ptr<ml::ann_mlp> ann = Algorithm::read<ml::ann_mlp>(ffs.root()); Mat test_data; for(int index=0;index<unique_object ;index++)="" {="" rect="" croproi;="" croproi.x="0;" croproi.y="index;" croproi.width="UNIQUE_FEATURE;" croproi.height="1;" mat="" singlerowimage="Mat::zeros(1,UNIQUE_FEATURE,CV_32FC1);" float="" floatindex="(float)(index);" singlerowimage.setto(floatindex);="" singlerowimage.convertto(singlerowimage,cv_32f);="" test_data.push_back(singlerowimage.reshape(1,1));="" }="" for(int="" i="0;" i<test_data.rows;="" i++)="" {="" float="" pred="ann-">predict(test_data.row(i), noArray()); cerr<<" Predict= "<<pred &lt;<endl;="" }="" }<="" p="">

This code is working OK if UNIQUE_OBJECT is equal to 15. I need it to be 300.

click to hide/show revision 2
No.2 Revision

updated 2017-04-21 06:22:19 -0500

berak gravatar image

opencv 3.1 ANN MLP

I have data of 300 unique object. Each object have 11 unique feature. I will feed this data in to ANN_MLP in teach .

After teaching i will get single object data. Using ANN_MLP predict i would like to know the best match out of 300 taught object.

My code is as follows:

define
#define UNIQUE_OBJECT 15

define 15 #define UNIQUE_FEATURE 11

11 void Teach() { Mat train_data; Mat train_labels = Mat::zeros(UNIQUE_OBJECT, UNIQUE_OBJECT, CV_32F); for(int index=0;index<unique_object ;index++)="" {="" train_labels.at<float="">(index, index=0;index<UNIQUE_OBJECT ;index++) { train_labels.at<float>(index, index) = 1.f; } for(int index=0;index<unique_object ;index++)="" {="" mat="" singlerowimage="Mat::zeros(1,UNIQUE_FEATURE,CV_32FC1);" float="" floatindex="(float)(index);" singlerowimage.setto(floatindex);="" singlerowimage.convertto(singlerowimage,cv_32f);="" train_data.push_back(singlerowimage.reshape(1,1));="" }="" ptr<ml::ann_mlp=""> index=0;index<UNIQUE_OBJECT ;index++) { Mat singleRowImage= Mat::zeros(1,UNIQUE_FEATURE,CV_32FC1); float floatIndex=(float)(index); singleRowImage.setTo(floatIndex); singleRowImage.convertTo(singleRowImage,CV_32F); train_data.push_back(singleRowImage.reshape(1,1)); } Ptr<ml::ANN_MLP> ann = ml::ANN_MLP::create(); Mat layersSize = Mat(4, 1, CV_16U); layersSize.row(0) = Scalar(train_data.cols); layersSize.row(1) = Scalar(15); layersSize.row(2) = Scalar(15); layersSize.row(3) = Scalar(train_labels.cols); ann->setLayerSizes(layersSize); ann->setActivationFunction(ml::ANN_MLP::ActivationFunctions::SIGMOID_SYM); TermCriteria termCrit = TermCriteria( TermCriteria::Type::COUNT + TermCriteria::Type::EPS, 100000, 0.0000001 ); ann->setTermCriteria(termCrit); ann->setTrainMethod(ml::ANN_MLP::TrainingMethods::BACKPROP); Ptr<ml::traindata> Ptr<ml::TrainData> trainingData = ml::TrainData::create( train_data, ml::SampleTypes::ROW_SAMPLE, train_labels ); ann->train(trainingData ); cv::FileStorage fs("mlp_NEW.yml", cv::FileStorage::WRITE); // or xml ann->write(fs); } void Inspect() { //Current sample FileStorage ffs("mlp_NEW.yml", FileStorage::READ); Ptr<ml::ann_mlp> Ptr<ml::ANN_MLP> ann = Algorithm::read<ml::ann_mlp>(ffs.root()); Algorithm::read<ml::ANN_MLP>(ffs.root()); Mat test_data; for(int index=0;index<unique_object ;index++)="" {="" rect="" croproi;="" croproi.x="0;" croproi.y="index;" croproi.width="UNIQUE_FEATURE;" croproi.height="1;" mat="" singlerowimage="Mat::zeros(1,UNIQUE_FEATURE,CV_32FC1);" float="" floatindex="(float)(index);" singlerowimage.setto(floatindex);="" singlerowimage.convertto(singlerowimage,cv_32f);="" test_data.push_back(singlerowimage.reshape(1,1));="" }="" for(int="" i="0;" i<test_data.rows;="" i++)="" {="" float="" pred="ann-">predict(test_data.row(i), index=0;index<UNIQUE_OBJECT ;index++) { Rect cropRoi; cropRoi.x=0; cropRoi.y=index; cropRoi.width=UNIQUE_FEATURE; cropRoi.height=1; Mat singleRowImage= Mat::zeros(1,UNIQUE_FEATURE,CV_32FC1); float floatIndex=(float)(index); singleRowImage.setTo(floatIndex); singleRowImage.convertTo(singleRowImage,CV_32F); test_data.push_back(singleRowImage.reshape(1,1)); } for(int i=0; i<test_data.rows; i++) { float pred = ann->predict(test_data.row(i), noArray()); cerr<<" Predict= "<<pred &lt;<endl;="" }="" }<="" p="">

<<endl; } }

This code is working OK if UNIQUE_OBJECT is equal to 15. I need it to be 300.