public static ImageWithName InvertImage(ImageWithName img)
        {
            Gpu gpu = Gpu.Default;

            gpu.For(0, img.height, (y) =>
            {
                gpu.For(0, img.width, (x) =>
                {
                    img.image[x, y] = ImageProcessor.InvertPixel(img.image[x, y]);
                });
            });
            return(img);
        }
Пример #2
0
        public Tuple <int, int> CompareAbsoluteOpt(double[] source, double[] target, double tolerance, double ThreshholdTol)
        {
            System.Diagnostics.Debug.WriteLine("starting an absolute comparison on GPU");
            if (source.Length != target.Length)
            {
                throw new ArgumentException("The source and target lengths need to match");
            }

            double epsilon          = ThreshholdTol;
            double MaxSource        = source.Max();
            double MaxTarget        = target.Max();
            double MinDoseEvaluated = (MaxSource * epsilon);
            double zero             = 0.0;
            double lowMultiplier    = (1 - tolerance);
            double highMultiplier   = (1 + tolerance);
            int    failed           = 0;
            int    isCounted        = 0;
            Gpu    gpu = Gpu.Default;

            // filter doses below threshold
            // TODO: should failure be -1?

            int dimension = source.Length;

            double[] sourceOnGPU     = gpu.Allocate(source);
            double[] targetOnGPU     = gpu.Allocate(target);
            double[] isCountedArray  = gpu.Allocate <double>(dimension);
            double[] sourceOnGPULow  = gpu.Allocate <double>(dimension);
            double[] sourceOnGPUHigh = gpu.Allocate <double>(dimension);
            double[] isGTtol         = gpu.Allocate <double>(dimension);

            gpu.For(0, dimension, i => sourceOnGPU[i]    = (sourceOnGPU[i] > epsilon) ? sourceOnGPU[i] : zero);
            gpu.For(0, dimension, i => targetOnGPU[i]    = (targetOnGPU[i] > epsilon) ? targetOnGPU[i] : zero);
            gpu.For(0, dimension, i => sourceOnGPU[i]    = (targetOnGPU[i] > epsilon) ? sourceOnGPU[i] : zero);
            gpu.For(0, dimension, i => targetOnGPU[i]    = (sourceOnGPU[i] > epsilon) ? targetOnGPU[i] : zero);
            gpu.For(0, dimension, i => isCountedArray[i] = (sourceOnGPU[i] > zero) ? 1.0 : zero);

            gpu.For(0, dimension, i => sourceOnGPULow[i]  = lowMultiplier * sourceOnGPU[i]);
            gpu.For(0, dimension, i => sourceOnGPUHigh[i] = highMultiplier * sourceOnGPU[i]);

            //determine if relative difference is greater than minDoseEvaluated
            // stores 1 as GT minDoseEvaluated is true
            gpu.For(0, isGTtol.Length,
                    i => isGTtol[i] = (targetOnGPU[i] < sourceOnGPULow[i] || targetOnGPU[i] > sourceOnGPUHigh[i]) ? 1 : 0);
            isCounted = (int)gpu.Sum(isCountedArray);
            failed    = (int)gpu.Sum(isGTtol);

            Gpu.Free(sourceOnGPU);
            Gpu.Free(targetOnGPU);
            Gpu.Free(sourceOnGPULow);
            Gpu.Free(sourceOnGPUHigh);
            Gpu.Free(isCountedArray);
            Gpu.Free(isGTtol);
            System.Diagnostics.Debug.WriteLine("finished an absolute comparison on GPU");
            //gpu.Dispose();

            return(new Tuple <int, int>(failed, isCounted));
        }
