Esempio n. 1
0
        /// <summary>
        /// Creates several splits of the dataset.
        /// </summary>
        /// <param name="sampleFrequency">How often to sample pixels within the training images.</param>
        /// <param name="boxRows">Vertical trim around the edges of images to avoid feature tests beyond the boundary of the image</param>
        /// <param name="boxColumns">Vertical trim around the edges of images to avoid feature tests beyond the boundary of the image</param>
        /// <param name="numSplits">Number of splits to create</param>
        /// <returns>Splits of the data</returns>
        public List <ImageDataPoint <T> >[] CreateDataPoints(int sampleFrequency, int boxRows, int boxColumns, int numSplits)
        {
            List <ImageDataPoint <T> > points = new List <ImageDataPoint <T> >();

            foreach (LabeledImage <T> labelledImage in _images)
            {
                IMultichannelImage <T> image  = labelledImage.Image;
                LabelImage             labels = labelledImage.Labels;
                LabelSet set = labels.Labels;
                string   id  = labelledImage.ID;
                bool[,] valid = labelledImage.Valid;
                int maxRows    = image.Rows - boxRows;
                int maxColumns = image.Columns - boxColumns;
                for (int r = boxRows; r < maxRows; r += sampleFrequency)
                {
                    for (int c = boxColumns; c < maxColumns; c += sampleFrequency)
                    {
                        short label  = getLabel(labels[r, c], set);
                        bool  sample = valid[r, c];
                        if (sample && label == LabelImage.BackgroundLabel)
                        {
                            switch (_backgroundSampleMode)
                            {
                            case BackgroundSampleMode.Ignore:
                                sample = false;
                                break;

                            case BackgroundSampleMode.Half:
                                sample = ThreadsafeRandom.Test(.5);
                                break;
                            }
                        }
                        if (sample)
                        {
                            points.Add(new ImageDataPoint <T>(image, (short)r, (short)c, label));
                        }
                    }
                }
            }
            List <ImageDataPoint <T> >[] splits = new List <ImageDataPoint <T> > [numSplits];
            for (int i = 0; i < numSplits; i++)
            {
                if (_byImage)
                {
                    splits[i] = sampleByImage(points);
                }
                else
                {
                    splits[i] = sample(points);
                }
            }

            return(splits);
        }
Esempio n. 2
0
        private List <ImageDataPoint <T> > sample(List <ImageDataPoint <T> > points)
        {
            List <ImageDataPoint <T> > result = new List <ImageDataPoint <T> >();

            for (int i = 0; i < points.Count; i++)
            {
                if (ThreadsafeRandom.Test(_dataPercentage))
                {
                    result.Add(points[i]);
                }
            }
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Computes the inverse label frequency array for the image.  This is an array in which each index holds a value equal
        /// to the total number of image labels divided by the total number of that particular label.
        /// </summary>
        /// <param name="numLabels">Total number of labels</param>
        /// <returns>Inverse label frequency</returns>
        public unsafe float[] ComputeInverseLabelFrequency(int numLabels)
        {
            int[] counts = new int[numLabels];
            foreach (LabeledImage <T> image in _images)
            {
                LabelImage labels = image.Labels;
                LabelSet   set    = labels.Labels;
                fixed(short *labelsSrc = labels.RawArray)
                {
                    int    count     = labels.Rows * labels.Columns;
                    short *labelsPtr = labelsSrc;

                    while (count-- > 0)
                    {
                        short index  = getLabel(*labelsPtr++, set);
                        bool  sample = true;
                        if (index == LabelImage.BackgroundLabel)
                        {
                            switch (_backgroundSampleMode)
                            {
                            case BackgroundSampleMode.Ignore:
                                sample = false;
                                break;

                            case BackgroundSampleMode.Half:
                                sample = ThreadsafeRandom.Test(.5);
                                break;
                            }
                        }
                        if (!sample)
                        {
                            continue;
                        }
                        counts[index]++;
                    }
                }
            }
            float[] frequency = new float[numLabels];
            float   sum       = 0;

            for (short i = 0; i < frequency.Length; i++)
            {
                frequency[i] = 1 + counts[i];
                sum         += frequency[i];
            }
            for (int i = 0; i < frequency.Length; i++)
            {
                frequency[i] = sum / frequency[i];
            }
            return(frequency);
        }
Esempio n. 4
0
        private List <ImageDataPoint <T> > sampleByImage(List <ImageDataPoint <T> > points)
        {
            List <ImageDataPoint <T> > result = new List <ImageDataPoint <T> >();
            var imageGroups = points.GroupBy(o => o.ImageID);

            foreach (var group in imageGroups)
            {
                if (ThreadsafeRandom.Test(_imagePercentage))
                {
                    result.AddRange(group.Where(o => ThreadsafeRandom.Test(_dataPercentage)));
                }
            }

            return(result);
        }
Esempio n. 5
0
        private List <LabelVector> findCenterCandidates(object arg)
        {
            Job job = arg as Job;
            List <LabelVector> result = new List <LabelVector>();
            double             norm   = OversampleRate / _phi;

            for (int i = job.StartIndex; i < job.EndIndex; i++)
            {
                LabelVector x      = _data[i];
                double      sample = x.FeatureValue * norm;
                if (ThreadsafeRandom.Test(sample))
                {
                    LabelVector center = x.Clone() as LabelVector;
                    result.Add(center);
                }
            }
            return(result);
        }
Esempio n. 6
0
        private unsafe List <ImageDataPoint <T> > createAllDataPointsLabels(BackgroundSampleMode mode)
        {
            List <ImageDataPoint <T> > points = new List <ImageDataPoint <T> >();
            int      rows    = _image.Rows;
            int      columns = _image.Columns;
            LabelSet set     = _labels.Labels;

            fixed(short *labelsSrc = _labels.RawArray)
            {
                fixed(bool *validSrc = _valid)
                {
                    short *labelsPtr = labelsSrc;
                    bool * validPtr  = validSrc;

                    for (short r = 0; r < rows; r++)
                    {
                        for (short c = 0; c < columns; c++)
                        {
                            short label  = getLabel(*labelsPtr++, set);
                            bool  sample = *validPtr++;
                            if (sample && label == LabelImage.BackgroundLabel)
                            {
                                switch (mode)
                                {
                                case BackgroundSampleMode.Ignore:
                                    sample = false;
                                    break;

                                case BackgroundSampleMode.Half:
                                    sample = ThreadsafeRandom.Test(.5);
                                    break;
                                }
                            }
                            if (sample)
                            {
                                points.Add(new ImageDataPoint <T>(_image, r, c, label));
                            }
                        }
                    }
                }
            }

            return(points);
        }