0

So i'm using emguCV to use the machine learning algorithms from OpenCV. My code is as follows, and when it enters the dtree.Train method it gives me exception (exc1) and if i wait ir gives me and error message (err1).If i try to debug and step into this method if gives me another exception (exc2) and the debugger doesn't advance. exc1: A first chance exception of type 'Emgu.CV.Util.CvException' occurred in Emgu.CV.dll

exc2: Step into: Stepping over non-user code 'Emgu.CV.ML.RTrees.Train' A first chance exception of type 'Emgu.CV.Util.CvException' occurred in Emgu.CV.dll Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'

err1: The CLR has been unable to transition from COM context 0x795fa8 to COM context 0x796118 . The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

My Code, using this example - http://www.emgu.com/wiki/index.php/Mushroom_Poisonous_Prediction_(Decision_Tree)_in_CSharp

   public void train()
    {

        Matrix<float> data, response;
       // data = new Matrix<float>(15, 200);
       // response = new Matrix<float>(15, 200);
        Console.WriteLine("reading shroom data");
        ReadMushroomData(out data, out response);

        ///data = new Matrix<float>(1, 5);

        //Use the first 80% of data as training sample
        int trainingSampleCount = (int)(data.Rows * 0.8);

        Matrix<Byte> varType = new Matrix<byte>(data.Cols + 1, 1);
        varType.SetValue((byte)Emgu.CV.ML.MlEnum.VAR_TYPE.CATEGORICAL); //the data is categorical

        Matrix<byte> sampleIdx = new Matrix<byte>(data.Rows, 1);
        using (Matrix<byte> sampleRows = sampleIdx.GetRows(0, trainingSampleCount, 1))
            sampleRows.SetValue(255);

        float[] priors = new float[] { 1, 0.5f };
        GCHandle priorsHandle = GCHandle.Alloc(priors, GCHandleType.Pinned);

        MCvRTParams param = new MCvRTParams();
        param.maxDepth = 8;// max depth
        param.minSampleCount = 10;// min sample count
        param.regressionAccuracy = 0;// regression accuracy: N/A here
        param.useSurrogates = true; //compute surrogate split, no missing data
        param.maxCategories = 15;// max number of categories (use sub-optimal algorithm for larger numbers)
        param.cvFolds = 10;
        //param.use1seRule = true;
        param.truncatePrunedTree = true;
        param.priors = priorsHandle.AddrOfPinnedObject(); // the array of priors


        Console.WriteLine("starting train");
        using (RTrees dtree = new RTrees())
        {

            bool success = dtree.Train(data, 
                Emgu.CV.ML.MlEnum.DATA_LAYOUT_TYPE.ROW_SAMPLE, 
                response, 
                null, 
                sampleIdx, 
                varType, 
                null, 
                param);


            Console.WriteLine("starting tests");

            if (!success) return;
            double trainDataCorrectRatio = 0;
            double testDataCorrectRatio = 0;
            for (int i = 0; i < data.Rows; i++)
            {
                using (Matrix<float> sample = data.GetRow(i))
                {
                    double r = dtree.Predict(sample, null);
                    r = Math.Abs(r - response[i, 0]);
                    if (r < 1.0e-5)
                    {
                        if (i < trainingSampleCount)
                            trainDataCorrectRatio++;
                        else
                            testDataCorrectRatio++;
                    }
                }
            }

            trainDataCorrectRatio /= trainingSampleCount;
            testDataCorrectRatio /= (data.Rows - trainingSampleCount);

            Console.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio * 100));
            Console.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio * 100));
        }
    }
4

2 回答 2

0

经过大量测试和反复试验后,我发现了这一点:-i debug emgucv with its source code 并找到了错误描述(这不是很有帮助)

-我开始查看错误发生的位置,我评论了这一行 param.priors =priorsHandle.AddrOfPinnedObject(); // 先验数组

-我让火车和测试开始工作,虽然它不能用 RTrees 预测任何东西。即使使用蘑菇示例,我也尝试将 Dtrees 更改为 Rtrees,但我也没有得到任何结果。也许我需要调整参数。无论如何,我解决了评论该行的错误。

于 2013-08-27T19:14:05.890 回答
0

为了让它工作,你应该简单地使用param = MCvRTParams.GetDefaultParameter();I got it to work with x64 configuration

于 2017-03-07T14:58:26.240 回答