opencv 3.1 ANN MLP

asked 2017-04-21 06:13:52 -0600

charge gravatar image

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

berak gravatar image

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;
        }
}

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

edit retag flag offensive close merge delete

Comments

1

and the error is ?

why did you choose an ann, if you only got 1 sample per class ?

(which could be better done with asimple 1-nearest-neighbour search)

100000 generations, really ?

layer_size should be CV_32S, not CV_16U

berak gravatar imageberak ( 2017-04-21 06:31:26 -0600 )edit