2016-02-25 50 views
0

我正在从30000个随机生成的功能中学习增强树。学习仅限于说最好的100个功能。在学习了如何从CvBoost对象中提取决策树使用的特征的索引之后。使用Adaboost时访问和修改OpenCV决策树节点

我这样做的动机是消除生成所有30000个功能并仅计算将要使用的功能的需求。我已经包含了从CvBoost.save函数生成的yml文件的打印输出。我想我要的是识别的功能,如深度为1的决策树如下图所示称为sample_count值:

trees: 
     - 
     best_tree_idx: -1 
     nodes: 
      - 
       depth: 0 
       sample_count: 11556 
       value: -1.8339875099775065e+00 
       norm_class_idx: 0 
       Tn: 0 
       complexity: 0 
       alpha: 0. 
       node_risk: 0. 
       tree_risk: 0. 
       tree_error: 0. 
       splits: 
        - { var:497, quality:8.6223608255386353e-01, 
         le:5.3123302459716797e+00 } 
      - 
       depth: 1 
       sample_count: 10702 
       value: -1.8339875099775065e+00 
       norm_class_idx: 0 
       Tn: 0 
       complexity: 0 
       alpha: 0. 
       node_risk: 0. 
       tree_risk: 0. 
       tree_error: 0. 
      - 
       depth: 1 
       sample_count: 854 
       value: 1.8339875099775065e+00 
       norm_class_idx: 1 
       Tn: 0 
       complexity: 0 
       alpha: 0. 
       node_risk: 0. 
       tree_risk: 0. 
       tree_error: 0. 

编辑

目前,我有访问数据下面的代码:

//Interrogate the Decision Tree. Each element is a Decision Tree, making up the classifer 
    CvSeq* decisionTree = boostDevice.get_weak_predictors(); 

    simplifyFeatureSet(decisionTree, firstOrderROIs); 

这个功能是:

inline void Chnftrs::simplifyFeatureSet(CvSeq* decisionTree, std::vector<boost::tuple<int, cv::Rect> >& rois) 
{ 
    //This variable stores the index of the feature used from rois and a pointer to the split so that the variable there can 
    //be updated when the rois are pruned and reordered. 
    std::vector<boost::tuple<int, CvDTreeSplit* > > featureIdx; 

    //Determine the max depth of the tree 

    printf("Size of boost %d \n", decisionTree->total); 

    for (int i = 0; i < decisionTree->total; i++) 
    { 
      //Get the root of the tree 
      CvBoostTree *tree =0; 
      tree = (CvBoostTree*)cvGetSeqElem(decisionTree, i); 

      if(tree == 0) 
       printf("Tree is NULL\n"); 
      else 
       printf("Tree Addr %ld\n", tree);    

      const CvDTreeNode *root = tree->get_root(); 

      printf("Class_idx %d, Value %f ", root->sample_count, root->value); 

      featureIdx.push_back(boost::tuple<int, CvDTreeSplit*>(root->split->var_idx, root->split)); 

        //Search down the right hand side 
      depthFirstSearch(root->right, featureIdx); 

      //Search down the left hand side 
      depthFirstSearch(root->left, featureIdx); 


    } 
} 

但是,当我尝试访问任何根的成员(如root->sample_count)时,出现分段错误。除非将CvTreeTrainData.shared设置为true(默认情况下它为false),否则CvTree的成员可能无法访问。指示here

任何帮助将是巨大的

欢呼

彼得

回答

0

好吧,

之所以能够通过以下对如何CV升压分类源的方法来编辑决策树保存并从磁盘读取。出于某种原因,对于决策树类型对象,cvGetSeqElem()不会从传递给它的CvSeq对象返回有效指针。

为了获得决策树的副本,CvSeqReader和宏cvStartReadSeq工作得最好。宏CV_READ_SEQ_ELEM()似乎在循环过程中获得Seq中的下一棵树。:

CvSeqReader reader; 
    cvStartReadSeq(weak, &reader); 

    for (int i = 0; i < weak->total; i++) 
     { 

      CvBoostTree* tree; 
      CV_READ_SEQ_ELEM(tree, reader); 

       const CvDTreeNode *root = 0; 
       root = tree->get_root(); 

       printf("Root Split VarIdx : %d c: %f, ", root->split->var_idx, root->split->ord.c); 

       featureIdx.push_back(boost::tuple<int, CvDTreeSplit*>(root->split->var_idx, root->split)); 

       //Search down the right hand side 
       depthFirstSearch(root->right, featureIdx); 

       //Search down the left hand side 
       depthFirstSearch(root->left, featureIdx); 


     }