Пример #1
0
        static void Main(string[] argsarray)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            List <string> args = new List <string>(argsarray);

            Excel.Init();

            // if (Args.Count > 0 && Args[0] == "/t") { Test.Run(); return; }
            if (args.Count > 0 && args[0] == "/u")
            {
                Updater.DelayDays = 0;
                args.RemoveAt(0);
            }

            MainForm form = new MainForm();

            form.Show();

            Updater.Start();

            Gradients.Reset();

            if (args.Count > 0)               // Load file if passed as argument
            {
                try {
                    form.Model.Load(args[0]);
                } catch { MessageBox.Show(Resources.FileOpenFail.Replace("#file", args[0])); }
            }
            Application.Run(form);             // FPlot runs normally
        }
Пример #2
0
        public override void CalcGradients(List <double[]> inputs, Layer outputlayer)
        {
            for (int b = 0; b < NN.BatchSize; b++)
            {
                //var input = inputs[i];
                //if (UsesTanh) { input = Maths.TanhDerriv(inputs[i]); }

                double[,] Input = Pad(Maths.Convert(inputs[b]));
                double[,] stochgradients;
                if (DownOrUp)
                {
                    stochgradients = Convolve(Maths.Convert(Errors[b]), Input);
                }
                else
                {
                    stochgradients = Convolve(Input, Maths.Convert(Errors[b]));
                }
                //Gradients = stochgradients;

                //Add the stochastic gradients to the batch gradients
                for (int j = 0; j < Gradients.GetLength(0); j++)
                {
                    for (int k = 0; k < Gradients.GetLength(1); k++)
                    {
                        Gradients[j, k] += stochgradients[j, k];
                    }
                }
            }
        }
Пример #3
0
        private void SelectPreset_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var presetBrush = (LinearGradientBrush)e.Parameter;

            if (presetBrush == null)
            {
                return;
            }

            UpdateBrush        = false;
            BrushSetInternally = true;

            Gradients.Clear();
            foreach (var presetBrushGradientStop in presetBrush.GradientStops)
            {
                Gradients.Add(presetBrushGradientStop.CloneCurrentValue());
            }
            StartX = presetBrush.StartPoint.X;
            StartY = presetBrush.StartPoint.Y;
            EndX   = presetBrush.EndPoint.X;
            EndY   = presetBrush.EndPoint.Y;

            UpdateBrush        = true;
            BrushSetInternally = false;
            SetBrush();
        }
Пример #4
0
        public void ExtractAnchors(string sourceFileName,
                                   GradientOperator gradientOperator, int gradientThreshold,
                                   int anchorScanInterval, int anchorThreshold)
        {
            // Arrange
            var source    = LoadImage(sourceFileName);
            var rows      = source.Size.Height;
            var columns   = source.Size.Width;
            var gradients = new Gradients(rows, columns);

            gradients.ComputeGradient(source, gradientOperator, gradientThreshold);

            // Act
            var anchors = gradients.ExtractAnchors(anchorScanInterval, anchorThreshold);

            // Assert
            var anchorMap = new Mat(rows, columns, Depth.U8, 1);

            anchorMap.Set(Scalar.All(0));
            foreach (var anchor in anchors)
            {
                anchorMap.SetReal(anchor.Y, anchor.X, 255);
            }
            var resultFileName = Path.Combine("Output", "ExtractAnchors", $"{Path.GetFileNameWithoutExtension(sourceFileName)}_{gradientOperator}_{gradientThreshold}_{anchorScanInterval}_{anchorThreshold}.png");

            SaveImage(anchorMap, resultFileName);
        }
