Neural network with 3.0.0

asked 2015-02-24 18:59:36 -0500

Hadi gravatar image

updated 2015-02-26 01:30:31 -0500

berak gravatar image

Hi, I am pretty new to neural networks in opencv. I read through the documentation and this is how i have implemented the training of the network

cv::Ptr<cv::ml::ANN_MLP> classifier = cv::ml::ANN_MLP::create();


ANN_MLP::Params params;
params.activateFunc = ANN_MLP::SIGMOID_SYM;
params.layerSizes = layers;
params.fparam1 = 0.6;
params.fparam2 = 1;
params.termCrit = TermCriteria(cv::TermCriteria::MAX_ITER+cv::TermCriteria::EPS, 1000, 0.000001);
params.trainMethod = ANN_MLP::Params::BACKPROP;
params.bpDWScale = 0.1;
params.bpMomentScale = 0.1;
params.rpDW0 = 0.1; 
params.rpDWPlus = 1.2; 
params.rpDWMinus = 0.5;
params.rpDWMin = FLT_EPSILON; 
params.rpDWMax = 50.;




classifier->setParams(params);

int iterations = classifier->train(training_set,ROW_SAMPLE,training_set_classifications);


FileStorage fs("C:\\Users\\Hadi\\Downloads\\output\\param.xml", FileStorage::WRITE);

classifier->write(fs);
fs.release();

But on running of the code it is giving me abort error on training part. It is saying something like:

Expression Error : Vector subscript out of range

Can anybody please guide me to some tutorial for training and predicting of neural networks using opencv 3.0. Thank you.

EDIT 1:

I am setting up the layers as follows :

 cv::Mat layers(3,1,CV_32S);
layers.at<int>(0,0) = ATTRIBUTES;//input layer
layers.at<int>(1,0) = ATTRIBUTES;//hidden layer
layers.at<int>(2,0) = CLASSES;//output layer

and reading the data from txt files with the following function :

void read_dataset(char *filename, cv::Mat &data, cv::Mat &classes,  int total_samples)
{
    int label;
    float pixelvalue;
    //open the file
    FILE* inputfile = fopen( filename, "r" );

    //read each row of the csv file
   for(int row = 0; row < total_samples; row++)
   {
       //for each attribute in the row
     for(int col = 0; col <=ATTRIBUTES; col++)
        {
            //if its the pixel value.
            if (col < ATTRIBUTES){

                fscanf(inputfile, "%f,", &pixelvalue);
                data.at<float>(row,col) = pixelvalue;

            }//if its the label
            else if (col == ATTRIBUTES){
                //make the value of label column in that row as 1.
                fscanf(inputfile, "%i", &label);
                classes.at<float>(row,label) = 1.0;

            }
        }
    }

    fclose(inputfile);
}

The call is as follows :

cv::Mat test_set_classifications(TEST_SAMPLES,CLASSES,CV_32F);

cv::Mat classificationResult(1, CLASSES, CV_32F);
//load the training and test data sets.
read_dataset("C:\\Users\\Hadi\\Downloads\\output\\trainingset.txt", training_set, training_set_classifications, TRAINING_SAMPLES);
read_dataset("C:\\Users\\Hadi\\Downloads\\output\\testset.txt", test_set, test_set_classifications, TEST_SAMPLES);

I further checked the functions and the error is generating on this line

x[i-1][n1] = 1.;

in the loop for train_backprop( const Mat& inputs, const Mat& outputs, const Mat& _sw, TermCriteria termCrit )

       // backward pass, update weights
        for( i = l_count-1; i > 0; i-- )
        {
            int n1 = layer_sizes[i-1], n2 = layer_sizes[i];
            Mat _df(1, n2, CV_64F, &df[i][0]);
            multiply( grad1, _df, grad1 );
            Mat _x(n1+1, 1, CV_64F, &x[i-1][0]);
            x[i-1][n1] = 1.;
            gemm( _x, grad1, params.bpDWScale, dw[i], params.bpMomentScale, dw[i] );
            add( weights[i], dw[i], weights[i] );
            if( i > 1 )
            {
                Mat grad2(1, n1, CV_64F, buf[i&1]);
                Mat _w = weights[i].rowRange(0, n1);
                gemm( grad1, _w, 1, noArray(), 0, grad2, GEMM_2_T );
                grad1 = grad2;
            }
        }

P.S : The method is working for RPROP but not for BACKPROP.

edit retag flag offensive close merge delete

Comments

2 relevant parts of your code are missing: how do you setup the layers ? how do you fill your train data/labels ?

berak gravatar imageberak ( 2015-02-25 03:59:21 -0500 )edit
1

I have updated the question with the call please check. Thank you

Hadi gravatar imageHadi ( 2015-02-25 18:15:26 -0500 )edit

Is there really noone who knows the solution of this error? I have exactly the same problem with my opencv program.

jogoeblla777 gravatar imagejogoeblla777 ( 2015-09-11 15:33:01 -0500 )edit

for(int col = 0; col <=ATTRIBUTES; col++) <-- this is one too many, you're reading ATTRIBUTES+1 values, but only allocate ATTRIBUTES

berak gravatar imageberak ( 2015-09-12 00:56:14 -0500 )edit