예제 #1
0
        public static Vol Image_To_Vol(double[] img, int width, int hight, bool convert_grayscale)
        {
            var p = img;
            var w = width;
            var h = hight;

            double[] pv = new double[img.Length];
            for (var i = 0; i < p.Length; i++)
            {
                pv[i] = (p[i] / 255.0 - 0.5); // normalize image pixels to [-0.5, 0.5]
            }
            var x = new Vol(w, h, 4, 0.0);

            x.W = pv;

            if (convert_grayscale)
            {
                var x1 = new Vol(w, h, 1, 0.0);
                for (int i = 0; i < w; i++)
                {
                    for (int j = 0; j < h; j++)
                    {
                        x1.Set(i, j, 0, x.Get(i, j, 0));
                    }
                }
                x = x1;
            }


            return(x);
        }
        private void DLRNforWidth(Vol V, double n2, int x)
        {
            for (var y = 0; y < V.SY; y++)
            {
                for (var i = 0; i < V.Depth; i++)
                {
                    var chain_grad = this.Output.Get_Grad(x, y, i);
                    var S          = this.S_cache_.Get(x, y, i);
                    var SB         = Math.Pow(S, this.beta);
                    var SB2        = SB * SB;

                    // normalize in a window of size n
                    for (var j = Math.Max(0, i - n2); j <= Math.Min(i + n2, V.Depth - 1); j++)
                    {
                        var aj = V.Get(x, y, (int)j);
                        var g  = -aj *this.beta *Math.Pow(S, this.beta - 1) * this.alpha / this.n * 2 * aj;

                        if (j == i)
                        {
                            g += SB;
                        }
                        g /= SB2;
                        g *= chain_grad;
                        V.Add_Grad(x, y, (int)j, g);
                    }
                }
            }
        }
예제 #3
0
        public static Vol Augment(Vol V, int corp, int dx, int dy, bool fliplr)
        {
            //if (dx==0)
            //{
            //    dx = Util.Randi(0, V.SX - corp);
            //}
            //if (dy == 0)
            //{
            //    dy = Util.Randi(0, V.SY- corp);
            //}


            Vol W;

            if (corp != V.SX || dx != 0 || dy != 0)
            {
                W = new Vol(corp, corp, V.Depth, 0.0);
                for (int x = 0; x < corp; x++)
                {
                    for (int y = 0; y < corp; y++)
                    {
                        if (x + dx < 0 || x + dx >= V.SX || y + dy < 0 || y + dy >= V.SY)
                        {
                            continue;
                        }
                        for (int d = 0; d < V.Depth; d++)
                        {
                            W.Set(x, y, d, V.Get(x + dx, y + dy, d));
                        }
                    }
                }
            }
            else
            {
                W = V;
            }

            if (fliplr)
            {
                var W2 = W.CloneAndZero();

                for (int x = 0; x < W.SX; x++)
                {
                    for (int y = 0; y < W.SY; y++)
                    {
                        for (int d = 0; d < W.Depth; d++)
                        {
                            W2.Set(x, y, d, W.Get(W.SX - x - 1, y, d));
                        }
                    }
                }
                W = W2;
            }


            return(W);
        }
예제 #4
0
        public static Vol Augment(Vol V, int corp)
        {
            int dx; int dy; bool fliplr = false;

            dx = (int)Util.Randi(0, V.Width - corp);


            dy = (int)Util.Randi(0, V.Height - corp);
            Vol W;

            if (corp != V.Width || dx != 0 || dy != 0)
            {
                W = new Vol(corp, corp, V.Depth, 0.0f);
                for (int x = 0; x < corp; x++)
                {
                    for (int y = 0; y < corp; y++)
                    {
                        if (x + dx < 0 || x + dx >= V.Width || y + dy < 0 || y + dy >= V.Height)
                        {
                            continue;
                        }
                        for (int d = 0; d < V.Depth; d++)
                        {
                            W.Set(x, y, d, V.Get(x + dx, y + dy, d));
                        }
                    }
                }
            }
            else
            {
                W = V;
            }

            if (fliplr)
            {
                var W2 = W.CloneAndZero();

                for (int x = 0; x < W.Width; x++)
                {
                    for (int y = 0; y < W.Height; y++)
                    {
                        for (int d = 0; d < W.Depth; d++)
                        {
                            W2.Set(x, y, d, W.Get(W.Width - x - 1, y, d));
                        }
                    }
                }
                W = W2;
            }


            return(W);
        }
        private void LRNforWidth(Vol V, Vol A, double n2, int x)
        {
            for (var y = 0; y < V.SY; y++)
            {
                for (var i = 0; i < V.Depth; i++)
                {
                    var ai = V.Get(x, y, i);

                    // normalize in a window of size n
                    var den = 0.0;
                    for (var j = Math.Max(0, i - n2); j <= Math.Min(i + n2, V.Depth - 1); j++)
                    {
                        var aa = V.Get(x, y, (int)j);
                        den += aa * aa;
                    }
                    den *= this.alpha / this.n;
                    den += this.k;
                    this.S_cache_.Set(x, y, i, den); // will be useful for backprop
                    den = Math.Pow(den, this.beta);
                    A.Set(x, y, i, ai / den);
                }
            }
        }