Пример #3
0
        public override int[][] CalcFrame2d(int[][] frame)
        {
            int s = strob;
            int c = channelsCount;

            int[][] array = new int[detectors.Length][];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = new int[channelsCount];
            }

            //for (int i = 0; i < frame.Length; i++)
            gpu.For(0, frame.Length, i =>
            {
                //gpu.For(0, frame[i].Length, index =>
                for (int index = 0; index < frame[i].Length; index++)
                {
                    int ch = frame[i][index];
                    int k1 = ch - s; if (k1 < 0)
                    {
                        k1 = 0;
                    }
                    int k2 = ch + s; if (k2 > c - 1)
                    {
                        k2 = c - 1;
                    }
                    for (int k = k1; k < k2; k++)
                    {
                        array[i][k] += 1;
                    }
                }//);
            });
            return(array);
        }
Пример #4
0
        public static Rgba32[] Apply(Rgba32[] pixelArray, Func <Rgba32, Rgba32> filter)
        {
            Gpu gpu = Gpu.Default;

            gpu.For(0, pixelArray.Length, i => pixelArray[i] = filter(pixelArray[i]));

            return(pixelArray);
        }
Пример #5
0
        private void Min(Gpu gpu, BufferField input, int reduction, int expansion, ref BufferField map, ref BufferField output)
        {
            var iw   = input.Width;
            var ih   = input.Height;
            var ic   = input.Channels;
            var ibuf = input.Buffer;

            var ow   = output.Width / expansion;
            var oh   = output.Height / expansion;
            var oc   = output.Channels;
            var obuf = output.Buffer;

            var mbuf = map.Buffer;

            gpu.For(0, output.Length / (expansion * expansion), n =>
            {
                int c = (int)(n / (ow * oh));
                int l = n - c * (ow * oh);
                int y = (int)(l / ow);
                int x = l - y * ow;

                double pool = double.MaxValue;
                for (int i = 0; i < reduction; i++)
                {
                    for (int j = 0; j < reduction; j++)
                    {
                        int _x       = x * reduction + i;
                        int _y       = y * reduction + j;
                        double _ibuf = ibuf[c][_x, _y];
                        if (_ibuf < pool)
                        {
                            pool = _ibuf;
                        }
                    }
                }
                for (int i = 0; i < reduction; i++)
                {
                    for (int j = 0; j < reduction; j++)
                    {
                        int _x       = x * reduction + i;
                        int _y       = y * reduction + j;
                        double _ibuf = ibuf[c][_x, _y];
                        if (_ibuf == pool)
                        {
                            mbuf[c][_x, _y] = 1;
                        }
                    }
                }

                for (int i = 0; i < expansion; i++)
                {
                    for (int j = 0; j < expansion; j++)
                    {
                        obuf[c][x * expansion + i, y * expansion + j] = pool;
                    }
                }
            });
        }
Пример #6
0
        public void Average(Gpu gpu, BufferField sigma, BufferField map, int reduction, int expansion, ref BufferField propagater)
        {
            var sw   = sigma.Width;
            var sh   = sigma.Height;
            var sc   = sigma.Channels;
            var sbuf = sigma.Buffer;

            var mbuf = map.Buffer;

            var pw   = propagater.Width / reduction;
            var ph   = propagater.Height / reduction;
            var pc   = propagater.Channels;
            var pbuf = propagater.Buffer;

            gpu.For(0, propagater.Length / (reduction * reduction), n =>
            {
                int c = (int)(n / (pw * ph));
                int l = n - c * (pw * ph);
                int y = (int)(l / pw);
                int x = l - y * pw;


                double pool = 0, count = 0;
                //bool check = false;
                for (int i = 0; i < expansion; i++)
                {
                    for (int j = 0; j < expansion; j++)
                    {
                        int _x       = x * expansion + i;
                        int _y       = y * expansion + j;
                        double _sbuf = sbuf[c][_x, _y];
                        count++;
                        pool += _sbuf;
                    }
                }
                if (count == 0)
                {
                    pool = 0;
                }
                else
                {
                    pool /= count;
                }

                for (int i = 0; i < reduction; i++)
                {
                    for (int j = 0; j < reduction; j++)
                    {
                        int _x = x * reduction + i;
                        int _y = y * reduction + j;
                        if (mbuf[c][_x, _y] > 0)
                        {
                            pbuf[c][_x, _y] = pool;
                        }
                    }
                }
            });
        }
