OpenCV SVM train_auto Insufficient Memory

爱⌒轻易说出口 提交于 2020-01-13 19:34:15

问题


This is my first post here so I hope to be able to ask my question properly :-)

I want to do "elephant detection" by classifying color samples (I was inspired by this paper). This is the pipeline of my "solution" until the training of the classifier:

  1. Loading a set of 4 training images (all containing an elephant), and then splitting them in two images: one containing the environment surrounding the elephant (the "background"), and one containing the elephant (the "foreground");
  2. Mean shift segmentation of the backgrounds and the foregrounds;
  3. RGB -> Luv color space conversion and pixel values extraction (in order to fill a Mat object with 3 columns and a number of rows equal to the number of samples);
  4. SVM training with an RBF Kernel by calling train_auto method.

So, the problem arises in this last step: after a while (a few hours), I get the following error message:

OpenCV Error: Insufficient memory (Failed to allocate 22165936 bytes) in OutOfMemoryError, file xxx\alloc.cpp, line 52

terminate called after throwing an instance of 'cv::Exception'

what(): xxx\alloc.cpp:52:error: (-4) Failed to allocate 22165936 bytes in function OutOfMemoryError

My training samples are a [3 columns x 1,385,235 rows] CV_32FC1 Mat object. I don't believe that this data structure is too large, isn't it? I've 16 GB of RAM, and I'm using OpenCV 2.4.9 and Codeblocks.

However, this is the source code:

//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);

Thank you for paying attention towards this question.

来源:https://stackoverflow.com/questions/26605249/opencv-svm-train-auto-insufficient-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!