Пример #5
0
        private void OnCanvasViewOnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            var info    = e.Info;
            var surface = e.Surface;
            var canvas  = surface.Canvas;

            canvas.Clear();

            if (Gradients != null && Gradients.Any())
            {
                _fillPaint.Shader = GetGradient(info, Gradients, GradientOrientation);
            }
            else
            {
                _fillPaint.Color = Color.ToSKColor();
            }

            var radius = (float)(info.Width >= info.Height ? info.Height : info.Width) / 2;

            if (IsOnlyBorder)
            {
                radius                 = radius - ConvertToDeviceScaleFactor(BorderWidth);
                _fillPaint.Style       = SKPaintStyle.Stroke;
                _fillPaint.StrokeWidth = CalculateScaled.Size(BorderWidth);
            }

            canvas.DrawCircle((float)info.Width / 2, (float)info.Height / 2, radius, _fillPaint);
        }
Пример #6
0
 public void AddGradient(string key, APLValue <APLGradient> gradient)
 {
     if (Gradients == null)
     {
         Gradients = new Dictionary <string, APLValue <APLGradient> >();
     }
     Gradients.Add(key, gradient);
 }
Пример #7
0
 private void RemoveGradientStop_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     if (Gradients != null && Gradients.Count > 2)
     {
         Gradients.Remove(SelectedGradient);
         SetBrush();
     }
 }