Пример #7
0
        public double[,] Gradient2D(double[] input, double[,] points)
        {
            // inputのgradientを取る。
            // points[i, 0], points[i, 1] はそれぞれinput[i, j] に対応するx座標、y座標
            // output[i, 0], output[i, 1] はそれぞれinputのgradientのx方向成分、y方向成分
            int pointnum      = points.GetLength(0);
            var output        = new double[pointnum, 2];
            var weights       = new double[pointnum, pointnum];
            var radius        = this.radius; // Aleaではクラスの変数を直接扱えない
            var nzero         = this.nzero;
            var gridtoindexes = this.gridtoindexes;
            var gridsupporter = this.gridsupporter;

            gpu.For(0, input.Length, i =>
            {
                var x         = points[i, 0];
                var y         = points[i, 1];
                var gridx     = (int)Math.Max(0, Math.Floor(2 + (x / radius)));
                var gridy     = (int)Math.Max(0, Math.Floor(2 + (y / radius)));
                double tempx  = 0;
                double tempy  = 0;
                int fromindex = gridsupporter[gridx, gridy, 0];
                int pnum      = gridsupporter[gridx, gridy, 1];
                for (int j0 = 0; j0 < pnum; j0++)
                {
                    int j = gridtoindexes[fromindex + j0];
                    if (i != j)
                    {
                        var xsa    = x - points[j, 0];
                        var ysa    = y - points[j, 1];
                        var dis    = Math.Sqrt(xsa * xsa + ysa * ysa);
                        var weight = Math.Max(0, radius / dis - 1);
                        var xvec   = points[j, 0] - points[i, 0];
                        var yvec   = points[j, 1] - points[i, 1];
                        var temp   = weight * (input[j] - input[i]) / (xvec * xvec + yvec * yvec);
                        tempx     += temp * xvec;
                        tempy     += temp * yvec;
                    }
                }
                output[i, 0] = tempx * 2 / nzero;
                output[i, 1] = tempy * 2 / nzero;
            });
            return(output);
        }
Пример #8
0
        private static int TestSummGPU(Gpu gpu, int length, int[] arg1, int[] arg2, int[] result)
        {
            //gpu.For(0, length, i => arg1[i] = 1);
            //gpu.For(0, length, i => arg2[i] = 1);
            gpu.For(0, length, i => result[i] = arg1[i] + arg2[i]);
            var s = gpu.Sum(result);

            //gpu.Synchronize();
            return(s);
        }
Пример #9
0
        public Tuple <int, int> CompareAbsoluteOld(double[] source, double[] target, double tolerance, double epsilon)
        {
            System.Diagnostics.Debug.WriteLine("starting an absolute comparison on GPU");
            if (source.Length != target.Length)
            {
                throw new ArgumentException("The source and target lengths need to match");
            }

            double MaxSource        = source.Max();
            double MaxTarget        = target.Max();
            double MinDoseEvaluated = MaxSource * epsilon;

            double[] isCountedArray       = new double[source.Length];
            double[] differenceDoubles    = new double[source.Length];
            double[] absDifferenceDoubles = new double[source.Length];
            double[] isGTtol   = new double[source.Length];
            int      failed    = 0;
            int      isCounted = 0;


            // filter doses below threshold
            // TODO: should failure be -1?
            gpu.For(0, source.Length, i => source[i]         = (source[i] > epsilon) ? source[i] : 0);
            gpu.For(0, target.Length, i => target[i]         = (target[i] > epsilon) ? target[i] : 0);
            gpu.For(0, source.Length, i => source[i]         = (target[i] > epsilon) ? source[i] : 0);
            gpu.For(0, target.Length, i => target[i]         = (source[i] > epsilon) ? target[i] : 0);
            gpu.For(0, source.Length, i => isCountedArray[i] = (source[i] > 0) ? 1 : 0);

            // find relative difference
            gpu.For(0, differenceDoubles.Length, i => differenceDoubles[i] = ((source[i] - target[i]) / source[i]));

            // absolute value of previous table
            gpu.For(0, absDifferenceDoubles.Length,
                    i => absDifferenceDoubles[i] = (differenceDoubles[i] < 0) ? -1 * differenceDoubles[i] : differenceDoubles[i]);

            //determine if relative difference is greater than minDoseEvaluated
            // stores 1 as GT minDoseEvaluated is true
            gpu.For(0, isGTtol.Length,
                    i => isGTtol[i] = (absDifferenceDoubles[i] > tolerance) ? 1 : 0);

            isCounted = (int)gpu.Sum(isCountedArray);


            failed = (int)gpu.Sum(isGTtol);

            System.Diagnostics.Debug.WriteLine("finished an absolute comparison on GPU");

            return(new Tuple <int, int>(failed, isCounted));
        }
