Ask Your Question
0

ANN in opencv 3

asked 2017-05-23 05:05:15 -0500

alexMm1 gravatar image

Hi,

I was trying to run this example found in this forum:

#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;
using namespace cv::ml;

int main(int argc, char* argv[])
{
    //create random training data
    Mat_<float> data(100, 100);
    randn(data, Mat::zeros(1, 1, data.type()), Mat::ones(1, 1, data.type()));

    //half of the samples for each class
    Mat_<float> responses(data.rows, 2);
    for (int i = 0; i<data.rows; ++i)
    {
        if (i < data.rows/2)
        {
            data(i, 0) = 1;
            data(i, 1) = 0;
        }
        else
        {
            data(i, 0) = 0;
            data(i, 1) = 1;
        }
    }

    /*
    Mat_<float> responses(data.rows, 1);
    for (int i=0; i<responses.rows; ++i)
        responses(i, 0) = i < responses.rows / 2 ? 0 : 1;
    */

    //create the neural network
    Mat_<int> layerSizes(1, 3);
    layerSizes(0, 0) = data.cols;
    layerSizes(0, 1) = 100;
    layerSizes(0, 2) = responses.cols;

    Ptr<ANN_MLP> network = ANN_MLP::create();
    network->setLayerSizes(layerSizes);
    network->setActivationFunction(ANN_MLP::SIGMOID_SYM, 0.1, 0.1);
    network->setTrainMethod(ANN_MLP::BACKPROP, 0.1, 0.1);
    Ptr<TrainData> trainData = TrainData::create(data, ROW_SAMPLE, responses);

    network->train(trainData);
    if (network->isTrained())
    {
        printf("Predict one-vector:\n");
        Mat result;
        network->predict(Mat::ones(1, data.cols, data.type()), result);
        cout << result << endl;

        printf("Predict training data:\n");
        for (int i=0; i<data.rows; ++i)
        {
            network->predict(data.row(i), result);
            cout << result << endl;
        }
    }

    return 0;
}

Unfortunately, I'm not able to understand the meaning of the output: im my case, considering my test data, I obtain something like: [0.20409864, -0.15044725]

what does it mean? should I obtain a label class, isn't it? Am I doind something wrong?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-05-23 05:29:56 -0500

berak gravatar image

ann responses are one-hot encoded

when training e.g. a 3-class problem, and the class is 0, you would feed a [1 0 0] vector into the training, for class 2 [0 0 1], one value for each output node of the network.

in the prediction, you get the similar network output back in the response matrix, the index of the highest value denotes the class-id. you could use minMaxLoc() to determine it, or in the case of predicting on a single feature, the return value of the predict() function.

edit flag offensive delete link more

Comments

so, in my case, considering only two classes, it means that the prediction label is 0? (ps in case of two negative numbers is the max abs?)

alexMm1 gravatar imagealexMm1 ( 2017-05-23 05:37:07 -0500 )edit

yes, exactly. but not the abs value, but the larger one, those output are logit values

berak gravatar imageberak ( 2017-05-23 05:41:32 -0500 )edit

what do you mean with "larger"? for example...having [-3.7452435, -4.7302642] is 1 the prediction label, isn't it?

alexMm1 gravatar imagealexMm1 ( 2017-05-23 05:44:09 -0500 )edit

no, prediction is 0. since -3.7452435 > -4.7302642

berak gravatar imageberak ( 2017-05-23 05:54:21 -0500 )edit

is it correct that my feature data should have in the first and second position the label of the class and the features themselves in the rest? as data matrix in the example above. furthermore, having a feature vector of 200 elements with 160 training samples (4 classes 40 elements per class), which should be the correct number of layerSize?

alexMm1 gravatar imagealexMm1 ( 2017-05-24 03:07:49 -0500 )edit

are you talking about loading train data from csv files ?

layerSize is the number of layers ? the 1st(input) should have same size as your input features, the last should have 4(or nClasses) nodes

berak gravatar imageberak ( 2017-05-24 03:40:54 -0500 )edit

no no...my question is: in the example above the labels are putted in the first two position of my feature vector: position 0 and position 1 of data matrix represent the label of the two classes

for (int i = 0; i<data.rows; ++i)
    {
        if (i < data.rows/2)
        {
            data(i, 0) = 1;
            data(i, 1) = 0;
        }
        else
        {
            data(i, 0) = 0;
            data(i, 1) = 1;
        }
    }

This is not correct in my opinion, isn't it? It shoud be responses instead of data

My second question was: how to choose the numbers of hidden layers? how many neurons I have to put?

alexMm1 gravatar imagealexMm1 ( 2017-05-24 03:55:00 -0500 )edit
1

oh, you're right, that should be labels, not data !

1 or 2 hidden layers should do, and maybe make each hidden layer 1/2 size of the previous, e.g: [200,100,50,4]

berak gravatar imageberak ( 2017-05-24 04:06:09 -0500 )edit

ok great....got the point. thanks! Last question (:P): do you know how to compute a confidence of the output label?

alexMm1 gravatar imagealexMm1 ( 2017-05-24 04:09:13 -0500 )edit

hehe, no such thing as a "last question", ever ;)

and umphh, idk. there'S a SCALE_OUTPUT flag for the prediction, never tried.

berak gravatar imageberak ( 2017-05-24 04:19:46 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-05-23 05:05:15 -0500

Seen: 2,169 times

Last updated: May 23 '17