Example #1
0
        public static LabeledImage[] Subset(int Rows, LabeledImage[] Images, int SubsetSize)
        {
            var ImagesByLabel = OrganizeByLabel(Rows, Images);

            var LeadSet = new LabeledImage[SubsetSize];

            int  count = 0;
            byte label = 0;

            while (count < SubsetSize)
            {
                var image = ImagesByLabel[label].Last();
                LeadSet[count] = image;
                ImagesByLabel[label].Remove(image);

                count++; label++;
                if (label >= NumLabels)
                {
                    label = 0;
                }
            }

            Debug.Assert(ImagesByLabel.Sum(pair => pair.Value.Count) == Rows - SubsetSize);

            return(Shuffle(LeadSet));
        }
Example #2
0
        public static LabeledImage[] Init()
        {
            // Read label meta file.
            var LabelNames = File.ReadAllLines(Path.Combine(TrainingDataDir, LabelFile));

            // Read training data.
            var bytes = File.ReadAllBytes(Path.Combine(TrainingDataDir, FileName));

            Debug.Assert(bytes.Length == Rows * RowSize);

            // Unpack training data.
            LabeledImage[] images       = new LabeledImage[Rows];
            var            source_index = 0;

            for (int row = 0; row < Rows; row++)
            {
                var image  = images[row] = new LabeledImage();
                var pixels = image.Pixels = new float[Width, Height, 3];

                image.Label = bytes[source_index++];

                for (int channel = 0; channel < 3; channel++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        for (int y = 0; y < Height; y++)
                        {
                            pixels[x, y, channel] = bytes[source_index++] / 255f;
                        }
                    }
                }

                //// Swap out source images for derivatives.
                //var diff = new float[Width, Height, 3];
                //for (int x = 0; x < Width; x++)
                //for (int y = 0; y < Height; y++)
                //{
                //    diff[x, y, 0] = .33333f * (pixels[x, y, 0] + pixels[x, y, 0] + pixels[x, y, 0]);
                //    diff[x, y, 1] = 0;
                //    diff[x, y, 2] = 0;
                //}
                //for (int x = 1; x < Width - 1; x++)
                //for (int y = 1; y < Height - 1; y++)
                //{
                //    diff[x, y, 1] = 10 * (pixels[x + 1, y, 0] - pixels[x, y, 0]);
                //    diff[x, y, 2] = 10 * (pixels[x, y + 1, 0] - pixels[x, y, 0]);
                //}
                //for (int x = 0; x < Width; x++)
                //for (int y = 0; y < Height; y++)
                //{
                //    diff[x, y, 0] = 0;
                //}

                //images[row].Pixels = diff;
            }

            Debug.Assert(source_index == Rows * RowSize);

            return(images);
        }
Example #3
0
        void ProcessLabeledImage(LabeledImage labeledImage)
        {
            ProcessedImageServer labeledAvImage = new ProcessedImageServer(labeledImage.Name,
                                                                           labeledImage.Label,
                                                                           labeledImage.ByteImage);

            _dispatcher.Post(() =>
            {
                Task t = new Task(() =>
                {
                    ProcessedImageCollection.Add(labeledAvImage);
                    _databaseMailbox.Post(labeledAvImage);
                    _processedImagesAmount++;
                    _addedNewProcessedImage.Set();

                    Console.Write($"[Ready {_processedImagesAmount}/{_newImagesAmount}]");
                    Console.WriteLine($" Image <{labeledAvImage.Name}> added into Database.");

                    if (_processedImagesAmount == _newImagesAmount)
                    {
                        _dispatcherCompletionEvent.Set();
                    }
                });
                t.Start();

                return(t);
            });
        }
Example #4
0
        public static LabeledImage[] Remainder(LabeledImage[] FullSet, LabeledImage[] OtherSet)
        {
            LabeledImage[] RemainderSet = new LabeledImage[FullSet.Length - OtherSet.Length];

            int index = 0;

            foreach (var image in FullSet)
            {
                if (!OtherSet.Contains(image))
                {
                    RemainderSet[index++] = image;
                }
            }

            return(Shuffle(RemainderSet));
        }
Example #5
0
        private void GetImg(LabeledImage j)
        {
            var img = new Image {
                Source = j.imagen
            };
            var lbl = new TextBlock {
                Text = j.titulo, FontSize = 11
            };
            var pnl = new StackPanel {
                Children = { img, lbl }
            };
            var bl = new BlockUIContainer(pnl);

            _fltImages.Blocks.Add(bl);
            Rsze();
            _imgAdjust += pnl.ActualHeight + 80;
        }
