使用具有HOG功能的SVM对车辆进行分类

使用具有HOG功能的SVM对车辆进行分类

本文介绍了使用具有HOG功能的SVM对车辆进行分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用SVM和HOG功能在SUV和轿车之间进行分类.

My goal here is to classify between SUVs and sedans using SVMs and HOG features.

首先,我阅读86幅训练图像,为每个图像计算HOG功能,然后将它们放在大小为86xdescriptorSize的训练垫中,称为HOGFeat_train.

First I read 86 training images, calculate the HOG features for each of them, and put them in a training Mat that is of size 86xdescriptorSize called HOGFeat_train.

Mat HOGFeat_train(num_train_images, derSize, CV_32FC1); //86xdescriptor size training Mat

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

    HOGDescriptor hog_train;
    vector<float> descriptors_train;
    vector<Point> locations_train;

    hog_train.compute(training_img, descriptors_train, Size(16, 16), Size(0, 0), locations_train); //not sure about these values

    for (int i = 0; i < descriptors_train.size(); i++)
        HOGFeat_train.at<float>(file_count-1, i) = descriptors_train.at(i);
}

接下来,我为SVM的监督学习部分创建一个86个标签的labels_mat(我知道这种方式不切实际且耗时,稍后将对此进行修复). 1表示SUV,-1表示轿车.不确定这些SVM参数,但是我尝试了不同的品种和值,但所有结果都是相同的.

Next I create a labels_mat of 86 labels for the supervised learning portion of the SVM (I know this way is impractical and time consuming, which I'll fix later). 1 means SUV, and a -1 means a sedan. Not sure about these SVM Parameters but I've tried different varieties and values but all results are the same.

float labels[86] = { 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, -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, -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};

Mat labels_mat(num_train_images, 1, CV_32S);

cout << "Beginning Training..." << endl;

Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
//svm->setDegree(3);
//svm->setGamma(2);
//svm->setC(.5);


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

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

cout << "Training Successful" << endl;

接下来,我以与训练图像相同的方式读取10张测试图像,然后再次计算HOG功能.在计算出HOG功能后,将它们放入1行x描述符化的HOGFeat_test Mat中,然后在该HOGFeat_test Mat上使用svm-> predict,该函数应返回值-1表示轿车或1表示SUV.

Next I read 10 test images the same way I did with the train images, and compute the HOG features again. After the HOG features are computed they are placed into 1 row x descriptorSized HOGFeat_test Mat, and then I use svm->predict on that HOGFeat_test Mat which should return a value of -1 to denote a sedan or 1 to denote an SUV.

    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++)
{

    ss2 << name2 << file_count << type2;        //'Test_1.jpg' ... 'Test_2.jpg' ... etc ...
    string filename2 = ss2.str();
    ss2.str("");

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

    HOGDescriptor hog_test;
    vector<float> descriptors_test;
    vector<Point> locations_test;

    hog_test.compute(test_image, descriptors_test, Size(16, 16), 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;

下图显示了结果,测试图像和HOGFeat_train Mat,以防对任何人有用.无论我使用什么值,参数或图像,结果(Sedan,-8.412e08)始终相同.结果不是-1或1,而是-800000000000,我假设负值对应于-1,但最重要的是,我想知道为什么结果没有变化.有人对此有任何见识吗?谢谢.

The following image shows the result, a test image, and the HOGFeat_train Mat in case it's useful to anyone. The result (Sedan, -8.412e08) is always the same no matter what values or parameters or images I use. The result is not a -1 or a 1 but -800000000000 and I'm assuming a negative value corresponds to a -1, but most importantly I'd like to know why the result isn't changing. Does anyone have any insight of this? Thanks.

编辑----------------------------------------

EDIT----------------------------------------

我从浮动标签[86]中删除了所有标签,然后简单地将其保留为浮动标签[86]; //{1、1,-1等...}

I removed all of the ones from float labels[86] and simply left it as float labels[86]; //{1, 1, -1, etc...}

这表明SVM结果没有差异,并且仍然能够成功进行训练.这告诉我,我的标签arent通过svm-> train函数之类的东西.我将继续调查.

This showed no difference in the SVM result and it was still able to train successfully. This tells me that my labels arent going through the svm->train function or something. I will continue to investigate.

推荐答案

所以这只是我对自己犯的一个愚蠢的错误.我用浮动标签替换了标签[86];基本上,我只是删除了SVM的监督学习部分,但得到的结果却完全相同.经过检查,我意识到我没有将标签填充到labels_mat中!!!!!

So this one was just a dumb mistake that I did to myself. I replaced the labels with float labels[86]; Basically I just removed the supervised learning portion of the SVM, yet I got the exact same results. Upon inspection I realize that I didnt fill the labels into the labels_mat!!!!!!

因此,执行以下操作可获得明确的解决方案.此外,结果变为1和-1,而不是-8.412e-8.

So performing the following results in a clear solution. Additionally, the result becomes 1 and -1 instead of -8.412e-8.

垫子标签垫(num_train_images,1,CV_32S,标签);

Mat labels_mat(num_train_images, 1, CV_32S, labels);

这篇关于使用具有HOG功能的SVM对车辆进行分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:58