【问题标题】:SVM Predict returns a large value that isnt 1 or -1SVM Predict 返回一个非 1 或 -1 的大值
【发布时间】:2018-06-02 13:25:27
【问题描述】:

所以我的目标是在轿车和 SUV 之间对车辆进行分类。我使用的训练图像是 29 个 150x200 的轿车和 SUV 图像,所以我的 training_mat 是一个 29x30000 的垫子,我使用双嵌套 for 循环而不是 .reshape 来执行此操作,因为 reshape 无法正常工作。

labels_mat 被写成 -1 对应于轿车,1 对应于 SUV。我终于让 svm->train 接受两个 Mats,并且我希望输入 svm->predict 的新 test_image 会产生 -1 或 1。不幸的是,svm->predict(test_image) 返回一个极高或极低值如 -8.38e08。谁能帮我解决这个问题?

这是我的大部分代码:

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->setC(.1);
svm->setKernel(SVM::POLY);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->setGamma(3);
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);    
        jj++;
    }
}

//Should return a 1 if its an SUV, or a -1 if its a sedan

float result = svm->predict(test_image);

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

【问题讨论】:

    标签: opencv machine-learning svm


    【解决方案1】:

    您忘记将标签填入labels_mat。一个简单的错误,但它发生在每个人身上......

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

    这应该很好。

    【讨论】:

      【解决方案2】:

      输出不会是 -1 和 1。机器学习方法(例如 SVM)将成员资格预测为结果的符号。所以负值表示-1,正值表示1。

      类似地,其他一些方法,例如逻辑回归方法,使用概率来预测成员资格,其中经常有0和1。如果概率

      顺便说一句:你的问题不是 C++ 问题。

      【讨论】:

        猜你喜欢
        • 2015-04-20
        • 2016-02-28
        • 2020-01-22
        • 2017-05-31
        • 1970-01-01
        • 2016-11-25
        • 1970-01-01
        • 2011-12-27
        • 2013-06-12
        相关资源
        最近更新 更多