Unknown Opencv Predict error (assertion Failed)

asked 2017-02-28 10:28:47 -0500

t24-white gravatar image

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!

edit retag flag offensive close merge delete

Comments

is that still the same network from your previous question ?

you need exactly the same image size for both training (you resized it to 10x10, remember ?) and testing

berak gravatar imageberak ( 2017-02-28 11:39:15 -0500 )edit

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

t24-white gravatar imaget24-white ( 2017-02-28 14:17:11 -0500 )edit

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

t24-white gravatar imaget24-white ( 2017-02-28 14:18:03 -0500 )edit

you probably need: Mat gray; cvtColor(frame, gray, COLOR_BGR2GRAY); , and then process the grayscale image (1 channel).

again, your image processing for training & testing has to be exactly the same !

berak gravatar imageberak ( 2017-03-01 00:36:31 -0500 )edit

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

t24-white gravatar imaget24-white ( 2017-03-01 14:43:10 -0500 )edit