Ask Your Question

t24-white's profile - activity

2017-03-07 01:54:59 -0600 commented question OpenCv training/testing data check error.

@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.

2017-03-07 01:50:08 -0600 received badge  Enthusiast
2017-03-06 18:15:20 -0600 asked a question OpenCv training/testing data check error.

Hi,

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 (!img.data)
        cout << "error no file found " << ss.str() << endl;


            Size size(10,10);
            Mat ImgCon;
            resize(img,ImgCon,size);
            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
                TestData.push_back(ImgCon);
                TestLabels.push_back(i);
            }
            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


printf("complete");
    }

void trainNetwork() {

    int nclasses = 3;
    cv::FileStorage fsa;
    fsa.open("TRAIN_VALUES.xml", cv::FileStorage::READ);
    cv::Mat train_data;
    cv::Mat train_labels;
    fsa ...
(more)
2017-03-01 14:43:10 -0600 commented question Unknown Opencv Predict error (assertion Failed)

@berak The code for image processing is exactly the same as that for the training data, I cannot see why from this when I use Cout << edges.size it presents [10 x 10], but then when i use cout<<edges it prints 300 values when surely it should be 100, this same issue occurs after the reshape function, which should convert the 10x10 image to 1x100 but instead it converts the image to 1x300, which the NN does not accept. to combat this I Use reshape(1,3) instead which creates 3x100 bits, which the NN will accept but in return due to there being 3 streams of 100 bits, it outputs 3x3 probablities for the 3 outputs per 100 bits. If that makes sense.

Any idea why this is? if you want me to link you to my gitHub so you can view then I am more than happy to.

2017-02-28 14:18:03 -0600 commented question Unknown Opencv Predict error (assertion Failed)

@berak Partial Solve- despite setting frame height and width to 10 at start of code it didnt like that, so did a resize back to 10x10 , however reshape(1,1) is converting a 10x10 image to 1x300... as a result of this NN refuses as is expecting 1x100 input, if i change reshape to reshape(1,3) it works but then I have 3 channels of 100 bits and the NN only reads the first channel. thus data is being lost.

2017-02-28 14:17:11 -0600 commented question Unknown Opencv Predict error (assertion Failed)

@berak Yes it is read from camera at 10x10 resolution, exactly the same format etc as training data.

2017-02-28 10:28:47 -0600 asked a question Unknown Opencv Predict error (assertion Failed)

I am using Opencv and having problems with the predict stage of the process.

The Preprocessing and Prediction stage are as followed:

cap.read(edges);//take frame from edges
    flat_Img = edges.reshape(1,1);//converts image row by row to 1 by x res


    flat_Img.convertTo(arr_Img,CV_32F);//,1/255.0); //converts 1 by x imaeg to an array  
    imshow("arr_img",arr_Img);//show array values on screen

    }

        Mat Result;
        Mat CurrentImg = Mat::ones(arr_Img.rows,arr_Img.cols, CV_32FC1);
        Neural_Net->predict(CurrentImg,Result);
        cout << Result << endl;

However I keep getting the error here when the predict stage occurs:

OpenCV Error: Assertion failed ((type == CV_32F || type == CV_64F) && inputs.cols == layer_sizes[0]) in predict, file /home/pi/selfdrivingcar/opencv/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 251
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/pi/selfdrivingcar/opencv/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:251: error: (-215) (type == CV_32F || type == CV_64F) && inputs.cols == layer_sizes[0] in function predict

any idea what could be causing it? I have trained the NN correctly etc but cant figure out why this wouldnt work!

2017-02-28 08:49:52 -0600 commented answer OpenCV neural Network training Error

@berak Thankyou! it works now! How does one go about loading these parameters into a Neural network which is to be used for processing? as I have not got that far

2017-02-28 08:44:47 -0600 received badge  Supporter (source)
2017-02-28 08:44:40 -0600 received badge  Scholar (source)
2017-02-27 20:03:43 -0600 commented question OpenCV neural Network training Error

N.B my output for the cerr << train_data.size() << " " << train_classes.size() << endl; is [10x2680] [3 x 2680] which seems it may be "the wrong way round"

2017-02-27 20:01:14 -0600 asked a question OpenCV neural Network training Error

I have been using the tutorial on this site titled "how to start implementing a neural network in C"( cant post link yet due to rep) and others: to create a neural network to process images of a track I have made for a car to be able to drive round it autonomously.

The process of reading in the training images (converted as required) and their respective classes works perfectly and stores to the data file Values.xml with the below code:

void readScanStore(){
cv::Mat trainingData;//mat collectionn of images to train with 
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[]={110,95,63}; //number of photos for each direction (fwd l r)
char const strDirs[]={'0','1','2'}; //optional outputs " 0=go,1 right 2 left
int const numDirs = 3;//number of directions


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] << "/" << j << ".jpg";//print current working image
            cv::Mat img = cv::imread(ss.str(),0);


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


            Size size(10,10);
            Mat ImgCon;
            resize(img,ImgCon,size);
            ImgCon.reshape(1,1);
        //assume img is continous
            //reshape image to 1xtotal res 

            trainingData.push_back(ImgCon); //push back image
            trainingLabels.push_back(i);//assign instruction corresponding to image in label set
        }
    }  

trainingData.convertTo(trainingData, CV_32F);//,1/255.0);//convert all images to cv32f (numeric values)

cv::FileStorage fs("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

printf("complete");
    }

which is then proceeded by the training procedure which loads the values of the images and uses them as training data to train the NN:

void trainNetwork(){
    int nclasses = 3;

    cv::FileStorage fs;
fs.open("VALUES.xml", cv::FileStorage::READ);
cv::Mat trainData;
cv::Mat classes;
fs["TrainingData"] >> trainData;
fs["classes"] >> classes;
    Mat confusion(nclasses,nclasses,CV_32S, Scalar(0)); // will hold our test results

 train_test(nclasses, trainData, classes, confusion);
}

which calls the following:

    void train_test(int nclasses, const Mat &train_data, const Mat &train_labels, Mat &confusion) {

        int nfeatures = train_data.cols;
        Ptr<ml::ANN_MLP> ann = ml::ANN_MLP::create();
        Mat_<int> layers(4,1);
        layers(0) = nfeatures;     // input
        layers(1) = nclasses * 8;  // hidden
        layers(2) = nclasses * 4;  // hidden
        layers(3) = nclasses;      // output, 1 pin per class.
        ann->setLayerSizes(layers);
        ann->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM,0,0);
        ann->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 300, 0.0001));
        ann->setTrainMethod(ml::ANN_MLP::BACKPROP, 0.0001);
    printf("sending data to ...
(more)
2017-02-27 09:07:32 -0600 commented answer How to start with neural network implementation with Opencv and C++??

@berak I changed the image path so it is correct (see output), From what I can see in the code there is a "no !" on line: if (image.empty()) {cerr << "no !" << fn[k] << endl; continue; } , i also added a +".jpg" on the imRead section to account for the file type. and still no luck, is there anything alse you can suggest?

2017-02-26 15:14:34 -0600 asked a question Neural Network for Image Recognition in C++/OpenCv

I am working on creating a Real-time image processor for a self driving small scale car project for uni, It uses a raspberry pi to get various information to send to the program to base a decision by.

the only stage i have left is to create a Neural network which will view the image displayed from the camera ( i already have to code to send the array of CV_32F values between 0-255 etc.

I have been scouring the internet and cannot seem to find any example code on how to implement a neural network of this kind, it will need 400 input nodes for each value (from 20x20 image) and produce 4 outputs of left right fwd or backwards respectively.

If anyone could point me in the direction of some sample code for creation/training of the neural network to do this that would be excellent as Im cutting it close on time and this is the last thing I need to do- however it is very complicated.

once it is trained I can load the neural network using ANN_MLP load etc. and pass the live stream frame (as an array of values) to it and it should be able to produce the correct output.

2017-02-26 15:01:32 -0600 commented answer How to start with neural network implementation with Opencv and C++??

@berak my output is as follows:

fold 0
no !/home/pi/selfdrivingcar/train_data/10.jpg
    no !/home/pi/selfdrivingcar/train_data/2.jpg
    no !/home/pi/selfdrivingcar/train_data/3.jpg
    no !/home/pi/selfdrivingcar/train_data/4.jpg
    no !/home/pi/selfdrivingcar/train_data/5.jpg
    no !/home/pi/selfdrivingcar/train_data/6.jpg
    no !/home/pi/selfdrivingcar/train_data/7.jpg
    no !/home/pi/selfdrivingcar/train_data/8.jpg
    no !/home/pi/selfdrivingcar/train_data/9.jpg
    no ! 
    no !
    no !
    no !
    no !
    no !

    (program exited with code:139)

what could be the problem?

2017-02-26 14:55:10 -0600 commented answer How to start with neural network implementation with Opencv and C++??

@berak hello, Ive used this to try and utilise it on linux (raspberry pi) and im struggling to make it work. i just keep getting the "no!" x10 every time i try and load it. i have 10 images named 1-10.jpg and it still wont work, any ideas?