示例#1
0
        public void KModesConstructorTest()
        {
            Accord.Math.Tools.SetupGenerator(0);


            // Declare some observations
            int[][] observations =
            {
                new int[] {  0,  0 }, // a
                new int[] {  0,  1 }, // a
                new int[] {  0,  1 }, // a
                new int[] {  1,  1 }, // a

                new int[] {  5,  3 }, // b
                new int[] {  6,  8 }, // b
                new int[] {  6,  8 }, // b
                new int[] {  6,  7 }, // b
                new int[] {  5,  8 }, // b

                new int[] { 12, 14 }, // c
                new int[] { 12, 14 }, // c
                new int[] { 13, 14 }, // c
            };

            int[][] orig = observations.MemberwiseClone();

            // Create a new K-Modes algorithm with 3 clusters
            KModes kmodes = new KModes(3);

            // Compute the algorithm, retrieving an integer array
            //  containing the labels for each of the observations
            int[] labels = kmodes.Compute(observations);

            // As a result, the first three observations should belong to the
            //  same cluster (thus having the same label). The same should
            //  happen to the next four observations and to the last two.

            Assert.AreEqual(labels[0], labels[1]);
            Assert.AreEqual(labels[0], labels[2]);
            Assert.AreEqual(labels[0], labels[3]);

            Assert.AreEqual(labels[4], labels[5]);
            Assert.AreEqual(labels[4], labels[6]);
            Assert.AreEqual(labels[4], labels[7]);
            Assert.AreEqual(labels[4], labels[8]);

            Assert.AreEqual(labels[9], labels[10]);
            Assert.AreEqual(labels[9], labels[11]);

            Assert.AreNotEqual(labels[0], labels[4]);
            Assert.AreNotEqual(labels[0], labels[9]);
            Assert.AreNotEqual(labels[4], labels[9]);


            int[] labels2 = kmodes.Clusters.Nearest(observations);
            Assert.IsTrue(labels.IsEqual(labels2));

            // the data must not have changed!
            Assert.IsTrue(orig.IsEqual(observations));
        }
示例#2
0
        public void KModesConstructorTest()
        {
            Accord.Math.Tools.SetupGenerator(0);


            // Declare some observations
            int[][] observations = 
            {
                new int[] { 0, 0   }, // a
                new int[] { 0, 1   }, // a
                new int[] { 0, 1   }, // a
                new int[] { 1, 1   }, // a
 
                new int[] { 5, 3   }, // b
                new int[] { 6, 8   }, // b
                new int[] { 6, 8   }, // b
                new int[] { 6, 7   }, // b
                new int[] { 5, 8   }, // b

                new int[] { 12, 14 }, // c
                new int[] { 12, 14 }, // c
                new int[] { 13, 14 }, // c
            };

            int[][] orig = observations.MemberwiseClone();

            // Create a new K-Modes algorithm with 3 clusters 
            KModes kmodes = new KModes(3);

            // Compute the algorithm, retrieving an integer array
            //  containing the labels for each of the observations
            int[] labels = kmodes.Compute(observations);

            // As a result, the first three observations should belong to the
            //  same cluster (thus having the same label). The same should
            //  happen to the next four observations and to the last two.

            Assert.AreEqual(labels[0], labels[1]);
            Assert.AreEqual(labels[0], labels[2]);
            Assert.AreEqual(labels[0], labels[3]);

            Assert.AreEqual(labels[4], labels[5]);
            Assert.AreEqual(labels[4], labels[6]);
            Assert.AreEqual(labels[4], labels[7]);
            Assert.AreEqual(labels[4], labels[8]);

            Assert.AreEqual(labels[9], labels[10]);
            Assert.AreEqual(labels[9], labels[11]);

            Assert.AreNotEqual(labels[0], labels[4]);
            Assert.AreNotEqual(labels[0], labels[9]);
            Assert.AreNotEqual(labels[4], labels[9]);


            int[] labels2 = kmodes.Clusters.Nearest(observations);
            Assert.IsTrue(labels.IsEqual(labels2));

            // the data must not have changed!
            Assert.IsTrue(orig.IsEqual(observations));
        }
