Esempio n. 1
0
        public void Test_AvgPool1D_1_KerasModel()
        {
            string          path   = @"tests\test_avgpool_1D_1_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 5, 2, 1);

            inp[0, 0, 0, 0] = 0;
            inp[0, 0, 1, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 1, 1, 0] = 1;
            inp[0, 2, 0, 0] = 0;
            inp[0, 2, 1, 0] = 0;
            inp[0, 3, 0, 0] = 2;
            inp[0, 3, 1, 0] = 1;
            inp[0, 4, 0, 0] = 2;
            inp[0, 4, 1, 0] = 1;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 2);
            Assert.AreEqual(ou.GetDimension().w, 3);

            Assert.AreEqual(ou[0, 0, 0, 0], 0.6666666865348816, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], 0.6666666865348816, 0.00001);
            Assert.AreEqual(ou[0, 1, 0, 0], 1.3333333730697632, 0.00001);
            Assert.AreEqual(ou[0, 1, 1, 0], 0.6666666865348816, 0.00001);
            Assert.AreEqual(ou[0, 2, 0, 0], 1.3333333730697632, 0.00001);
            Assert.AreEqual(ou[0, 2, 1, 0], 0.6666666865348816, 0.00001);
        }
Esempio n. 2
0
        public void Test_SoftSign_KerasModel()
        {
            string path   = @"tests\test_softsign_model.json";
            var    reader = new ReaderKerasModel(path);

            SequentialModel model = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 8, 1, 1);

            inp[0, 0, 0, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 2, 0, 0] = -1;
            inp[0, 3, 0, 0] = 0;

            inp[0, 4, 0, 0] = 3;
            inp[0, 5, 0, 0] = 1;
            inp[0, 6, 0, 0] = 1;
            inp[0, 7, 0, 0] = 2;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 4);
            Assert.AreEqual(ou.GetDimension().w, 1);

            Assert.AreEqual(ou[0, 0, 0, 0], -0.5, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], 0.8461538553237915, 0.00001);
            Assert.AreEqual(ou[0, 0, 2, 0], 0.9487179517745972, 0.00001);
            Assert.AreEqual(ou[0, 0, 3, 0], 0.8999999761581421, 0.00001);
        }
Esempio n. 3
0
        public void Test_MaxPool1D_2_KerasModel()
        {
            string          path   = @"tests\test_maxpool_1D_2_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 5, 2, 1);

            inp[0, 0, 0, 0] = 0;
            inp[0, 0, 1, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 1, 1, 0] = 1;
            inp[0, 2, 0, 0] = 0;
            inp[0, 2, 1, 0] = 0;
            inp[0, 3, 0, 0] = 2;
            inp[0, 3, 1, 0] = 1;
            inp[0, 4, 0, 0] = 2;
            inp[0, 4, 1, 0] = 1;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 2);
            Assert.AreEqual(ou.GetDimension().w, 2);

            Assert.AreEqual(ou[0, 0, 0, 0], 2.0, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], 1.0, 0.00001);
            Assert.AreEqual(ou[0, 1, 0, 0], 2.0, 0.00001);
            Assert.AreEqual(ou[0, 1, 1, 0], 1.0, 0.00001);
        }
Esempio n. 4
0
        public void Test_Sigmoid_KerasModel()
        {
            string path   = @"tests\test_sigmoid_model.json";
            var    reader = new ReaderKerasModel(path);

            SequentialModel model = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 8, 1, 1);

            inp[0, 0, 0, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 2, 0, 0] = -1;
            inp[0, 3, 0, 0] = 0;

            inp[0, 4, 0, 0] = 3;
            inp[0, 5, 0, 0] = 1;
            inp[0, 6, 0, 0] = 1;
            inp[0, 7, 0, 0] = 2;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 4);
            Assert.AreEqual(ou.GetDimension().w, 1);

            Assert.AreEqual(ou[0, 0, 0, 0], 0.2689414322376251, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], 0.9959298968315125, 0.00001);
            Assert.AreEqual(ou[0, 0, 2, 0], 1.0, 0.00001);
            Assert.AreEqual(ou[0, 0, 3, 0], 0.9998766183853149, 0.00001);
        }
Esempio n. 5
0
        public void Test_SoftPlus_KerasModel()
        {
            string path   = @"tests\test_softplus_model.json";
            var    reader = new ReaderKerasModel(path);

            SequentialModel model = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 8, 1, 1);

            inp[0, 0, 0, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 2, 0, 0] = -1;
            inp[0, 3, 0, 0] = 0;

            inp[0, 4, 0, 0] = 3;
            inp[0, 5, 0, 0] = 1;
            inp[0, 6, 0, 0] = 1;
            inp[0, 7, 0, 0] = 2;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 4);
            Assert.AreEqual(ou.GetDimension().w, 1);

            Assert.AreEqual(ou[0, 0, 0, 0], 0.31326162815093994, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], 5.504078388214111, 0.00001);
            Assert.AreEqual(ou[0, 0, 2, 0], 18.5, 0.00001);
            Assert.AreEqual(ou[0, 0, 3, 0], 9.000123023986816, 0.00001);
        }
        public double[] Evaluate(Bitmap img)
        {
            if (model == null)
            {
                var reader = new ReaderKerasModel(cnn_nn);
                model = reader.GetSequentialExecutor();
            }

            var array = new Data2D(28, 28, 1, 1);

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    Color  pixel = img.GetPixel(j, i);
                    double value = 255 - pixel.A;
                    value = value / 255;

                    array[i, j, 0, 0] = value;
                }
            }

            var result = model.ExecuteNetwork(array) as Data2D;

            double[] toreturn = new double[10];

            for (int i = 0; i < 10; i++)
            {
                toreturn[i] = result[0, 0, i, 0];
            }

            return(toreturn);
        }
