Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

the train responses for an ann differ a bit from the usual opencv ml approach.

if you have 2 output neurons in your ann, you need 2 output neurons for each training feature too, not a single "class label" (like with e.g. an SVM).

it should look like this:

[train_data]                [train_responses]

lbpfeature1                 -1   1     // class A
lbpfeature2                 -1   1     // class A
lbpfeature3                 1   -1     // class B
lbpfeature4                 1   -1     // class B

so, the responses must have num_features rows X 2 cols.

one way (there are endless..) would be:

    // instead of 
    //Lables.push_back(Class1);
    Lables.push_back(1);
    Lables.push_back(-1);

    // and instead of 
    //Lables.push_back(Class2);
    Lables.push_back(-1);
    Lables.push_back(1);

    // then, before training, reshape to N x 2:
    Labels = Labels.reshape(1, Labels.rows / 2);

the train responses for an ann differ a bit from the usual opencv ml approach.

if you have 2 output neurons in your ann, you need 2 output neurons for each training feature too, not a single "class label" (like with e.g. an SVM).

it should look like this:

[train_data]                [train_responses]

lbpfeature1                 -1   1     // class A
lbpfeature2                 -1   1     // class A
lbpfeature3                 1   -1     // class B
lbpfeature4                 1   -1     // class B

so, the responses must have num_features rows X 2 cols.

one way (there are endless..) would be:

    // leave Labels empty (btw, your original code seems to leave the 1st element empty)
    // Lables = Mat(numberOfClass1 + numberOfClass2,1 , CV_32SC1);

    // instead of 
    //Lables.push_back(Class1);
    Lables.push_back(1);
    Lables.push_back(-1);

    // and instead of 
    //Lables.push_back(Class2);
    Lables.push_back(-1);
    Lables.push_back(1);

    // then, before training, reshape to N x 2:
    Labels = Labels.reshape(1, Labels.rows / 2);

the train responses for an ann differ a bit from the usual opencv ml approach.

if you have 2 output neurons in your ann, you need 2 output neurons for each training feature too, not a single "class label" (like with e.g. an SVM).

it should look like this:

[train_data]                [train_responses]

lbpfeature1                 -1   1     // class A
lbpfeature2                 -1   1     // class A
lbpfeature3                 1   -1     // class B
lbpfeature4                 1   -1     // class B

so, the responses must have num_features rows X 2 cols.

one way (there are endless..) would be:

    // leave Labels empty (btw, your original code seems to leave the 1st element empty)
    // Lables = Mat(numberOfClass1 + numberOfClass2,1 , CV_32SC1);

    // instead of 
    //Lables.push_back(Class1);
    Lables.push_back(1);
    Lables.push_back(-1);
Lables.push_back(1.0f);
    Lables.push_back(-1.0f);

    // and instead of 
    //Lables.push_back(Class2);
    Lables.push_back(-1);
    Lables.push_back(1);
Lables.push_back(-1.0f);
    Lables.push_back(1.0f);

    // then, before training, reshape to N x 2:
    Labels = Labels.reshape(1, Labels.rows / 2);