Ask Your Question

Revision history [back]

SVM Response array error

Hi, I used some sort of SVM:

include "opencv2/core/core.hpp"

include "opencv2/features2d/features2d.hpp"

include "opencv2/highgui/highgui.hpp"

include "opencv2/nonfree/features2d.hpp"

include "opencv2/nonfree/nonfree.hpp"

include <iostream>

include <stdio.h>

include <stdlib.h>

using namespace std;

include <opencv2 core="" core.hpp="">

include <opencv2 highgui="" highgui.hpp="">

include <opencv2 ml="" ml.hpp="">

using namespace cv;

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

//Load Data and Labels from csv files
CvMLData mlData;
mlData.read_csv("/home/rahim/workspace/svm/DataTrain1-svm.csv");
const CvMat* tmpDataTr = mlData.get_values();
cv::Mat DataTrt(tmpDataTr, true);
tmpDataTr->CvMat::~CvMat();
cout << "M = "<< DataTrt << " "  << DataTrt << endl << endl;
Mat DataTr = DataTrt.t();

mlData.read_csv("/home/rahim/workspace/svm/LabelTrain1-svm.csv");
const CvMat* tmpLabelTr = mlData.get_values();
cv::Mat LabelTrt(tmpLabelTr, true);
tmpLabelTr->CvMat::~CvMat();
cout << "M = "<< LabelTrt << " "  << LabelTrt << endl << endl;
Mat LabelTr = LabelTrt.t();
cout << "Data size = " << DataTr.size() << endl;
cout << "labels size = " << LabelTr.size() << endl;

// Set up SVM's parameters
CvSVMParams params;
params.svm_type    = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

// Train the SVM
CvSVM SVM;
//SVM.train(trainingData, traininglabels, Mat(), Mat(), params);
SVM.train(DataTr, LabelTr, Mat(), Mat(), params);
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;
int c     = SVM.get_support_vector_count();

for (int i = 0; i < c; ++i)
{
    const float* v = SVM.get_support_vector(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);

}

but I got the following error : guments do not match (Response array must contain as many elements as the total number of samples) in cvPreprocessCategoricalResponses

the output for DataTr.size() and LabelTr.size() are [18009] and [18001] respectively

click to hide/show revision 2
No.2 Revision

SVM Response array error

Hi, I used some sort of SVM:

include "opencv2/core/core.hpp"

include "opencv2/features2d/features2d.hpp"

include "opencv2/highgui/highgui.hpp"

include "opencv2/nonfree/features2d.hpp"

include "opencv2/nonfree/nonfree.hpp"

include <iostream>

include <stdio.h>

include <stdlib.h>

#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

include <opencv2 core="" core.hpp="">

include <opencv2 highgui="" highgui.hpp="">

include <opencv2 ml="" ml.hpp="">

std; #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/ml/ml.hpp> using namespace cv;

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

CV_8UC3);

    //Load Data and Labels from csv files
 CvMLData mlData;
 mlData.read_csv("/home/rahim/workspace/svm/DataTrain1-svm.csv");
 const CvMat* tmpDataTr = mlData.get_values();
 cv::Mat DataTrt(tmpDataTr, true);
 tmpDataTr->CvMat::~CvMat();
 cout << "M = "<< DataTrt << " "  << DataTrt << endl << endl;
 Mat DataTr = DataTrt.t();

 mlData.read_csv("/home/rahim/workspace/svm/LabelTrain1-svm.csv");
 const CvMat* tmpLabelTr = mlData.get_values();
 cv::Mat LabelTrt(tmpLabelTr, true);
 tmpLabelTr->CvMat::~CvMat();
 cout << "M = "<< LabelTrt << " "  << LabelTrt << endl << endl;
 Mat LabelTr = LabelTrt.t();
 cout << "Data size = " << DataTr.size() << endl;
 cout << "labels size = " << LabelTr.size() << endl;

 // Set up SVM's parameters
 CvSVMParams params;
 params.svm_type    = CvSVM::C_SVC;
 params.kernel_type = CvSVM::LINEAR;
 params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

 // Train the SVM
 CvSVM SVM;
 //SVM.train(trainingData, traininglabels, Mat(), Mat(), params);
 SVM.train(DataTr, LabelTr, Mat(), Mat(), params);
 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;
 int c     = SVM.get_support_vector_count();

 for (int i = 0; i < c; ++i)
 {
     const float* v = SVM.get_support_vector(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);

}

}

but I got the following error : guments do not match (Response array must contain as many elements as the total number of samples) in cvPreprocessCategoricalResponses

the output for DataTr.size() and LabelTr.size() are [18009] and [18001] respectively

click to hide/show revision 3
No.3 Revision

updated 2015-10-07 02:49:28 -0600

berak gravatar image

SVM Response array error

Hi, I used some sort of SVM:

#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>

