예제 #1
0
        static void Evaluate(Func<Tuple<INetwork, IFactory>> GetNetwork, bool verbose)
        {
            var StartTimingLayer = new TimingLayer() {StartCounters = new string[] { "Prediction-Time" } };
            var StopTimingLayer = new TimingLayer() {StopCounters = new string[] { "Prediction-Time" } };
            IInputLayer ReaderLayer = null;
            var NetworkAndFactory = GetNetwork();
            var Network = NetworkAndFactory.Item1;
            var Factory = NetworkAndFactory.Item2;
            {
                var p = Network;
                while (!(p.GetSource() is EncryptLayer)) p = p.GetSource();

                StartTimingLayer.Source = p.GetSource();
                var b = p as BaseLayer;
                b.Source = StartTimingLayer;

                // find the reader
                while (p.GetSource() != null) p = p.GetSource();
                ReaderLayer = p as IInputLayer;

                // stop the timing counters after computing the entire network
                StopTimingLayer.Source = Network;
                Network = StopTimingLayer;
                p = Network;
                while (p != null)
                {
                    p.Factory = Factory;
                    if (p is BaseLayer bas) bas.Verbose = verbose;
                    p = p.GetSource();
                }

                Network.PrepareNetwork();
            }
            int errs = 0;
            for (int i = 0; i < 10000; i++)
            {
                using (var m = Network.GetNext())
                {
                    var l = ReaderLayer.Labels[0];
                    int pred = 0;
                    Utils.ProcessInEnv(env =>
                    {
                        var dec = m.Decrypt(env);
                        for (int j = 0; j < 10; j++)
                            if (dec[j, 0] > dec[pred, 0]) pred = j;
                        if (pred != l) errs++;
                    }, Factory);

                    Console.WriteLine("errs {0}/{1} accuracy {2:0.000}% {3} prediction {4} label {5}", errs, i + 1, 100 - (100.0 * errs / (i + 1)), TimingLayer.GetStats(), pred, l);
                }
            }
            Network.DisposeNetwork();
        }
