Opencv Unable to Set Up Svm Parameters

OpenCV unable to set up SVM Parameters

A lot of things changed from OpenCV 2.4 to OpenCV 3.0. Among others, the machine learning module, which isn't backward compatible.

This is the OpenCV tutorial code for the SVM, update for OpenCV 3.0:

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>

using namespace cv;
using namespace cv::ml;

int main(int, char**)
{
// Data for visual representation
int width = 512, height = 512;
Mat image = Mat::zeros(height, width, CV_8UC3);

// Set up training data
int labels[4] = { 1, -1, -1, -1 };
Mat labelsMat(4, 1, CV_32SC1, labels);

float trainingData[4][2] = { { 501, 10 }, { 255, 10 }, { 501, 255 }, { 10, 501 } };
Mat trainingDataMat(4, 2, CV_32FC1, trainingData);

// Set up SVM's parameters
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

// Train the SVM with given parameters
Ptr<TrainData> td = TrainData::create(trainingDataMat, ROW_SAMPLE, labelsMat);
svm->train(td);

// Or train the SVM with optimal parameters
//svm->trainAuto(td);

Vec3b green(0, 255, 0), blue(255, 0, 0);
// Show the decision regions given by the SVM
for (int i = 0; i < image.rows; ++i)
for (int j = 0; j < image.cols; ++j)
{
Mat sampleMat = (Mat_<float>(1, 2) << j, i);
float response = svm->predict(sampleMat);

if (response == 1)
image.at<Vec3b>(i, j) = green;
else if (response == -1)
image.at<Vec3b>(i, j) = blue;
}

// Show the training data
int thickness = -1;
int lineType = 8;
circle(image, Point(501, 10), 5, Scalar(0, 0, 0), thickness, lineType);
circle(image, Point(255, 10), 5, Scalar(255, 255, 255), thickness, lineType);
circle(image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType);
circle(image, Point(10, 501), 5, Scalar(255, 255, 255), thickness, lineType);

// Show support vectors
thickness = 2;
lineType = 8;
Mat sv = svm->getSupportVectors();

for (int i = 0; i < sv.rows; ++i)
{
const float* v = sv.ptr<float>(i);
circle(image, Point((int)v[0], (int)v[1]), 6, Scalar(128, 128, 128), thickness, lineType);
}

imwrite("result.png", image); // save the image

imshow("SVM Simple Example", image); // show it to the user
waitKey(0);

}

The output should look like:

Sample Image

How to adjust parameter of SVM for better training

You'd getter better results with searching for the optimal parameter values using trainAuto. You cannot copy the param values from some other application and expect to get good results on your own data.

When you call say svm.getType() you get an int value, for example 100. To understand then the type see this page, under Public Types you find this:

enum Types { C_SVC =100, NU_SVC =101, ONE_CLASS =102,

EPS_SVR =103, NU_SVR =104 }

Hence, 100 means C_SVC. And for the kernel type you find:

enum KernelTypes { CUSTOM =-1, LINEAR =0, POLY =1, RBF
=2, SIGMOID =3, CHI2 =4, INTER =5 }

Then 2 means the RBF kernel yields the best accuracy on your data.

SVM Training Data Error

the problem was that the test vector should have 1 row, and 2 cols

 cv::Mat testDataMat(1, 2, CV_32FC1, testData);

OpenCV error SVM identifier undefined

as of opencv3.0, you will have to use:

Ptr<ml::SVM> svm = ml::SVM::create();

(no, you can't use a 'stack instance, like SVM svm; anymore. also note the additional namespace)

(( also, if all you got is a youtube video, you basically got nothing ))

OpenCV SVM Kernel Sample

You can get the score with 2-class SVM, and if you pass RAW_OUTPUT to predict:

// svm.cpp, SVMImpl::predict(...) , line 1917 
bool returnDFVal = (flags & RAW_OUTPUT) != 0;

// svm.cpp, PredictBody::operator(), line 1896,
float result = returnDFVal && class_count == 2 ?
(float)sum : (float)(svm->class_labels.at<int>(k));

Then you need to train 4 different 2 class SVM, one against rest.

These are the result I get on these samples:

Sample Image

INTER with trainAuto

Sample Image

CHI2 with trainAuto

Sample Image

RBF with train (C = 0.1, gamma = 0.001) (trainAuto overfits in this case)

Sample Image

Here is the code. You can enable trainAuto with AUTO_TRAIN_ENABLED boolean variable, and you can set the KERNEL as well as images dimensions, etc.

#include <opencv2/opencv.hpp>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;
using namespace cv::ml;