Esempio n. 7
0
        public void Test_ReLu_KerasModel()
        {
            string path   = @"tests\test_relu_model.json";
            var    reader = new ReaderKerasModel(path);

            SequentialModel model = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 8, 1, 1);

            inp[0, 0, 0, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 2, 0, 0] = -1;
            inp[0, 3, 0, 0] = 0;

            inp[0, 4, 0, 0] = 3;
            inp[0, 5, 0, 0] = 1;
            inp[0, 6, 0, 0] = 1;
            inp[0, 7, 0, 0] = 2;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 4);
            Assert.AreEqual(ou.GetDimension().w, 1);

            Assert.AreEqual(ou[0, 0, 0, 0], 0.0, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], 5.5, 0.00001);
            Assert.AreEqual(ou[0, 0, 2, 0], 18.5, 0.00001);
            Assert.AreEqual(ou[0, 0, 3, 0], 9.0, 0.00001);
        }
Esempio n. 8
0
        public void Test_TanH_KerasModel()
        {
            string path   = @"tests\test_tanh_model.json";
            var    reader = new ReaderKerasModel(path);

            SequentialModel model = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 8, 1, 1);

            inp[0, 0, 0, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 2, 0, 0] = -1;
            inp[0, 3, 0, 0] = 0;

            inp[0, 4, 0, 0] = 3;
            inp[0, 5, 0, 0] = 1;
            inp[0, 6, 0, 0] = 1;
            inp[0, 7, 0, 0] = 2;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 4);
            Assert.AreEqual(ou.GetDimension().w, 1);

            Assert.AreEqual(ou[0, 0, 0, 0], -0.7615941762924194, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], 0.9999667406082153, 0.00001);
            Assert.AreEqual(ou[0, 0, 2, 0], 1.0, 0.00001);
            Assert.AreEqual(ou[0, 0, 3, 0], 1.0, 0.00001);
        }
Esempio n. 9
0
        public void Test_Softmax_KerasModel()
        {
            string path   = @"tests\test_softmax_model.json";
            var    reader = new ReaderKerasModel(path);

            SequentialModel model = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 8, 1, 1);

            inp[0, 0, 0, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 2, 0, 0] = -1;
            inp[0, 3, 0, 0] = 0;

            inp[0, 4, 0, 0] = 3;
            inp[0, 5, 0, 0] = 1;
            inp[0, 6, 0, 0] = 1;
            inp[0, 7, 0, 0] = 2;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 4);
            Assert.AreEqual(ou.GetDimension().w, 1);

            Assert.AreEqual(ou[0, 0, 0, 0], 3.3980058766758248e-09, 1e-10);
            Assert.AreEqual(ou[0, 0, 1, 0], 2.26015504267707e-06, 1e-7);
            Assert.AreEqual(ou[0, 0, 2, 0], 0.9999228715896606, 0.00001);
            Assert.AreEqual(ou[0, 0, 3, 0], 7.484605885110795e-05, 1e-6);
        }