예제 #6
0
        public Vol Forward(Vol V, bool is_training)
        {
            this.in_Act = V;

            var A = new Vol(this.OutputWidth, this.OutputHeight, this.OutputDepth, 0.0);

            var n = 0; // a counter for switches

            for (var d = 0; d < this.OutputDepth; d++)
            {
                var x = -this.Pad;
                var y = -this.Pad;
                for (var ax = 0; ax < this.OutputWidth; x += this.Stride, ax++)
                {
                    y = -this.Pad;
                    for (var ay = 0; ay < this.OutputHeight; y += this.Stride, ay++)
                    {
                        // convolve centered at this particular location
                        double a = -99999; // hopefully small enough ;\
                        var    winx = -1; var winy = -1;
                        for (var fx = 0; fx < this.SX; fx++)
                        {
                            for (var fy = 0; fy < this.SY; fy++)
                            {
                                var oy = y + fy;
                                var ox = x + fx;
                                if (oy >= 0 && oy < V.SY && ox >= 0 && ox < V.SX)
                                {
                                    var v = V.Get(ox, oy, d);
                                    // perform max pooling and store pointers to where
                                    // the max came from. This will speed up backprop
                                    // and can help make nice visualizations in future
                                    if (v > a)
                                    {
                                        a = v; winx = ox; winy = oy;
                                    }
                                }
                            }
                        }
                        this.Switchx[n] = winx;
                        this.Switchy[n] = winy;
                        n++;
                        A.Set(ax, ay, d, a);
                    }
                }
            }
            this.Output = A;
            return(this.Output);
        }
예제 #7
0
        private void Conv(Vol V, bool is_training, Vol A)
        {
            var n = 0; // a counter for switches

            for (int d = 0; d < this.OutputDepth; d++)
            {
                for (var ax = 0; ax < this.OutputWidth; ax++)
                {
                    var x = -this.Pad;
                    x += (this.Stride * ax);
                    for (var ay = 0; ay < this.OutputHeight; ay++)
                    {
                        var y = -this.Pad;
                        y += (this.Stride * ay);
                        // convolve centered at this particular location
                        float a = -99999; // hopefully small enough ;\
                        var   winx = -1; var winy = -1;
                        for (var fx = 0; fx < this.KernelWidth; fx++)
                        {
                            for (var fy = 0; fy < this.KernelHeight; fy++)
                            {
                                var oy = y + fy;
                                var ox = x + fx;
                                if (oy >= 0 && oy < V.Height && ox >= 0 && ox < V.Width)
                                {
                                    var v = V.Get(ox, oy, d);
                                    // perform max pooling and store pointers to where
                                    // the max came from. This will speed up backprop
                                    // and can help make nice visualizations in future
                                    if (v > a)
                                    {
                                        a = v; winx = ox; winy = oy;
                                    }
                                }
                            }
                        }
                        this.Switchx[n] = winx;
                        this.Switchy[n] = winy;
                        n++;
                        A.Set(ax, ay, d, a);
                    }
                }
            }
            //var source = Enumerable.Range(0, this.OutputDepth);
            //var pquery = from num in source.AsParallel()
            //             select num;
            //  pquery.ForAll((d) => );
        }