示例#3
0
        public void doc_learn()
        {
            #region doc_learn
            Accord.Math.Random.Generator.Seed = 0;

            // Declare some observations
            byte[][] observations = new[]
            {
                new byte[] { 0, 0 },   // a
                new byte[] { 0, 1 },   // a
                new byte[] { 0, 1 },   // a
                new byte[] { 1, 1 },   // a

                new byte[] { 5, 3 },   // b
                new byte[] { 6, 8 },   // b
                new byte[] { 6, 8 },   // b
                new byte[] { 6, 7 },   // b
                new byte[] { 5, 8 },   // b

                new byte[] { 12, 14 }, // c
                new byte[] { 12, 14 }, // c
                new byte[] { 13, 14 }, // c
            };

            // Create a new 3-Modes algorithm using the Hamming distance
            var kmodes = new KModes <byte>(k: 3, distance: new Hamming())
            {
                MaxIterations = 100
            };

            // Compute and retrieve the data centroids
            var clusters = kmodes.Learn(observations);

            // Use the centroids to parition all the data
            int[] labels = clusters.Decide(observations);
            #endregion



            Assert.AreEqual(labels[0], labels[1]);
            Assert.AreEqual(labels[0], labels[2]);
            Assert.AreEqual(labels[0], labels[3]);
            Assert.AreEqual(labels[0], labels[4]);
            Assert.AreEqual(labels[0], labels[7]);

            Assert.AreEqual(labels[5], labels[6]);
            Assert.AreEqual(labels[5], labels[8]);

            Assert.AreEqual(labels[9], labels[10]);
            Assert.AreEqual(labels[9], labels[11]);

            Assert.AreNotEqual(labels[0], labels[5]);
            Assert.AreNotEqual(labels[5], labels[7]);
            Assert.AreNotEqual(labels[0], labels[9]);
        }
示例#4
0
        public void KModesConstructorTestHamming()
        {
            Accord.Math.Random.Generator.Seed = 0;

            // Declare some observations
            byte[][] observations = new int[][]
            {
                new int[] { 0, 0 },   // a
                new int[] { 0, 1 },   // a
                new int[] { 0, 1 },   // a
                new int[] { 1, 1 },   // a

                new int[] { 5, 3 },   // b
                new int[] { 6, 8 },   // b
                new int[] { 6, 8 },   // b
                new int[] { 6, 7 },   // b
                new int[] { 5, 8 },   // b

                new int[] { 12, 14 }, // c
                new int[] { 12, 14 }, // c
                new int[] { 13, 14 }, // c
            }.To <byte[][]>();

            byte[][] orig = observations.MemberwiseClone();

            // Create a new K-Modes algorithm with 3 clusters
            var kmodes = new KModes <byte>(3, Distance.BitwiseHamming);

            Assert.IsTrue(kmodes.Distance is Hamming);

            int[] labels = kmodes.Compute(observations);

            Assert.AreEqual(labels[0], labels[1]);
            Assert.AreEqual(labels[0], labels[2]);
            Assert.AreEqual(labels[0], labels[3]);
            Assert.AreEqual(labels[0], labels[4]);
            Assert.AreEqual(labels[0], labels[7]);

            Assert.AreEqual(labels[5], labels[6]);
            Assert.AreEqual(labels[5], labels[8]);

            Assert.AreEqual(labels[9], labels[10]);
            Assert.AreEqual(labels[9], labels[11]);

            Assert.AreNotEqual(labels[0], labels[5]);
            Assert.AreNotEqual(labels[5], labels[7]);
            Assert.AreNotEqual(labels[0], labels[9]);



            // the data must not have changed!
            Assert.IsTrue(orig.IsEqual(observations));
        }
