상속: KMeans
예제 #1
0
        public static void createBoW()
        {
            string[] files = Directory.GetFiles ("testset");
            Dictionary<string, Bitmap> testImages = new Dictionary<string, Bitmap>();

            for (int i = 0; i < files.Length; i++) {
                string name = Path.GetFileNameWithoutExtension (files [i]);
                testImages.Add(name, (Bitmap)Bitmap.FromFile(files[i]));
            }

            int numberOfWords = 6; // number of cluster centers: typically >>100

            // 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
            Bitmap[] bmps = new Bitmap[testImages.Count];
            testImages.Values.CopyTo(bmps, 0);
            surfBow.Compute(bmps);

            surfBow.Save ("bagOfWords");
        }
예제 #2
0
        public void BinarySplitConstructorTest()
        {

            Accord.Math.Tools.SetupGenerator(0);


            // Declare some observations
            double[][] observations = 
            {
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },
                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },
                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

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

            // Create a new binary split with 3 clusters 
            BinarySplit binarySplit = new BinarySplit(3);

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

            // As a result, the first two 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 three.

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

            Assert.AreEqual(labels[2], labels[3]);
            Assert.AreEqual(labels[2], labels[4]);
            Assert.AreEqual(labels[2], labels[5]);

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

            Assert.AreNotEqual(labels[0], labels[2]);
            Assert.AreNotEqual(labels[2], labels[6]);
            Assert.AreNotEqual(labels[0], labels[6]);


            int[] labels2 = binarySplit.Clusters.Nearest(observations);

            Assert.IsTrue(labels.IsEqual(labels2));

            // the data must not have changed!
            Assert.IsTrue(orig.IsEqual(observations));
        }
예제 #3
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;
        }
예제 #4
0
        public void binary_split_new_method()
        {
            #region doc_sample1
            // Use a fixed seed for reproducibility
            Accord.Math.Random.Generator.Seed = 0;

            // Declare some data to be clustered
            double[][] input = 
            {
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },
                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },
                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            // Create a new binary split with 3 clusters 
            BinarySplit binarySplit = new BinarySplit(3);

            // Learn a data partitioning using the Binary Split algorithm
            KMeansClusterCollection clustering = binarySplit.Learn(input);

            // Predict group labels for each point
            int[] output = clustering.Decide(input);

            // As a result, the first two 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 three.
            #endregion

            Assert.AreEqual(output[0], output[1]);

            Assert.AreEqual(output[2], output[3]);
            Assert.AreEqual(output[2], output[4]);
            Assert.AreEqual(output[2], output[5]);

            Assert.AreEqual(output[6], output[7]);
            Assert.AreEqual(output[6], output[8]);

            Assert.AreNotEqual(output[0], output[2]);
            Assert.AreNotEqual(output[2], output[6]);
            Assert.AreNotEqual(output[0], output[6]);

            int[] labels2 = binarySplit.Clusters.Nearest(input);

            Assert.IsTrue(output.IsEqual(labels2));
        }
예제 #5
0
 public void BinarySplitConstructorTest3()
 {
     // Create a new algorithm
     BinarySplit binarySplit = new BinarySplit(3);
     Assert.IsNotNull(binarySplit.Clusters);
     Assert.IsNotNull(binarySplit.Distance);
     Assert.IsNotNull(binarySplit.Clusters.Centroids);
     Assert.IsNotNull(binarySplit.Clusters.Count);
     Assert.IsNotNull(binarySplit.Clusters.Covariances);
     Assert.IsNotNull(binarySplit.Clusters.Proportions);
 }
예제 #6
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);

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

            if (cbExtended.Checked)
                bow.Detector.ComputeDescriptors = SpeededUpRobustFeatureDescriptorType.Extended;

            Stopwatch sw1 = Stopwatch.StartNew();

            // Compute the BoW codebook using training images only
            var points = bow.Compute(originalTrainImages.Values.ToArray());

            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;
        }
 private void setBinaryCluster(System.IO.StreamReader sr)
 {
     BinarySplit bSplit = new BinarySplit(k);
     KMeansClusterCollection kmeansColl = bSplit.Clusters;
     for (int i = 0; i < k; i++)
     {
         double[] mns = (from s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
         sr.ReadLine();
         double p = System.Convert.ToDouble(sr.ReadLine());
         KMeansCluster kc = new KMeansCluster(kmeansColl, i);
         kc.Mean = mns;
         kc.Proportion = p;
     }
     clusterCollection = kmeansColl;
     model = bSplit;
 }
        public void buildModel()
        {
            if (inputMatrix == null) getMatrix();
            switch (cType)
            {
                case clusterType.KMEANS:
                    KMeans kmeans = new KMeans(k);
                    kmeans.Compute(inputMatrix, precision);
                    clusterCollection = kmeans.Clusters;
                    model = kmeans;
                    break;
                case clusterType.BINARY:
                    BinarySplit bSplit = new BinarySplit(k);
                    bSplit.Compute(inputMatrix, precision);
                    clusterCollection = bSplit.Clusters;
                    model = bSplit;
                    //Console.WriteLine("BinarySplit");
                    break;
                case clusterType.GAUSSIANMIXTURE:
                    GaussianMixtureModel gModel = new GaussianMixtureModel(k);
                    gModel.Compute(inputMatrix, precision);
                    clusterCollection = gModel.Gaussians;
                    model = gModel;
                    break;
                default:
                    break;
            }

            lbl = new List<string>();
            for (int i = 0; i < k; i++)
            {
                lbl.Add(i.ToString());
            }
        }