예제 #2
0
        public static void Main(string[] args)
        {
            var options = new Options();
            var parsed  = Parser.Default.ParseArguments <Options>(args).WithParsed(x => options = x);

            if (parsed.Tag == ParserResultType.NotParsed)
            {
                Environment.Exit(-2);
            }

            WeightsReader wr = new WeightsReader("CifarWeight.csv", "CifarBias.csv");

            // Model has accuracy of 76.5
            // Current parameters (scale) provide accuracy of 76.31% and uses 78.55 + 1 bits in message length
            // It has a latency of 740 seconds on the reference machine (Azure B8ms server at rest)

            Console.WriteLine("Generating encryption keys {0}", DateTime.Now);
            IFactory factory = null;

            if (options.Encrypt)
            {
                factory = new EncryptedSealBfvFactory(new ulong[] { 957181001729, 957181034497 }, 16384, DecompositionBitCount: 60, GaloisDecompositionBitCount: 60, SmallModulusCount: 8);
            }
            else
            {
                factory = new RawFactory(16 * 1024);
            }
            Console.WriteLine("Encryption keys ready {0}", DateTime.Now);
            int  numberOfRecords = 10000;
            bool verbose         = options.Verbose;

            string fileName    = "cifar-test.tsv";
            var    readerLayer = new LLConvReader
            {
                FileName            = fileName,
                SparseFormat        = false,
                InputShape          = new int[] { 3, 32, 32 },
                KernelShape         = new int[] { 3, 8, 8 },
                Upperpadding        = new int[] { 0, 1, 1 },
                Lowerpadding        = new int[] { 0, 1, 1 },
                Stride              = new int[] { 1000, 2, 2 },
                NormalizationFactor = 1.0 / 256.0,
                Scale   = 8,
                Verbose = verbose
            };


            var EncryptLayer = new EncryptLayer()
            {
                Source = readerLayer, Factory = factory
            };
            var StartTimingLayer = new TimingLayer()
            {
                Source = EncryptLayer, StartCounters = new string[] { "Inference-Time" }
            };


            var ConvLayer1 = new LLPoolLayer()
            {
                Source       = StartTimingLayer,
                InputShape   = new int[] { 3, 32, 32 },
                KernelShape  = new int[] { 3, 8, 8 },
                Upperpadding = new int[] { 0, 1, 1 },
                Lowerpadding = new int[] { 0, 1, 1 },
                Stride       = new int[] { 1000, 2, 2 },
                MapCount     = new int[] { 83, 1, 1 },
                WeightsScale = 256.0,
                Weights      = (double[])wr.Weights[0],
                Bias         = (double[])wr.Biases[0],
                Verbose      = verbose
            };

            var VectorizeLayer2 = new LLVectorizeLayer()
            {
                Source  = ConvLayer1,
                Verbose = verbose
            };

            var ActivationLayer3 = new SquareActivation()
            {
                Source  = VectorizeLayer2,
                Verbose = verbose
            };



            var ConvEngine = new ConvolutionEngine()
            {
                InputShape   = new int[] { 83, 14, 14 },
                KernelShape  = new int[] { 83, 10, 10 },
                Upperpadding = new int[] { 0, 4, 4 },
                Lowerpadding = new int[] { 0, 4, 4 },
                Stride       = new int[] { 83, 2, 2 },
                MapCount     = new int[] { 112, 1, 1 }
            };

            var DenseLayer4 = new LLDenseLayer
            {
                Source           = ActivationLayer3,
                WeightsScale     = 512.0,
                Weights          = ConvEngine.GetDenseWeights((double[])wr.Weights[1]),
                Bias             = ConvEngine.GetDenseBias((double[])wr.Biases[1]),
                InputFormat      = EVectorFormat.dense,
                ForceDenseFormat = true,
                Verbose          = verbose
            };


            var ActivationLayer5 = new SquareActivation()
            {
                Source  = DenseLayer4,
                Verbose = verbose
            };

            var DenseLayer6 = new LLDenseLayer()
            {
                Source       = ActivationLayer5,
                Weights      = (double[])wr.Weights[2],
                Bias         = (double[])wr.Biases[2],
                WeightsScale = 512.0,
                InputFormat  = EVectorFormat.dense,
                Verbose      = verbose
            };

            var StopTimingLayer = new TimingLayer()
            {
                Source = DenseLayer6, StopCounters = new string[] { "Inference-Time" }
            };

            var network = StopTimingLayer;

            Console.WriteLine("Preparing");
            network.PrepareNetwork();
            int count     = 0;
            int errs      = 0;
            int batchSize = 1;

            while (count < numberOfRecords)
            {
                using (var m = network.GetNext())
                {
                    Utils.ProcessInEnv(env =>
                    {
                        var decrypted = m.Decrypt(env);
                        int pred      = 0;
                        for (int j = 1; j < decrypted.RowCount; j++)
                        {
                            if (decrypted[j, 0] > decrypted[pred, 0])
                            {
                                pred = j;
                            }
                        }
                        if (pred != readerLayer.Labels[0])
                        {
                            errs++;
                        }
                        count++;
                        if (count % batchSize == 0)
                        {
                            Console.Write("errs {0}/{1} accuracy {2:0.000}% prediction {3} label {4} {5}ms", errs, count, 100 - (100.0 * errs / (count)), pred, readerLayer.Labels[0], TimingLayer.GetStats());
                            if (options.Encrypt)
                            {
                                Console.WriteLine();
                            }
                            else
                            {
                                Console.WriteLine(" 2^{0} largest-value", Math.Log(RawMatrix.Max) / Math.Log(2));
                            }
                        }
                    }, factory);
                }
            }
            Console.WriteLine("errs {0}/{1} accuracy {2:0.000}%", errs, count, 100 - (100.0 * errs / (count)));
            network.DisposeNetwork();
            if (!options.Encrypt)
            {
                Console.WriteLine("Max computed value 2^{1}", Math.Log(RawMatrix.Max) / Math.Log(2));
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            string fileName        = "MNIST-28x28-test.txt";
            int    batchSize       = 8192;
            int    numberOfRecords = 10000;
            var    Factory         = new EncryptedSealBfvFactory(new ulong[] { 2148728833, 2148794369, 2149810177 }, 16384, DecompositionBitCount: 60, GaloisDecompositionBitCount: 60, SmallModulusCount: 7);
            int    weightscale     = 32;

            var ReaderLayer = new BatchReader
            {
                FileName            = fileName,
                SparseFormat        = true,
                MaxSlots            = batchSize,
                NormalizationFactor = 1.0 / 256.0,
                Scale = 16.0
            };

            var EncryptedLayer = new EncryptLayer()
            {
                Source = ReaderLayer, Factory = Factory
            };

            var StartTimingLayer = new TimingLayer()
            {
                Source = EncryptedLayer, StartCounters = new string[] { "Batch-Time" }
            };

            var ConvLayer1 = new PoolLayer()
            {
                Source       = StartTimingLayer,
                InputShape   = new int[] { 28, 28 },
                KernelShape  = new int[] { 5, 5 },
                Upperpadding = new int[] { 1, 1 },
                Stride       = new int[] { 2, 2 },
                MapCount     = new int[] { 5, 1 },
                WeightsScale = weightscale,
                Weights      = Weights.Weights_0
            };

            //var ActivationLayer2 = new SquareActivation() { Source = ConvLayer1 };
            var ActivationLayer2 = new AppxReLUActivation()
            {
                Source = ConvLayer1
            };

            var DenseLayer3 = new PoolLayer()
            {
                Source       = ActivationLayer2,
                InputShape   = new int[] { 5 * 13 * 13 },
                KernelShape  = new int[] { 5 * 13 * 13 },
                Stride       = new int[] { 1000 },
                MapCount     = new int[] { 100 },
                Weights      = Transpose(Weights.Weights_1, 5 * 13 * 13, 100),
                Bias         = Weights.Biases_2,
                WeightsScale = weightscale * weightscale
            };

            //var ActivationLayer4 = new SquareActivation() { Source = DenseLayer3 };
            var ActivationLayer4 = new AppxReLUActivation()
            {
                Source = DenseLayer3
            };

            var DenseLayer5 = new PoolLayer()
            {
                Source       = ActivationLayer4,
                InputShape   = new int[] { 100 },
                KernelShape  = new int[] { 100 },
                Stride       = new int[] { 1000 },
                MapCount     = new int[] { 10 },
                Weights      = Weights.Weights_3,
                Bias         = Weights.Biases_3,
                WeightsScale = weightscale
            };

            var StopTimingLayer = new TimingLayer()
            {
                Source = DenseLayer5, StopCounters = new string[] { "Batch-Time" }
            };
            var network = StopTimingLayer;

            OperationsCount.Reset();
            Console.WriteLine("Preparing");
            network.PrepareNetwork();
            OperationsCount.Print();
            OperationsCount.Reset();
            for (var p = (INetwork)network; p != null; p = p.Source)
            {
                if (p is BaseLayer b)
                {
                    b.Verbose = true;
                }
            }

            int count = 0;
            int errs  = 0;

            while (count < numberOfRecords)
            {
                using (var m = network.GetNext())
                    Utils.ProcessInEnv(env =>
                    {
                        var decrypted = m.Decrypt(env);
                        for (int i = 0; i < decrypted.RowCount; i++)
                        {
                            int pred = 0;
                            for (int j = 1; j < decrypted.ColumnCount; j++)
                            {
                                if (decrypted[i, j] > decrypted[i, pred])
                                {
                                    pred = j;
                                }
                            }
                            if (pred != ReaderLayer.Labels[i])
                            {
                                errs++;
                            }
                            count++;
                            if (count % 100 == 0)
                            {
                                Console.WriteLine("errs {0}/{1} accuracy {2:0.000}% prediction {3} label {4}", errs, count, 100 - (100.0 * errs / (count)), pred, ReaderLayer.Labels[i]);
                            }
                        }
                        Console.WriteLine("Batch size {0} {1}", batchSize, TimingLayer.GetStats());
                    }, Factory);
            }
            Console.WriteLine("errs {0}/{1} accuracy {2:0.000}%", errs, count, 100 - (100.0 * errs / (count)));
            network.DisposeNetwork();
        }
예제 #4
0
        static void Main(string[] args)
        {
            var ini = new IniReader(@"cal.model.ini", 4096, 102);

            ini.Normalize(@"cal.AffineNormalizer.txt");
            var    start       = DateTime.Now;
            var    Factory     = new EncryptedSealBfvFactory(new ulong[] { 4300801 }, 4096, DecompositionBitCount: 60, GaloisDecompositionBitCount: 60, SmallModulusCount: 2);
            double weightscale = 256;
            string FileName    = "cal_deep_test.tsv";

            if (!File.Exists(FileName))
            {
                Console.WriteLine("ERROR: Can't find data file {0}", FileName);
                Console.WriteLine("Please use DataPreprocess to obtain the Caltech-101 dataset");
                return;
            }

            var readerLayer = new LLSingleLineReader()
            {
                FileName            = FileName,
                SparseFormat        = true,
                NormalizationFactor = 1.0,
                Scale = 256,
            };

            var encryptLayer = new EncryptLayer()
            {
                Source = readerLayer, Factory = Factory
            };
            var denseLayer = new LLDenseLayer()
            {
                Source       = encryptLayer,
                Weights      = ini.Weights,
                Bias         = ini.Bias,
                WeightsScale = weightscale,
                InputFormat  = EVectorFormat.dense
            };

            var network = denseLayer;

            network.PrepareNetwork();
            int     errs = 0;
            var     N    = 1020;
            IMatrix m    = null;

            Utils.ProcessInEnv(env =>
            {
                for (int i = 0; i < N; i++)
                {
                    Utils.Time("Prediction+Encryption", () => m = network.GetNext());
                    var dec = m.Decrypt(env);
                    m.Dispose();
                    var l    = readerLayer.Labels[0];
                    int pred = 0;
                    for (int j = 0; j < 101; j++)
                    {
                        if (dec[j, 0] > dec[pred, 0])
                        {
                            pred = j;
                        }
                    }
                    if (pred != l)
                    {
                        errs++;
                    }
                    Console.WriteLine("errs {0}/{1} accuracy {2:0.000}% {3} prediction {4} label {5}", errs, i + 1, 100 - (100.0 * errs / (i + 1)), TimingLayer.GetStats(), pred, l);
                }
            }, Factory);
            network.DisposeNetwork();
        }