Ask Your Question

memory exception while training SVM

asked 2018-12-30 16:29:28 -0500

manef gravatar image

to train SVM classifier i use features extracted from different images ( each image have number of features =! of other images beacause i use SLIC classifier and from each region i extract 20 features) after preparing the TraingMatData and LabelsMat i got this bugwhith a notification : Microsoft C++ exception: cv::Exception at memory location 0x000000E5176E2740. I couldn't understand what's the wrong in my code here's my code and i'm waiting your helps.) whith a notification : Microsoft C++ exception: cv::Exception at memory location 0x000000E5176E2740. I couldn't understand what's the wrong in my code here's my code and i'm waiting your helps.

int main()
    /// training 
    cv::Mat image;
    //load the features from files 

    Mat trainingDataMat(47,27000, CV_32FC1);

    float features[1350][20];
    float labels[47] = {-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,... i delet the rest of them};

    vector<int> size;
    for (int i = 1; i < 48; i++) {
        vector<float> numbers;
        //Mat features(1200, 20, CV_32FC1);

        string filename = "D:\\mémoire\\training\\trainingData\\featuers";
        filename = filename + std::to_string(i);
        filename = filename + ".txt";
        int size_e = 0;
        //read(numbers, filename);
        ifstream in(filename);
        float number;
        string aa;

        while (in.good()) {

            getline(in, aa, ',');
            number = strtod(aa.c_str(), NULL);
            size_e = size_e + 1;


        int pp = numbers.size()/ 20;

        int d = 0;
        for (int p = 0; p < 1350; p++) {
            for (int p1 = 0; p1 < 20; p1++) {
                if (pp <= d) {
                    features[p][p1] = 0;
                else {
                    //<uchar>(p, p1) = numbers[d];
                    features[p][p1] = numbers[d];
        cv::Mat A(1350, 20, CV_32FC1, features);
        trainingDataMat.push_back(A.reshape(1, 1));

        Mat labelsMat(47, 1, CV_32FC1, labels);
        //seting the SVM

        Ptr<ml::SVM> svm = ml::SVM::create();
        // edit: the params struct got removed,


        //Mat trainData; // one row per feature

        svm->train(trainingDataMat, ml::ROW_SAMPLE, labelsMat);
        // ...
edit retag flag offensive close merge delete


imho, you should train / classify on SLIC regions, not on whole images.

  1. ml needs a certain ratio of feature count and feature size. you can't seperate 27000 features with 50 images only. (same problem for LDA)
  2. as you say, the number of SLIC regions differs, so 1350 will be the wrong number for almost all of them. you're throwing away features, if there are more, and zero-padding if there are less is also a bad idea.
  3. if you line up all the features per image, like pearls on a string, the order matters. it will compare an exudate region to a non exudate one, just because they are in the same position in your feature vector.
berak gravatar imageberak ( 2018-12-30 20:43:43 -0500 )edit

also: if you preallocate a vector / Mat , AND use push_back(), you will end up with twice the expected size, and a ton of uninitialized elements in the front, just saying ...

then - you need integer, not float labels for a classification here.

berak gravatar imageberak ( 2018-12-30 20:48:33 -0500 )edit

i changed the Labels Mat to integer and nothing i still have the same error. i have question, the training Mat and the Labels Mat must have the same number of Row or the number of rows of the trainMat = cols of the labels Mat ?!

manef gravatar imagemanef ( 2019-01-02 04:59:10 -0500 )edit

same number of rows.

and look at your push_back() operations , again.

what do you think of the objection, above ? (you cannot do this "per image")

(imho, you'll have to rewrite this part anyway)

berak gravatar imageberak ( 2019-01-02 05:00:30 -0500 )edit

'm using the push_back(reshape(1,1)) to put all my features ( from files) in the same Mat, i looked in many tuto and i followed them that it ;)

manef gravatar imagemanef ( 2019-01-02 05:05:19 -0500 )edit

no, i mean:

   Mat trainingDataMat(47,27000, CV_32FC1);

if you push_back() further rows, you'll end up with 2 * 47 rows here

berak gravatar imageberak ( 2019-01-02 05:27:21 -0500 )edit

this is my first time with reshape and i know that reshape transform my Mat (1350,20) in my case to one row in other Mat , so the cols of the TrainMat must be (1350*20 = 27000). is that right or i'm missing something ?!

manef gravatar imagemanef ( 2019-01-02 05:35:34 -0500 )edit

all i'm saying is: you have to leave trainingDataMatempty.

berak gravatar imageberak ( 2019-01-02 05:44:57 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2019-01-02 06:20:15 -0500

manef gravatar image

updated 2019-01-02 06:20:29 -0500

ow i changed the trainingDataMat from Mat trainingDataMat (47, 27000, CV_32FC1) to only Mat trainingDataMat and it worked thanks but i got this bug now i'm going to search what's the cause of it

 if( !isTrained() )
            CV_Error( CV_StsParseError, "SVM model data is invalid, check sv_count, var_* and class_count tags" );
edit flag offensive delete link more



you have to check the return value from svm->train()

berak gravatar imageberak ( 2019-01-02 06:33:51 -0500 )edit

the value returned for train ? how and what's is suppose to be like ?! sorry but i have no ideaaa

manef gravatar imagemanef ( 2019-01-02 07:23:01 -0500 )edit

@break i verified that the svm is trained but the problem in the save function link text

manef gravatar imagemanef ( 2019-01-02 12:31:14 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-12-30 16:29:28 -0500

Seen: 61 times

Last updated: Jan 02