opencv decision tree regression, predict unseen responses? - opencv

I am using opencv3 for Visual Studio 2017, coding in C++, on a surface pro (windows 10 64-bit).
I want to train a decision tree so that it can predict with regression. I need it to be able to predict in between responses which were not in the represented training set. For example, this code:
cv::Mat samples(6, 1, CV_32FC1);
samples.at<float>(0, 0) = 1.5;
samples.at<float>(1, 0) = 2.7;
samples.at<float>(2, 0) = 3.8;
samples.at<float>(3, 0) = 4.3;
samples.at<float>(4, 0) = 5.4;
samples.at<float>(5, 0) = 5.2;
cv::Mat responses = samples.clone();
cv::Mat samples2(5, 1, CV_32FC1);
samples2.at<float>(0, 0) = 1.8;
samples2.at<float>(1, 0) = 2.5;
samples2.at<float>(2, 0) = 3.6;
samples2.at<float>(3, 0) = 4.4;
samples2.at<float>(4, 0) = 5.3;
cv::Ptr< cv::ml::DTrees > model = cv::ml::DTrees::create();
model->setMinSampleCount(1);
model->setCVFolds(1);
model->setMaxDepth(10);
model->setRegressionAccuracy(.01f);
model->train(samples, cv::ml::ROW_SAMPLE, responses);
cv::Mat results;
model->predict(samples2, results);
for (int i = 0; i < results.rows; i++) {
for (int j = 0; j < results.cols; j++)
cout << results.at<float>(i, j) << ", ";
cout << endl;
}
The output is:
1.5
2.7
3.8
4.3
5.2
This is a simple identity problem, whatever the sample is that's what the response should be. However, from this example, it can only predict the responses it has seen. From my understanding, this is because each response leaf is a constant value. After a decision tree finishes, it lands on one of those leaves with a constant value.
My question is, does anyone know of a way to manipulate a decision tree (preferably using OpenCV) so that it can predict responses outside of what it has seen? e.g. using interpolation and/or extrapolation.

Related

OpenCV SVM prediction inconsistent?

This question has been asked here, but still no answer/solution. My problem is this: I trained an SVM (with RBF kernel) for smoke detection, using the RGB histogram distribution (in 8 bins - so M = 24) of the smoke:
cv::Mat labelsMat = cv::Mat(N, 1, CV_32SC1);
for (int i = 0; i < N; i++)
{
labelsMat.at<int>(i, 0) = labels[i];
}
cv::Mat trainingDataMat = cv::Mat(N, M, CV_32FC1);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
trainingDataMat.at<float>(i, j) = histogramData[i][j];
}
}
// Create the SVM
cv::Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::RBF);
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 1000, 1e-8));
// Train the SVM
svm->trainAuto(trainingDataMat, ml::ROW_SAMPLE, labelsMat);
svm->save(SVMFileName);
Then I saved the SVM model in a file. For the detection, after loading the SVM model:
svm = cv::ml::SVM::load(SVMFile);
I proceeded with the smoke detection; in this case to decide for each detected blob in a frame whether it's smoke or not:
for (int i = 0; i < 8; i++)
histogramData.at<float>(0, i) = Rhist[i];
for (int i = 8; i < 16; i++)
histogramData.at<float>(0, i) = Ghist[i];
for (int i = 16; i < 24; i++)
histogramData.at<float>(0, i) = Bhist[i];
float response = svm->predict(histogramData);
The frames where detection (true/false positive) occurs are saved, with the frame no. When I run this on the same video several times, each time different results (frame no.) will be produced (the blob detection always produces the same blobs). Regarding the detection, sometimes (most of the time) the smoke will be detected, but there are some cases where the same smoke will not be detected (the same video).
Anybody has any idea how to resolve this? Or is this still a known problem in OpenCV SVM?
Just realized my stupid error in the code: the indexing of Ghist & Bhist to form the data for prediction is totally incorrect, hence the inconsistencies!

Using SVMs to classify between SUVs and sedans

