简体   繁体   English

使用不同的参数测试我的SVM模型会得出完全相同的结果

[英]Testing my SVM Model with different parameters yields exact same results

My goal is to use an SVM w/ HOG features to classify vehicles in traffic under sedans and SUVs. 我的目标是使用具有HOG功能的SVM对轿车和SUV下的车辆进行分类。

I've used various kernels (RBF, LINEAR, POLY) and each give different results, but they give the same results no matter the parameters changed. 我使用了各种内核(RBF,LINEAR,POLY),每个内核给出不同的结果,但是无论参数如何更改,它们都给出相同的结果。 For example, if I am using a POLY kernel and the degree is greater than or equal to .65 it will classify everything as an SUV, if its less than .65 then it will classify all my testing images as sedans. 例如,如果我使用的是POLY内核,并且度数大于或等于0.65,它将把一切归类为SUV;如果小于0.65,它将把我所有的测试图像归类为轿车。

With a LINEAR kernel, the only parameter changed is C. No matter what the parameter C is, I always get 8/10 images classified as sedans and the same 2 classified as SUVs. 使用LINEAR内核,唯一更改的参数是C。无论参数C是什么,我总是得到8/10分类为轿车的图像和相同的2分类为SUV的图像。

Now I only have about 70 training images and 10 testing images, I haven't been able to find a good dataset of vehicles from the rear and up like from a bridge that I will be using this for. 现在我只有大约70幅训练图像和10幅测试图像,我无法从后部和上方找到好的车辆数据集,就像从桥梁中将要使用的那样。 Could the problem be due to this small dataset, or the parameters, or something else? 问题可能是由于这个小的数据集,参数或其他原因造成的吗? Also, I see that my support vectors are usually very high, like 58 out of the 70 training images, so that may be a problem with the dataset? 另外,我看到我的支持向量通常很高,例如70幅训练图像中的58幅,这可能与数据集有关吗? Is there a way for me to visualize the training points somehow--in the SVM examples they always have a nice 2D plot of points and draw a line through it, but is there a way to plot those points with images so I can see if my data is linearly separable and make adjustments accordingly? 我有办法以某种方式可视化训练点吗?在SVM示例中,他们总是有一个漂亮的2D点图并在其中画一条线,但是有没有办法用图像来绘制这些点,以便我可以查看是否我的数据是线性可分离的,并据此进行调整? Are my HOG parameters accurate for a 150x200 image of a car? 我的HOG参数对于150x200的汽车图片准确吗?

Also note that when I use testing images that are the same as training images, then the SVM model predicts perfectly, but obviously that's cheating. 还要注意,当我使用与训练图像相同的测试图像时,SVM模型可以完美预测,但这显然是作弊行为。

The following image shows the result, and an example of a testing image 下图显示了结果以及测试图像的示例

结果

Here is my code, I didn't include most of it because I'm not sure the code is the problem. 这是我的代码,我没有包含其中的大部分内容,因为我不确定代码是否有问题。 First I take the positive images, extract HOG features, then load them into the training Mat, and then do the same for the negative images in the same way that I do for the included testing part. 首先,我拍摄正图像,提取HOG特征,然后将其加载到训练垫中,然后对负图像进行相同的处理,方法与对包含的测试部分相同。

    //Set SVM Parameters (not sure about these values, but just wanna see something)
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::POLY);
svm->setC(50);
svm->setGamma(100);
svm->setDegree(.65);
//svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

cout << "Parameters Set..." << endl;

svm->train(HOGFeat_train, ROW_SAMPLE, labels_mat);

Mat SV = svm->getSupportVectors();
Mat USV = svm->getUncompressedSupportVectors();

cout << "Support Vectors: " << SV.rows << endl;
cout << "Uncompressed Support Vectors: " << USV.rows << endl;

cout << "Training Successful" << endl;

waitKey(0);

//TESTING PORTION

cout << "Begin Testing..." << endl;

int num_test_images = 10;
Mat HOGFeat_test(1, derSize, CV_32FC1); //Creates a 1 x descriptorSize Mat to house the HoG features from the test image

for (int file_count = 1; file_count < (num_test_images + 1); file_count++)
{

    test << nameTest << file_count << type;     //'Test_1.jpg' ... 'Test_2.jpg' ... etc ...
    string filenameTest = test.str();
    test.str("");

    Mat test_image = imread(filenameTest, 0);           //Read the file folder

    HOGDescriptor hog_test;// (Size(64, 64), Size(32, 32), Size(16, 16), Size(32, 32), 9, 1, -1, 0, .2, 1, 64, false);
    vector<float> descriptors_test;
    vector<Point> locations_test;

    hog_test.compute(test_image, descriptors_test, Size(64, 64), Size(0, 0), locations_test);

    for (int i = 0; i < descriptors_test.size(); i++)
        HOGFeat_test.at<float>(0, i) = descriptors_test.at(i);

    namedWindow("Test Image", CV_WINDOW_NORMAL);
    imshow("Test Image", test_image);

    //Should return a 1 if its an SUV, or a -1 if its a sedan
    float result = svm->predict(HOGFeat_test);

    if (result <= 0)
        cout << "Sedan" << endl;
    else
        cout << "SUV" << endl;

    cout << "Result: " << result << endl;

    waitKey(0);
}

Two things solved this issue: 两件事解决了这个问题:

1) I got a larger dataset of vehicles. 1)我得到了更大的车辆数据集。 I used about 400 SUV images and 400 sedan images for the training portion and then another 50 images for the testing portion. 我在训练部分使用了大约400张SUV图像和400张轿车图像,然后在测试部分使用了另外50张图像。

2) In: Mat HOGFeat_test(1, derSize, CV_32FC1), I had the wrong derSize by about an order of magnitude larger. 2)在:Mat HOGFeat_test(1,derSize,CV_32FC1)中,我的derSize错误大约大了一个数量级。 The actual size was 15120, but I had the Mat have 113400 columns. 实际大小为15120,但是我让Mat拥有113400列。 Thus, I filled only about 10% of the testing mat with useful feature data, so it was much harder for the SVM to tell any difference between SUVs and Sedans. 因此,我仅在测试垫上填充了约10%的有用特征数据,因此SVM很难分辨出SUV和Sedans之间的区别。

Now it works great with both the linear and poly kernel (C = 10), and my accuracy is better than I expected at a whopping 96%. 现在,它既适用于线性核也适用于多边形核(C = 10),我的准确性比我预期的要好,高达96%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM