Ejemplo n.º 1
0
        public void CopyConstructorTest1()
        {
            MaxPoolingLayer layer1 = new MaxPoolingLayer(MaxPoolingLayerTest.shape, new Kernel(3, 2, 1, 2));
            MaxPoolingLayer layer2 = new MaxPoolingLayer(layer1);

            Assert.AreEqual(JsonConvert.SerializeObject(layer1), JsonConvert.SerializeObject(layer2));
        }
Ejemplo n.º 2
0
        public void CloneTest()
        {
            MaxPoolingLayer layer1 = new MaxPoolingLayer(MaxPoolingLayerTest.shape, new Kernel(2, 2, 2, 2));
            MaxPoolingLayer layer2 = layer1.Clone() as MaxPoolingLayer;

            Assert.AreEqual(JsonConvert.SerializeObject(layer1), JsonConvert.SerializeObject(layer2));
        }
Ejemplo n.º 3
0
        public void SerializeTest()
        {
            MaxPoolingLayer layer1 = new MaxPoolingLayer(MaxPoolingLayerTest.shape, new Kernel(2, 2, 2, 2));
            string          s1     = JsonConvert.SerializeObject(layer1);
            MaxPoolingLayer layer2 = JsonConvert.DeserializeObject <MaxPoolingLayer>(s1);
            string          s2     = JsonConvert.SerializeObject(layer2);

            Assert.AreEqual(s1, s2);
        }
Ejemplo n.º 4
0
        private static NeuralNetwork InitializeNeuralNetwork(int seed)
        {
            Random random = new Random(seed == 0 ? new Random().Next() : seed);

            float RandomWeight() => (float)(random.NextDouble() * 2 - 1);

            Layer prevLayer;

            InputLayer li = new InputLayer(28, 28);

            prevLayer = li;

            ConvolutionalLayer l0 = new ConvolutionalLayer(15, 5, 1, 0, prevLayer, ActivationFunctions.ReLU(true));

            prevLayer = l0;
            prevLayer.InitializeWeights(RandomWeight);

            MaxPoolingLayer l1 = new MaxPoolingLayer(2, 2, prevLayer);

            prevLayer = l1;

            ConvolutionalLayer l2 = new ConvolutionalLayer(30, 4, 1, 0, prevLayer, ActivationFunctions.ReLU(true));

            prevLayer = l2;
            prevLayer.InitializeWeights(RandomWeight);

            MaxPoolingLayer l3 = new MaxPoolingLayer(3, 2, prevLayer);

            prevLayer = l3;

            ConvolutionalLayer l4 = new ConvolutionalLayer(45, 2, 2, 0, prevLayer, ActivationFunctions.ReLU(true));

            prevLayer = l4;
            prevLayer.InitializeWeights(RandomWeight);

            MaxPoolingLayer l5 = new MaxPoolingLayer(2, 1, prevLayer);

            prevLayer = l5;

            FullyConnectedLayer l6 = new FullyConnectedLayer(64, prevLayer, ActivationFunctions.Sigmoid(1));

            prevLayer = l6;
            prevLayer.InitializeWeights(RandomWeight);

            FullyConnectedLayer l7 = new FullyConnectedLayer(32, prevLayer, ActivationFunctions.Sigmoid(1));

            prevLayer = l7;
            prevLayer.InitializeWeights(RandomWeight);

            FullyConnectedLayer l8 = new FullyConnectedLayer(10, prevLayer, ActivationFunctions.SoftMax(1));

            prevLayer = l8;
            prevLayer.InitializeWeights(RandomWeight);

            return(new NeuralNetwork(li, l0, l1, l2, l3, l4, l5, l6, l7, l8));
        }
Ejemplo n.º 5
0
        public void TestConstructor()
        {
            LayerStub prevLayer = new LayerStub(4, 4, 2);

            MaxPoolingLayer layer = new MaxPoolingLayer(2, 2, prevLayer);

            Assert.AreEqual(2, layer.Width);
            Assert.AreEqual(2, layer.Height);
            Assert.AreEqual(prevLayer.Depth, layer.Depth);
        }
