How to read memory-based FileStorage? (OpenCV 3.1)

asked 2016-06-08 11:32:20 -0600

JerryR gravatar image

updated 2016-06-08 13:41:35 -0600

In OpenCV 3.1, it's either broken or I'm using it wrong. Can anyone either tell me what I'm doing wrong or confirm that it's broken?

My code:

cv::FileStorage fsWrite("my_tree", cv::FileStorage::WRITE + cv::FileStorage::MEMORY + FileStorage::FORMAT_XML);
Ptr<cv::ml::Rtrees> model;
[code to train model]
model->write(fsWrite);
std::string modelString = fsWrite.releaseAndGetString(); // produces proper-looking string with xml in it

Ptr<cv::ml::RTrees> resultModel = Algorithm::read<ml::RTrees>(fsWrite.getFirstTopLevelNode());

resultModel ends up having a null contained Rtrees.

This is what modelString looks like:

<?xml version="1.0"?>
<opencv_storage>
<is_classifier>0</is_classifier>
<var_all>13</var_all>
<var_count>12</var_count>
<ord_var_count>13</ord_var_count>
<cat_var_count>0</cat_var_count>
<training_params>
  <use_surrogates>0</use_surrogates>
  <max_categories>10</max_categories>
  <regression_accuracy>9.9999997764825821e-03</regression_accuracy>
  <max_depth>25</max_depth>
  <min_sample_count>10</min_sample_count>
  <cross_validation_folds>0</cross_validation_folds>
  <nactive_vars>4</nactive_vars></training_params>
<global_var_idx>1</global_var_idx>
<var_idx>
  0 1 2 3 4 5 6 7 8 9 10 11</var_idx>
<var_type>
  0 0 0 0 0 0 0 0 0 0 0 0 0</var_type>
<cat_ofs>
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0</cat_ofs>
<missing_subst>
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.</missing_subst>
<oob_error>0.</oob_error>
<ntrees>1</ntrees>
<trees>
  <_>
    <nodes>
      <_>
        <depth>0</depth>
        <value>2.7725000000000000e+00</value>
        <splits>
          <_><var>7</var>
            <quality>1.2565179687500000e+04</quality>
            <le>5.8210000991821289e+00</le></_></splits></_>
      <_>
        <depth>1</depth>
        <value>2.9880095923261392e+00</value>
        <splits>
          <_><var>7</var>
            <quality>1.1172947265625000e+04</quality>
            <le>1.2557047605514526e+00</le></_></splits></_>
      <_>
        <depth>2</depth>
        <value>3.</value></_>
      <_>
        <depth>2</depth>
        <value>2.7368421052631580e+00</value>
        <splits>
          <_><var>3</var>
            <quality>438.</quality>
            <le>-1.2632201194763184e+01</le></_></splits></_>
      <_>
        <depth>3</depth>
        <value>3.</value></_>
      <_>
        <depth>3</depth>
        <value>2.</value></_>
      <_>
        <depth>1</depth>
        <value>2.</value></_></nodes></_></trees>
</opencv_storage>
edit retag flag offensive close merge delete

Comments

does it make any difference, if you write to file (instead of memory) ?

(i guess, there's a bug in the dtree serialization, not in the filestorage)

berak gravatar imageberak ( 2016-06-08 23:33:20 -0600 )edit
1

I just tried it with a file, and it crashed in the same place on read. Code:

cv::FileStorage fsWrite("D:\\test.xml", cv::FileStorage::WRITE + FileStorage::FORMAT_XML);
m_ptrModel->write(fsWrite);
FileStorage fs("D:\\test.xml", FileStorage::READ + FileStorage::FORMAT_XML);
m_ptrModel = cv::Algorithm::read<ml::RTrees>(fs.getFirstTopLevelNode());

In rtrees.cpp, line 335, in the read() method of DTreesImplForRTrees:

oobError = (double)fn["oob_error"];

the FileNode passed in seems to not have any of the nodes expected, despite the file containing them.

I'll give this another day for any responses in case I'm doing the reading incorrectly, and then log a bug on the OpenCV issue site.

JerryR gravatar imageJerryR ( 2016-06-09 09:41:20 -0600 )edit

yes, making an official issue seems to be the right thing here.

berak gravatar imageberak ( 2016-06-09 10:19:14 -0600 )edit
1

I just registered on code.opencv.org, logged in, and clicked on "New Issue". I get a "You are not authorized to access this page" 403 error. Is there something else I need to do before I can report an issue?

JerryR gravatar imageJerryR ( 2016-06-09 10:29:51 -0600 )edit

^^ sorry, forgot to mention: all issues were moved to github

berak gravatar imageberak ( 2016-06-09 10:32:46 -0600 )edit