Пример #1
0
        static void Main(string[] args)
        {
            var dir = ConfigurationManager.AppSettings["FileDirectory"];

            var dataSet = new DataSet();
            dataSet.Id = 1;//int.Parse(currDir.Split('\\').Last());

            var files = Directory.GetFiles(dir+"/singatures").ToList();
            //files.Remove(files.Last());

            IFeatureExtractor featureEngine = new RadonTransformFeatureExtractor();

            foreach (var file in files)
            {
                dataSet.Signatures.Add(featureEngine.Extract(file));
            }
            double max = 0;

            foreach (var st in dataSet.Signatures)
            {
                for (int i = 0; i < 8; i++)
                {
                    for(int j=0; j< 21;j++)
                        if (max < st.Data[i, j])
                            max = st.Data[i, j];
                }
            }

            foreach (var st in dataSet.Signatures)
            {
                for (int i = 0; i < 8; i++)
                {
                    for (int j = 0; j < 21; j++)
                        st.Data[i, j] /= max;
                }
            }

            //var network = new NeuralNetworkBp();
            //network.Train(dataSet);

            //var toCheck = Directory.GetFiles(dir +"/test").First();
            //var check = featureEngine.Extract(toCheck);

            //for (int i = 0; i < 8; i++)
            //{
            //    for (int j = 0; j < 21; j++)
            //        check.Data[i, j] /= max;
            //}

            //network.Verify(check);

            //var forgeryName = Directory.GetFiles(dir + "/forgery").First();
            //var forgery = featureEngine.Extract(forgeryName);

            //network.Verify(forgery);
        }
 public int Train(DataSet dataSet)
 {
     var data = dataSet.Signatures.Select(i => i.Data.Cast<double>().ToArray()).ToArray();
     var dict = new Dictionary<string, double[]>();
     for (int i = 0; i < data.Count(); i++)
     {
         dict.Add((i+1).ToString(), data[i]);
     }
     network = new NeuralNetwork<string>(new Layer3<string>(8*21,  (int)((double)((8*21  + 4)*FirstLayerParameter)),
         (int)((double)((8 * 21 + 4) * SecondLayerParameter)), 4), dict);
     return 1;
 }
        public int Train(DataSet dataSet)
        {
            Network = new BasicNetwork();
            Network.AddLayer(new BasicLayer(null, true, 8 * 21));
            var first = ((8 * 21 + 4) * FirstLayerParameter);
            Network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, (int)first));
            var second = ((8 * 21 + 4) * SecondLayerParameter);
            Network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, (int)second));
            Network.AddLayer(new BasicLayer(null, false, 1));
               // Network.AddLayer(new );
            Network.Structure.FinalizeStructure();
            Network.Reset();
            //IMLData x = new BasicNeuralData();
            var set = new double[dataSet.Signatures.Count + dataSet.Forgeries.Count][];
            var ideal = new double[dataSet.Signatures.Count + dataSet.Forgeries.Count][];
            for (int i = 0; i < dataSet.Signatures.Count; i++)
            {
                set[i] = dataSet.Signatures[i].Data.Cast<double>().ToArray();
                ideal[i] = new double[] {1};
            }
            for (int i = dataSet.Signatures.Count; i < dataSet.Signatures.Count  + dataSet.Forgeries.Count; i++)
            {
                set[i] = dataSet.Forgeries[i- dataSet.Signatures.Count].Data.Cast<double>().ToArray();
                ideal[i] = new double[] { 0 };
            }

            IMLDataSet trainingSet = new BasicMLDataSet(set, ideal);

            IMLTrain train = new ResilientPropagation(Network, trainingSet);

            int epoch = 1;
            var errors = new List<double>();
            do
            {
                train.Iteration();
                // Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
                errors.Add(train.Error);

            } while ( epoch < 10000);

            train.FinishTraining();

            return 1;
        }
        public DataSet GetTeachingDataset(string[] files, string[] forgeries = null)
        {
            if(forgeries == null)
                forgeries = new string[0];
            var dataSet = new DataSet();
            foreach (var file in files)
            {
                dataSet.Signatures.Add(Extract(file));
            }
            foreach (var forgery in forgeries)
            {
                dataSet.Forgeries.Add(Extract(forgery));
            }

            //WTF is going on?
            double max = 0;
            foreach (var st in dataSet.Signatures)
            {
                for (int i = 0; i < 8; i++)
                {
                    for (int j = 0; j < 21; j++)
                        if (max < st.Data[i, j])
                            max = st.Data[i, j];
                }
            }
            foreach (var st in dataSet.Forgeries)
            {
                for (int i = 0; i < 8; i++)
                {
                    for (int j = 0; j < 21; j++)
                        if (max < st.Data[i, j])
                            max = st.Data[i, j];
                }
            }

            //foreach (var st in dataSet.Signatures)
            //{
            //    for (int i = 0; i < 8; i++)
            //    {
            //        for (int j = 0; j < 21; j++)
            //            st.Data[i, j] /= max;
            //    }
            //}
            //foreach (var st in dataSet.Forgeries)
            //{
            //    for (int i = 0; i < 8; i++)
            //    {
            //        for (int j = 0; j < 21; j++)
            //            st.Data[i, j] /= max;
            //    }
            //}

            return dataSet;
        }