I am trying to implement an SVM with OpenCV that classifies images of sedans and SUVs. I have heavily referenced this post: using OpenCV and SVM with images
I have 29 training images of sedans and SUVs, and I stretch each image out to be 1 really long row, thus making my training Mat a size of 29ximage_area. The picture below shows that the training_mat comes out all in white, which I'm not sure is correct and it may be affecting my result.
This may be due to the training_mat being a float type. If the training_mat is changed to be CV_8UC1 for example, I can see clearly that each image is unfurled in the training_mat but the svm->train function does not accept the training_mat.
I use the labels_mat as the supervised version of the implementation. A 1 means an SUV, and a -1 means a sedan. In the picture below, when I attempt to use the SVM model to predict an SUV, I get a value of like -800000000000. No matter what I do (change parameters, use an all white test image, all black test image, change labels to only be 1 or -1) I always get that same -80000000000 value. Now any negative result may just mean -1 (sedan) but I cant be sure because it never changes. If anyone has insight on this that would be appreciated
Here is my code, result, and all white training_mat.
int num_train_images = 29; //29 images will be used to train the SVM
int image_area = 150 * 200;
Mat training_mat(num_train_images, image_area, CV_32FC1); // Creates a 29 rows by 30000 columns... 29 150x200 images will be put into 1 row per image
//Converts 29 2D images into a really long row per image
for (int file_count = 1; file_count < (num_train_images + 1); file_count++)
{
ss << name << file_count << type; //'Vehicle_1.jpg' ... 'Vehicle_2.jpg' ... etc ...
string filename = ss.str();
ss.str("");
Mat training_img = imread(filename, 0); //Reads the training images from the folder
int ii = 0; //Scans each column
for (int i = 0; i < training_img.rows; i++)
{
for (int j = 0; j < training_img.cols; j++)
{
training_mat.at<float>(file_count - 1, ii) = training_img.at<uchar>(i, j); //Fills the training_mat with the read image
ii++;
}
}
}
imshow("Training Mat", training_mat);
waitKey(0);
//Labels are used as the supervised learning portion of the SVM. If it is a 1, its an SUV test image. -1 means a sedan.
int labels[29] = { 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1 };
//Place the labels into into a 29 row by 1 column matrix.
Mat labels_mat(num_train_images, 1, CV_32S);
cout << "Beginning Training..." << endl;
//Set SVM Parameters (not sure about these values)
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::RBF);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->setGamma(1);
svm->setDegree(3);
cout << "Parameters Set..." << endl;
svm->train(training_mat, ROW_SAMPLE, labels_mat);
cout << "End Training" << endl;
waitKey(0);
Mat test_image(1, image_area, CV_32FC1); //Creates a 1 x 1200 matrix to house the test image.
Mat SUV_image = imread("SUV_1.jpg", 0); //Read the file folder
int jj = 0;
for (int i = 0; i < SUV_image.rows; i++)
{
for (int j = 0; j < SUV_image.cols; j++)
{
test_image.at<float>(0, jj) = SUV_image.at<uchar>(i, j); //Fills the training_mat
jj++;
}
}
//Should return a 1 if its an SUV, or a -1 if its a sedan
float result = svm->predict(test_image);
if (result < 0)
cout << "Sedan" << endl;
else
cout << "SUV" << endl;
cout << "Result: " << result << endl;
namedWindow("Test Image", CV_WINDOW_NORMAL);
imshow("Test Image", SUV_image);
waitKey(0);
Refer to this post for a solution to this problem I was having. Using SVM with HOG Features to Classify Vehicles
In this, I use HOG features instead of just plain pixel values of the images. The training_mat is no longer white, and the classifier works well. Additionally, the output result is a 1 or -1.

partition a set of images into k clusters with opencv

I have an image data set that I would like to partition into k clusters. I am trying to use the opencv implementation of k-means clustering.
Firstly, I store my Mat images into a vector of Mat and then I am trying to use the kmeans function. However, I am getting an assertion error.
Should the images be stored into a different kind of structure? I have read the k-means documentation and I dont seem to understand what I am doing wrong. This is my code:
Thank you in advance,
vector <Mat> images;
string folder = "D:\\football\\positive_clustering\\";
string mask = "*.bmp";
vector<string> files = getFileList(folder + mask);
for (int i = 0; i < files.size(); i++)
{
Mat img = imread(folder + files[i]);
images.push_back(img);
}
cout << "Vector of positive samples created" << endl;
int k = 10;
cv::Mat bestLabels;
cv::kmeans(images, k, bestLabels, TermCriteria(), 3, KMEANS_PP_CENTERS);
//have a look
vector<cv::Mat> clusterViz(bestLabels.rows);
for (int i = 0; i<bestLabels.rows; i++)
{
clusterViz[bestLabels.at<int>(i)].push_back(cv::Mat(images[bestLabels.at<int>(i)]));
}
namedWindow("clusters", WINDOW_NORMAL);
for (int i = 0; i<clusterViz.size(); i++)
{
cv::imshow("clusters", clusterViz[i]);
cv::waitKey();
}

How can I get the Kernel of a SVM classifier in OpenCV?

