示例#1
0
        static Kernel FormHypothesis(int BlockSize, float[] w_block, float[] b_block)
        {
            var kernel = new AffineKernel(BlockSize);

            for (int j = 0; j < BlockSize; j++)
            {
                kernel.threshold = .01f + .5f * (float)rnd.NextDouble();
                //kernel.threshold = 1;

                //kernel.w[j] = (float)rnd.NextDouble();
                //kernel.b[j] = (float)rnd.NextDouble();

                kernel.w[j] = w_block[j] - b_block[j];
                kernel.b[j] = b_block[j];

                //kernel.w[j] = w_block[j];
                //kernel.b[j] = 0;

                //kernel.w[j] = 1;
                //kernel.b[j] = b_block[j];
            }

            Normalize(kernel.w);

            return(kernel);
        }
示例#2
0
        static void Test()
        {
            return;

            UtilTest.Run();

            Console.WriteLine(s(new float[, ] {
                { 1, 2 }, { 4, 5 }
            }));

            var k     = new AffineKernel(new float[] { 1, 2, 3 }, new float[] { 0, 0, 0 });
            var input = new float[] { 2.1f, 4.1f, 6.1f };

            Normalize(k.w);
            var d = k.Apply(input);

            Console.WriteLine(d);

            var m = k.ArgMinInverseError(input);

            Console.WriteLine(m);

            var result = Min(x => (float)Math.Cos(x), threshold: .000001f);

            Console.WriteLine(result);

            Console.ReadLine();
        }
示例#3
0
        static Kernel[] LearnLevel(int Rows, float[][,,] input, int Width, int Height, int Channels,
                                   int NumKernels = 3, int BlockDim = 3)
        {
            int BlockSize = BlockDim * BlockDim * Channels;

            // Unpack blocks.
            int num_blocks    = Rows * (Width - (BlockDim - 1)) * (Height - (BlockDim - 1));
            var blocks        = new float[num_blocks][];
            var block_labels  = new byte[num_blocks];
            var block_sources = new int[num_blocks];
            int block_index   = 0;

            for (int row = 0; row < Rows; row++)
            {
                var image = input[row];

                for (int x = 0; x < Width - (BlockDim - 1); x++)
                {
                    for (int y = 0; y < Height - (BlockDim - 1); y++)
                    {
                        var block = blocks[block_index] = new float[BlockSize];

                        block_sources[block_index] = row;
                        block_index++;

                        int i = 0;
                        for (int _x = x; _x < x + BlockDim; _x++)
                        {
                            for (int _y = y; _y < y + BlockDim; _y++)
                            {
                                for (int _c = 0; _c < Channels; _c++)
                                {
                                    block[i++] = (float)image[_x, _y, _c];
                                }
                            }
                        }
                    }
                }
            }

            Debug.Assert(block_index == blocks.Length);

            // Find kernels.
            var Kernels = new AffineKernel[NumKernels];

            for (int i = 0; i < NumKernels; i++)
            {
                Console.WriteLine($"Searching for kernel {i + 1} of {NumKernels}");

                var kernel = Kernels[i] = FindKernel(blocks, BlockSize);
                //kernel.Plot();

                var remainder = SetRemainder(blocks, kernel, GroupThreshold, labels: block_labels);
                blocks = remainder;
            }

            return(Kernels);
        }
示例#4
0
        static AffineKernel FindKernel(float[][] blocks, int BlockSize, int[] sources = null)
        {
            float        best_score  = 0;
            AffineKernel best_kernel = null;

            for (int i = 0; i < 500000; i++)
            {
                var kernel = new AffineKernel(BlockSize);

                int w_index, b_index;
                w_index = rnd.Next(0, blocks.Length - 1);
                b_index = rnd.Next(0, blocks.Length - 1);
                for (int j = 0; j < BlockSize; j++)
                {
                    //kernel.w[j] = (float)rnd.NextDouble();
                    //kernel.b[j] = (float)rnd.NextDouble();

                    kernel.w[j]      = blocks[w_index][j] - blocks[b_index][j];
                    kernel.b[j]      = blocks[b_index][j];
                    kernel.threshold = .01f + .5f * (float)rnd.NextDouble();

                    //kernel.w[j] = blocks[w_index][j];
                    //kernel.b[j] = 0;
                }

                Normalize(kernel.w);

                var score = TestKernel(blocks, kernel,
                                       GroupThreshold, step: 599);

                if (score > best_score)
                {
                    best_score  = score;
                    best_kernel = kernel;

                    Console.WriteLine($"(Iteration {i}) New best with a score of {best_score}.");
                }
            }

            return(best_kernel);
        }
示例#5
0
        static List <Tuple <AffineKernel, float> > Learn_SupervisedKernelHg(int Rows, LabeledImage[] images, int InLabel,
                                                                            float InLabelMinRatio = 0.05f, int SearchLength = 1000000, int preferred_source = -1)
        {
            const int BlockDim       = 5;
            const int Channels       = 3;
            const int BlockSize      = BlockDim * BlockDim * Channels;
            var       blocked_images = GetBlocks(images, Rows, Width, Height, Channels: 3, BlockDim: BlockDim, AddMirrors: true);

            var list = new List <Tuple <AffineKernel, float> >();

            Console.WriteLine("\n\nSearching for kernels via supervised hypothesis generation\n\n");

            float        best_score  = 0;
            AffineKernel best_kernel = null;

            for (int i = 0; i < SearchLength; i++)
            {
                int w_index, b_index;
                if (preferred_source >= 0)
                {
                    w_index = b_index = preferred_source;
                }
                else
                {
                    do
                    {
                        w_index = rnd.Next(0, blocked_images.Length - 1);
                    } while (images[w_index].Label != InLabel);
                    do
                    {
                        b_index = rnd.Next(0, blocked_images.Length - 1);
                    } while (images[b_index].Label != InLabel);
                }

                var w_blocks = blocked_images[w_index].Blocks;
                var b_blocks = blocked_images[b_index].Blocks;

                var w_block = w_blocks[rnd.Next(0, w_blocks.Length)];
                var b_block = b_blocks[rnd.Next(0, b_blocks.Length)];

                var kernel = (AffineKernel)FormHypothesis(BlockSize, w_block, b_block);

                var score = OptimizeKernelCutoff(Rows, blocked_images, kernel,
                                                 InLabel: InLabel, InLabelMinRatio: InLabelMinRatio);

                if (score.InHitRatio > InLabelMinRatio)
                {
                    list.Add(new Tuple <AffineKernel, float>(kernel, score.InOutHitRatio));

                    if (score.InOutHitRatio > best_score)
                    {
                        best_score  = score.InOutHitRatio;
                        best_kernel = kernel;

                        Console.WriteLine($"(Iteration {i}) New best with a score of {best_score}, in label ratio of {score.InHitRatio} with a threshold of {kernel.ClassificationCutoff}.");
                        best_kernel.Plot();
                    }
                }
            }

            return(list);
        }