Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

svm predict function always return a large number

Hi all,

I trying to implement a simple sample, the purpose is tranning and detect a number. Source as following:

Step 1: Initial data for tranning: I used a collect of images number for traning data,

    Mat vectorMatToMat(vector<Mat> list) {
        if (list.empty()) {
            return Mat();
        }
        int row = list.size();
        int col = list.at(0).rows * list.at(0).cols;
        Mat data(row, col, CV_32FC1);
        int i = 0;
        for (i = 0; i < row; i++) {
            Mat rowMat = list.at(i).reshape(1, 1);
            rowMat.copyTo(data.row(i));
        }
        return data;
    }
    Mat vectorIntToMat(vector<int> list) {
        int row = list.size();
        Mat data(row, 1, CV_32SC1, &list[0]);
        return data;
    }

    Mat dataMat = vectorMatToMat(listImageForTraining);
    Mat dataLabel = vectorIntToMat(listLabel);

Step 2: Init SVM:

Ptr<TrainData> trainData = TrainData::create(dataMat, ROW_SAMPLE, dataLabel);

Ptr<SVM> model = SVM::create();
model->setType(SVM::C_SVC);
model->setKernel(SVM::LINEAR);
model->setC(7);
model->setNu(SVM::NU_SVC);
model->setP(0);
model->setDegree(0);
model->setGamma(20);
model->setCoef0(0);
TermCriteria term(CV_TERMCRIT_ITER +CV_TERMCRIT_EPS, 1000, 1e-6);
model->setTermCriteria(term);
model->train(trainData);

Step 3: trying using SVM for predict:

    int i = 0;
    for (i = 0; i < 15; i++) {
        Mat check = dataMat.row(i);
        ostringstream oss;
        oss << i;
        imshow(oss.str(), check.reshape(1, 128));

        check = check.reshape(1, 1);
        int lable = svm.predict(check);

        cout << "Result: " << lable << endl;
    }

Step 4: Result:

Result: -1237234688
Result: 159407376
Result: 159407376
Result: 167908848
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216

As we can see, my result are very large number, although my labels are numbers in 0 to 10. I can not understand why, i think i have a mistake when I init SVM model.

But i dont know how to fix this issue, If there is any idea, please help me.

Thanks & Best Regards,

Thiep

svm predict function always return a large number

Hi all,

I trying to implement a simple sample, the purpose is tranning and detect a number. Source as following:

Step 1: Initial data for tranning: I used a collect of images number for traning data,

    Mat vectorMatToMat(vector<Mat> list) {
        if (list.empty()) {
            return Mat();
        }
        int row = list.size();
        int col = list.at(0).rows * list.at(0).cols;
        Mat data(row, col, CV_32FC1);
        int i = 0;
        for (i = 0; i < row; i++) {
            Mat rowMat = list.at(i).reshape(1, 1);
            rowMat.copyTo(data.row(i));
        }
        return data;
    }
    Mat vectorIntToMat(vector<int> list) {
        int row = list.size();
        Mat data(row, 1, CV_32SC1, &list[0]);
        return data;
    }

    Mat dataMat = vectorMatToMat(listImageForTraining);
    Mat dataLabel = vectorIntToMat(listLabel);

Step 2: Init SVM:

Ptr<TrainData> trainData = TrainData::create(dataMat, ROW_SAMPLE, dataLabel);

Ptr<SVM> model = SVM::create();
model->setType(SVM::C_SVC);
model->setKernel(SVM::LINEAR);
model->setC(7);
model->setNu(SVM::NU_SVC);
model->setP(0);
model->setDegree(0);
model->setGamma(20);
model->setCoef0(0);
TermCriteria term(CV_TERMCRIT_ITER +CV_TERMCRIT_EPS, 1000, 1e-6);
model->setTermCriteria(term);
model->train(trainData);

Step 3: trying using SVM for predict:

    int i = 0;
    for (i = 0; i < 15; i++) {
        Mat check = dataMat.row(i);
        ostringstream oss;
        oss << i;
        imshow(oss.str(), check.reshape(1, 128));

        check = check.reshape(1, 1);
        int lable = svm.predict(check);
model->predict(check);

        cout << "Result: " << lable << endl;
    }

Step 4: Result:

Result: -1237234688
Result: 159407376
Result: 159407376
Result: 167908848
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216
Result: 1065353216

As we can see, my result are very large number, although my labels are numbers in 0 to 10. I can not understand why, i think i have a mistake when I init SVM model.

But i dont know how to fix this issue, If there is any idea, please help me.

Thanks & Best Regards,

Thiep