public void AveragePoolingLayer_Forward()
        {
            var bottom = new Tensor(1, 1, 3, 3);

            var filler = new ConstantFiller(2.0d);
            filler.Fill(bottom);

            var layer = new AveragePoolingLayer(3, 1, 1);
            layer.Setup(bottom, top);

            Assert.Equal(1, top.Num);
            Assert.Equal(1, top.Channels);
            Assert.Equal(3, top.Height);
            Assert.Equal(3, top.Width);

            layer.Forward(bottom, top);

            using (var topCpu = top.OnCpu())
            {
                var topData = topCpu.Data;
                AssertInRange(8.0d / 9, topData[0]);
                AssertInRange(4.0d / 3, topData[1]);
                AssertInRange(8.0d / 9, topData[2]);
                AssertInRange(4.0d / 3, topData[3]);
                AssertInRange(2.0d, topData[4]);
                AssertInRange(4.0d / 3, topData[5]);
                AssertInRange(8.0d / 9, topData[6]);
                AssertInRange(4.0d / 3, topData[7]);
                AssertInRange(8.0d / 9, topData[8]);
            }
        }
Ejemplo n.º 2
0
        public void Blob_Initialization()
        {
            var blob = new Tensor();
            Assert.Equal(0, blob.Num);
            Assert.Equal(0, blob.Count);
            Assert.Equal(0, blob.Channels);
            Assert.Equal(0, blob.Height);
            Assert.Equal(0, blob.Width);

            Assert.Throws<InvalidOperationException>(() => blob.OnCpu());
            // Assert.Throws<InvalidOperationException>(() => blob.Data);
            // Assert.Throws<InvalidOperationException>(() => blob.Diff);

            var preshapedBlob = new Tensor(2, 3, 4, 5);
            Assert.Equal(2, preshapedBlob.Num);
            Assert.Equal(3, preshapedBlob.Channels);
            Assert.Equal(4, preshapedBlob.Height);
            Assert.Equal(5, preshapedBlob.Width);
            Assert.Equal(120, preshapedBlob.Count);

            using (var preshapedBlobCpu = preshapedBlob.OnCpu())
            {
                Assert.NotNull(preshapedBlobCpu.Data);
                Assert.NotNull(preshapedBlobCpu.Diff);
                Assert.Equal(preshapedBlob.Count, preshapedBlobCpu.Data.Count);
                Assert.Equal(preshapedBlob.Count, preshapedBlobCpu.Diff.Count);
            }
        }
Ejemplo n.º 3
0
        public void Filler_GaussianDense(double meanParam, double stdParam)
        {
            var blob = new Tensor(2, 3, 4, 5);
            var config = new GaussianFillerConfiguration(meanParam, stdParam);
            var filler = new GaussianFiller(config);
            filler.Fill(blob);

            double mean = 0;
            double var = 0;

            int count = blob.Count;

            using (var blobCpu = blob.OnCpu())
            {
                for (int i = 0; i < count; i++)
                {
                    mean += blobCpu.DataAt(i);
                    var += (blobCpu.DataAt(i) - config.Mean) * (blobCpu.DataAt(i) - config.Mean);
                }

                mean /= count;
                var /= count;

                Assert.True(mean >= config.Mean - config.Std * 5);
                Assert.True(mean <= config.Mean + config.Std * 5);

                double targetVar = config.Std * config.Std;
                Assert.True(var >= (targetVar / 5.0d));
                Assert.True(var <= (targetVar * 5.0d));
            }
        }
        public EuclideanLossLayerTests()
        {
            var filler = new GaussianFiller();

            blobBottomData = new Tensor(10, 5, 1, 1);
            filler.Fill(blobBottomData);
            bottom.Add(blobBottomData);

            blobBottomLabel = new Tensor(10, 5, 1, 1);
            filler.Fill(blobBottomLabel);
            bottom.Add(blobBottomLabel);
        }
