OpenCv training/testing data check error. [closed]
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 ...
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 anelse
clause for the traindata, so you would not show your test images to the training.int pred = (int)Neural_Net->predict(test_data.row(i));
, and skip the minMaxLoc part@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.
ah, right. will take a look at the images ..