I developed a multi-class SVM with OpenCV 3.0 and I want to compute the distance between each class and data (input) in order to estimate the confidence of the prediction.
I used the below code at link1 but I have errors when I try to get the Kernel of my SVM!
Thanks for your help.
Mat sv = svm->getSupportVectors();
Ptr<SVM::Kernel> kernel = svm->getKernel(); // ??
Mat buffer(1, sv.rows, CV_32F);
kernel->calc(sv.rows, sv.cols, sv.ptr<float>(), data.ptr<float>(), buffer.ptr<float>()); // apply kernel on data (CV_32F vector) and support vectors
Mat alpha, svidx;
int N = 11;
vector<int> votes(N, 0); // results of majority vote will be stored here (N is number of classes)
int i, j, dfi;
for (i = dfi = 0; i < N; i++)
{
for (j = i + 1; j < N; j++, dfi++)
{
// compute score for each binary svm
double rho = svm->getDecisionFunction(dfi, alpha, svidx);
double sum = -rho;
for (int k = 0; k < sv.rows; k++)
sum += alpha.at<float>(k)*buffer.at<float>(sv.at<int>(k));
// majority vote
votes[sum > 0 ? i : j]++;
}
}

cv::SVM response one class for every sample

I am new in Match faces , I am trying to learn how to use SVM with HOG descriptors.
I wrote a simple face recognizer with SVM, but when i activate it , code always returns 1
float *getHOG(const cv::Mat &image, int* count)//Compute HOG
{
cv::HOGDescriptor hog;
std::vector<float> res;
cv::Mat img2;
cv::resize(image, img2, cv::Size(64, 128));
hog.compute(img2, res, cv::Size(8, 8), cv::Size(0, 0));
*count = res.size();
float* result = new float[*count];
for(int i = 0; i < res.size(); i++)
{
result[i] = res[i];
}
return result;
}
const int dataSetLength = 10;
float **getTraininigData(int* setlen, int* veclen)//Load some samples of data
{
char *names[dataSetLength] = {
"../faces/s1/1.pgm",
"../faces/s1/2.pgm",
"../faces/s1/3.pgm",
"../faces/s1/4.pgm",
"../faces/s1/5.pgm",
"../faces/cars/1.jpg",
"../faces/cars/2.jpg",
"../faces/cars/3.jpg",
"../faces/cars/4.jpg",
"../faces/cars/5.jpg",
};
float **res = new float* [dataSetLength];
for(int i = 0; i < dataSetLength; i++)
{
std::cout<<names[i]<<"\n";
cv::Mat img = cv::imread(names[i], 0);
res[i] = getHOG(img, veclen);
}
*setlen = dataSetLength;
return res;
}
void test()//Training and activate SVM
{
int setlen, veclen;
float **trainingData = getTraininigData(&setlen, &veclen);
float *labels = new float[dataSetLength];
for(int i = 0; i < dataSetLength; i++)
{
labels[i] = (i < dataSetLength/2)? 0.0 : 1.0;
}
cv::Mat labelsMat(setlen, 1, CV_32FC1, labels);
cv::Mat trainingDataMat(setlen, veclen, CV_32FC1, trainingData);
cv::SVMParams params;
params.svm_type = cv::SVM::C_SVC;
params.kernel_type = cv::SVM::LINEAR;
params.term_crit = cv::TermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
std::cout<<labelsMat<<"\n";
cv::SVM SVM;
SVM.train(trainingDataMat, labelsMat, cv::Mat(), cv::Mat(), params);
cv::Mat img = cv::imread("../faces/s1/2.pgm", 0);//sample from train data, but ansewer is 1 for every sample
auto desc = getHOG(img, &veclen);
cv::Mat sampleMat(1, veclen, CV_32FC1, desc);
float response = SVM.predict(sampleMat);
std::cout<<"resp "<< response<<"\n";
}
What wrong with my code ?
PS sorry for my writing mistakes. English in not my native language
You don't have much training data. Note how Dalal and Triggs in their original paper on HOG (http://lear.inrialpes.fr/people/triggs/pubs/Dalal-cvpr05.pdf) used thousands of examples to train the SVM, you have just 5 negative and 5 positive.
You haven't set the C parameter (you need to find a good value via cross validation) - you will need more data.
Possibly HOG descriptors for faces and cars are not separable with a linear kernel, try RBF.
But this is unlikely to be an issue since D&L use a linear SVM in their paper.
Read this: http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf
If you haven't done this yet, get the SVM working for a simpler case (e.g. just use image patches instead of HOG).

Resources