示例#5
0
        public void SerializeTest2()
        {
            var images = BagOfVisualWordsTest.images.DeepClone();

            Accord.Math.Tools.SetupGenerator(0);

            FastCornersDetector fast = new FastCornersDetector();

            FastRetinaKeypointDetector freak = new FastRetinaKeypointDetector(fast);

            var kmodes = new KModes <byte>(5, Distance.BitwiseHamming);

            var bow = new BagOfVisualWords <FastRetinaKeypoint, byte[]>(freak, kmodes);

            bow.Compute(images);


            double[][] expected = new double[images.Length][];
            for (int i = 0; i < expected.Length; i++)
            {
                expected[i] = bow.GetFeatureVector(images[i]);
            }

            MemoryStream    stream = new MemoryStream();
            BinaryFormatter fmt    = new BinaryFormatter();

            fmt.Serialize(stream, bow);
            stream.Seek(0, SeekOrigin.Begin);
            bow = (BagOfVisualWords <FastRetinaKeypoint, byte[]>)fmt.Deserialize(stream);

            double[][] actual = new double[expected.Length][];
            for (int i = 0; i < actual.Length; i++)
            {
                actual[i] = bow.GetFeatureVector(images[i]);
            }


            Assert.IsTrue(expected.IsEqual(actual));
        }
示例#6
0
        public void SerializeTest2()
        {
            var images = GetImages();

            Accord.Math.Random.Generator.Seed = 0;

            FastCornersDetector fast = new FastCornersDetector();

            FastRetinaKeypointDetector freak = new FastRetinaKeypointDetector(fast);

            var kmodes = new KModes <byte>(5, new Hamming());

            kmodes.ParallelOptions.MaxDegreeOfParallelism = 1;

            var bow = new BagOfVisualWords <FastRetinaKeypoint, byte[]>(freak, kmodes);

            bow.Compute(images);

            double[][] expected = new double[images.Length][];
            for (int i = 0; i < expected.Length; i++)
            {
                expected[i] = bow.GetFeatureVector(images[i]);
            }

            MemoryStream    stream = new MemoryStream();
            BinaryFormatter fmt    = new BinaryFormatter();

            fmt.Serialize(stream, bow);
            stream.Seek(0, SeekOrigin.Begin);
            bow = (BagOfVisualWords <FastRetinaKeypoint, byte[]>)fmt.Deserialize(stream);

            double[][] actual = new double[expected.Length][];
            for (int i = 0; i < actual.Length; i++)
            {
                actual[i] = bow.GetFeatureVector(images[i]);
            }

            Assert.IsTrue(expected.IsEqual(actual));
        }
示例#7
0
        /// <summary>
        ///   This methods computes the Bag-of-Visual-Words with the training images.
        /// </summary>
        /// 
        private void btnBagOfWords_Click(object sender, EventArgs e)
        {
            int numberOfWords = (int)numWords.Value;


            // Create a Binary-Split clustering algorithm
            BinarySplit binarySplit = new BinarySplit(numberOfWords);

            Stopwatch sw1 = Stopwatch.StartNew();

            IBagOfWords<Bitmap> bow;

            if (rbSurf.Checked)
            {
                // Create bag-of-words (BoW) with the given algorithm
                var surfBow = new BagOfVisualWords(binarySplit);

                // Compute the BoW codebook using training images only
                surfBow.Compute(originalTrainImages.Values.ToArray());

                bow = surfBow;
            }

            else
            {

                // Alternative creation using the FREAK detector

                // Create a Binary-Split clustering algorithm
                var kmodes = new KModes<byte>(numberOfWords, Distance.BitwiseHamming);
                var detector = new FastRetinaKeypointDetector();

                // Create bag-of-words (BoW) with the given algorithm
                var freakBow = new BagOfVisualWords<FastRetinaKeypoint, byte[]>(detector, kmodes);

                // Compute the BoW codebook using training images only
                freakBow.Compute(originalTrainImages.Values.ToArray());

                bow = freakBow;
            }

            sw1.Stop();

            Stopwatch sw2 = Stopwatch.StartNew();

            // Extract features for all images
            foreach (ListViewItem item in listView1.Items)
            {
                // Get item image
                Bitmap image = originalImages[item.ImageKey] as Bitmap;

                // Process image
                double[] featureVector = bow.GetFeatureVector(image);
                string featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture);

                if (item.SubItems.Count == 2)
                    item.SubItems[1].Text = featureString;
                else item.SubItems.Add(featureString);

                int classLabel = (item.Tag as Tuple<double[], int>).Item2;
                item.Tag = Tuple.Create(featureVector, classLabel);
            }

            sw2.Stop();

            lbStatus.Text = "BoW constructed in " + sw1.Elapsed + "s. Features extracted in " + sw2.Elapsed + "s.";
            btnSampleRunAnalysis.Enabled = true;
        }