Ejemplo n.º 6
0
        internal static void MaxPoolingTest()
        {
            InputLayer inputLayer = new InputLayer(20, 20, 2);

            MaxPoolingLayer mPLayer = new MaxPoolingLayer(2, 2, inputLayer);

            Debug.WriteLine(mPLayer.Width + " " + mPLayer.Height + " " + mPLayer.Depth);

            float[,,] inputValues = new float[20, 20, 2];
            int i = 0;

            for (int z = 0; z < 2; z++)
            {
                Debug.WriteLine($"{z} --------------------");
                for (int y = 0; y < 20; y++)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int x = 0; x < 20; x++, i++)
                    {
                        if (x != 0)
                        {
                            sb.Append(", ");
                        }

                        inputValues[x, y, z] = i;
                        sb.Append(i);
                    }
                    Debug.WriteLine(sb.ToString());
                }
            }
            NNFeedData inputData = new NNFeedData(inputValues);

            NNDetailedFeedData outputData = inputLayer.FeedForward(inputData);

            outputData = mPLayer.FeedForward(outputData.OutputData);

            for (int z = 0; z < mPLayer.Depth; z++)
            {
                Debug.WriteLine($"{z} --------------------");
                for (int y = 0; y < mPLayer.Height; y++)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int x = 0; x < mPLayer.Width; x++)
                    {
                        if (x != 0)
                        {
                            sb.Append(", ");
                        }

                        sb.Append($"{outputData.OutputData[outputData.OutputData.ToNeuronIndex(x, y, z)]}");
                    }
                    Debug.WriteLine(sb.ToString());
                }
            }
        }
        public void MaxPoolingLayer_SetupPadded()
        {
            var layer = new MaxPoolingLayer(3, 2, 1);

            layer.Setup(bottom, top);

            Assert.Equal(bottom.Num, top.Num);
            Assert.Equal(bottom.Channels, top.Channels);
            Assert.Equal(4, top.Height);
            Assert.Equal(3, top.Width);
        }
Ejemplo n.º 8
0
        public void ConstructorTest2()
        {
            MaxPoolingLayer layer = new MaxPoolingLayer(MaxPoolingLayerTest.shape, new Kernel(3, 2, 1, 2));

            CollectionAssert.AreEqual(new[] { -1, 3, 2, 2 }, layer.OutputShape.Axes);
            Assert.AreEqual("MP3x2+1x2(S)", layer.Architecture);
            Assert.AreEqual(3, layer.Kernel.Width);
            Assert.AreEqual(2, layer.Kernel.Height);
            Assert.AreEqual(1, layer.Kernel.StrideX);
            Assert.AreEqual(2, layer.Kernel.StrideY);
        }
Ejemplo n.º 9
0
        public void ArchitectureConstructorTest4()
        {
            string          architecture = "MP2";
            MaxPoolingLayer layer        = new MaxPoolingLayer(MaxPoolingLayerTest.shape, architecture, null);

            Assert.AreEqual(architecture, layer.Architecture);
            Assert.AreEqual(2, layer.Kernel.Width);
            Assert.AreEqual(2, layer.Kernel.Height);
            Assert.AreEqual(2, layer.Kernel.StrideX);
            Assert.AreEqual(2, layer.Kernel.StrideY);
            Assert.AreEqual(0, layer.Kernel.PaddingX);
            Assert.AreEqual(0, layer.Kernel.PaddingY);
        }
Ejemplo n.º 10
0
        public void ArchitectureConstructorTest5()
        {
            string architecture = "MP";

            try
            {
                MaxPoolingLayer layer = new MaxPoolingLayer(MaxPoolingLayerTest.shape, architecture, null);
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual(
                    new ArgumentException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.E_InvalidLayerArchitecture, architecture), nameof(architecture)).Message,
                    e.Message);
                throw;
            }
        }