Пример #8
0
        private void ComputeGradient(Gradients gradients, List <double> inputs, List <double> requiredOutputs)
        {
            Activities(inputs);
            for (int i = NumberOfLayers() - 1; i >= 1; i--)
            {
                NeuralLayer currentLayer = GetLayer(i);

                if (currentLayer.IsLayerTop())
                {
                    for (int j = 0; j < currentLayer.NumberOfNeurons(); j++)
                    {
                        Neuron currentNeuron = currentLayer.GetNeuron(j);
                        gradients.SetThreshold(i, j,
                                               currentNeuron.Output * (1 - currentNeuron.Output) *
                                               (currentNeuron.Output - requiredOutputs[j]));
                    }

                    for (int j = 0; j < currentLayer.NumberOfNeurons(); j++)
                    {
                        Neuron currentNeuron = currentLayer.GetNeuron(j);
                        for (int k = 0; k < currentNeuron.NumberOfInputs(); k++)
                        {
                            NeuralInput currentInput = currentNeuron.GetInput(k);
                            gradients.SetWeight(i, j, k,
                                                gradients.GetThreshold(i, j) * currentLayer.LowerLayer().GetNeuron(k).Output);
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < currentLayer.NumberOfNeurons(); j++)
                    {
                        double aux = 0;
                        for (int ia = 0; ia < currentLayer.UpperLayer().NumberOfNeurons(); ia++)
                        {
                            aux += gradients.GetThreshold(i + 1, ia) *
                                   currentLayer.UpperLayer().GetNeuron(ia).GetInput(j).Weight;
                        }
                        gradients.SetThreshold(i, j,
                                               currentLayer.GetNeuron(j).Output *(1 - currentLayer.GetNeuron(j).Output) * aux);
                    }

                    for (int j = 0; j < currentLayer.NumberOfNeurons(); j++)
                    {
                        Neuron currentNeuron = currentLayer.GetNeuron(j)
                        ;
                        for (int k = 0; k < currentNeuron.NumberOfInputs(); k++)
                        {
                            NeuralInput currentInput = currentNeuron.GetInput(k);
                            gradients.SetWeight(i, j, k,
                                                gradients.GetThreshold(i, j) * currentLayer.LowerLayer().GetNeuron(k).Output);
                        }
                    }
                }
            }
        }
        private float[] ApplyRadialMask(float[] map)
        {
            var output = new float[map.Length];

            for (int i = 0; i < map.Length; i++)
            {
                int x = i % HeightmapResolution;
                int y = i / HeightmapResolution;
                output[i] = map[i] * Gradients.RadialGradient(x, y, HeightmapResolution, HeightmapResolution);
            }
            return(output);
        }
Пример #10
0
        private static LinearGradientBrush GradientColor(Gradients color, Rectangle rect)
        {
            Color c1;
            Color c2;
            Color c3;

            switch (color)
            {
            case Gradients.Green:
                c1 = Color.FromArgb(255, 0, 100, 0);
                c2 = Color.FromArgb(255, 50, 205, 50);
                c3 = Color.FromArgb(255, 0, 255, 0);
                break;

            case Gradients.Blue:
                c1 = Color.FromArgb(255, 0, 255, 255);
                c2 = Color.FromArgb(255, 0, 0, 205);
                c3 = Color.FromArgb(255, 0, 0, 139);
                break;

            case Gradients.Red:
                c1 = Color.FromArgb(255, 128, 0, 0);
                c2 = Color.FromArgb(255, 139, 0, 0);
                c3 = Color.FromArgb(255, 255, 0, 0);
                break;

            case Gradients.SlateGray:
                c1 = Color.FromArgb(255, 112, 128, 144);
                c2 = Color.FromArgb(255, 32, 178, 178);
                c3 = Color.FromArgb(255, 112, 128, 144);
                break;

            case Gradients.Orange:
                c1 = Color.FromArgb(255, 210, 105, 30);
                c2 = Color.FromArgb(255, 139, 69, 19);
                c3 = Color.FromArgb(255, 205, 133, 63);
                break;

            default:
                c1 = Color.FromArgb(255, 169, 169, 169);
                c2 = Color.FromArgb(255, 105, 105, 105);
                c3 = Color.FromArgb(255, 0, 0, 0);
                break;
            }

            LinearGradientBrush br = new LinearGradientBrush(rect, c1, c3, 90, true);
            ColorBlend          cb = new ColorBlend();

            cb.Positions           = new[] { 0, (float)0.5, 1 };
            cb.Colors              = new[] { c1, c2, c3 };
            br.InterpolationColors = cb;
            return(br);
        }
Пример #11
0
    void ScanTriangle(Vertex minYVert, Vertex midYVert, Vertex maxYVert,
                      bool handedness, Bitmap texture)
    {
        var gradients = new Gradients(minYVert, midYVert, maxYVert);

        Edge topBot = new Edge(gradients, minYVert, maxYVert, 0);
        Edge topMid = new Edge(gradients, minYVert, midYVert, 0);
        Edge midBot = new Edge(gradients, midYVert, maxYVert, 1);

        ScanEdges(gradients, topBot, topMid, handedness, texture);
        ScanEdges(gradients, topBot, midBot, handedness, texture);
    }
Пример #12
0
 private void MiniBatch(List <Matrix> inputs, List <Matrix> outputs)
 {
     Backpropagate(inputs[0], outputs[0]);
     MiniBatchGradients     = Gradients.ToList();
     MiniBatchDeltaMatrices = DeltaMatrices.ToList();
     for (int i = 1; i < inputs.Count; ++i)
     {
         Backpropagate(inputs[i], outputs[i]);
         for (int j = 0; j < MiniBatchGradients.Count; ++j)
         {
             MiniBatchGradients[j]     += Gradients[j];
             MiniBatchDeltaMatrices[j] += DeltaMatrices[j];
         }
     }
 }
Пример #13
0
    Edge(Gradients gradients, Vertex minYVert, Vertex maxYVert, int minYVertIdx)
    {
        YMin = (int)Math.Ceiling(minYVert.Y);
        YMax = (int)Math.Ceiling(maxYVert.Y);

        float yDist = maxYVert.Y - minYVert.Y;
        float xDist = maxYVert.X - minYVert.X;

        float yPrestep = YMin - minYVert.Y;

        xStep = xDist / yDist;
        X     = minYVert.X + yPrestep * xStep;
        float xPrestep = X - minYVert.X;

        Gradients g = gradients;

        texUStep = g.TexUXStep * xStep + g.TexUYStep;
        TexU     =
            g.TexU[minYVertIdx] +
            g.TexUXStep * xPrestep +
            g.TexUYStep * yPrestep;

        texVStep = g.TexVXStep * xStep + g.TexVYStep;
        TexV     =
            g.TexV[minYVertIdx] +
            g.TexVXStep * xPrestep +
            g.TexVYStep * yPrestep;

        oneOvrZStep = g.OneOvrZXStep * xStep + g.OneOvrZYStep;
        OneOvrZ     =
            g.OneOvrZ[minYVertIdx] +
            g.OneOvrZXStep * xPrestep +
            g.OneOvrZYStep * yPrestep;

        depthStep = g.DepthXStep * xStep + g.DepthYStep;
        Depth     =
            g.Depth[minYVertIdx] +
            g.DepthXStep * xPrestep +
            g.DepthYStep * yPrestep;

        lightStep = g.LightXStep * xStep + g.LightYStep;
        Light     =
            g.Light[minYVertIdx] +
            g.LightXStep * xPrestep +
            g.LightYStep * yPrestep;
    }
Пример #14
0
        public void ComputeGradient(string sourceFileName, GradientOperator gradientOperator, int gradientThreshold)
        {
            // Arrange
            var source    = LoadImage(sourceFileName);
            var rows      = source.Size.Height;
            var columns   = source.Size.Width;
            var gradients = new Gradients(rows, columns);

            // Act
            gradients.ComputeGradient(source, gradientOperator, gradientThreshold);

            // Assert
            var gradientFileName   = Path.Combine("Output", "ComputeGradient", $"{Path.GetFileNameWithoutExtension(sourceFileName)}_{gradientOperator}_{gradientThreshold}_GradientMap.png");
            var directionsFileName = Path.Combine("Output", "ComputeGradient", $"{Path.GetFileNameWithoutExtension(sourceFileName)}_{gradientOperator}_{gradientThreshold}_DirectionsMap.png");

            SaveImage(gradients.GradientMap, gradientFileName);
            SaveImage(gradients.DirectionMap, directionsFileName);
        }
Пример #15
0
    void DrawScanLine(Gradients gradients, Edge left, Edge right, int j,
                      Bitmap texture)
    {
        int   xMin     = (int)Math.Ceiling(left.X);
        int   xMax     = (int)Math.Ceiling(right.X);
        float xPrestep = xMin - left.X;
        float xDist    = right.X - left.X;

        float texUXStep    = gradients.TexUXStep;
        float texVXStep    = gradients.TexVXStep;
        float oneOvrZXStep = gradients.OneOvrZXStep;
        float depthXStep   = gradients.DepthXStep;
        float lightXStep   = gradients.LightXStep;

        float texU     = left.TexU + texUXStep * xPrestep;
        float texV     = left.TexV + texVXStep * xPrestep;
        float oneOverZ = left.OneOvrZ + oneOvrZXStep * xPrestep;
        float depth    = left.Depth + depthXStep * xPrestep;
        float light    = left.Light + lightXStep * xPrestep;

        for (int i = xMin; i < xMax; ++i)
        {
            int index = i + j * Width;

            if (depth < zBuffer[index])
            {
                zBuffer[index] = depth;

                float z    = 1.0f / oneOverZ;
                int   srcX = (int)(texU * z * (texture.Width - 1) + 0.5f);
                int   srcY = (int)(texV * z * (texture.Height - 1) + 0.5f);

                CopyPixel(i, j, srcX, srcY, texture, light);
            }

            oneOverZ += oneOvrZXStep;
            texU     += texUXStep;
            texV     += texVXStep;
            depth    += depthXStep;
            light    += lightXStep;
        }
    }
Пример #16
0
 private void ImportClick(object sender, EventArgs e)
 {
     if (openFileDialog.ShowDialog() == DialogResult.OK)
     {
         string src  = openFileDialog.FileName;
         string dest = Path.Combine(Resources.GradientPath, Path.GetFileName(src));
         System.IO.File.Copy(src, dest);
         try {
             Gradients.Reset();
         } catch (Exception ex) {
             string file = "";
             if (ex.Data.Contains("filename"))
             {
                 file = ex.Data["filename"] as string;
             }
             MessageBox.Show(Properties.Resources.GIMPError.Replace("$f", file));
         }
         gradientChooser.Reset();
         gradientChooser.Refresh();
     }
 }
Пример #17
0
        private void ComputeTotalGradient(Gradients totalGradients, Gradients partialGradients,
                                          SetOfIOPairs trainingSet)
        {
            totalGradients.ResetGradients();

            foreach (SetOfIOPairs.IOPair pair in trainingSet.Pairs)
            {
                ComputeGradient(partialGradients, pair.Inputs, pair.Outputs);
                for (int i = NumberOfLayers() - 1; i >= 1; i--)
                {
                    NeuralLayer currentLayer = GetLayer(i);
                    for (int j = 0; j < currentLayer.NumberOfNeurons(); j++)
                    {
                        totalGradients.IncrementThreshold(i, j, partialGradients.GetThreshold(i, j));
                        for (int k = 0; k < currentLayer.LowerLayer().NumberOfNeurons(); k++)
                        {
                            totalGradients.IncrementWeight(i, j, k, partialGradients.GetWeight(i, j, k));
                        }
                    }
                }
            }
        }
Пример #18
0
    void ScanEdges(Gradients gradients, Edge a, Edge b, bool handedness,
                   Bitmap texture)
    {
        Edge left  = a;
        Edge right = b;

        if (handedness)
        {
            Edge temp = left;
            left  = right;
            right = temp;
        }

        int yStart = b.YMin;
        int yEnd   = b.YMax;

        for (int j = yStart; j < yEnd; ++j)
        {
            DrawScanLine(gradients, left, right, j, texture);
            left.Step();
            right.Step();
        }
    }
Пример #19
0
        private void OnCanvasViewOnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            var info    = e.Info;
            var surface = e.Surface;
            var canvas  = surface.Canvas;

            canvas.Clear();

            if (Gradients != null && Gradients.Any())
            {
                _fillPaint.Shader = GetGradient(info, Gradients, GradientOrientation);
            }
            else
            {
                _fillPaint.Color = Color.ToSKColor();
            }

            var topRadius         = ConvertToDeviceScaleFactor(CornerRadius.Left);
            var bottomRightRadius = ConvertToDeviceScaleFactor(CornerRadius.Top);
            var bottomLeftRadius  = ConvertToDeviceScaleFactor(CornerRadius.Right);

            var top         = new SKPoint((float)info.Width / 2, 0);
            var bottomRight = new SKPoint(info.Width, info.Height);
            var bottomLeft  = new SKPoint(0, info.Height);

            using (var path = new SKPath())
            {
                path.MoveTo(bottomLeft);
                path.ArcTo(top, bottomRight, topRadius);
                path.ArcTo(bottomRight, bottomLeft, bottomRightRadius);
                path.ArcTo(bottomLeft, top, bottomLeftRadius);
                path.Close();

                canvas.DrawPath(path, _fillPaint);
            }
        }