示例#8
0
        public void KModesConstructorTestHamming()
        {
            Accord.Math.Random.Generator.Seed = 0;

            // Declare some observations
            byte[][] observations = new int[][]
            {
                new int[] { 0, 0   }, // a
                new int[] { 0, 1   }, // a
                new int[] { 0, 1   }, // a
                new int[] { 1, 1   }, // a
 
                new int[] { 5, 3   }, // b
                new int[] { 6, 8   }, // b
                new int[] { 6, 8   }, // b
                new int[] { 6, 7   }, // b
                new int[] { 5, 8   }, // b

                new int[] { 12, 14 }, // c
                new int[] { 12, 14 }, // c
                new int[] { 13, 14 }, // c
            }.To<byte[][]>();

            byte[][] orig = observations.MemberwiseClone();

            // Create a new K-Modes algorithm with 3 clusters 
            var kmodes = new KModes<byte>(3, Distance.BitwiseHamming);

            Assert.IsTrue(kmodes.Distance is Hamming);

            int[] labels = kmodes.Compute(observations);

            Assert.AreEqual(labels[0], labels[1]);
            Assert.AreEqual(labels[0], labels[2]);
            Assert.AreEqual(labels[0], labels[3]);
            Assert.AreEqual(labels[0], labels[4]);
            Assert.AreEqual(labels[0], labels[7]);

            Assert.AreEqual(labels[5], labels[6]);
            Assert.AreEqual(labels[5], labels[8]);

            Assert.AreEqual(labels[9], labels[10]);
            Assert.AreEqual(labels[9], labels[11]);

            Assert.AreNotEqual(labels[0], labels[5]);
            Assert.AreNotEqual(labels[5], labels[7]);
            Assert.AreNotEqual(labels[0], labels[9]);



            // the data must not have changed!
            Assert.IsTrue(orig.IsEqual(observations));
        }
示例#9
0
        /// <summary>
        ///   This methods computes the Bag-of-Visual-Words with the training images.
        /// </summary>
        ///
        private void btnBagOfWords_Click(object sender, EventArgs e)
        {
            int numberOfWords = (int)numWords.Value;


            // Create a Binary-Split clustering algorithm
            BinarySplit binarySplit = new BinarySplit(numberOfWords);

            Stopwatch sw1 = Stopwatch.StartNew();

            IBagOfWords <Bitmap> bow;

            if (rbSurf.Checked)
            {
                // Create bag-of-words (BoW) with the given algorithm
                var surfBow = new BagOfVisualWords(binarySplit);

                // Compute the BoW codebook using training images only
                surfBow.Compute(originalTrainImages.Values.ToArray());

                bow = surfBow;
            }

            else
            {
                // Alternative creation using the FREAK detector

                // Create a Binary-Split clustering algorithm
                var kmodes   = new KModes <byte>(numberOfWords, new Hamming());
                var detector = new FastRetinaKeypointDetector();

                // Create bag-of-words (BoW) with the given algorithm
                var freakBow = new BagOfVisualWords <FastRetinaKeypoint, byte[]>(detector, kmodes);

                // Compute the BoW codebook using training images only
                freakBow.Compute(originalTrainImages.Values.ToArray());

                bow = freakBow;
            }

            sw1.Stop();

            Stopwatch sw2 = Stopwatch.StartNew();

            // Extract features for all images
            foreach (ListViewItem item in listView1.Items)
            {
                // Get item image
                Bitmap image = originalImages[item.ImageKey] as Bitmap;

                // Process image
                double[] featureVector = bow.GetFeatureVector(image);
                string   featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture);

                if (item.SubItems.Count == 2)
                {
                    item.SubItems[1].Text = featureString;
                }
                else
                {
                    item.SubItems.Add(featureString);
                }

                int classLabel = (item.Tag as Tuple <double[], int>).Item2;
                item.Tag = Tuple.Create(featureVector, classLabel);
            }

            sw2.Stop();

            lbStatus.Text = "BoW constructed in " + sw1.Elapsed + "s. Features extracted in " + sw2.Elapsed + "s.";
            btnSampleRunAnalysis.Enabled = true;
        }
