示例#1
0
        public void Test_ValidateImage_Correct()
        {
            var input = new double[, , ]
            {
                {
                    { 1, 2, 3 },
                    { 4, 5, 6 },
                    { 7, 8, 9 }
                },
                {
                    { 1, 2, 3 },
                    { 4, 5, 6 },
                    { 7, 8, 9 }
                },
                {
                    { 1, 2, 3 },
                    { 4, 5, 6 },
                    { 7, 8, 9 }
                }
            };
            var filter = new double[, , ]
            {
                {
                    { 1.1, 1.2, 1.3 },
                    { 1.4, 1.5, 1.6 }
                },
                {
                    { 1.1, 1.2, 1.3 },
                    { 1.4, 1.5, 1.6 }
                }
            };
            var filter2 = new double[, , ]
            {
                {
                    { 2.1, 2.2, 2.3 },
                    { 2.4, 2.5, 2.6 }
                },
                {
                    { 2.1, 2.2, 2.3 },
                    { 2.4, 2.5, 2.6 }
                }
            };

            var layer = new ConvolutionalLayer();

            layer.Neurals.Add(new Filter {
                Weights = new Array3D(filter)
            });
            layer.Neurals.Add(new Filter {
                Weights = new Array3D(filter2)
            });

            layer.FormOutput(new Array3D(input));
        }