int main()
{
const int WIDTH = 512;
const int HEIGHT = 512;
const int N_SAMPLES_PER_CLASS = 10;
const float NON_LINEAR_SAMPLES_RATIO = 0.1;
const int KERNEL = SVM::CHI2;
const bool AUTO_TRAIN_ENABLED = false;

int N_NON_LINEAR_SAMPLES = N_SAMPLES_PER_CLASS * NON_LINEAR_SAMPLES_RATIO;
int N_LINEAR_SAMPLES = N_SAMPLES_PER_CLASS - N_NON_LINEAR_SAMPLES;

vector<Scalar> colors{Scalar(255,0,0), Scalar(0,255,0), Scalar(0,0,255), Scalar(0,255,255)};
vector<Vec3b> colorsv{ Vec3b(255, 0, 0), Vec3b(0, 255, 0), Vec3b(0, 0, 255), Vec3b(0, 255, 255) };
vector<Vec3b> colorsv_shaded{ Vec3b(200, 0, 0), Vec3b(0, 200, 0), Vec3b(0, 0, 200), Vec3b(0, 200, 200) };

Mat1f data(4 * N_SAMPLES_PER_CLASS, 2);
Mat1i labels(4 * N_SAMPLES_PER_CLASS, 1);

RNG rng(0);

////////////////////////
// Set training data
////////////////////////

// Class 1
Mat1f class1 = data.rowRange(0, 0.5 * N_LINEAR_SAMPLES);
Mat1f x1 = class1.colRange(0, 1);
Mat1f y1 = class1.colRange(1, 2);
rng.fill(x1, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y1, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT / 8));

class1 = data.rowRange(0.5 * N_LINEAR_SAMPLES, 1 * N_LINEAR_SAMPLES);
x1 = class1.colRange(0, 1);
y1 = class1.colRange(1, 2);
rng.fill(x1, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y1, RNG::UNIFORM, Scalar(7*HEIGHT / 8), Scalar(HEIGHT));

class1 = data.rowRange(N_LINEAR_SAMPLES, 1 * N_SAMPLES_PER_CLASS);
x1 = class1.colRange(0, 1);
y1 = class1.colRange(1, 2);
rng.fill(x1, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y1, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));

// Class 2
Mat1f class2 = data.rowRange(N_SAMPLES_PER_CLASS, N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES);
Mat1f x2 = class2.colRange(0, 1);
Mat1f y2 = class2.colRange(1, 2);
rng.fill(x2, RNG::NORMAL, Scalar(3 * WIDTH / 4), Scalar(WIDTH/16));
rng.fill(y2, RNG::NORMAL, Scalar(HEIGHT / 2), Scalar(HEIGHT/4));

class2 = data.rowRange(N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES, 2 * N_SAMPLES_PER_CLASS);
x2 = class2.colRange(0, 1);
y2 = class2.colRange(1, 2);
rng.fill(x2, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y2, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));

// Class 3
Mat1f class3 = data.rowRange(2 * N_SAMPLES_PER_CLASS, 2 * N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES);
Mat1f x3 = class3.colRange(0, 1);
Mat1f y3 = class3.colRange(1, 2);
rng.fill(x3, RNG::NORMAL, Scalar(WIDTH / 4), Scalar(WIDTH/8));
rng.fill(y3, RNG::NORMAL, Scalar(HEIGHT / 2), Scalar(HEIGHT/8));

class3 = data.rowRange(2*N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES, 3 * N_SAMPLES_PER_CLASS);
x3 = class3.colRange(0, 1);
y3 = class3.colRange(1, 2);
rng.fill(x3, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y3, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));

// Class 4
Mat1f class4 = data.rowRange(3 * N_SAMPLES_PER_CLASS, 3 * N_SAMPLES_PER_CLASS + 0.5 * N_LINEAR_SAMPLES);
Mat1f x4 = class4.colRange(0, 1);
Mat1f y4 = class4.colRange(1, 2);
rng.fill(x4, RNG::NORMAL, Scalar(WIDTH / 2), Scalar(WIDTH / 16));
rng.fill(y4, RNG::NORMAL, Scalar(HEIGHT / 4), Scalar(HEIGHT / 16));

class4 = data.rowRange(3 * N_SAMPLES_PER_CLASS + 0.5 * N_LINEAR_SAMPLES, 3 * N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES);
x4 = class4.colRange(0, 1);
y4 = class4.colRange(1, 2);
rng.fill(x4, RNG::NORMAL, Scalar(WIDTH / 2), Scalar(WIDTH / 16));
rng.fill(y4, RNG::NORMAL, Scalar(3 * HEIGHT / 4), Scalar(HEIGHT / 16));

class4 = data.rowRange(3 * N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES, 4 * N_SAMPLES_PER_CLASS);
x4 = class4.colRange(0, 1);
y4 = class4.colRange(1, 2);
rng.fill(x4, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y4, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));