Пример #10
0
        public void Process(Gpu gpu, BufferField input, KernelField kernel, int dilation, int expand, ref BufferField output)
        {
            var iw   = input.Width;
            var ih   = input.Height;
            var ic   = input.Channels;
            var ibuf = input.Buffer;

            var ow   = output.Width;
            var oh   = output.Height;
            var oc   = output.Channels;
            var obuf = output.Buffer;

            var ks    = kernel.Size;
            var kbias = kernel.Bias;
            var kbuf  = kernel.Buffer;

            gpu.For(0, output.Length, n =>
            {
                int c = (int)(n / (ow * oh));
                int l = n - c * (ow * oh);
                int y = (int)(l / ow);
                int x = l - y * ow;

                double v = kbias[c];
                for (int _c = 0; _c < ic; _c++)
                {
                    for (int s = -ks; s <= ks; s++)
                    {
                        for (int t = -ks; t <= ks; t++)
                        {
                            int i = x + s * dilation;
                            int j = y + t * dilation;
                            if (i % expand == 0 && j % expand == 0)
                            {
                                int _i = i / expand;
                                int _j = j / expand;
                                if (_i >= 0 && _i < iw && _j >= 0 && _j < ih)
                                {
                                    v += ibuf[_c][_i, _j] * kbuf[_c][c][s + ks, t + ks];
                                }
                            }
                        }
                    }
                }
                obuf[c][x, y] = v;
            });
        }
Пример #11
0
        public override void Forward(Gpu gpu, BufferField temporaryOutput, ref BufferField output)
        {
            var ow    = output.Width;
            var oh    = output.Height;
            var oc    = output.Channels;
            var obuf  = output.Buffer;
            var tobuf = temporaryOutput.Buffer;

            gpu.For(0, output.Length, n =>
            {
                int c = (int)(n / (ow * oh));
                int l = n - c * (ow * oh);
                int y = (int)(l / ow);
                int x = l - y * ow;

                obuf[c][x, y] = tobuf[c][x, y];
            });
        }