Пример #20
0
        private void Adaptation(SetOfIOPairs trainingSet, int maxK, double eps, double lambda, double micro)
        {
            double    delta;
            Gradients deltaGradients   = new Gradients(this);
            Gradients totalGradients   = new Gradients(this);
            Gradients partialGradients = new Gradients(this);

            Console.WriteLine("setting up random weights and thresholds ...");

            for (int i = NumberOfLayers() - 1; i >= 1; i--)
            {
                NeuralLayer currentLayer = GetLayer(i);
                for (int j = 0; j < currentLayer.NumberOfNeurons(); j++)
                {
                    Neuron currentNeuron = currentLayer.GetNeuron(j)
                    ;
                    currentNeuron.Threshold = 2 * Random() - 1;
                    for (int k = 0; k < currentNeuron.NumberOfInputs(); k++)
                    {
                        currentNeuron.GetInput(k).Weight = 2 * Random() - 1;
                    }
                }
            }

            int    currK = 0;
            double currE = double.PositiveInfinity;

            Console.WriteLine("entering adaptation loop ... (maxK = " + maxK + ")");

            while (currK < maxK && currE > eps)
            {
                ComputeTotalGradient(totalGradients, partialGradients, trainingSet);
                for (int i = NumberOfLayers() - 1; i >= 1; i--)
                {
                    NeuralLayer currentLayer = GetLayer(i);
                    for (int j = 0; j < currentLayer.NumberOfNeurons(); j++)
                    {
                        Neuron currentNeuron = currentLayer.GetNeuron(j);
                        delta = -lambda *totalGradients.GetThreshold(i, j)
                                + micro * deltaGradients.GetThreshold(i, j);

                        currentNeuron.Threshold += delta;
                        deltaGradients.SetThreshold(i, j, delta);
                    }

                    for (int k = 0; k < currentLayer.NumberOfNeurons(); k++)
                    {
                        Neuron currentNeuron = currentLayer.GetNeuron(k);
                        for (int l = 0; l < currentNeuron.NumberOfInputs(); l++)
                        {
                            delta = -lambda *totalGradients.GetWeight(i, k, l) +
                                    micro * deltaGradients.GetWeight(i, k, l);

                            currentNeuron.GetInput(l).Weight += delta;
                            deltaGradients.SetWeight(i, k, l, delta);
                        }
                    }
                }

                currE = totalGradients.GetGradientAbs();
                currK++;
                if (currK % 25 == 0)
                {
                    Console.WriteLine("currK=" + currK + "   currE=" + currE);
                }
            }
        }
