コード例 #1
0
        public static void SupervisedKernelHgForLabel(int Rows, LabeledImage[] images, int InLabel)
        {
            // Select training set and test set.
            const int TrainingSize = 9000;
            int       TestSize     = Rows - TrainingSize;
            var       TrainingSet  = Subset(Rows, images, TrainingSize);
            var       TestSet      = Remainder(images, TrainingSet);

            // Select smaller set to generate leads with.
            const int LeadSize    = 200;
            const int InLabelSize = LeadSize / 2;

            LabeledImage[] LeadSet = SampleSet(TrainingSize, TrainingSet, InLabel, LeadSize, InLabelSize);

            // Get list of candidate kernels from the lead set.
            var list = Learn_SupervisedKernelHg(LeadSize, LeadSet, InLabel: InLabel, InLabelMinRatio: 0.2f, SearchLength: 50);

            list.Sort((kernelScore1, kernelScore2) => kernelScore2.Item2.CompareTo(kernelScore1.Item2));

            foreach (var kernelScore in list)
            {
                Console.WriteLine($"{kernelScore.Item2}");
            }

            // Test candidates on test set.
            const int BlockDim = 5;
            var       blocks   = GetBlocks(TestSet, TestSize, Width, Height, Channels: 3, BlockDim: BlockDim, AddMirrors: true);

            foreach (var kernel in list)
            {
                var tracker = new BlockTracker();
                var result  = GetScoreStats(TestSize, blocks, kernel.Item1, InLabel, tracker);
                Console.WriteLine($"Score is {result.InOutHitRatio} with an in ratio of {result.InHitRatio}");

                //pl.plot(Average(tracker.in_remainders), Average(tracker.out_remainders));
                //pl.plot(Average(tracker.in_blocks), Average(tracker.out_blocks));
                pl.plot(minus(Average(tracker.in_remainders), Average(tracker.out_remainders)));
                pl.plot(minus(Average(tracker.in_blocks), Average(tracker.out_blocks)));

                //var score = OptimizeKernelCutoff(TestSize, blocks, kernel.Item1, InLabel: InLabel, InLabelMinRatio: 0.2f);
                //Console.WriteLine($"Score is {score.InOutHitRatio} with an in ratio of {score.InHitRatio}");
            }
        }
コード例 #2
0
        static ScoreStats GetScoreStats(int Rows, BlockedImage[] images, Kernel kernel, int InLabel, BlockTracker tracker = null)
        {
            var stats = new ScoreStats();

            for (int i = 0; i < Rows; i++)
            {
                var image = images[i];

                var   action = tracker == null ? null : tracker.GetTrackInOut(image, InLabel);
                float error  = GetScore(image, kernel, action: action);

                if (image.Label == InLabel)
                {
                    stats.InCount++;

                    if (error < kernel.ClassificationCutoff)
                    {
                        stats.InHit++;
                    }
                }
                else
                {
                    stats.OutCount++;

                    if (error < kernel.ClassificationCutoff)
                    {
                        stats.OutHit++;
                    }
                }
            }

            Console.WriteLine($"  - in label {stats.InHit} / {stats.InCount}  - out label {stats.OutHit} / {stats.OutCount}  - {kernel.ShortDescription()}");

            return(stats);
        }