Пример #12
0
        public override void Back(Gpu gpu, BufferField sigma, BufferField temporaryOutput, ref BufferField temporarySigma)
        {
            var tsw   = temporarySigma.Width;
            var tsh   = temporarySigma.Height;
            var tsc   = temporarySigma.Channels;
            var tsbuf = temporarySigma.Buffer;

            var sbuf = sigma.Buffer;

            gpu.For(0, temporarySigma.Length, n =>
            {
                int c = (int)(n / (tsw * tsh));
                int l = n - c * (tsw * tsh);
                int y = (int)(l / tsw);
                int x = l - y * tsw;

                tsbuf[c][x, y] = sbuf[c][x, y];
            });
        }
        private static double[,] CosineSimilarityGpu(Gpu gpu, double[][] dataset)
        {
            int size       = dataset.Length * dataset.Length;
            var gpuDataset = gpu.Allocate(dataset);

            // Allocate directly on gpu.
            var gpuDistances = gpu.Allocate <double>(dataset.Length, dataset.Length);

            gpu.For(0, size, index =>
            {
                int i               = index / dataset.Length;
                int j               = index % dataset.Length;
                double dotProduct   = 0;
                double magnitudeOne = 0;
                double magnitudeTwo = 0;
                for (int k = 0; k < dataset[i].Length; k++)
                {
                    dotProduct   += (dataset[i][k] * dataset[j][k]);
                    magnitudeOne += (dataset[i][k] * dataset[i][k]);
                    magnitudeTwo += (dataset[j][k] * dataset[j][k]);
                }
                double distance    = Math.Max(0, 1 - (dotProduct / Math.Sqrt(magnitudeOne * magnitudeTwo)));
                gpuDistances[i, j] = distance;
            });

            // Gpu -> Cpu.
            var result = new double[dataset.Length, dataset.Length];

            Gpu.Copy(gpuDistances, result);

            // Release gpu memory.
            Gpu.Free(gpuDataset);
            Gpu.Free(gpuDistances);

            return(result);
        }
Пример #14
0
        public void Process(Gpu gpu, BufferField input, BufferField sigma, int dilation, int expand, ref BufferField propagater, ref KernelField kernel)
        {
            var iw   = input.Width;
            var ih   = input.Height;
            var ic   = input.Channels;
            var ibuf = input.Buffer;

            var sw   = sigma.Width;
            var sh   = sigma.Height;
            var sc   = sigma.Channels;
            var sbuf = sigma.Buffer;

            var ks    = kernel.Size;
            var kbias = kernel.Bias;
            var kbuf  = kernel.Buffer;

            var dkbias = kernel.dBias;
            var dkbuf  = kernel.dBuffer;

            var pw   = propagater.Width;
            var ph   = propagater.Height;
            var pc   = propagater.Channels;
            var pbuf = propagater.Buffer;

            #region Update Kernel
            gpu.For(0, sc, c =>
            {
                double db  = dkbias[c];
                double ddb = 0;
                for (int x = 0; x < sw; x++)
                {
                    for (int y = 0; y < sh; y++)
                    {
                        ddb += sbuf[c][x, y];
                    }
                }
                dkbias[c] = db + (ddb / (sw * sh));
            });

            gpu.For(0, (2 * ks + 1) * (2 * ks + 1), n =>
            {
                int s  = (int)(n / (2 * ks + 1));
                int t  = n - s * (2 * ks + 1);
                int _s = s - ks;
                int _t = t - ks;

                for (int c = 0; c < ic; c++)
                {
                    for (int d = 0; d < sc; d++)
                    {
                        double dk  = dkbuf[c][d][s, t];
                        double ddk = 0;
                        int cnk    = 0;
                        for (int x = 0; x < sw; x++)
                        {
                            for (int y = 0; y < sh; y++)
                            {
                                int _x = x + _s * dilation;
                                int _y = y + _t * dilation;
                                if (_x % expand == 0 && _y % expand == 0)
                                {
                                    int __x = _x / expand;
                                    int __y = _y / expand;
                                    if (__x >= 0 && __x < iw && __y >= 0 && __y < ih)
                                    {
                                        cnk++;
                                        ddk += ibuf[c][__x, __y] * sbuf[d][x, y];
                                    }
                                }
                            }
                        }
                        dkbuf[c][d][s, t] = dk + ((ddk / (sw * sh * (2 * (ks - 1) + 1))));
                    }
                }
            });
            #endregion

            #region Calculate Propagater
            gpu.For(0, propagater.Length, n =>
            {
                int c = (int)(n / (pw * ph));
                int l = n - c * (pw * ph);
                int y = (int)(l / pw);
                int x = l - y * pw;

                double v = 0;
                for (int _c = 0; _c < sc; _c++)
                {
                    for (int s = ks; s >= -ks; s--)
                    {
                        for (int t = ks; t >= -ks; t--)
                        {
                            int i = x + s * dilation;
                            int j = y + t * dilation;
                            if (i % expand == 0 && j % expand == 0)
                            {
                                int _i = i / expand;
                                int _j = j / expand;
                                if (_i >= 0 && _i < sw && _j >= 0 && _j < sh)
                                {
                                    v += sbuf[_c][_i, _j] * kbuf[c][_c][s + ks, t + ks];
                                }
                            }
                        }
                    }
                }
                pbuf[c][x, y] = v;
            });
            #endregion
        }