Esempio n. 10
0
        public void Test_RepeatVector_KerasModel()
        {
            string          path   = @"tests\test_repeatvector_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D data = new Data2D(1, 1, 4, 2);

            for (int c = 0; c < 4; ++c)
            {
                data[0, 0, c, 0] = c + 1;
                data[0, 0, c, 1] = -(c + 1);
            }


            Data2D output = model.ExecuteNetwork(data) as Data2D;

            // Checking sizes
            Dimension dim = output.GetDimension();

            Assert.AreEqual(dim.b, 2);
            Assert.AreEqual(dim.c, 4);
            Assert.AreEqual(dim.h, 1);
            Assert.AreEqual(dim.w, 3);

            // Checking calculation
            Assert.AreEqual(output[0, 0, 0, 0], 1, 0.0000001);
            Assert.AreEqual(output[0, 0, 1, 0], 2, 0.0000001);
            Assert.AreEqual(output[0, 0, 2, 0], 3, 0.0000001);
            Assert.AreEqual(output[0, 0, 3, 0], 4, 0.0000001);

            Assert.AreEqual(output[0, 1, 0, 0], 1, 0.0000001);
            Assert.AreEqual(output[0, 1, 1, 0], 2, 0.0000001);
            Assert.AreEqual(output[0, 1, 2, 0], 3, 0.0000001);
            Assert.AreEqual(output[0, 1, 3, 0], 4, 0.0000001);

            Assert.AreEqual(output[0, 2, 0, 0], 1, 0.0000001);
            Assert.AreEqual(output[0, 2, 1, 0], 2, 0.0000001);
            Assert.AreEqual(output[0, 2, 2, 0], 3, 0.0000001);
            Assert.AreEqual(output[0, 2, 3, 0], 4, 0.0000001);

            Assert.AreEqual(output[0, 0, 0, 1], -1, 0.0000001);
            Assert.AreEqual(output[0, 0, 1, 1], -2, 0.0000001);
            Assert.AreEqual(output[0, 0, 2, 1], -3, 0.0000001);
            Assert.AreEqual(output[0, 0, 3, 1], -4, 0.0000001);

            Assert.AreEqual(output[0, 1, 0, 1], -1, 0.0000001);
            Assert.AreEqual(output[0, 1, 1, 1], -2, 0.0000001);
            Assert.AreEqual(output[0, 1, 2, 1], -3, 0.0000001);
            Assert.AreEqual(output[0, 1, 3, 1], -4, 0.0000001);

            Assert.AreEqual(output[0, 2, 0, 1], -1, 0.0000001);
            Assert.AreEqual(output[0, 2, 1, 1], -2, 0.0000001);
            Assert.AreEqual(output[0, 2, 2, 1], -3, 0.0000001);
            Assert.AreEqual(output[0, 2, 3, 1], -4, 0.0000001);
        }
Esempio n. 11
0
        public void Test_Conv1D_2_KerasModel()
        {
            string          path   = @"tests\test_conv_1D_2_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 6, 4, 1);

            inp[0, 0, 0, 0] = 0;
            inp[0, 0, 1, 0] = 1;
            inp[0, 0, 2, 0] = 2;
            inp[0, 0, 3, 0] = 1.5;

            inp[0, 1, 0, 0] = 1;
            inp[0, 1, 1, 0] = 0;
            inp[0, 1, 2, 0] = 0;
            inp[0, 1, 3, 0] = 0.6;

            inp[0, 2, 0, 0] = 2;
            inp[0, 2, 1, 0] = 1;
            inp[0, 2, 2, 0] = 2;
            inp[0, 2, 3, 0] = 2.5;

            inp[0, 3, 0, 0] = 1;
            inp[0, 3, 1, 0] = 0;
            inp[0, 3, 2, 0] = -1;
            inp[0, 3, 3, 0] = 0;

            inp[0, 4, 0, 0] = 1;
            inp[0, 4, 1, 0] = -2;
            inp[0, 4, 2, 0] = 3;
            inp[0, 4, 3, 0] = 3.5;

            inp[0, 5, 0, 0] = 2;
            inp[0, 5, 1, 0] = 1;
            inp[0, 5, 2, 0] = 4;
            inp[0, 5, 3, 0] = 3.5;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 3);
            Assert.AreEqual(ou.GetDimension().w, 3);

            Assert.AreEqual(ou[0, 0, 0, 0], 9.399999618530273, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], -1.6999998092651367, 0.00001);
            Assert.AreEqual(ou[0, 0, 2, 0], 4.550000190734863, 0.00001);

            Assert.AreEqual(ou[0, 1, 0, 0], 8.5, 0.00001);
            Assert.AreEqual(ou[0, 1, 1, 0], -4.0, 0.00001);
            Assert.AreEqual(ou[0, 1, 2, 0], 12.25, 0.00001);

            Assert.AreEqual(ou[0, 2, 0, 0], 23.0, 0.00001);
            Assert.AreEqual(ou[0, 2, 1, 0], 7.5, 0.00001);
            Assert.AreEqual(ou[0, 2, 2, 0], 14.5, 0.00001);
        }
Esempio n. 12
0
        public void Test_SimpleRNN_KerasModel()
        {
            string          path   = @"tests\test_simplernn_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            // Initialize data.
            Data2D data = new Data2D(1, 3, 3, 4);

            int l = 0;

            for (int b = 0; b < 4; ++b)
            {
                for (int w = 0; w < 3; ++w)
                {
                    for (int c = 0; c < 3; ++c)
                    {
                        l += 1;
                        data[0, w, c, b] = l % 5 + 1;
                    }
                }
            }

            Data2D output = model.ExecuteNetwork(data) as Data2D;

            // Checking sizes
            Dimension dim = output.GetDimension();

            Assert.AreEqual(dim.b, 4);
            Assert.AreEqual(dim.c, 4);
            Assert.AreEqual(dim.h, 1);
            Assert.AreEqual(dim.w, 1);

            // Checking calculation
            Assert.AreEqual(output[0, 0, 0, 0], -54, 0.000001);
            Assert.AreEqual(output[0, 0, 1, 0], -39, 0.000001);
            Assert.AreEqual(output[0, 0, 2, 0], 36, 0.000001);
            Assert.AreEqual(output[0, 0, 3, 0], 72, 0.000001);

            Assert.AreEqual(output[0, 0, 0, 1], 12, 0.000001);
            Assert.AreEqual(output[0, 0, 1, 1], -19, 0.000001);
            Assert.AreEqual(output[0, 0, 2, 1], -10, 0.000001);
            Assert.AreEqual(output[0, 0, 3, 1], 10, 0.000001);

            Assert.AreEqual(output[0, 0, 0, 2], -72, 0.000001);
            Assert.AreEqual(output[0, 0, 1, 2], 16, 0.000001);
            Assert.AreEqual(output[0, 0, 2, 2], 74, 0.000001);
            Assert.AreEqual(output[0, 0, 3, 2], 68, 0.000001);

            Assert.AreEqual(output[0, 0, 0, 3], -161, 0.000001);
            Assert.AreEqual(output[0, 0, 1, 3], -14, 0.000001);
            Assert.AreEqual(output[0, 0, 2, 3], 158, 0.000001);
            Assert.AreEqual(output[0, 0, 3, 3], 141, 0.000001);
        }
