Ask Your Question

OpenCv training/testing data check error. [closed]

asked 2017-03-06 18:15:20 -0600

t24-white gravatar image


As many of you are aware i am using an MLP to use real time image recogniton to drive a car.

my testing data and training data are used from the same set of images- meaning all test data is currently also in the training set, which is why I cannot figure out when running my testNetwork funciton, it produces a score of 0/114 correct answers, as naturally the fact that it has been trained using this data and more, it should work. I know training data should be seperate but I wanted to get it working like this first..

all 3 functions are as follows, I used @berak 's code as a good baseline to adapt it to my needs, I highly recommend taking a look at it if you are learning to use Opencv MLP abilities as it is very useful.

void readScanStore(){
cv::Mat trainingData;//mat collectionn of images to train with 
cv::Mat TestData;
char const *path = "/home/pi/selfdrivingcarV1/train_data/";//needs folders of 0 1 2 3  holdig 50 pics each for representing outputs
int const numFilesDirs[]={128,217,217}; //number of photos for each direction ()LEFT =0 RIGHT =1 straight =2
char const strDirs[]={'0','1','2'}; //optional outputs " 0=go,1 right 2 left
int const numDirs = 3;//number of directions

cv::Mat TestLabels (0,0,(CV_32S));
cv::Mat trainingLabels (0,0,(CV_32S));
//same as svm??
for (int i=0;i!=numDirs; i++){//outer for loop to go through all 4 output options
    int numFiles = numFilesDirs[i];//assign inner loop based on size of samples from current outerloop val
        for (int j=0;j!=numFiles;j++){//loop through all files within current output value
            std::cout << "direction" << strDirs[i] << "file: " << j <<".jpg" << "\n";// print current output val and files associated with that direction
            std::stringstream ss;
            ss << path << strDirs[i] << "/" <<"i (" << j+1 << ").jpg";//print current working image
            cv::Mat img = cv::imread(ss.str(),0);

        if (!
        cout << "error no file found " << ss.str() << endl;

            Size size(10,10);
            Mat ImgCon;
            ImgCon =ImgCon.reshape(1,1);
            //assume img is continous
            //reshape image to 1xtotal res 
            if (j%5==0){//push every 5 images to test set
            trainingData.push_back(ImgCon); //push back image
            trainingLabels.push_back(i);//assign instruction corresponding to image in label set
    TestData.convertTo(TestData, CV_32F);
trainingData.convertTo(trainingData, CV_32F);//,1/255.0);//convert all images to cv32f (numeric values)

cv::FileStorage fs("TRAIN_VALUES.xml",FileStorage::WRITE);//store numeric values in xml file as training data values for each image.
fs << "TrainingData" <<trainingData;//assign each image 
fs << "classes" << trainingLabels;//assign associated classes

cv::FileStorage ffs("TEST_VALUES.xml",FileStorage::WRITE);//store numeric values in xml file as training data values for each image.
ffs << "TestData" <<TestData;//assign each image 
ffs << "classes" << TestLabels;//assign associated classes


void trainNetwork() {

    int nclasses = 3;
    cv::FileStorage fsa;"TRAIN_VALUES.xml", cv::FileStorage::READ);
    cv::Mat train_data;
    cv::Mat train_labels;
    fsa ...
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2017-03-08 01:14:50.775099


so, what is coming out of your prediction now ? some thoughts:

  • if (j%5==0){//push every 5 images to test set - if you'd be really strict here, it would need an else clause for the traindata, so you would not show your test images to the training.
  • yes, the train responses for the ANN have to be float.
  • if you're predicting single images/rows, you could more simply use: int pred = (int)Neural_Net->predict(test_data.row(i)); , and skip the minMaxLoc part
  • i do not think, that ~100 images per class are enough. get more data.
  • you had to "label" your traindata manually before. can you try to explain, on what your decision (left,right,straight) was based there ?
  • learning a "decision" based on images is like 100 x harder, than say, cat classification.
berak gravatar imageberak ( 2017-03-07 01:42:51 -0600 )edit

@berak currently coming out of my prediction is a set of 3 floats based on probability for left/right/forwards. I agree with you regarding the training data however this can be seperate day a later date once I see better results.

I labelled the images based on what move instruction they represented in their respective folders ... this is simply a 10x10 image which has had canny edge applied to it to reveal the track I made (black paper with a white line drawn either side) to store images based on that- you can see it on my GitHub under training data if you have had a chance.

t24-white gravatar imaget24-white ( 2017-03-07 01:54:59 -0600 )edit

ah, right. will take a look at the images ..

berak gravatar imageberak ( 2017-03-07 02:08:07 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-08 01:14:09 -0600

berak gravatar image

you currently retrieve the min pos from your prediction result, not the desired max position.

please use:

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-03-06 18:15:20 -0600

Seen: 431 times

Last updated: Mar 08 '17