示例#2
0
        public void Test_FormOutput_Correct()
        {
            var inputArray = new double[, , ]
            {
                {
                    { 1, 3, 5, 8, 7, 4, 2, 8, 6 },
                    { 4, 2, 3, 5, 1, 0, 1, 2, 7 },
                    { 8, 3, 1, 2, 3, 6, 5, 8, 5 },
                    { 7, 2, 4, 2, 9, 3, 6, 4, 6 },
                    { 9, 8, 0, 8, 8, 3, 7, 1, 0 },
                    { 9, 4, 1, 2, 2, 4, 7, 4, 3 },
                    { 0, 5, 9, 1, 6, 4, 5, 4, 3 },
                    { 3, 0, 5, 8, 6, 5, 7, 0, 1 },
                    { 1, 4, 2, 3, 6, 1, 5, 4, 3 }
                },
                {
                    { 4, 3, 5, 0, 1, 2, 4, 8, 9 },
                    { 4, 8, 0, 1, 3, 4, 5, 6, 7 },
                    { 4, 4, 2, 3, 6, 5, 4, 2, 1 },
                    { 0, 7, 8, 6, 4, 4, 1, 0, 0 },
                    { 1, 0, 1, 2, 4, 5, 6, 2, 3 },
                    { 1, 2, 3, 7, 8, 8, 7, 4, 6 },
                    { 0, 3, 3, 2, 1, 7, 2, 3, 0 },
                    { 1, 8, 4, 4, 1, 2, 0, 0, 5 },
                    { 7, 9, 9, 9, 1, 3, 1, 4, 6 }
                },
                {
                    { 7, 1, 4, 3, 2, 1, 0, 4, 6 },
                    { 5, 3, 1, 9, 7, 5, 3, 1, 1 },
                    { 4, 2, 0, 8, 6, 4, 2, 0, 0 },
                    { 7, 8, 7, 4, 3, 8, 0, 1, 4 },
                    { 5, 2, 1, 3, 6, 5, 4, 7, 5 },
                    { 1, 6, 2, 4, 2, 3, 7, 6, 1 },
                    { 0, 3, 7, 1, 3, 0, 3, 1, 8 },
                    { 6, 4, 8, 8, 9, 0, 1, 2, 1 },
                    { 5, 6, 7, 8, 9, 0, 2, 4, 7 }
                }
            };
            var filterFirstArray = new double[, , ]
            {
                {
                    { 1, 4, 5 },
                    { 0, 3, 4 },
                    { 6, 8, 7 }
                },
                {
                    { 4, 1, 0 },
                    { 6, 3, 1 },
                    { 5, 4, 2 }
                },
                {
                    { 2, 2, 5 },
                    { 3, 7, 4 },
                    { 4, 1, 0 }
                }
            };
            var filterSecondArray = new double[, , ]
            {
                {
                    { 4, 7, 1 },
                    { 3, 0, 2 },
                    { 4, 2, 1 }
                },
                {
                    { 3, 2, 2 },
                    { 8, 3, 4 },
                    { 7, 7, 1 }
                },
                {
                    { 2, 3, 0 },
                    { 4, 0, 7 },
                    { 1, 5, 4 }
                }
            };
            var expectedArray = new double[, , ]
            {
                {
                    { 336, 320, 339, 402 },
                    { 360, 402, 448, 266 },
                    { 272, 377, 444, 353 },
                    { 408, 486, 299, 253 }
                },
                {
                    { 287, 312, 334, 336 },
                    { 330, 335, 379, 317 },
                    { 272, 300, 424, 352 },
                    { 410, 481, 250, 248 }
                }
            };

            var input        = Converters.Convert(inputArray);
            var filterFirst  = Converters.Convert(filterFirstArray);
            var filterSecond = Converters.Convert(filterSecondArray);
            var expected     = Converters.Convert(expectedArray);

            var layer = new ConvolutionalLayer()
            {
                Stride = 2
            };

            layer.ActivationFunction = new ConstActivationFunction();
            layer.Neurals.Add(new Filter {
                Weights = filterFirst
            });
            layer.Neurals.Add(new Filter {
                Weights = filterSecond
            });

            var result = layer.FormOutput(input) as Array3D;

            for (var i = 0; i < expected.Height; i++)
            {
                for (var j = 0; j < expected.Width; j++)
                {
                    for (var k = 0; k < expected.Depth; k++)
                    {
                        Assert.AreEqual(expected[i, j, k], result[i, j, k]);
                    }
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="originImage"></param>
        /// <param name="mask1"></param>
        /// <param name="mask2"></param>
        /// <param name="filteredImage">Full path to store applyed filter image</param>
        private void ApplyFilter(string originImage, double [,,] mask1, double [,,] mask2, string filteredImage)
        {
            Bitmap bitmap = new Bitmap(originImage);

            var image = new Array3D(bitmap.Height, bitmap.Width, 3);

            for (var i = 0; i < bitmap.Height; i++)
            {
                for (var j = 0; j < bitmap.Width; j++)
                {
                    var pixel = bitmap.GetPixel(j, i);
                    image[i, j, 0] = pixel.R;
                    image[i, j, 1] = pixel.G;
                    image[i, j, 2] = pixel.B;
                }
            }

            var filter = new ConvolutionalLayer
            {
                ActivationFunction = new ConstActivationFunction()
            };

            filter.Neurals.Add(new Filter {
                Weights = Converters.Convert(mask1)
            });
            filter.Neurals.Add(new Filter {
                Weights = Converters.Convert(mask2)
            });

            var output = filter.FormOutput(image) as Array3D;

            var g1 = output.ExtractLayer(0);
            var g2 = output.ExtractLayer(1);

            var resultArray = new double[g1.Height, g1.Width];

            for (var i = 0; i < g1.Height; i++)
            {
                for (var j = 0; j < g1.Width; j++)
                {
                    resultArray[i, j] = Math.Sqrt(Math.Pow(g1[i, j], 2) + Math.Pow(g2[i, j], 2));
                }
            }

            var imageBuffer = new byte[g1.Width * g1.Height * 4];
            var indexBytes  = 0;

            for (var i = 0; i < g1.Height; i++)
            {
                for (var j = 0; j < g1.Width; j++)
                {
                    imageBuffer[indexBytes++] = (byte)resultArray[i, j];
                    imageBuffer[indexBytes++] = 0;
                    imageBuffer[indexBytes++] = 0;
                    imageBuffer[indexBytes++] = 255;
                }
            }

            unsafe
            {
                fixed(byte *ptr = imageBuffer)
                {
                    using (Bitmap outImage = new Bitmap(g1.Width, g1.Height, g1.Width * 4, PixelFormat.Format32bppPArgb,
                                                        new IntPtr(ptr)))
                    {
                        outImage.Save(filteredImage);
                    }
                }
            }
        }