Memory allocation problem with SVMs in OpenCV

Posted by worksintheory on Stack Overflow See other posts from Stack Overflow or by worksintheory
Published on 2011-02-13T23:22:59Z Indexed on 2011/02/13 23:25 UTC
Read the original article Hit count: 565

Filed under:
|
|
|
|

Hi,

I've been using OpenCV happily for a while, but now I have a problem which has bugged me for quite some time.

The following code is reasonably minimal example of my problem:

#include <cv.h>
#include <ml.h>
using namespace cv;

int main(int argc, char **argv) {

    int sampleCountForTesting = 2731; //BROKEN: Breaks svm.train_auto(...) for values of 2731 or greater!
    Mat trainingData( sampleCountForTesting, 1, CV_32FC1, Scalar::all(0.0) );
    Mat trainingResponses( sampleCountForTesting, 1, CV_32FC1, Scalar::all(0.0) );

    for(int j = 0; j < 6; j++) {
        trainingData.at<float>( j, 0 ) = (float) (j%2);
        trainingResponses.at<float>( j, 0 ) = (float) (j%2); //Setting a few values so I don't get a "single class" error
    }

    CvSVMParams svmParams( 100, //100 is CvSVM::C_SVC,
                          2, //2 is CvSVM::RBF,
                          1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
                          NULL,
                          TermCriteria( TermCriteria::MAX_ITER | TermCriteria::EPS, 2, 1.0 ) );
    CvSVM svm = CvSVM();
    svm.train_auto( trainingData, trainingResponses, Mat(), Mat(), svmParams );

    return 0;
}

I just create matrices to hold the training data and responses, then set a few entries to some value other than zero, then run the SVM. But it breaks whenever there are 2731 rows or more:

OpenCV Error: One of arguments' values is out of range (requested size is negative or too big) in cvMemStorageAlloc, file [omitted]/opencv/OpenCV-2.2.0/modules/core/src/datastructs.cpp, line 332

With fewer rows, it seems to be fine and a classifier trained in a similar manner to the above seems to be giving reasonable output. Am I doing something wrong? I'm pretty sure it's not actually anything to do with lack of memory, as I've got 6GB and also the code works fine when the data has 2730 rows and 10000 columns, which is a much bigger allocation.

I'm running OpenCV 2.2 on OSX 10.6 and initially I thought the problem might be related to this bug if for some reason the fix wasn't included in the MacPorts version. Now I've also tried downloading the most recent stable version from the OpenCV site and building with cmake and using that, but I still get the same error, and the fix is definitely included in that version.

Any help would be much appreciated!

Thanks,

© Stack Overflow or respective owner

Related posts about c++

Related posts about osx