【问题标题】:Accessing and modifying OpenCV Decision Tree Nodes when using Adaboost使用 Adaboost 时访问和修改 OpenCV 决策树节点
【发布时间】:2016-06-08 23:50:41
【问题描述】:

我正在从 30000 个随机生成的特征中学习一棵增强树。学习仅限于说最好的 100 个特征。在学习了如何从 CvBoost 对象中提取决策树使用的特征的索引之后。

我这样做的动机是消除生成所有 30000 个特征的要求,并且只计算将使用的那些特征。我已经包含了从 CvBoost.save 函数生成的 yml 文件的打印输出。我想我想要的是名为 sample_count 的值,它在深度 1 的决策树中标识如下所示的特征:

 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 的任何成员(例如 root-&gt;sample_count)时,我遇到了分段错误。除非 CvTreeTrainData.shared 设置为 true(默认情况下为 false),否则 CvTree 的成员可能无法访问。如图所示here

任何帮助都会很棒

干杯

彼得

【问题讨论】:

    标签: opencv machine-learning decision-tree adaboost


    【解决方案1】:

    好的,

    能够按照源代码中关于 CvBoost 分类器如何从磁盘保存和读取自身的方法来编辑决策树。出于某种原因,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);
    
    
            }
    

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 2015-01-31
      • 2019-06-08
      • 2015-07-25
      • 2016-03-14
      • 2017-03-26
      • 2012-05-23
      • 2016-01-02
      • 2017-01-21
      相关资源
      最近更新 更多