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 <<endl;="" }="" }<="" p="">
This code is working OK if UNIQUE_OBJECT is equal to 15. I need it to be 300.