示例#10
0
        /// <summary>
        ///   This methods computes the Bag-of-Visual-Words with the training images.
        /// </summary>
        ///
        private void btnBagOfWords_Click(object sender, EventArgs e)
        {
            int numberOfWords = (int)numWords.Value;


            Stopwatch sw1 = Stopwatch.StartNew();

            IBagOfWords <Bitmap> bow;

            // Check if we will use SURF or FREAK as the feature detector
            if (rbSurf.Checked)
            {
                // We will use SURF, so we can use a standard clustering
                // algorithm that is based on Euclidean distances. A good
                // algorithm for clustering codewords is the Binary Split
                // variant of the K-Means algorithm.

                // Create a Binary-Split clustering algorithm
                BinarySplit binarySplit = new BinarySplit(numberOfWords);

                // Create bag-of-words (BoW) with the given algorithm
                BagOfVisualWords surfBow = new BagOfVisualWords(binarySplit);

                // Compute the BoW codebook using training images only
                bow = surfBow.Learn(originalTrainImages.Values.ToArray());
            }
            else if (rbFreak.Checked)
            {
                // We will use the FREAK detector. The features generated by FREAK
                // are represented as bytes. While it is possible to transform those
                // to standard double vectors, we will demonstrate how to use a non-
                // Euclidean distance based algorithm to generate codewords for it.

                // Note: Using Binary-Split with normalized FREAK features would
                // possibly work better than the k-modes. This is just an example.

                // Create a k-Modes clustering algorithm
                var kmodes = new KModes <byte>(numberOfWords, new Hamming());

                // Create a FREAK detector explicitly (if no detector was passed,
                // the BagOfVisualWords would be using a SURF detector by default).
                var freak = new FastRetinaKeypointDetector();

                // Create bag-of-words (BoW) with the k-modes clustering and FREAK detector
                var freakBow = new BagOfVisualWords <FastRetinaKeypoint, byte[]>(freak, kmodes);

                // Compute the BoW codebook using training images only
                bow = freakBow.Learn(originalTrainImages.Values.ToArray());
            }
            else
            {
                // We will use HOG, so we can use a standard clustering
                // algorithm that is based on Euclidean distances. A good
                // algorithm for clustering codewords is the Binary Split
                // variant of the K-Means algorithm.

                // Create a Binary-Split clustering algorithm
                BinarySplit binarySplit = new BinarySplit(numberOfWords);

                // Create a HOG detector explicitly (if no detector was passed,
                // the BagOfVisualWords would be using a SURF detector by default).
                var hog = new HistogramsOfOrientedGradients();

                // Create bag-of-words (BoW) with the given algorithm
                var hogBow = BagOfVisualWords.Create(hog, binarySplit);

                // Compute the BoW codebook using training images only
                bow = hogBow.Learn(originalTrainImages.Values.ToArray());
            }

            sw1.Stop();

            // Now that we have already created and computed the BoW model, we
            // will use it to extract representations for each of the images in
            // both training and testing sets.

            Stopwatch sw2 = Stopwatch.StartNew();

            // Extract features for all images
            foreach (ListViewItem item in listView1.Items)
            {
                // Get item image
                Bitmap image = originalImages[item.ImageKey] as Bitmap;

                // Get a feature vector representing this image
                double[] featureVector = (bow as ITransform <Bitmap, double[]>).Transform(image);

                // Represent it as a string so we can show it onscreen
                string featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture);

                // Show it in the visual grid
                if (item.SubItems.Count == 2)
                {
                    item.SubItems[1].Text = featureString;
                }
                else
                {
                    item.SubItems.Add(featureString);
                }

                // Retrieve the class labels, that we had stored in the Tag
                int classLabel = (item.Tag as Tuple <double[], int>).Item2;

                // Now, use the Tag to store the feature vector too
                item.Tag = Tuple.Create(featureVector, classLabel);
            }

            sw2.Stop();

            lbStatus.Text = "BoW constructed in " + sw1.Elapsed + "s. Features extracted in " + sw2.Elapsed + "s.";
            btnSampleRunAnalysis.Enabled = true;
        }
        /// <summary>
        /// This methods only for admin, and this recompute bagOfContourFragments and svm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCompute_Click(object sender, EventArgs e)
        {
            //Accord.Math.Random.Generator.Seed = 1;

            DirectoryInfo path = new DirectoryInfo(Path.Combine(Application.StartupPath, "Resources/Res"));

            ///Create dictionary for train images
            originalTrainImages = new Dictionary <int, Bitmap>();

            int j = 0;

            int k = 0;

            foreach (DirectoryInfo classFolder in path.EnumerateDirectories())
            {
                ///Add name of folder
                string name = classFolder.Name;

                ///Upload all files in aarray
                FileInfo[] files = GetFilesByExtensions(classFolder, ".jpg", ".tif").ToArray();

                //Shuffle objects in array
                Vector.Shuffle(files);

                //For each image complite some easy operations
                for (int i = 0; i < files.Length; i++)
                {
                    //Uploat only train images
                    //70%
                    if ((i / (double)files.Length) < 0.7)
                    {
                        //Add file
                        FileInfo file = files[i];

                        //Create image from file
                        Bitmap image = (Bitmap)Bitmap.FromFile(file.FullName);

                        //Use detector
                        CannyEdgeDetector filterCanny = new CannyEdgeDetector();

                        //Apply changes
                        filterCanny.ApplyInPlace(image);

                        //Add some information of image
                        string shortName = file.Name;
                        int    imageKey  = j;

                        //Add image to dictionary
                        originalTrainImages.Add(j, image);

                        //Save correct key of class for image
                        outputsResult[j] = k;
                        j++;
                    }
                }
                //Change key of folder
                k++;
            }

            //Create teacher for svm, using Histogram Intersection
            var teacher = new MulticlassSupportVectorLearning <HistogramIntersection>()
            {
                //Add leaner params
                Learner = (param) => new SequentialMinimalOptimization <HistogramIntersection>()
                {
                    //Create kernel with optimal params
                    Kernel = new HistogramIntersection(0.25, 1),
                }
            };

            //Create KMeans algr
            var kmodes = new KModes <byte>(numberOfContour, new Hamming());

            //Create detector
            var detector = new FastRetinaKeypointDetector();

            //Create bagOfContourFragments
            bagOfContourFragments = new BagOfVisualWords(numberOfContour);

            //Learned bagOfContourFragments
            bagOfContourFragments.Learn(originalTrainImages.Values.ToArray());

            //For each iamge add inputs info
            for (int i = 0; i < originalTrainImages.Count; i++)
            {
                Bitmap image = originalTrainImages[i] as Bitmap;

                inputsInfo[i] = (bagOfContourFragments as ITransform <Bitmap, double[]>).Transform(image);
            }

            //Save condition of bagOfContourFragments
            BinarySave.WriteBinary(bagOfContourFragments);

            //Teach svm
            multiSVM = teacher.Learn(inputsInfo, outputsResult);

            //Save condition of svm
            BinarySave.WriteBinary(multiSVM);
        }
        public void SerializeTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            FastCornersDetector fast = new FastCornersDetector();

            FastRetinaKeypointDetector freak = new FastRetinaKeypointDetector(fast);

            var kmodes = new KModes<byte[]>(5, Distance.BitwiseHamming);

            var bow = new BagOfVisualWords<FastRetinaKeypoint, byte[]>(freak, kmodes);

            bow.Compute(images);



            double[][] expected = new double[images.Length][];
            for (int i = 0; i < expected.Length; i++)
                expected[i] = bow.GetFeatureVector(images[i]);

            MemoryStream stream = new MemoryStream();
            BinaryFormatter fmt = new BinaryFormatter();
            fmt.Serialize(stream, bow);
            stream.Seek(0, SeekOrigin.Begin);
            bow = (BagOfVisualWords<FastRetinaKeypoint, byte[]>)fmt.Deserialize(stream);

            double[][] actual = new double[expected.Length][];
            for (int i = 0; i < actual.Length; i++)
                actual[i] = bow.GetFeatureVector(images[i]);


            Assert.IsTrue(expected.IsEqual(actual));
        }