Example #6
0
        public static LabeledImage[] SampleSet(int Rows, LabeledImage[] Images, int InLabel, int SampleSize, int InLabelSize)
        {
            var LeadSet = new LabeledImage[SampleSize];

            int lead_index = 0, source_index = rnd.Next(0, Rows);

            while (lead_index < SampleSize)
            {
                source_index++; if (source_index >= Rows)
                {
                    source_index = 0;
                }

                if (lead_index < InLabelSize && Images[source_index].Label == InLabel ||
                    lead_index >= InLabelSize && Images[source_index].Label != InLabel)
                {
                    LeadSet[lead_index] = Images[source_index];
                    lead_index++;
                }
            }

            return(LeadSet);
        }
Example #7
0
 public void ProcessResults(LabeledImage image)
 {
     Console.WriteLine(image.ToString());
 }
Example #8
0
        /// <summary>
        /// Classifies each point from <paramref name="image"/> and trackes which nodes it visits.
        /// </summary>
        /// <param name="tree">The tree used for the computation</param>
        /// <param name="image">Image to add to the tree</param>
        /// <param name="mode">Mode to use when sampling the image</param>
        public static void Fill <T>(this DecisionTree <ImageDataPoint <T>, T[]> tree, LabeledImage <T> image, BackgroundSampleMode mode)
        {
            List <ImageDataPoint <T> > points = image.CreateAllDataPoints(mode);

            tree.Fill(points);
        }
Example #9
0
        /// <summary>
        /// Classifies every pixel in the provided image, returning a full distribution over all labels.
        /// </summary>
        /// <param name="forest">The forest used for the computation</param>
        /// <param name="image">Image to classify</param>
        /// <returns>The classified image</returns>
        public static DistributionImage ClassifySoft <T>(this DecisionForest <ImageDataPoint <T>, T[]> forest, LabeledImage <T> image)
        {
            DistributionImage dist = new DistributionImage(image.Image.Rows, image.Image.Columns, forest.LabelCount);

            dist.ID = image.ID;
            for (int t = 0; t < forest.TreeCount; t++)
            {
                forest[t].ClassifySoft(image, dist);
            }
            dist.DivideThrough(forest.TreeCount);
            dist.Normalize();
            return(dist);
        }
Example #10
0
 /// <summary>
 /// Computes a histogram for all trees from the provided image.
 /// </summary>
 /// <param name="forest">The forest used for the computation</param>
 /// <param name="image">Image to classify</param>
 /// <returns>The histogram</returns>
 public static TreeHistogram ComputeHistogram <T>(this DecisionForest <ImageDataPoint <T>, T[]> forest, LabeledImage <T> image)
 {
     return(forest.ComputeHistogram(image.CreateAllDataPoints(BackgroundSampleMode.Full)));
 }
Example #11
0
 /// <summary>
 /// Adds the data from <paramref name="image"/> to each tree in the forest.
 /// </summary>
 /// <param name="forest">The forest used for the computation</param>
 /// <param name="image">Image to learn from</param>
 /// <param name="mode">Mode to use when sampling the image background</param>
 public static void Fill <T>(this DecisionForest <ImageDataPoint <T>, T[]> forest, LabeledImage <T> image, BackgroundSampleMode mode)
 {
     for (int i = 0; i < forest.TreeCount; i++)
     {
         forest[i].Fill(image, mode);
     }
     forest.RefreshMetadata();
 }
Example #12
0
        /// <summary>
        /// Classifies points from <paramref name="labeledImage"/> (using the mask if present) and adds the distributions at each pixel to <paramref name="dist"/>.
        /// </summary>
        /// <param name="tree">The tree used for the computation</param>
        /// <param name="labeledImage">The image to classify</param>
        /// <param name="dist">Image which is used to store the distributions</param>
        public static void ClassifySoft <T>(this DecisionTree <ImageDataPoint <T>, T[]> tree, LabeledImage <T> labeledImage, DistributionImage dist)
        {
            List <ImageDataPoint <T> > points = labeledImage.CreateAllDataPoints(BackgroundSampleMode.Full);
            List <int> indices = new List <int>();

            for (int i = 0; i < points.Count; i++)
            {
                indices.Add(i);
            }
            INodeInfo <ImageDataPoint <T>, T[]>[] info = new INodeInfo <ImageDataPoint <T>, T[]> [points.Count];
            DecisionTree <ImageDataPoint <T>, T[]> .assignLabels(tree._root, points, info, indices);

            for (int i = 0; i < info.Length; i++)
            {
                dist.Add(points[i].Row, points[i].Column, info[i].Distribution);
            }
        }
Example #13
0
 private static void PrintLogs(LabeledImage img)
 {
     System.Console.WriteLine(img.Name + " " + img.Label);
 }
Example #14
0
 public BlockedImage(LabeledImage image)
 {
     this.Pixels = image.Pixels;
     this.Label  = image.Label;
 }