// Labels
labels.rowRange(0*N_SAMPLES_PER_CLASS, 1*N_SAMPLES_PER_CLASS).setTo(1);
labels.rowRange(1*N_SAMPLES_PER_CLASS, 2*N_SAMPLES_PER_CLASS).setTo(2);
labels.rowRange(2*N_SAMPLES_PER_CLASS, 3*N_SAMPLES_PER_CLASS).setTo(3);
labels.rowRange(3*N_SAMPLES_PER_CLASS, 4*N_SAMPLES_PER_CLASS).setTo(4);

// Draw training data
Mat3b samples(HEIGHT, WIDTH, Vec3b(0,0,0));
for (int i = 0; i < labels.rows; ++i)
{
circle(samples, Point(data(i, 0), data(i, 1)), 3, colors[labels(i,0) - 1], CV_FILLED);
}

//////////////////////////
// SVM
//////////////////////////

// SVM label 1
Ptr<SVM> svm1 = SVM::create();
svm1->setType(SVM::C_SVC);
svm1->setKernel(KERNEL);

Mat1i labels1 = (labels != 1) / 255;

if (AUTO_TRAIN_ENABLED)
{
Ptr<TrainData> td1 = TrainData::create(data, ROW_SAMPLE, labels1);
svm1->trainAuto(td1);
}
else
{
svm1->setC(0.1);
svm1->setGamma(0.001);
svm1->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
svm1->train(data, ROW_SAMPLE, labels1);
}

// SVM label 2
Ptr<SVM> svm2 = SVM::create();
svm2->setType(SVM::C_SVC);
svm2->setKernel(KERNEL);

Mat1i labels2 = (labels != 2) / 255;

if (AUTO_TRAIN_ENABLED)
{
Ptr<TrainData> td2 = TrainData::create(data, ROW_SAMPLE, labels2);
svm2->trainAuto(td2);
}
else
{
svm2->setC(0.1);
svm2->setGamma(0.001);
svm2->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
svm2->train(data, ROW_SAMPLE, labels2);
}

// SVM label 3
Ptr<SVM> svm3 = SVM::create();
svm3->setType(SVM::C_SVC);
svm3->setKernel(KERNEL);

Mat1i labels3 = (labels != 3) / 255;

if (AUTO_TRAIN_ENABLED)
{
Ptr<TrainData> td3 = TrainData::create(data, ROW_SAMPLE, labels3);
svm3->trainAuto(td3);
}
else
{
svm3->setC(0.1);
svm3->setGamma(0.001);
svm3->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
svm3->train(data, ROW_SAMPLE, labels3);
}

// SVM label 4
Ptr<SVM> svm4 = SVM::create();
svm4->setType(SVM::C_SVC);
svm4->setKernel(KERNEL);

Mat1i labels4 = (labels != 4) / 255;

if (AUTO_TRAIN_ENABLED)
{
Ptr<TrainData> td4 = TrainData::create(data, ROW_SAMPLE, labels4);
svm4->trainAuto(td4);
}
else
{
svm4->setC(0.1);
svm4->setGamma(0.001);
svm4->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
svm4->train(data, ROW_SAMPLE, labels4);
}

//////////////////////////
// Show regions
//////////////////////////

Mat3b regions(HEIGHT, WIDTH);
Mat1f R(HEIGHT, WIDTH);

Mat1f R1(HEIGHT, WIDTH);
Mat1f R2(HEIGHT, WIDTH);
Mat1f R3(HEIGHT, WIDTH);
Mat1f R4(HEIGHT, WIDTH);

for (int r = 0; r < HEIGHT; ++r)
{
for (int c = 0; c < WIDTH; ++c)
{
Mat1f sample = (Mat1f(1,2) << c, r);

vector<float> responses(4);

responses[0] = svm1->predict(sample, noArray(), StatModel::RAW_OUTPUT);
responses[1] = svm2->predict(sample, noArray(), StatModel::RAW_OUTPUT);
responses[2] = svm3->predict(sample, noArray(), StatModel::RAW_OUTPUT);
responses[3] = svm4->predict(sample, noArray(), StatModel::RAW_OUTPUT);

int best_class = distance(responses.begin(), max_element(responses.begin(), responses.end()));
float best_response = responses[best_class];

// View responses for each SVM, and the best responses
R(r,c) = best_response;
R1(r, c) = responses[0];
R2(r, c) = responses[1];
R3(r, c) = responses[2];
R4(r, c) = responses[3];

if (best_response >= 0) {
regions(r, c) = colorsv[best_class];
}
else {
regions(r, c) = colorsv_shaded[best_class];
}

}
}

imwrite("svm_samples.png", samples);
imwrite("svm_x.png", regions);

imshow("Samples", samples);
imshow("Regions", regions);
waitKey();

return 0;
}


Related Topics



Leave a reply



Submit