public static extern bool CvGBTreesTrain( IntPtr model, IntPtr trainData, MlEnum.DataLayoutType tFlag, IntPtr responses, IntPtr varIdx, IntPtr sampleIdx, IntPtr varType, IntPtr missingMask, ref MCvGBTreesParams param, [MarshalAs(CvInvoke.BoolMarshalType)] bool update);
/// <summary> /// Train the gradient boost trees using the specific traning data /// </summary> /// <param name="trainData">The training data. A 32-bit floating-point, single-channel matrix, one vector per row</param> /// <param name="tflag">data layout type</param> /// <param name="responses">A floating-point matrix of the corresponding output vectors, one vector per row. </param> /// <param name="varType">The types of input variables</param> /// <param name="missingMask">Can be null if not needed. When specified, it is an 8-bit matrix of the same size as <paramref name="trainData"/>, is used to mark the missed values (non-zero elements of the mask)</param> /// <param name="param">The parameters for training the Gradient Boosting tree</param> /// <param name="update">specifies whether the classifier needs to be updated (i.e. the new weak tree classifiers added to the existing ensemble), or the classifier needs to be rebuilt from scratch</param> /// <returns></returns> public bool Train( Matrix <float> trainData, MlEnum.DATA_LAYOUT_TYPE tflag, Matrix <float> responses, Matrix <Byte> varType, Matrix <Byte> missingMask, MCvGBTreesParams param, bool update) { return(MlInvoke.CvGBTreesTrain( _ptr, trainData.Ptr, tflag, responses.Ptr, IntPtr.Zero, IntPtr.Zero, varType == null ? IntPtr.Zero : varType.Ptr, missingMask == null ? IntPtr.Zero : missingMask.Ptr, ref param, update)); }
/// <summary> /// Train the gradient boost trees using the specific traning data /// </summary> /// <param name="trainData">The training data. A 32-bit floating-point, single-channel matrix, one vector per row</param> /// <param name="tflag">data layout type</param> /// <param name="responses">A floating-point matrix of the corresponding output vectors, one vector per row. </param> /// <param name="varMask">Can be null if not needed. When specified, it is a mask that identifies variables (features) of interest. It must be a Matrix>Byte< of n x 1 where n is the number of rows in <paramref name="trainData"/></param> /// <param name="sampleMask">Can be null if not needed. When specified, it is a mask identifies samples of interest. It must be a Matrix>Byte< of nx1, where n is the number of rows in <paramref name="trainData"/></param> /// <param name="varType">The types of input variables</param> /// <param name="missingMask">Can be null if not needed. When specified, it is an 8-bit matrix of the same size as <paramref name="trainData"/>, is used to mark the missed values (non-zero elements of the mask)</param> /// <param name="param">The parameters for training the Gradient Boosting tree</param> /// <param name="update">specifies whether the classifier needs to be updated (i.e. the new weak tree classifiers added to the existing ensemble), or the classifier needs to be rebuilt from scratch</param> /// <returns></returns> public bool Train( Matrix <float> trainData, MlEnum.DataLayoutType tflag, Matrix <float> responses, Matrix <Byte> varMask, Matrix <Byte> sampleMask, Matrix <Byte> varType, Matrix <Byte> missingMask, MCvGBTreesParams param, bool update) { return(MlInvoke.CvGBTreesTrain( _ptr, trainData.Ptr, tflag, responses.Ptr, varMask == null ? IntPtr.Zero : varMask.Ptr, sampleMask == null ? IntPtr.Zero : sampleMask.Ptr, varType == null ? IntPtr.Zero : varType.Ptr, missingMask == null ? IntPtr.Zero : missingMask.Ptr, ref param, update)); }
public void TestGBTrees() { Bgr[] colors = new Bgr[] { new Bgr(0, 0, 255), new Bgr(0, 255, 0), new Bgr(255, 0, 0) }; int trainSampleCount = 150; #region Generate the training data and classes Matrix <float> trainData = new Matrix <float>(trainSampleCount, 2); Matrix <int> trainClasses = new Matrix <int>(trainSampleCount, 1); Image <Bgr, Byte> img = new Image <Bgr, byte>(500, 500); Matrix <float> sample = new Matrix <float>(1, 2); Matrix <float> trainData1 = trainData.GetRows(0, trainSampleCount / 3, 1); trainData1.GetCols(0, 1).SetRandNormal(new MCvScalar(100), new MCvScalar(50)); trainData1.GetCols(1, 2).SetRandNormal(new MCvScalar(300), new MCvScalar(50)); Matrix <float> trainData2 = trainData.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainData2.SetRandNormal(new MCvScalar(400), new MCvScalar(50)); Matrix <float> trainData3 = trainData.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainData3.GetCols(0, 1).SetRandNormal(new MCvScalar(300), new MCvScalar(50)); trainData3.GetCols(1, 2).SetRandNormal(new MCvScalar(100), new MCvScalar(50)); Matrix <int> trainClasses1 = trainClasses.GetRows(0, trainSampleCount / 3, 1); trainClasses1.SetValue(1); Matrix <int> trainClasses2 = trainClasses.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainClasses2.SetValue(2); Matrix <int> trainClasses3 = trainClasses.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainClasses3.SetValue(3); #endregion using (GBTrees classifier = new GBTrees()) { classifier.Train(trainData, MlEnum.DataLayoutType.RowSample, trainClasses.Convert <float>(), null, null, MCvGBTreesParams.GetDefaultParameter(), false); #if !NETFX_CORE String fileName = Path.Combine(Path.GetTempPath(), "GBTrees.xml"); classifier.Save(fileName); if (File.Exists(fileName)) { File.Delete(fileName); } #endif #region Classify every image pixel for (int i = 0; i < img.Height; i++) { for (int j = 0; j < img.Width; j++) { sample.Data[0, 0] = i; sample.Data[0, 1] = j; int response = (int)Math.Round(classifier.Predict(sample, null, null, MCvSlice.WholeSeq, false)); Bgr color = colors[response - 1]; img[j, i] = new Bgr(color.Blue * 0.5, color.Green * 0.5, color.Red * 0.5); } } #endregion } // display the original training samples for (int i = 0; i < (trainSampleCount / 3); i++) { PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]); img.Draw(new CircleF(p1, 2.0f), colors[0], -1); PointF p2 = new PointF(trainData2[i, 0], trainData2[i, 1]); img.Draw(new CircleF(p2, 2.0f), colors[1], -1); PointF p3 = new PointF(trainData3[i, 0], trainData3[i, 1]); img.Draw(new CircleF(p3, 2.0f), colors[2], -1); } //Emgu.CV.UI.ImageViewer.Show(img); }