using namespace cv;

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

    //Load Data and Labels from csv files
    CvMLData mlData;
    mlData.read_csv("/home/rahim/workspace/svm/DataTrain1-svm.csv");
    const CvMat* tmpDataTr = mlData.get_values();
    cv::Mat DataTrt(tmpDataTr, true);
    tmpDataTr->CvMat::~CvMat();
    cout << "M = "<< DataTrt << " "  << DataTrt << endl << endl;
    Mat DataTr = DataTrt.t();

    mlData.read_csv("/home/rahim/workspace/svm/LabelTrain1-svm.csv");
    const CvMat* tmpLabelTr = mlData.get_values();
    cv::Mat LabelTrt(tmpLabelTr, true);
    tmpLabelTr->CvMat::~CvMat();
    cout << "M = "<< LabelTrt << " "  << LabelTrt << endl << endl;
    Mat LabelTr = LabelTrt.t();
    cout << "Data size = " << DataTr.size() << endl;
    cout << "labels size = " << LabelTr.size() << endl;

    // Set up SVM's parameters
    CvSVMParams params;
    params.svm_type    = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

    // Train the SVM
    CvSVM SVM;
    //SVM.train(trainingData, traininglabels, Mat(), Mat(), params);
    SVM.train(DataTr, LabelTr, Mat(), Mat(), params);
    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;
    int c     = SVM.get_support_vector_count();

    for (int i = 0; i < c; ++i)
    {
        const float* v = SVM.get_support_vector(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);

}

but I got the following error : guments do not match (Response array must contain as many elements as the total number of samples) in cvPreprocessCategoricalResponses

the output for DataTr.size() and LabelTr.size() are [1800[1800 * 9] and [1800[1800 * 1] respectively

SVM Response array error

Hi, I used some sort of SVM:

#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>

using namespace cv;

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

    //Load Data and Labels from csv files
    CvMLData mlData;
    mlData.read_csv("/home/rahim/workspace/svm/DataTrain1-svm.csv");
    const CvMat* tmpDataTr = mlData.get_values();
    cv::Mat DataTrt(tmpDataTr, true);
    tmpDataTr->CvMat::~CvMat();
    cout << "M = "<< DataTrt << " "  << DataTrt << endl << endl;
    Mat DataTr = DataTrt.t();

    mlData.read_csv("/home/rahim/workspace/svm/LabelTrain1-svm.csv");
    const CvMat* tmpLabelTr = mlData.get_values();
    cv::Mat LabelTrt(tmpLabelTr, true);
    tmpLabelTr->CvMat::~CvMat();
    cout << "M = "<< LabelTrt << " "  << LabelTrt << endl << endl;
    Mat LabelTr = LabelTrt.t();
    cout << "Data size = " << DataTr.size() << endl;
    cout << "labels size = " << LabelTr.size() << endl;

    // Set up SVM's parameters
    CvSVMParams params;
    params.svm_type    = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

    // Train the SVM
    CvSVM SVM;
    //SVM.train(trainingData, traininglabels, Mat(), Mat(), params);
    SVM.train(DataTr, LabelTr, Mat(), Mat(), params);
    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;
    int c     = SVM.get_support_vector_count();

    for (int i = 0; i < c; ++i)
    {
        const float* v = SVM.get_support_vector(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);

}

but I got the following error : guments do not match (Response array must contain as many elements as the total number of samples) in cvPreprocessCategoricalResponses

the output for DataTr.size() and LabelTr.size() are [1800 * 9] and [1800 * 1] respectively

SVM Response array error

Hi, I used some sort of SVM:

#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>

using namespace cv;

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

    //Load Data and Labels from csv files
    CvMLData mlData;
    mlData.read_csv("/home/rahim/workspace/svm/DataTrain1-svm.csv");
    const CvMat* tmpDataTr = mlData.get_values();
    cv::Mat DataTrt(tmpDataTr, true);
    tmpDataTr->CvMat::~CvMat();
    cout << "M = "<< DataTrt << " "  << DataTrt << endl << endl;
    Mat DataTr = DataTrt.t();

    mlData.read_csv("/home/rahim/workspace/svm/LabelTrain1-svm.csv");
    const CvMat* tmpLabelTr = mlData.get_values();
    cv::Mat LabelTrt(tmpLabelTr, true);
    tmpLabelTr->CvMat::~CvMat();
    cout << "M = "<< LabelTrt << " "  << LabelTrt << endl << endl;
    Mat LabelTr = LabelTrt.t();
    cout << "Data size = " << DataTr.size() << endl;
    cout << "labels size = " << LabelTr.size() << endl;

    // Set up SVM's parameters
    CvSVMParams params;
    params.svm_type    = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

    // Train the SVM
    CvSVM SVM;
    //SVM.train(trainingData, traininglabels, Mat(), Mat(), params);
    SVM.train(DataTr, LabelTr, Mat(), Mat(), params);
    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;
    int c     = SVM.get_support_vector_count();

    for (int i = 0; i < c; ++i)
    {
        const float* v = SVM.get_support_vector(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);

}

but I got the following error : guments do not match (Response array must contain as many elements as the total number of samples) in cvPreprocessCategoricalResponses

the output for DataTr.size() and LabelTr.size() are [1800 * 9] and [1800 * 1] respectively