Ask Your Question
0

How to copy NormalBayesClassifier?

asked 2014-06-23 09:28:39 -0600

thdrksdfthmn gravatar image

updated 2014-06-25 12:02:19 -0600

berak gravatar image

I am training and testing more classifiers and I want to save just the best of them. I have tried to use an if and =

// for each train/eval
if (smallestError > errorRate)
{
  best_Classifier = classifier;
}
// end for
best_Classifier.save("name");

but it seems that it gives me some null pointer error:

OpenCV Error: Null pointer (Null pointer to the written object) in cvWrite, file /home/me/opencv/modules/core/src/persistence.cpp, line 5011
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/me/opencv/modules/core/src/persistence.cpp:5011: error: (-27) Null pointer to the written object in function cvWrite

Can you suggest me some other way if this is not possible?


EDIT:

I have also tried using Ptr, but it still does not work:

cv::Ptr< cv::NormalBayesClassifier > bestClassifier;
// for loop where train and evaluate the classifier cv::NormalBayesClassifier classifier;
if (smallestError > errorRate)
{
  best_Classifier = &classifier;
  smallesError = errorRate;
}

// end loop

best_Classifier->save("name");

And it gives me the following error:

OpenCV Error: The function/feature is not implemented () in CvStatModel::write, file /home/me/opencv/modules/ml/src/inner_functions.cpp, line 114
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/me/opencv/modules/ml/src/inner_functions.cpp:114: error: (-213)  in function CvStatModel::write
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-06-27 04:14:50 -0600

thdrksdfthmn gravatar image

updated 2014-06-27 04:15:12 -0600

The answer is by using cv::Ptr< cv::NormalBayesClassifier >, but do not forget to allocate memory with new:

cv::Ptr< cv::NormalBayesClassifier > bestClassifier;
for (int i = 0; i < 10; i++)
{
  // define pointer to classifier and allocate memory
  cv::Ptr< cv::NormalBayesClassifier > classifier = new cv::NormalBayesClassifier;
  // train, test
  if (smallestError > errorRate)
  {
    bestClassifier = classifier;
    smallesError = errorRate;
  }
}
bestClassifier->save("name");
edit flag offensive delete link more

Comments

You should release your classifier if it isn't the best, and also the previous best classifier.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2014-06-27 04:49:14 -0600 )edit

@MathieuBarnachon So, you mean that I shall add bestClassifier-&gt;clear(); before bestClassifier = classifier; and classifier-&gt;clear(); before closing the for loop?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-06-27 07:30:28 -0600 )edit

Sorry, I just miss the cv::Ptr which is a smart pointer ;-)

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2014-06-27 08:26:26 -0600 )edit

Anyway, I have tried it and there were errors...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-06-28 12:14:03 -0600 )edit

Question Tools

Stats

Asked: 2014-06-23 09:28:39 -0600

Seen: 277 times

Last updated: Jun 27 '14