Esempio n. 13
0
        public void Test_Reshape2D_KerasModel()
        {
            string          path   = @"tests\test_reshape_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D data = new Data2D(3, 3, 2, 1);

            int l = 0;

            for (int h = 0; h < 3; ++h)
            {
                for (int w = 0; w < 3; ++w)
                {
                    for (int c = 0; c < 2; ++c)
                    {
                        l += 1;
                        data[h, w, c, 0] = l + 1;
                    }
                }
            }

            Data2D output = model.ExecuteNetwork(data) as Data2D;

            // Checking sizes
            Dimension dim = output.GetDimension();

            Assert.AreEqual(dim.b, 1);
            Assert.AreEqual(dim.c, 3);
            Assert.AreEqual(dim.h, 3);
            Assert.AreEqual(dim.w, 2);

            // Checking calculation
            Assert.AreEqual(output[0, 0, 0, 0], 2, 0.0000001);
            Assert.AreEqual(output[0, 0, 1, 0], 3, 0.0000001);
            Assert.AreEqual(output[0, 0, 2, 0], 4, 0.0000001);
            Assert.AreEqual(output[0, 1, 0, 0], 5, 0.0000001);
            Assert.AreEqual(output[0, 1, 1, 0], 6, 0.0000001);
            Assert.AreEqual(output[0, 1, 2, 0], 7, 0.0000001);

            Assert.AreEqual(output[1, 0, 0, 0], 8, 0.0000001);
            Assert.AreEqual(output[1, 0, 1, 0], 9, 0.0000001);
            Assert.AreEqual(output[1, 0, 2, 0], 10, 0.0000001);
            Assert.AreEqual(output[1, 1, 0, 0], 11, 0.0000001);
            Assert.AreEqual(output[1, 1, 1, 0], 12, 0.0000001);
            Assert.AreEqual(output[1, 1, 2, 0], 13, 0.0000001);

            Assert.AreEqual(output[2, 0, 0, 0], 14, 0.0000001);
            Assert.AreEqual(output[2, 0, 1, 0], 15, 0.0000001);
            Assert.AreEqual(output[2, 0, 2, 0], 16, 0.0000001);
            Assert.AreEqual(output[2, 1, 0, 0], 17, 0.0000001);
            Assert.AreEqual(output[2, 1, 1, 0], 18, 0.0000001);
            Assert.AreEqual(output[2, 1, 2, 0], 19, 0.0000001);
        }
Esempio n. 14
0
        public void Test_LSTM_KerasModel()
        {
            string          path   = @"tests\test_lstm_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            // Initialize data.
            Data2D data = new Data2D(1, 3, 3, 5);

            int l = 0;

            for (int b = 0; b < 5; ++b)
            {
                for (int w = 0; w < 3; ++w)
                {
                    for (int c = 0; c < 3; ++c)
                    {
                        l += 1;
                        data[0, w, c, b] = (l % 5 + 1) / 10.0;
                    }
                }
            }

            Data2D output = model.ExecuteNetwork(data) as Data2D;

            // Checking sizes
            Dimension dim = output.GetDimension();

            Assert.AreEqual(dim.b, 5);
            Assert.AreEqual(dim.c, 2);
            Assert.AreEqual(dim.h, 1);
            Assert.AreEqual(dim.w, 1);

            // Checking calculation
            Assert.AreEqual(output[0, 0, 0, 0], 0.015777, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 0], 0.0, 0.00001);

            Assert.AreEqual(output[0, 0, 0, 1], 0.01605, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 1], 0.0, 0.00001);

            Assert.AreEqual(output[0, 0, 0, 2], 0.016398, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 2], 0.0, 0.00001);

            Assert.AreEqual(output[0, 0, 0, 3], 0.006314, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 3], 0.0, 0.00001);

            Assert.AreEqual(output[0, 0, 0, 4], 0.016303, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 4], 0.0, 0.00001);
        }
