SVM 预测 returns 一个不是 1 或 -1 的大值
SVM Predict returns a large value that isnt 1 or -1
所以我的目标是在轿车和 SUV 之间对车辆进行分类。我使用的训练图像是轿车和 SUV 的 29 张 150x200 图像,所以我的 training_mat 是一个 29x30000 垫子,我使用双嵌套 for 循环而不是 .reshape 来执行此操作,因为重塑无法正常工作。
labels_mat这样写,-1对应轿车,1对应SUV。我终于让 svm->train 接受了两个垫子,我预计一个新的 test_image 被输入 svm->predict 会产生 -1 或 1。不幸的是, svm->predict(test_image) returns 极高或极低的值,例如 -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;
输出不会是 -1 和 1。机器学习方法,例如 SVM,预测成员资格作为结果的标志。所以负值表示 -1,正值表示 1。
类似地,其他一些方法,如逻辑回归方法,使用概率来预测隶属度,其中经常出现0和1。如果概率<0.5,则其隶属度为0,否则为1。
顺便说一句:你的问题不是 C++ 问题。
您忘记将标签填写到 labels_mat。一个简单的错误,但每个人都会发生...
垫子 labels_mat(num_train_images, 1, CV_32S, 标签);
那应该没问题。
所以我的目标是在轿车和 SUV 之间对车辆进行分类。我使用的训练图像是轿车和 SUV 的 29 张 150x200 图像,所以我的 training_mat 是一个 29x30000 垫子,我使用双嵌套 for 循环而不是 .reshape 来执行此操作,因为重塑无法正常工作。
labels_mat这样写,-1对应轿车,1对应SUV。我终于让 svm->train 接受了两个垫子,我预计一个新的 test_image 被输入 svm->predict 会产生 -1 或 1。不幸的是, svm->predict(test_image) returns 极高或极低的值,例如 -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;
输出不会是 -1 和 1。机器学习方法,例如 SVM,预测成员资格作为结果的标志。所以负值表示 -1,正值表示 1。
类似地,其他一些方法,如逻辑回归方法,使用概率来预测隶属度,其中经常出现0和1。如果概率<0.5,则其隶属度为0,否则为1。
顺便说一句:你的问题不是 C++ 问题。
您忘记将标签填写到 labels_mat。一个简单的错误,但每个人都会发生...
垫子 labels_mat(num_train_images, 1, CV_32S, 标签);
那应该没问题。