Adding speed as a feature in an image based ANN
Good day everyone, thanks for taking the time to look into my question.
based on this example : http://answers.opencv.org/question/11...
I've started implementing an ANN classification to drive a car in unity.
I read the camera feed as I control the car to record my training data.
Then I train a neural network using this code :
int nclasses = 5;
String att = FOLDER;
vector<String> fn;
Mat train_data, train_labels, test_data, test_labels;
int cnt = 0;
for (int p = 0; p < nclasses; p++)
{
cerr << "p " << p << "\r";
glob(att + std::to_string(p), fn, false);
for (int i = 0; i < fn.size(); i++)
{
cv::Mat image = cv::imread(fn[i], 0);
if (image.empty()) {
cerr << "no !" << fn[i] << endl; continue;
}
image.convertTo(image, CV_32F);//1.0/255);
resize(image, image, Size(80, 80));
Mat feature = image;
train_data.push_back(feature.reshape(1, 1));
train_labels.push_back(p);
}
}
// setup the ann:
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);
// ann requires "one-hot" encoding of class labels:
Mat train_classes = Mat::zeros(train_data.rows, nclasses, CV_32FC1);
for (int i = 0; i < train_classes.rows; i++)
{
train_classes.at<float>(i, train_labels.at<int>(i)) = 1.f;
}
cerr << train_data.size() << " " << train_classes.size() << endl;
ann->train(train_data, ml::ROW_SAMPLE, train_classes);
ann->save("output.ann");
return 0;
At runtime I then read the camera feed and predict if I should go forward, left, right... https://youtu.be/PB5NiIGFTNo
Using only 3500 images (2 laps) I have the result shown in the video.
My question is,
How can I add the speed as a feature, so that the same image won't output the same classification depending of the speed. I'm using 80x80 image, so 6400 floats, I'm afraid adding the speed as 1 float won't have any weight in the calculation.
Speed is an example, but friction, weather, mass of the car, stuff like that could also be added during the training phase.
Thanks you for your help.
Good jobs. Some questions and remarks in my bad english. 3500 X 6400 features to train a neural network how long does it takes?
Now If you forget neural network and try to localise center line to No training and I think it could be give you good results too.
Does it works if you change the track? it's a real test for a neural network.
Finally your neural network give you a vector using only line direction = road diretion. I think you have to preprocess image to extract road direction =2 features X N lines. N is equal road distance (horizontal line in road image) then you can add speed acceleration....
It would be interested to compare NN to kaman filter
Hey,
Training takes 1m38sec, running on 1 core (intel i7)
I understand I could use different techniques for good result, but I'm trying to learn more on NN at the same time.
I've tried the same track in reserve because I did the training in both directions. However I haven't tested another track. A 5-fold validation gives me around 80% of accuracy.
It makes sense to extract the lines to significantly reduce the number of features, thanks.
Is there a way to weight features differently ?
I don't know how to weight column in machine learning in opencv You can weight sample but that's not your question