Esempio n. 15
0
        public void Test_GRU_KerasModel()
        {
            string          path   = @"tests\test_gru_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            // Initialize data.
            Data2D data = new Data2D(1, 3, 3, 5);

            int l = 0;

            for (int b = 0; b < 5; ++b)
            {
                for (int w = 0; w < 3; ++w)
                {
                    for (int c = 0; c < 3; ++c)
                    {
                        l += 1;
                        data[0, w, c, b] = (l % 5 + 1) / 10.0;
                    }
                }
            }

            Data2D output = model.ExecuteNetwork(data) as Data2D;

            // Checking sizes
            Dimension dim = output.GetDimension();

            Assert.AreEqual(dim.b, 5);
            Assert.AreEqual(dim.c, 2);
            Assert.AreEqual(dim.h, 1);
            Assert.AreEqual(dim.w, 1);

            // Checking calculation
            Assert.AreEqual(output[0, 0, 0, 0], 0.19632, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 0], 0.37259, 0.00001);

            Assert.AreEqual(output[0, 0, 0, 1], 0.21991, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 1], 0.37473, 0.00001);

            Assert.AreEqual(output[0, 0, 0, 2], 0.24834, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 2], 0.38176, 0.00001);

            Assert.AreEqual(output[0, 0, 0, 3], 0.18727, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 3], 0.35267, 0.00001);

            Assert.AreEqual(output[0, 0, 0, 4], 0.166619, 0.00001);
            Assert.AreEqual(output[0, 0, 1, 4], 0.35275, 0.00001);
        }
Esempio n. 16
0
        public static void Sample1(string filePath)
        {
            // Read the previously created json.
            var             reader = new ReaderKerasModel(filePath);
            SequentialModel model  = reader.GetSequentialExecutor();

            // Then create the data to run the executer on.
            // batch: should be set in the Keras model.
            int    height  = 0;
            int    width   = 0;
            int    channel = 0;
            int    batch   = 0;
            Data2D input   = new Data2D(height, width, channel, batch);

            // Calculate the network's output.
            IData output = model.ExecuteNetwork(input);
        }
Esempio n. 17
0
        static public void KerasModelTest(string pathIn, string pathModel, string pathOut, double accuracy = 0.00001)
        {
            var             reader = new ReaderKerasModel(pathModel);
            SequentialModel model  = reader.GetSequentialExecutor();

            // Initialize data.
            Data2D data = Utils.ReadDataFromFile(pathIn);

            // Load expected output and calculate the actual output.
            Data2D expected = Utils.ReadDataFromFile(pathOut);
            Data2D output   = model.ExecuteNetwork(data) as Data2D;

            // Checking sizes
            Utils.CheckDimensions(output, expected);

            // Checking calculation
            Utils.CheckResults(output, expected, accuracy);
        }
Esempio n. 18
0
        public IData Run()
        {
            var reader = new ReaderKerasModel(xor_path);
            var model  = reader.GetSequentialExecutor();

            var arr = new Data2D(1, 1, 2, 4);

            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    arr[0, 0, j, i] = j == 0 ? i / 2 : i % 2;
                }
            }

            Data2D result = model.ExecuteNetwork(arr) as Data2D;

            return(result);
        }
Esempio n. 19
0
        public void Test_GlobalMaxPool2D_KerasModel()
        {
            string          path   = @"tests\test_globalmaxpool_2D_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(3, 3, 2, 1);

            inp[0, 0, 0, 0] = 1;
            inp[1, 0, 0, 0] = 2;
            inp[2, 0, 0, 0] = 0;

            inp[0, 1, 0, 0] = 3;
            inp[1, 1, 0, 0] = 4;
            inp[2, 1, 0, 0] = 0;

            inp[0, 2, 0, 0] = 2;
            inp[1, 2, 0, 0] = 2;
            inp[2, 2, 0, 0] = 0;


            inp[0, 0, 1, 0] = 0;
            inp[1, 0, 1, 0] = 3;
            inp[2, 0, 1, 0] = 1;

            inp[0, 1, 1, 0] = 1;
            inp[1, 1, 1, 0] = 1;
            inp[2, 1, 1, 0] = -1;

            inp[0, 2, 1, 0] = -3;
            inp[1, 2, 1, 0] = -1;
            inp[2, 2, 1, 0] = 0;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 2);
            Assert.AreEqual(ou.GetDimension().w, 1);

            Assert.AreEqual(ou[0, 0, 0, 0], 4.0, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], 3.0, 0.00001);
        }
Esempio n. 20
0
        public void Test_GlobalAvgPool1D_KerasModel()
        {
            string          path   = @"tests\test_globalavgpool_1D_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 3, 2, 1);

            inp[0, 0, 0, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 2, 0, 0] = 0;

            inp[0, 0, 1, 0] = 3;
            inp[0, 1, 1, 0] = 4;
            inp[0, 2, 1, 0] = 0;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 2);
            Assert.AreEqual(ou.GetDimension().w, 1);

            Assert.AreEqual(ou[0, 0, 0, 0], 1.0, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], 2.3333332538604736, 0.00001);
        }
