protected override byte[] ExecuteBody()
        {
            InitProcnum();

            var  task             = GetElbow();
            uint numberOfClusters = task.Result;

            InitEmptyKmeansClusters(numberOfClusters);
            AssignPointsToClusters();
            uint  iter       = 1;
            float prevSSE    = float.MaxValue;
            float currentSSE = TotalSSE();

            while (iter <= MaxIter && (currentSSE / prevSSE) < ChangeThreshold)
            {
                prevSSE = currentSSE;
                UpdateCentroids();
                ReassignPointsToClusters();
                currentSSE = TotalSSE();
                CancellationToken.ThrowIfCancellationRequested();
                Progress.Report(ClusteringProgress.StepTaken((int)iter));
                ++iter;
            }
            System.Diagnostics.Debug.WriteLine("Final numberOfClusters: " + numberOfClusters + " with iterations: " + (iter - 1) +
                                               " and SSE = " + currentSSE);

            return(GetResultInByteArray());
        }
Exemplo n.º 2
0
        protected override byte[] ExecuteBody()
        {
            if (_isPoly)
            {
                InitManualClustersPoly(_polysInPoints);
            }
            else
            {
                InitManualClusters(_Clusters);
            }

            AssignPointsToClusters();
            uint  iter       = 1;
            float prevSSE    = float.MaxValue;
            float currentSSE = TotalSSE();

            while (iter <= MaxIter && (currentSSE / prevSSE) < _ChangeThreshold)
            {
                prevSSE = currentSSE;
                UpdateCentroids();
                ReassignPointsToClusters();
                currentSSE = TotalSSE();

                CancellationToken.ThrowIfCancellationRequested();
                Progress.Report(ClusteringProgress.StepTaken((int)iter));
                ++iter;
                System.Diagnostics.Debug.WriteLine("(currentSSE / prevSSE): " + (currentSSE / prevSSE));
            }
            System.Diagnostics.Debug.WriteLine("Clusternum: " + ClusterNum + " with iterations: " + (iter - 1) + " and SSE = " + currentSSE);

            return(GetResultInByteArray());
        }
Exemplo n.º 3
0
 protected override byte[] Execute(ImageLoader imageLoader)
 {
     Progress.Report(ClusteringProgress.Initializing(MaxStep));
     LoadPoints(imageLoader);
     CancellationToken.ThrowIfCancellationRequested();
     return(ExecuteBody());
 }
Exemplo n.º 4
0
        private TreeModel[] GetTreeModelsInParallel(byte[][] points, byte[] targets, Random[] randoms)
        {
            TreeModel[] treeModels = new TreeModel[TreeCount];

            int treesTrainedCount = 0;
            var rangePartitioner  = Partitioner.Create(0, TreeCount);

            ParallelOptions parallelOptions = new ParallelOptions();

            parallelOptions.CancellationToken = _cancellationToken;

            Parallel.ForEach(rangePartitioner, parallelOptions, range =>
            {
                for (int i = range.Item1; i < range.Item2; ++i)
                {
                    TreeTrainer trainer = new TreeTrainer(BandCountPerSplit, MaxTreeHeight, MinNodeSize, BootstrappingRatio, randoms[i]);
                    treeModels[i]       = trainer.Train(points, targets, _cancellationToken);

                    Interlocked.Increment(ref treesTrainedCount);
                    _progress.Report(ClusteringProgress.StepTaken(treesTrainedCount));
                    parallelOptions.CancellationToken.ThrowIfCancellationRequested();
                }
            });

            return(treeModels);
        }
Exemplo n.º 5
0
        private byte[] PredictPoints(byte[][] points, IProgress <ClusteringProgress> progress, CancellationToken cancellationToken)
        {
            byte[] result = new byte[points.Length];

            int pointsPredictedCount = 0;
            int lastPercentage       = 0;
            var rangePartitioner     = Partitioner.Create(0, points.Length);

            ParallelOptions parallelOptions = new ParallelOptions();

            parallelOptions.CancellationToken = cancellationToken;

            Parallel.ForEach(rangePartitioner, parallelOptions, range =>
            {
                for (int i = range.Item1; i < range.Item2; ++i)
                {
                    result[i] = Predict(points[i]);

                    parallelOptions.CancellationToken.ThrowIfCancellationRequested();

                    Interlocked.Increment(ref pointsPredictedCount);
                    int percentage = (int)((float)pointsPredictedCount / points.Length * 100.0F);
                    if (percentage > lastPercentage)
                    {
                        Interlocked.Increment(ref lastPercentage);
                        progress.Report(ClusteringProgress.StepTaken(percentage));
                    }
                }
            });
            return(result);
        }
