Beispiel #1
0
        private List <Item> SampleTestingInstance()
        {
            var result = new List <Item>();
            var n      = this.random.Next(this.testing.Count);
            var entry  = this.testing[n];

            // Create volume from image data
            var x = new Volume(28, 28, 1, 0.0);

            for (var i = 0; i < 28; i++)
            {
                for (var j = 0; j < 28; j++)
                {
                    x.Set(j + i * 28, entry.Image[j + i * 28] / 255.0);
                }
            }

            for (var i = 0; i < 4; i++)
            {
                result.Add(new Item {
                    Volume = x.Augment(24), Label = entry.Label
                });
            }

            return(result);
        }
Beispiel #2
0
        private Item SampleTrainingInstance()
        {
            var n     = this.random.Next(this.trainingCount);
            var entry = this.training[n];

            // load more batches over time
            if (this.stepCount % 5000 == 0 && this.stepCount > 0)
            {
                this.trainingCount = Math.Min(this.trainingCount + BatchSize, this.training.Count);
            }

            // Create volume from image data
            var x = new Volume(28, 28, 1, 0.0);

            for (var i = 0; i < 28; i++)
            {
                for (var j = 0; j < 28; j++)
                {
                    x.Set(j + i * 28, entry.Image[j + i * 28] / 255.0);
                }
            }

            var result = x.Augment(24);

            return(new Item {
                Volume = result, Label = entry.Label, IsValidation = n % 10 == 0
            });
        }
        private Item SampleTrainingInstance()
        {
            var n     = random.Next(trainingCount);
            var entry = training[n];

            // load more batches over time
            if (stepCount % 5000 == 0 && stepCount > 0)
            {
                trainingCount = Math.Min(trainingCount + BatchSize, training.Count);
            }

            // Create volume from image data
            var x = new Volume(24, 24, 1, 0.0);

            for (var i = 0; i < 24; i++)
            {
                for (var j = 0; j < 24; j++)
                {
                    x.Weights[j + i * 24] = entry.Image[j + i * 24] / 255.0;
                }
            }

            x = x.Augment(24);

            return(new Item {
                Volume = x, Label = entry.Label, IsValidation = n % 10 == 0
            });
        }
        private Volume GetVolume(Signature signature)
        {
            var x = new Volume(_imageSize, _imageSize, 1, 0.0);

            foreach (var point in signature.SignaturePointList)
            {
                x.Weights[point.X * _imageSize + point.Y] = 1;
            }

            x = x.Augment(_imageSize);

            return(x);
        }
        private TrainingItem GenerateTrainingInstance()
        {
            Random random       = new Random();
            bool   isValidation = random.Next(10) == 0;

            int userId = random.Next(_userList.Count);
            var user   = _userList[userId];

            // Calculate signature index
            int signatureIndex;
            var testRange = user.SignatureList.Count * 3 / 4;

            if (!isValidation)
            {
                signatureIndex = random.Next(testRange);
            }
            else
            {
                signatureIndex = testRange + random.Next(user.SignatureList.Count - testRange);
            }

            var signature = user.SignatureList[signatureIndex];

            // Create volume from image data
            var x = new Volume(imageSize, imageSize, 1, 0.0);

            foreach (var point in signature.SignaturePointList)
            {
                x.Weights[point.X * imageSize + point.Y] = 1;
            }

            x = x.Augment(imageSize);
            return(new TrainingItem {
                Volume = x, ClassIndex = user.UserId == neededUserId? 1 : 0, IsValidation = isValidation
            });
        }
        public void Demo()
        {
            // Load data
            _userList = ReadingManager.ReadData(@"C:\Users\RustamSalakhutdinov\Documents\visual studio 2015\Projects\signatureChecker\data_new");

            // Create network
            _net = new Net();
            _net.AddLayer(new InputLayer(imageSize, imageSize, 1));
            _net.AddLayer(new ConvLayer(5, 5, 16)
            {
                Stride = 1, Pad = 1, Activation = Activation.Relu
            });
            _net.AddLayer(new PoolLayer(2, 2)
            {
                Stride = 2
            });
            _net.AddLayer(new ConvLayer(5, 5, 8)
            {
                Stride = 1, Pad = 1, Activation = Activation.Relu
            });
            _net.AddLayer(new PoolLayer(2, 2)
            {
                Stride = 2
            });
            _net.AddLayer(new SoftmaxLayer(2));

            _trainer = new Trainer(_net)
            {
                BatchSize      = 20,
                L2Decay        = 0.001,
                TrainingMethod = Trainer.Method.Adagrad
            };

            Stopwatch sw = Stopwatch.StartNew();

            do
            {
                var sample = GenerateTrainingInstance();
                if (!Step(sample))
                {
                    break;
                }
            } while (!Console.KeyAvailable);
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds / 1000.0);

            foreach (User user in _userList)
            {
                Random random    = new Random();
                var    signature = user.SignatureList[random.Next(user.SignatureList.Count)];

                var x = new Volume(imageSize, imageSize, 1, 0.0);

                foreach (var point in signature.SignaturePointList)
                {
                    x.Weights[point.X * imageSize + point.Y] = 1;
                }

                x = x.Augment(imageSize);

                var result = _net.Forward(x);
                Console.WriteLine("UserId: {0}. Result: {1} | {2}", user.UserId, result.Weights[0], result.Weights[1]);
            }
        }