Esempio n. 21
0
        static void Main(string[] args)
        {
            var reader = new ReaderKerasModel(cnn_nn);
            var model  = reader.GetSequentialExecutor();

            var array = new Data2D(28, 28, 1, 1);

            Bitmap img = new Bitmap(zero_test_path);

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    Color  pixel = img.GetPixel(j, i);
                    double value = (pixel.R + pixel.G + pixel.B) / 3;
                    value = value / 255;

                    array[i, j, 0, 0] = value;
                }
            }

            var result = model.ExecuteNetwork(array);
            int a      = 5;
        }
Esempio n. 22
0
        public void Test_Cropping2D_KerasModel()
        {
            string          path   = @"tests\test_crop_2D_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(4, 5, 2, 1);

            int l = 0;

            for (int h = 0; h < 4; ++h)
            {
                for (int w = 0; w < 5; ++w)
                {
                    l += 1;
                    inp[h, w, 0, 0] = l + 1;
                    inp[h, w, 1, 0] = -(l + 1);
                }
            }

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 2);
            Assert.AreEqual(ou.GetDimension().w, 2);
            Assert.AreEqual(ou.GetDimension().h, 2);

            Assert.AreEqual(ou[0, 0, 0, 0], 8.0, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], -8.0, 0.00001);
            Assert.AreEqual(ou[0, 1, 0, 0], 9.0, 0.00001);
            Assert.AreEqual(ou[0, 1, 1, 0], -9.0, 0.00001);

            Assert.AreEqual(ou[1, 0, 0, 0], 13.0, 0.00001);
            Assert.AreEqual(ou[1, 0, 1, 0], -13.0, 0.00001);
            Assert.AreEqual(ou[1, 1, 0, 0], 14.0, 0.00001);
            Assert.AreEqual(ou[1, 1, 1, 0], -14.0, 0.00001);
        }
Esempio n. 23
0
        public void Test_Cropping1D_KerasModel()
        {
            string          path   = @"tests\test_crop_1D_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(1, 5, 2, 1);

            for (int l = 0; l < 5; ++l)
            {
                inp[0, l, 0, 0] = l + 1;
                inp[0, l, 1, 0] = -(l + 1);
            }

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 2);
            Assert.AreEqual(ou.GetDimension().w, 2);

            Assert.AreEqual(ou[0, 0, 0, 0], 2.0, 0.00001);
            Assert.AreEqual(ou[0, 0, 1, 0], -2.0, 0.00001);
            Assert.AreEqual(ou[0, 1, 0, 0], 3.0, 0.00001);
            Assert.AreEqual(ou[0, 1, 1, 0], -3.0, 0.00001);
        }
Esempio n. 24
0
        public void Test_Conv2D_2_KerasModel()
        {
            string          path   = @"tests\test_conv_2D_2_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(4, 5, 2, 1);

            inp[0, 0, 0, 0] = 0;
            inp[0, 0, 1, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 1, 1, 0] = 1;
            inp[0, 2, 0, 0] = 0;
            inp[0, 2, 1, 0] = 0;
            inp[0, 3, 0, 0] = 2;
            inp[0, 3, 1, 0] = 1;
            inp[0, 4, 0, 0] = 2;
            inp[0, 4, 1, 0] = 1;


            inp[1, 0, 0, 0] = 0;
            inp[1, 0, 1, 0] = -1;
            inp[1, 1, 0, 0] = 1;
            inp[1, 1, 1, 0] = -2;
            inp[1, 2, 0, 0] = 3;
            inp[1, 2, 1, 0] = 1;
            inp[1, 3, 0, 0] = 2;
            inp[1, 3, 1, 0] = 0;
            inp[1, 4, 0, 0] = 2;
            inp[1, 4, 1, 0] = -3;


            inp[2, 0, 0, 0] = 1;
            inp[2, 0, 1, 0] = 2;
            inp[2, 1, 0, 0] = -2;
            inp[2, 1, 1, 0] = 0;
            inp[2, 2, 0, 0] = 3;
            inp[2, 2, 1, 0] = -3;
            inp[2, 3, 0, 0] = 2;
            inp[2, 3, 1, 0] = 1;
            inp[2, 4, 0, 0] = 2;
            inp[2, 4, 1, 0] = 0;


            inp[3, 0, 0, 0] = 1;
            inp[3, 0, 1, 0] = 2;
            inp[3, 1, 0, 0] = 0;
            inp[3, 1, 1, 0] = -2;
            inp[3, 2, 0, 0] = 3;
            inp[3, 2, 1, 0] = 1;
            inp[3, 3, 0, 0] = 2;
            inp[3, 3, 1, 0] = 3;
            inp[3, 4, 0, 0] = -3;
            inp[3, 4, 1, 0] = 1;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 2);
            Assert.AreEqual(ou.GetDimension().w, 2);
            Assert.AreEqual(ou.GetDimension().h, 2);

            Assert.AreEqual(ou[0, 0, 0, 0], 2.0, 0.0001);
            Assert.AreEqual(ou[0, 0, 1, 0], 1.0, 0.0001);
            Assert.AreEqual(ou[0, 1, 0, 0], 7.5, 0.0001);
            Assert.AreEqual(ou[0, 1, 1, 0], 4.0, 0.0001);

            Assert.AreEqual(ou[1, 0, 0, 0], 19.5, 0.0001);
            Assert.AreEqual(ou[1, 0, 1, 0], 20.5, 0.0001);
            Assert.AreEqual(ou[1, 1, 0, 0], -19.5, 0.0001);
            Assert.AreEqual(ou[1, 1, 1, 0], -0.5, 0.0001);
        }