Exemplo n.º 6
0
        protected override byte[] ExecuteBody()
        {
            uint  ClusterNum     = MinClusterNum;
            bool  RelativeChange = false;
            float GlobalPrevSSE  = float.MaxValue;

            while (ClusterNum <= MaxClusterNum && !RelativeChange)
            {
                InitEmptyKmeansClusters(ClusterNum);
                AssignPointsToClusters();
                uint  iter       = 1;
                float prevSSE    = float.MaxValue;
                float currentSSE = TotalSSE();
                while (iter <= MaxIter && (currentSSE / prevSSE) < ChangeThreshold)
                {
                    prevSSE = currentSSE;
                    UpdateCentroids();
                    ReassignPointsToClusters();
                    currentSSE = TotalSSE();

                    CancellationToken.ThrowIfCancellationRequested();
                    Progress.Report(ClusteringProgress.StepTaken((int)iter));
                    ++iter;
                    System.Diagnostics.Debug.WriteLine("(currentSSE / prevSSE): " + (currentSSE / prevSSE));
                }
                System.Diagnostics.Debug.WriteLine("Clusternum: " + ClusterNum + " with iterations: " + (iter - 1) +
                                                   " and SSE = " + currentSSE + " and relative change = " + (currentSSE / GlobalPrevSSE) + " current elbow: " + Elbow);
                RelativeChange = (1 - (currentSSE / GlobalPrevSSE) < Elbow);
                GlobalPrevSSE  = currentSSE;
                ++ClusterNum;
            }

            FinalSSE = GlobalPrevSSE;
            return(GetResultInByteArray());
        }
Exemplo n.º 7
0
 private void UpdateStatusLabel(ClusteringProgress progress)
 {
     if (progress.IsInitializing)
     {
         toolStripStatusLabel.Text = "Initializing...";
     }
     else
     {
         toolStripStatusLabel.Text = $"{progress.Value} tree(s) trained...";
     }
 }
Exemplo n.º 8
0
        protected override byte[] Execute(ImageLoader imageLoader)
        {
            Progress.Report(ClusteringProgress.Initializing(100));
            byte[][] points = imageLoader.LoadJaggedPoints();
            CancellationToken.ThrowIfCancellationRequested();

            var clusters = _model.Predict(points, Progress, CancellationToken);

            GetResult(clusters);
            return(clusters);
        }
Exemplo n.º 9
0
 private void UpdateProgressBar(ClusteringProgress progress)
 {
     if (progress.IsInitializing)
     {
         toolStripProgressBar.Value   = 0;
         toolStripProgressBar.Maximum = progress.MaxValue;
     }
     else
     {
         toolStripProgressBar.Value = progress.Value;
     }
 }
Exemplo n.º 10
0
        private RandomForestModel GetModel(byte[][] points, byte[] targets, int bandCount)
        {
            TreeModel[] treeModels = new TreeModel[TreeCount];

            TreeTrainer trainer = new TreeTrainer(BandCountPerSplit, MaxTreeHeight, MinNodeSize, BootstrappingRatio, new Random(_random.Next()));

            for (int i = 0; i < TreeCount; ++i)
            {
                treeModels[i] = trainer.Train(points, targets, _cancellationToken);

                _progress.Report(ClusteringProgress.StepTaken(i + 1));
            }

            return(new RandomForestModel(treeModels, bandCount));
        }
Exemplo n.º 11
0
        private RandomForestModel Train(GeoImageData image, int[] includedBands, GeoImageData clustering, int clusteringBand)
        {
            ValidateInputImages(image, clustering);
            HandleBandCountPerSplitValue(includedBands.Length);

            _progress.Report(ClusteringProgress.Initializing(TreeCount));

            var imageLoader = new GeoImageLoader(image, includedBands);

            byte[][] points = imageLoader.LoadJaggedBands();

            var targetLoader = new GeoImageLoader(clustering, clusteringBand);

            byte[] targets = targetLoader.LoadPoints(); //RandomForestUtilities.LoadPoints(clustering, clusteringBand);

            return(TrainForestModel(points, targets, includedBands.Length));
        }
Exemplo n.º 12
0
 private void ReportProgress(ClusteringProgress progress)
 {
     if (progress.IsInitializing)
     {
         toolStripStatusLabel.Text = "Initializing...";
         SetProgressBarToBlocks(progress.MaxValue);
     }
     else if (!progress.IsUnbounded)
     {
         toolStripStatusLabel.Text  = $"Step {progress.Value} complete...";
         toolStripProgressBar.Value = progress.Value;
     }
     else
     {
         toolStripStatusLabel.Text = "Clustering in progress...";
         SetProgressBarToMarquee();
     }
 }
Exemplo n.º 13
0
        public float ClusteringAtGivenNumber(uint numberOfClusters)
        {
            InitEmptyKmeansClusters(numberOfClusters);
            AssignPointsToClusters();
            uint  iter       = 1;
            float prevSSE    = float.MaxValue;
            float currentSSE = TotalSSE();

            while (iter <= MaxIter && (currentSSE / prevSSE) < ChangeThreshold)
            {
                prevSSE = currentSSE;
                UpdateCentroids();
                ReassignPointsToClusters();
                currentSSE = TotalSSE();
                CancellationToken.ThrowIfCancellationRequested();
                Progress.Report(ClusteringProgress.StepTaken((int)iter));
                ++iter;
            }
            System.Diagnostics.Debug.WriteLine("Async numberOfClusters: " + numberOfClusters + " with iterations: " + (iter - 1) +
                                               " and SSE = " + currentSSE);

            return(currentSSE);
        }
Exemplo n.º 14
0
 private void ReportProgress(ClusteringProgress progress)
 {
     UpdateProgressBar(progress);
     UpdateStatusLabel(progress);
 }