2013-08-20 44 views
0

所以我使用emguCV来使用OpenCV中的机器学习算法。我的代码如下,当它进入dtree.Train方法它给了我异常(exc1),如果我等待ir给我和错误消息(err1)。如果我尝试调试并进入此方法,如果给我另一个异常(exc2)并且调试器不前进。 EXC1: 类型“Emgu.CV.Util.CvException”的第一次机会异常发生在Emgu.CV.dllemgucv抛出异常的代码示例?

EXC2: 步骤为:步进比非用户代码“Emgu.CV.ML.RTrees .Train' 类型的第一次机会异常‘Emgu.CV.Util.CvException’发生在Emgu.CV.dll 步骤为:跨过非用户代码‘MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen’

err1: CLR无法从COM上下文0x795fa8转换到COM上下文0x796118。拥有目的地上下文/公寓的线程很可能要么进行非抽水等待,要么处理非常长的运行操作而不抽取Windows消息。这种情况通常会对性能产生负面影响,甚至可能导致应用程序无法响应或内存使用量不断累积。为了避免这个问题,所有的单线程单元(STA)线程都应该使用抽取等待原语(比如CoWaitForMultipleHandles),并在长时间运行的操作中定期抽取消息。

我的代码,使用这个例子 - 大量的试验和试错我发现这之后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)); 
     } 
    } 

回答

0

: -i调试emgucv与它的源代码,发现了错误的说明(这不是很有帮助)

-i开始看到错误发生的地方,我评论了这一行param.priors = priorsHandle.AddrOfPinnedObject(); //前辈阵列

-i得到了火车和测试工作,虽然它不能用RTrees预测什么。即使有蘑菇的例子,我只是试图将Dtrees改为Rtrees,我也没有得到任何结果。也许我需要调整参数。无论如何,我解决了评论该行的错误。

0

为了使它工作,你应该简单地使用param = MCvRTParams.GetDefaultParameter();我得到它与x64配置