Esempio n. 25
0
        public void Test_AvgPool2D_1_KerasModel()
        {
            string          path   = @"tests\test_avgpool_2D_1_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D inp = new Data2D(4, 5, 2, 1);

            inp[0, 0, 0, 0] = 0;
            inp[0, 0, 1, 0] = 1;
            inp[0, 1, 0, 0] = 2;
            inp[0, 1, 1, 0] = 1;
            inp[0, 2, 0, 0] = 0;
            inp[0, 2, 1, 0] = 0;
            inp[0, 3, 0, 0] = 2;
            inp[0, 3, 1, 0] = 1;
            inp[0, 4, 0, 0] = 2;
            inp[0, 4, 1, 0] = 1;


            inp[1, 0, 0, 0] = 0;
            inp[1, 0, 1, 0] = -1;
            inp[1, 1, 0, 0] = 1;
            inp[1, 1, 1, 0] = -2;
            inp[1, 2, 0, 0] = 3;
            inp[1, 2, 1, 0] = 1;
            inp[1, 3, 0, 0] = 2;
            inp[1, 3, 1, 0] = 0;
            inp[1, 4, 0, 0] = 2;
            inp[1, 4, 1, 0] = -3;


            inp[2, 0, 0, 0] = 1;
            inp[2, 0, 1, 0] = 2;
            inp[2, 1, 0, 0] = -2;
            inp[2, 1, 1, 0] = 0;
            inp[2, 2, 0, 0] = 3;
            inp[2, 2, 1, 0] = -3;
            inp[2, 3, 0, 0] = 2;
            inp[2, 3, 1, 0] = 1;
            inp[2, 4, 0, 0] = 2;
            inp[2, 4, 1, 0] = 0;


            inp[3, 0, 0, 0] = 1;
            inp[3, 0, 1, 0] = 2;
            inp[3, 1, 0, 0] = 0;
            inp[3, 1, 1, 0] = -2;
            inp[3, 2, 0, 0] = 3;
            inp[3, 2, 1, 0] = 1;
            inp[3, 3, 0, 0] = 2;
            inp[3, 3, 1, 0] = 3;
            inp[3, 4, 0, 0] = -3;
            inp[3, 4, 1, 0] = 1;

            Data2D ou = model.ExecuteNetwork(inp) as Data2D;

            Assert.AreEqual(ou.GetDimension().c, 2);
            Assert.AreEqual(ou.GetDimension().w, 2);
            Assert.AreEqual(ou.GetDimension().h, 2);

            Assert.AreEqual(ou[0, 0, 0, 0], 1.1666666269302368, 0.0001);
            Assert.AreEqual(ou[0, 0, 1, 0], 0.0833333358168602, 0.0001);
            Assert.AreEqual(ou[0, 1, 0, 0], 1.5833333730697632, 0.0001);
            Assert.AreEqual(ou[0, 1, 1, 0], -0.25, 0.0001);

            Assert.AreEqual(ou[1, 0, 0, 0], 1.3333333730697632, 0.0001);
            Assert.AreEqual(ou[1, 0, 1, 0], 0.1666666716337204, 0.0001);
            Assert.AreEqual(ou[1, 1, 0, 0], 1.25, 0.0001);
            Assert.AreEqual(ou[1, 1, 1, 0], -0.25, 0.0001);
        }