Пример #15
0
        static int TrainNetwork(int hiddenSize)
        {
            int maxGen           = 1000;
            int playCount        = 16;
            int populationSize   = 100;
            int currBestAvg      = 0;
            int highAverageScore = 0;

            Gamer[] population = new Gamer[populationSize];

            int inputSize = 20;

            int[]            netShape = { hiddenSize, 4 };
            ActivationType[] acts     = Enumerable.Repeat(ActivationType.LRELU, netShape.Length).ToArray();
            acts[acts.Length - 1] = ActivationType.Sigmoid;

            Random trainRand = new Random();

            //Initialize Gamers
            for (int i = 0; i < population.Length; i++)
            {
                population[i] = new Gamer(trainRand, acts, inputSize, netShape);
            }

            for (int gen = 0; gen < maxGen; gen++)
            {
                if (gen != 0)
                {
                    //Mutate Generation
                    int start = (int)(population.Length * 0.10);
                    int end   = (int)(population.Length * 0.90);
                    for (int i = start; i < end; i++)
                    {
                        population[i].Net.Crossover(trainRand, population[trainRand.Next(start)].Net);
                        population[i].Net.Mutate(trainRand, 0.15);
                    }
                    for (int i = end; i < population.Length; i++)
                    {
                        population[i].Net.Randomize(trainRand);
                    }
                }

                currBestAvg = 0;
                for (int i = 0; i < population.Length; i++)
                {
                    population[i].AverageScore = 0;
                }

                //thread local data
                int seed = Guid.NewGuid().GetHashCode();
                Gpu gpu  = Gpu.Default;

                //System.Exception: Constant variable is immutable.
                gpu.For(0, population.Length, (i) =>
                {
                    Random gameRand = new Random(seed);

                    //each net should play a # of games and get an average score
                    for (int gameNum = 0; gameNum < playCount; gameNum++)
                    {
                        //Reset Game
                        population[i].Restart(gameRand);

                        //Each Net Plays game until completed
                        while (!population[i].GameOver)
                        {
                            population[i].Play(gameRand);
                        }

                        //calculate avg score after each game
                        population[i].AverageScore += population[i].Score;
                    }

                    //average score of all games
                    population[i].AverageScore /= playCount;
                });

                //Sort Fitness
                Array.Sort(population, (a, b) => b.AverageScore.CompareTo(a.AverageScore));

                currBestAvg = population[0].AverageScore;
                if (population[0].AverageScore > highAverageScore)
                {
                    highAverageScore = population[0].AverageScore;
                }

                //Display Progress
                //Console.SetCursorPosition(0, 0);
                //Console.WriteLine($"Gen: {gen}");
                //Console.WriteLine($"Gen Top Avg Score: {currBestAvg}");
                //Console.WriteLine($"All Top Avg Score: {highAverageScore}");
                //Console.WriteLine($"% {(int)(gen / (double)maxGen * 100)}");
            }

            //save best net to json
            string save = JsonConvert.SerializeObject(population[0].Net);

            File.WriteAllText($"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}/network.json", save);
            return(highAverageScore);
        }