Ejemplo n.º 5
0
        public void Filler_Constant()
        {
            var blob = new Tensor(2, 3, 4, 5);

            var config = new ConstantFillerConfiguration(10.0d);
            var filler = new ConstantFiller(config);
            filler.Fill(blob);

            using (var blobCpu = blob.OnCpu())
            {
                int count = blobCpu.Count;
                var data = blobCpu.Data;
                for (int i = 0; i < count; i++)
                    Assert.Equal(data[i], 10.0d);
            }
        }
Ejemplo n.º 6
0
        public void Blob_Reshape()
        {
            var blob = new Tensor();
            blob.Reshape(2, 3, 4, 5);
            Assert.Equal(2, blob.Num);
            Assert.Equal(3, blob.Channels);
            Assert.Equal(4, blob.Height);
            Assert.Equal(5, blob.Width);
            Assert.Equal(120, blob.Count);

            using (var blobCpu = blob.OnCpu())
            {
                Assert.NotNull(blobCpu.Data);
                Assert.NotNull(blobCpu.Diff);
                Assert.Equal(blobCpu.Count, blobCpu.Data.Count);
                Assert.Equal(blobCpu.Count, blobCpu.Diff.Count);
            }
        }
        public void MaxPoolingLayer_BackwardsRectangularWithSquareKernel(int topLayer)
        {
            Contract.Requires(topLayer > 0);

            const int num = 2;
            const int channels = 2;
            var bottom = new Tensor(num, channels, 3, 5);

            var topList = new Tensor[topLayer];
            for (int i = 0; i < topLayer; i++)
                topList[i] = new Tensor();

            var filler = new ConstantFiller(2);
            filler.Fill(bottom);

            // Input: 2x 2 channels of:
            //     [2 2 2 2 2]
            //     [2 2 2 2 2]
            //     [2 2 2 2 2]

            var layer = new MaxPoolingLayer(2, 1, 0);
            layer.Setup(new TensorCollection { bottom }, topList);
            layer.Forward(new TensorCollection { bottom }, topList);

            // Input: 2x 2 channels of:
            //     [1 1 1 1]
            //     [0 0 0 0]

            using (var topCpu = topList[0].OnCpu())
            {
                var topDiff = topCpu.Diff;
                for (int i = 0; i < 8 * num * channels; i += 8)
                {

                    topDiff[i + 0] = 1;
                    topDiff[i + 1] = 1;
                    topDiff[i + 2] = 1;
                    topDiff[i + 3] = 1;
                }
            }

            // Input: 2x 2 channels of:
            //     [1 2 2 2 1]
            //     [1 2 2 2 1]
            //     [0 0 0 0 0]

            layer.Backward(topList, new[] { true }, new TensorCollection { bottom });

            using (var bottomCpu = bottom.OnCpu())
            {
                var bottomDiff = bottomCpu.Diff;
                for (int i = 0; i < 15 * num * channels; i += 15)
                {
                    Assert.Equal(1, bottomDiff[i + 0]);
                    Assert.Equal(2, bottomDiff[i + 1]);
                    Assert.Equal(2, bottomDiff[i + 2]);
                    Assert.Equal(2, bottomDiff[i + 3]);
                    Assert.Equal(1, bottomDiff[i + 4]);
                    Assert.Equal(1, bottomDiff[i + 5]);
                    Assert.Equal(2, bottomDiff[i + 6]);
                    Assert.Equal(2, bottomDiff[i + 7]);
                    Assert.Equal(2, bottomDiff[i + 8]);
                    Assert.Equal(1, bottomDiff[i + 9]);
                    Assert.Equal(0, bottomDiff[i + 10]);
                    Assert.Equal(0, bottomDiff[i + 11]);
                    Assert.Equal(0, bottomDiff[i + 12]);
                    Assert.Equal(0, bottomDiff[i + 13]);
                    Assert.Equal(0, bottomDiff[i + 14]);
                }
            }
        }
        public void MaxPoolingLayer_ForwardRectangularWithSquareKernel(int topLayer)
        {
            Contract.Requires(topLayer > 0);

            const int num = 2;
            const int channels = 2;
            var bottom = new Tensor(num, channels, 3, 5);

            var topList = new Tensor[topLayer];
            for (int i = 0; i < topLayer; i++)
                topList[i] = new Tensor();

            // Input: 2x 2 channels of:
            //     [1 2 5 2 3]
            //     [9 4 1 4 8]
            //     [1 2 5 2 3]

            using (var bottomCpu = bottom.OnCpu())
            {
                var bottomData = bottomCpu.Data;
                for (int i = 0; i < 15 * num * channels; i += 15)
                {
                    bottomData[i + 0] = 1;
                    bottomData[i + 1] = 2;
                    bottomData[i + 2] = 5;
                    bottomData[i + 3] = 2;
                    bottomData[i + 4] = 3;
                    bottomData[i + 5] = 9;
                    bottomData[i + 6] = 4;
                    bottomData[i + 7] = 1;
                    bottomData[i + 8] = 4;
                    bottomData[i + 9] = 8;
                    bottomData[i + 10] = 1;
                    bottomData[i + 11] = 2;
                    bottomData[i + 12] = 5;
                    bottomData[i + 13] = 2;
                    bottomData[i + 14] = 3;
                }
            }

            var layer = new MaxPoolingLayer(2, 1, 0);
            layer.Setup(new TensorCollection { bottom }, topList);

            foreach ( var top in topList)
            {
                Assert.Equal(num, top.Num);
                Assert.Equal(channels, top.Channels);
                Assert.Equal(2, top.Height);
                Assert.Equal(4, top.Width);
            }

            layer.Forward(new TensorCollection { bottom }, topList);

            // Expected output: 2x 2 channels of:
            //     [9 5 5 8]
            //     [9 5 5 8]
            for (int i = 0; i < 8 * num * channels; i += 8)
            {
                using (var topCpu = topList[0].OnCpu())
                {
                    var topData = topCpu.Data;
                    Assert.Equal(9, topData[i + 0]);
                    Assert.Equal(5, topData[i + 1]);
                    Assert.Equal(5, topData[i + 2]);
                    Assert.Equal(8, topData[i + 3]);
                    Assert.Equal(9, topData[i + 4]);
                    Assert.Equal(5, topData[i + 5]);
                    Assert.Equal(5, topData[i + 6]);
                    Assert.Equal(8, topData[i + 7]);
                }
            }

            if ( topList.Length > 1 )
            {
                // Expected mask output: 2x 2 channels of:
                //     [5  2  2 9]
                //     [5 12 12 9]
                for (int i = 0; i < 8 * num * channels; i += 8)
                {
                    using (var topCpu = topList[1].OnCpu())
                    {
                        var topData = topCpu.Data;
                        Assert.Equal(5, topData[i + 0]);
                        Assert.Equal(2, topData[i + 1]);
                        Assert.Equal(2, topData[i + 2]);
                        Assert.Equal(9, topData[i + 3]);
                        Assert.Equal(5, topData[i + 4]);
                        Assert.Equal(12, topData[i + 5]);
                        Assert.Equal(12, topData[i + 6]);
                        Assert.Equal(9, topData[i + 7]);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public void CheckSingle(Layer layer, TensorCollection bottom, TensorCollection top, int checkBottom, int topId, int topDataId, bool elementwise = false)
        {
            //TODO If implemented at all the ability of the layer to access stored blobs, we need to recheck this.
            if ( elementwise )
            {
                Assert.True(topId >= 0);
                Assert.True(topDataId >= 0);

                int topCount = top[topId].Count;
                for (int blobId = 0; blobId < bottom.Count; blobId++)
                    Assert.Equal(topCount, bottom[blobId].Count);
            }

            // First, figure out what blobs we need to check against.
            var blobsToCheck = new TensorCollection();
            var propagateDown = new List<bool>().Repeated(bottom.Count, checkBottom < 0);
            if ( checkBottom < 0 )
            {
                // We are not checking the bottom.
                for (int i = 0; i < bottom.Count; i++)
                    blobsToCheck.Add(bottom[i]);
            }
            else
            {
                // We are checking the bottom, therefore we must ensure that the blob checked exists.
                Assert.True(checkBottom < bottom.Count);
                blobsToCheck.Add(bottom[checkBottom]);
                propagateDown[checkBottom] = true;
            }

            //TODO Add a general random generator that layers should use, to ensure we always apply it when layers are non-deterministic.

            // Compute the gradient analytically using Backward
            // Get any loss from the layer
            double computedObjective = layer.Forward(bottom, top);

            // Get additional loss from the objective
            computedObjective += GetObjectiveAndGradient(top, topId, topDataId);
            layer.Backward(top, propagateDown, bottom);

            // Store computed gradients for all checked blobs
            var computedGradientsBlob = new Tensor[blobsToCheck.Count];
            for ( int blobId = 0; blobId < blobsToCheck.Count; blobId++ )
            {
                var currentBlob = blobsToCheck[blobId];
                computedGradientsBlob[blobId] = new Tensor(currentBlob);

                using (var currentBlobCpu = currentBlob.OnCpu())
                using (var computedGradientsBlobCpu = computedGradientsBlob[blobId].OnCpu())
                {
                    var currentDiff = currentBlobCpu.Diff;
                    var computedGradients = computedGradientsBlobCpu.Data;
                    currentDiff.CopyTo(computedGradients);
                }
            }

            // Compute derivative of top w.r.t. each bottom and parameter input using
            // finite differencing.

            for (int blobId = 0; blobId < blobsToCheck.Count; blobId++ )
            {
                var currentBlob = blobsToCheck[blobId];

                using (var currentBlobCpu = currentBlob.OnCpu())
                using (var computedGradientsBlobCpu = computedGradientsBlob[blobId].OnCpu())
                {
                    var computedGradients = computedGradientsBlobCpu.Data;
                    for (int featId = 0; featId < currentBlob.Count; featId++)
                    {
                        // For an element-wise layer, we only need to do finite differencing to
                        // compute the derivative of topData[top_id][top_data_id] w.r.t.
                        // bottomData[blob_id][i] only for i == top_data_id.  For any other
                        // i != top_data_id, we know the derivative is 0 by definition, and simply
                        // check that that's true.
                        double estimatedGradient = 0;
                        if (!elementwise || featId == topDataId)
                        {
                            //TODO Add a general random generator that layers should use, to ensure we always apply it when layers are non-deterministic.

                            // Do finite differencing.
                            // Compute loss with step-size added to input.
                            currentBlobCpu.Data[featId] += step;
                            double positiveObjective = layer.Forward(bottom, top);
                            positiveObjective += GetObjectiveAndGradient(top, topId, topDataId);

                            // Compute loss with step-size subtracted from input.
                            currentBlobCpu.Data[featId] -= step * 2;

                            //TODO Add a general random generator that layers should use, to ensure we always apply it when layers are non-deterministic.

                            double negativeObjective = layer.Forward(bottom, top);
                            negativeObjective += GetObjectiveAndGradient(top, topId, topDataId);

                            // Recover original input value.
                            currentBlobCpu.Data[featId] += step;
                            estimatedGradient = (positiveObjective - negativeObjective) / step / 2.0d;
                        }

                        double computedGradient = computedGradients[featId];
                        double feature = currentBlobCpu.Data[featId];
                        if (kink - kinkRange > Math.Abs(feature) || Math.Abs(feature) > kink + kinkRange)
                        {
                            // We check relative accuracy, but for too small values, we threshold
                            // the scale factor by 1

                            double scale = Math.Max(Math.Max(Math.Abs(computedGradient), Math.Abs(estimatedGradient)), 1.0d);
                            Assert.InRange(computedGradient - estimatedGradient, -threshold * scale, threshold * scale);
                        }
                    }
                }
            }
        }
Ejemplo n.º 10
0
 public void CheckSingle( Layer layer,  Tensor bottom, Tensor top, int checkBottom, int topId, int topDataId, bool elementWise = false)
 {
     this.CheckSingle(layer, new TensorCollection { bottom }, new TensorCollection { top }, checkBottom, topId, topDataId, elementWise);
 }
Ejemplo n.º 11
0
 public void CheckExhaustive( Layer layer, Tensor bottom, Tensor top, int checkBottom = -1)
 {
     this.CheckExhaustive(layer, new TensorCollection { bottom }, new TensorCollection { top }, checkBottom);
 }
Ejemplo n.º 12
0
 public void CheckEltwise( Layer layer, Tensor bottom, Tensor top)
 {
     this.CheckEltwise(layer, new TensorCollection { bottom }, new TensorCollection { top });
 }
Ejemplo n.º 13
0
        public void Filler_GaussianSparse(double meanParam, double stdParam)
        {
            var blob = new Tensor(2, 3, 4, 5);
            var config = new GaussianFillerConfiguration(meanParam, stdParam) { IsSparse = true };

            var filler = new GaussianFiller(config);
            filler.Fill(blob);

            double mean = 0;
            double var = 0;

            using (var blobCpu = blob.OnCpu())
            {
                int count = blob.Count;
                int zeroes = 0;

                for (int i = 0; i < count; i++)
                {
                    if (blobCpu.DataAt(i) == 0.0d)
                    {
                        zeroes++;
                    }
                    else
                    {
                        mean += blobCpu.DataAt(i);
                        var += (blobCpu.DataAt(i) - config.Mean) * (blobCpu.DataAt(i) - config.Mean);
                    }
                }

                mean /= (count - zeroes);
                var /= (count - zeroes);

                Assert.True(mean >= config.Mean - config.Std * 5);
                Assert.True(mean <= config.Mean + config.Std * 5);

                double targetVar = config.Std * config.Std;
                Assert.True(var >= (targetVar / 5.0d));
                Assert.True(var <= (targetVar * 5.0d));
            }
        }
Ejemplo n.º 14
0
        public void Filler_Xavier()
        {
            var blob = new Tensor(2, 3, 4, 5);
            var filler = new XavierFiller();
            filler.Fill(blob);

            int fanIn = blob.Count / blob.Num;
            double scale = Math.Sqrt(3 / fanIn);

            using (var blobCpu = blob.OnCpu())
            {
                int count = blob.Count;
                var data = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.True(data[i] >= -scale);
                    Assert.True(data[i] <= scale);
                }
            }
        }
Ejemplo n.º 15
0
        public void Filler_Uniform(double min, double max)
        {
            var blob = new Tensor(2, 3, 4, 5);
            var config = new UniformFillerConfiguration(min, max);
            var filler = new UniformFiller(config);
            filler.Fill(blob);

            using (var blobCpu = blob.OnCpu())
            {
                int count = blob.Count;
                var data = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.True(data[i] >= min);
                    Assert.True(data[i] <= max);
                }
            }
        }
Ejemplo n.º 16
0
        public void Filler_PositiveUnitball()
        {
            var blob = new Tensor(2, 3, 4, 5);
            var filler = new PositiveUnitballFiller();
            filler.Fill(blob);

            int num = blob.Num;
            int count = blob.Count;
            int dim = count / num;

            using (var blobCpu = blob.OnCpu())
            {
                var data = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.True(data[i] >= 0.0d);
                    Assert.True(data[i] <= 1.0d);
                }

                for (int i = 0; i < num; i++)
                {
                    double sum = 0;
                    for (int j = 0; j < dim; j++)
                        sum += blobCpu.DataAt(i * dim + j);

                    Assert.True(sum >= 0.999f);
                    Assert.True(sum <= 1.001f);
                }
            }
        }