Esempio n. 26
0
        public void Test_Permute_KerasModel()
        {
            string          path   = @"tests\test_permute_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D data = new Data2D(2, 3, 4, 1);

            int cntr = 0;

            for (int h = 0; h < 2; ++h)
            {
                for (int w = 0; w < 3; ++w)
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        cntr            += 1;
                        data[h, w, c, 0] = cntr + 1;
                    }
                }
            }

            Data2D output = model.ExecuteNetwork(data) as Data2D;

            // Checking sizes
            Dimension dim = output.GetDimension();

            Assert.AreEqual(dim.b, 1);
            Assert.AreEqual(dim.c, 3);
            Assert.AreEqual(dim.h, 4);
            Assert.AreEqual(dim.w, 2);

            // Checking calculation
            Assert.AreEqual(output[0, 0, 0, 0], 2.0, 0.0000001);
            Assert.AreEqual(output[0, 0, 1, 0], 6.0, 0.0000001);
            Assert.AreEqual(output[0, 0, 2, 0], 10.0, 0.0000001);
            Assert.AreEqual(output[0, 1, 0, 0], 14.0, 0.0000001);
            Assert.AreEqual(output[0, 1, 1, 0], 18.0, 0.0000001);
            Assert.AreEqual(output[0, 1, 2, 0], 22.0, 0.0000001);

            Assert.AreEqual(output[1, 0, 0, 0], 3.0, 0.0000001);
            Assert.AreEqual(output[1, 0, 1, 0], 7.0, 0.0000001);
            Assert.AreEqual(output[1, 0, 2, 0], 11.0, 0.0000001);
            Assert.AreEqual(output[1, 1, 0, 0], 15.0, 0.0000001);
            Assert.AreEqual(output[1, 1, 1, 0], 19.0, 0.0000001);
            Assert.AreEqual(output[1, 1, 2, 0], 23.0, 0.0000001);

            Assert.AreEqual(output[2, 0, 0, 0], 4.0, 0.0000001);
            Assert.AreEqual(output[2, 0, 1, 0], 8.0, 0.0000001);
            Assert.AreEqual(output[2, 0, 2, 0], 12.0, 0.0000001);
            Assert.AreEqual(output[2, 1, 0, 0], 16.0, 0.0000001);
            Assert.AreEqual(output[2, 1, 1, 0], 20.0, 0.0000001);
            Assert.AreEqual(output[2, 1, 2, 0], 24.0, 0.0000001);

            Assert.AreEqual(output[3, 0, 0, 0], 5.0, 0.0000001);
            Assert.AreEqual(output[3, 0, 1, 0], 9.0, 0.0000001);
            Assert.AreEqual(output[3, 0, 2, 0], 13.0, 0.0000001);
            Assert.AreEqual(output[3, 1, 0, 0], 17.0, 0.0000001);
            Assert.AreEqual(output[3, 1, 1, 0], 21.0, 0.0000001);
            Assert.AreEqual(output[3, 1, 2, 0], 25.0, 0.0000001);
        }
Esempio n. 27
0
        public void Test_BatchNorm_KerasModel()
        {
            string          path   = @"tests\test_batchnorm_model.json";
            var             reader = new ReaderKerasModel(path);
            SequentialModel model  = reader.GetSequentialExecutor();

            Data2D data = new Data2D(2, 1, 3, 4);

            int l = 0;

            for (int b = 0; b < 4; ++b)
            {
                for (int h = 0; h < 2; ++h)
                {
                    for (int w = 0; w < 1; ++w)
                    {
                        for (int c = 0; c < 3; ++c)
                        {
                            l += 1;
                            data[h, w, c, b] = l % 7 - 3;
                        }
                    }
                }
            }

            Data2D output = model.ExecuteNetwork(data) as Data2D;

            Assert.AreEqual(output.GetDimension().b, 4);
            Assert.AreEqual(output.GetDimension().c, 3);
            Assert.AreEqual(output.GetDimension().w, 1);
            Assert.AreEqual(output.GetDimension().h, 2);

            // Checking calculation
            Assert.AreEqual(output[0, 0, 0, 0], -4.3660264, 0.001);
            Assert.AreEqual(output[0, 0, 1, 0], -2.02451992, 0.001);
            Assert.AreEqual(output[0, 0, 2, 0], -3.6830132, 0.001);
            Assert.AreEqual(output[1, 0, 0, 0], -0.3415066, 0.001);
            Assert.AreEqual(output[1, 0, 1, 0], 2.0, 0.001);
            Assert.AreEqual(output[1, 0, 2, 0], 0.34150672, 0.001);

            Assert.AreEqual(output[0, 0, 0, 1], -5.70753288, 0.001);
            Assert.AreEqual(output[0, 0, 1, 1], -3.3660264, 0.001);
            Assert.AreEqual(output[0, 0, 2, 1], -5.02451992, 0.001);
            Assert.AreEqual(output[1, 0, 0, 1], -1.6830132, 0.001);
            Assert.AreEqual(output[1, 0, 1, 1], 0.6584934, 0.001);
            Assert.AreEqual(output[1, 0, 2, 1], -1.0, 0.001);

            Assert.AreEqual(output[0, 0, 0, 2], 2.34150672, 0.001);
            Assert.AreEqual(output[0, 0, 1, 2], -4.70753288, 0.001);
            Assert.AreEqual(output[0, 0, 2, 2], -6.3660264, 0.001);
            Assert.AreEqual(output[1, 0, 0, 2], -3.02451992, 0.001);
            Assert.AreEqual(output[1, 0, 1, 2], -0.6830132, 0.001);
            Assert.AreEqual(output[1, 0, 2, 2], -2.34150648, 0.001);

            Assert.AreEqual(output[0, 0, 0, 3], 1.0, 0.001);
            Assert.AreEqual(output[0, 0, 1, 3], 3.34150672, 0.001);
            Assert.AreEqual(output[0, 0, 2, 3], -7.70753288, 0.001);
            Assert.AreEqual(output[1, 0, 0, 3], -4.3660264, 0.001);
            Assert.AreEqual(output[1, 0, 1, 3], -2.02451992, 0.001);
            Assert.AreEqual(output[1, 0, 2, 3], -3.6830132, 0.001);
        }