Пример #21
0
        protected virtual void OnCanvasViewOnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            var info    = e.Info;
            var surface = e.Surface;
            var canvas  = surface.Canvas;

            canvas.Clear();

            if (Gradients != null && Gradients.Any())
            {
                _fillPaint.Shader = GetGradient(info, Gradients, GradientOrientation);
            }
            else
            {
                _fillPaint.Color = Color.ToSKColor();
            }

            var topLeftRadius     = ConvertToDeviceScaleFactor(CornerRadius.Left);
            var topRightRadius    = ConvertToDeviceScaleFactor(CornerRadius.Top);
            var bottomRightRadius = ConvertToDeviceScaleFactor(CornerRadius.Right);
            var bottomLeftRadius  = ConvertToDeviceScaleFactor(CornerRadius.Bottom);

            var topLeft     = new SKPoint(0, 0);
            var topRight    = new SKPoint(info.Width, 0);
            var bottomRight = new SKPoint(info.Width, info.Height);
            var bottomLeft  = new SKPoint(0, info.Height);

            using (var path = new SKPath())
            {
                path.MoveTo(bottomLeft);
                path.ArcTo(topLeft, topRight, topLeftRadius);
                path.ArcTo(topRight, bottomRight, topRightRadius);
                path.ArcTo(bottomRight, bottomLeft, bottomRightRadius);
                path.ArcTo(bottomLeft, topLeft, bottomLeftRadius);
                path.Close();

                canvas.ClipPath(path);

                if (IsOnlyBorder)
                {
                    var border = ConvertToDeviceScaleFactor(BorderWidth) * 2;

                    var scalaX = (info.Width - border) / info.Width;
                    var scalaY = (info.Height - border) / info.Height;

                    canvas.Translate(ConvertToDeviceScaleFactor(BorderWidth), ConvertToDeviceScaleFactor(BorderWidth));

                    canvas.Scale(scalaX, scalaY);

                    using (var center = new SKPath())
                    {
                        center.MoveTo(bottomLeft);
                        center.ArcTo(topLeft, topRight, topLeftRadius);
                        center.ArcTo(topRight, bottomRight, topRightRadius);
                        center.ArcTo(bottomRight, bottomLeft, bottomRightRadius);
                        center.ArcTo(bottomLeft, topLeft, bottomLeftRadius);
                        center.Close();

                        canvas.ClipPath(center, SKClipOperation.Difference);
                    }
                }
            }

            canvas.DrawPaint(_fillPaint);
        }
 public FunctionWrapper(Function function, Gradients gradient, Hessian hessian = null)
 {
     _function = function;
     _gradient = gradient;
     _hessian  = hessian;
 }