OpenCV SVM train_auto内存不足

时间:2014-10-28 09:51:40

标签: opencv image-processing classification svm training-data

这是我在这里发表的第一篇文章,所以我希望能够正确地提出我的问题: - )

我想做"大象检测"通过分类颜色样本(我的灵感来自this paper)。这是我的解决方案"直到训练分类器:

  1. 加载一组4个训练图像(全部包含大象),然后将它们分成两个图像:一个包含大象周围的环境("背景"),另一个包含大象( "前景");
  2. 背景和前景的平均移位分割;
  3. RGB - > Luv颜色空间转换和像素值提取(为了用3列填充Mat对象和等于样本数的行数);
  4. 通过调用train_auto方法使用RBF内核进行SVM训练。
  5. 所以,问题出现在最后一步:过了一会儿(几个小时),我收到以下错误信息:

    OpenCV错误:OutOfMemoryError中的内存不足(无法分配22165936个字节),文件xxx \ alloc.cpp,第52行

    抛出' cv :: Exception' 的实例后,

    终止

    what():xxx \ alloc.cpp:52:错误:( - 4)无法在函数OutOfMemoryError中分配22165936个字节

    我的训练样本是[3列x 1,385,235行] CV_32FC1 Mat对象。我不相信这个数据结构太大了,不是吗?我有16 GB的RAM,而且我使用的是OpenCV 2.4.9和Codeblocks。

    但是,这是源代码:

    //Loading Background & Foreground Training Images
    Mat train_00_background = imread("training_set/train_00_background.png");
    cvtColor(train_00_background, train_00_background, CV_BGR2BGRA);
    oclMat t_00_bg;
    t_00_bg.upload(train_00_background);
    
    (...)
    
    
    Mat train_00_foreground = imread("training_set/train_00_foreground.png");
    cvtColor(train_00_foreground, train_00_foreground, CV_BGR2BGRA);
    oclMat t_00_fg;
    t_00_fg.upload(train_00_foreground);
    
    (...)
    
    //COLOR SEGMENTATION by MEAN SHIFT CLUSTERING
    meanShiftSegmentation(t_00_bg, train_00_background_clustered, 5, 5, 50, TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
    
    (...)
    
    //CV_8UC3 -> CV_32FC3
    train_00_background_clustered.convertTo(train_00_background_clustered, CV_32FC3);
    
    (...)
    
    //Normalization
    train_00_background_clustered *= 1./255;
    
    (...)
    
    //RGB -> Luv
    cvtColor(train_00_background_clustered, train_00_background_clustered, CV_BGR2Luv);
    
    (...)
    
    //COLOR LIST EXTRACTION FROM BACKGROUND AND FOREGROUND
    //colorListBg_00 is a 3 x NumberOfColors CV_32FC1 Mat
    extractColorList(train_00_background_clustered, colorListBg_00);
    
    // Memory release
    train_00_background_clustered.release(); 
    
    (...)
    
    //Color List concatenation
    //background
    colorListBg_00.copyTo(colorList);
    colorList.push_back(colorListBg_01);
    (...)
    Size s = colorList.size();
    int s1 = s.height;
    
    //foreground  
    colorList.push_back(colorListFg_00);
    (...)
    s = colorList.size();
    int s2 = s.height - s1;
    
    // ASSIGNING THE LABELS
    Mat labelsBg(s1, 1, CV_32FC1, -1.0);
    Mat labelsFg(s2, 1, CV_32FC1, 1.0);
    Mat labels;
    labelsBg.copyTo(labels);
    labels.push_back(labelsFg);
    
    // SVM TRAINING
    CvSVMParams params;
    params.svm_type    = CvSVM::C_SVC;
    params.kernel_type = CvSVM::RBF;
    params.term_crit   = cvTermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000, FLT_EPSILON );
    
    CvSVM SVM;   
    SVM.train_auto(colorList, labels, Mat(), Mat(), params, 2);
    

    感谢您关注此问题。

0 个答案:

没有答案