Пример #1
0
        public void SaveLoadTest()
        {
            float[] trainFeaturesData =
            {
                2, 2, 2, 2,
                3, 3, 3, 3,
                4, 4, 4, 4,
                5, 5, 5, 5,
                6, 6, 6, 6,
                7, 7, 7, 7
            };
            var trainFeatures = new Mat(6, 4, MatType.CV_32F, trainFeaturesData);

            int[] trainLabelsData = { 2, 3, 4, 5, 6, 7 };
            var   trainLabels     = new Mat(1, 6, MatType.CV_32S, trainLabelsData);

            const string fileName = "knearest.yml";

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (var model = KNearest.Create())
            {
                model.Train(trainFeatures, SampleTypes.RowSample, trainLabels);

                model.Save(fileName);
            }

            Assert.True(File.Exists(fileName));

            string content = File.ReadAllText(fileName);

            //Console.WriteLine(content);

            // Assert.DoesNotThrow
            using (var model2 = KNearest.Load(fileName))
            {
                GC.KeepAlive(model2);
            }
            using (var model2 = KNearest.LoadFromString(content))
            {
                GC.KeepAlive(model2);
            }
        }
Пример #2
0
        public void TestKNearest()
        {
            int K = 10;
            int trainSampleCount = 100;

            #region Generate the training data and classes

            Matrix <float> trainData    = new Matrix <float>(trainSampleCount, 2);
            Matrix <float> trainClasses = new Matrix <float>(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 >> 1, 1);
            trainData1.SetRandNormal(new MCvScalar(200), new MCvScalar(50));
            Matrix <float> trainData2 = trainData.GetRows(trainSampleCount >> 1, trainSampleCount, 1);
            trainData2.SetRandNormal(new MCvScalar(300), new MCvScalar(50));

            Matrix <float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount >> 1, 1);
            trainClasses1.SetValue(1);
            Matrix <float> trainClasses2 = trainClasses.GetRows(trainSampleCount >> 1, trainSampleCount, 1);
            trainClasses2.SetValue(2);

            #endregion

            Matrix <float> results, neighborResponses;
            results           = new Matrix <float>(sample.Rows, 1);
            neighborResponses = new Matrix <float>(sample.Rows, K);
            //dist = new Matrix<float>(sample.Rows, K);

            using (KNearest knn = new KNearest())
            {
                knn.DefaultK     = K;
                knn.IsClassifier = true;
                knn.Train(trainData, MlEnum.DataLayoutType.RowSample, trainClasses);
                //ParamDef[] defs =  knn.GetParams();


                for (int i = 0; i < img.Height; i++)
                {
                    for (int j = 0; j < img.Width; j++)
                    {
                        sample.Data[0, 0] = j;
                        sample.Data[0, 1] = i;

                        // estimates the response and get the neighbors' labels
                        float response = knn.Predict(sample);
                        //knn.FindNearest(sample, K, results, null, neighborResponses, null);

                        int accuracy = 0;
                        // compute the number of neighbors representing the majority
                        for (int k = 0; k < K; k++)
                        {
                            if (neighborResponses.Data[0, k] == response)
                            {
                                accuracy++;
                            }
                        }
                        // highlight the pixel depending on the accuracy (or confidence)
                        img[i, j] =
                            response == 1
                                ? (accuracy > 5 ? new Bgr(90, 0, 0) : new Bgr(90, 40, 0))
                                : (accuracy > 5 ? new Bgr(0, 90, 0) : new Bgr(40, 90, 0));
                    }
                }

                String knnModelStr;
                //save stat model to string
                using (FileStorage fs = new FileStorage(".yml", FileStorage.Mode.Write | FileStorage.Mode.Memory))
                {
                    knn.Write(fs, "knn");
                    knnModelStr = fs.ReleaseAndGetString();
                }

                KNearest knn2 = new KNearest();
                knn2.LoadFromString(knnModelStr, "knn");

                String   knnModelStr2 = knn.SaveToString();
                KNearest knn3         = new KNearest();
                knn3.LoadFromString(knnModelStr2);

#if !NETFX_CORE
                String fileName = "knnModel.xml";
                knn.Save(fileName);
                String text = File.ReadAllText(fileName);
#endif
            }

            // display the original training samples
            for (int i = 0; i < (trainSampleCount >> 1); i++)
            {
                PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]);
                img.Draw(new CircleF(p1, 2.0f), new Bgr(255, 100, 100), -1);
                PointF p2 = new PointF(trainData2[i, 0], trainData2[i, 1]);
                img.Draw(new CircleF(p2, 2.0f), new Bgr(100, 255, 100), -1);
            }

            //Emgu.CV.UI.ImageViewer.Show(img);
        }