Ejemplo n.º 11
0
        public void ForwardBackwardTest()
        {
            foreach (string format in new[] { Shape.BWHC, Shape.BHWC, Shape.BCHW })
            {
                Shape shape = new Shape(format, -1, 13, 11, 2);
                foreach (int kw in new[] { 1, 2, 3, 4, 5 })
                {
                    foreach (int kh in new[] { 1, 2, 3, 4, 5 })
                    {
                        foreach (int kstridex in new[] { 1, 2, 3 })
                        {
                            foreach (int kstridey in new[] { 1, 2, 3 })
                            {
                                foreach (int kpaddingx in new[] { 0, 2, -2 })
                                {
                                    foreach (int kpaddingy in new[] { 0, 2, -2 })
                                    {
                                        Kernel          kernel = new Kernel(kw, kh, kstridex, kstridey, kpaddingx, kpaddingy);
                                        MaxPoolingLayer layer  = new MaxPoolingLayer(shape, kernel);

                                        for (int mb = 1; mb <= 3; mb++)
                                        {
                                            Session session = new Session(true);

                                            Tensor x = new Tensor(null, shape.Reshape(Axis.B, mb));
                                            x.Randomize(this.random);

                                            Tensor y = layer.Forward(session, new[] { x })[0];

                                            Tensor expected = MaxPoolingLayerTest.CalculateY(x, kernel);
                                            Helpers.AreTensorsEqual(expected, y);

                                            // unroll the graph
                                            y.RandomizeGradient(this.random);
                                            session.Unroll();

                                            Tensor expectedDX = MaxPoolingLayerTest.CalculateDX(x, y, kernel);
                                            Helpers.AreGradientsEqual(expectedDX, x);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 12
0
        public void TestCalculateErrorByOutputGradient()
        {
            LayerStub       prevLayer = new LayerStub(4, 4, 2);
            MaxPoolingLayer layer     = new MaxPoolingLayer(2, 2, prevLayer);



            //PrivateObject obj = new PrivateObject(layer);
            //int[] maxIndices = new[] { 5, 7, 13, 15 };

            //for (int fNIdx = 0; fNIdx < prevLayer.Neurons; fNIdx++) {
            //    for (int tNIdx = 0; tNIdx < layer.Neurons; tNIdx++) {
            //        float weight = (float)obj.Invoke("GetWeight", maxIndices, fNIdx, tNIdx);


            //    }
            //}
        }
Ejemplo n.º 13
0
        public void TestFeedForward()
        {
            float[] data = new float[4 * 4 * 2];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = i + 1;
            }
            LayerStub       prevLayer = new LayerStub(4, 4, 2);
            MaxPoolingLayer layer     = new MaxPoolingLayer(2, 2, prevLayer);
            NNFeedData      feedData  = new NNFeedData(4, 4, 2, data);

            NNDetailedFeedData result = layer.FeedForward(feedData);

            Assert.AreEqual(6, result.OutputData[result.OutputData.ToNeuronIndex(0, 0, 0)]);
            Assert.AreEqual(8, result.OutputData[result.OutputData.ToNeuronIndex(1, 0, 0)]);
            Assert.AreEqual(14, result.OutputData[result.OutputData.ToNeuronIndex(0, 1, 0)]);
            Assert.AreEqual(16, result.OutputData[result.OutputData.ToNeuronIndex(1, 1, 0)]);
        }
Ejemplo n.º 14
0
        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.º 15
0
        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]);
                }
            }
        }
Ejemplo n.º 16
0
        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]);
                }
            }
        }
Ejemplo n.º 17
0
        public void MaxPoolingLayer_SetupPadded()
        {
            var layer = new MaxPoolingLayer(3, 2, 1);
            layer.Setup(bottom, top);

            Assert.Equal(bottom.Num, top.Num);
            Assert.Equal(bottom.Channels, top.Channels);
            Assert.Equal(4, top.Height);
            Assert.Equal(3, top.Width);
        }
Ejemplo n.º 18
0
        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]);
                    }
                }
            }
        }