예제 #1
0
        public MainForm()
        {
            InitializeComponent();
            this.comboBoxActivation.SelectedIndex     = 0;
            this.comboBoxLearningMethod.SelectedIndex = 0;

            m_QueueForLOOCV       = new Queue <string>();
            m_CurrentUserForLOOCV = "";
            m_AllFeaturesForLOOCV = new Dictionary <string, FeatureSet[]>();

            m_Info = new NeuralNetworkInfo();
            UpdateNetworkInfoFromForm();
            m_Network = new NeuralNetwork(m_Info);

            m_Output     = textBoxOutput.Text;
            m_Source     = new List <string>();
            m_DomainFile = textBoxDomainFile.Text;

            m_Worker = new BackgroundWorker();
            m_Worker.WorkerSupportsCancellation = true;
            m_Worker.WorkerReportsProgress      = true;
            m_Worker.ProgressChanged           += new ProgressChangedEventHandler(worker_ProgressChanged);
            m_Worker.DoWork             += new DoWorkEventHandler(worker_DoWork);
            m_Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

            toolStripStatusLabel1.Text = "Ready";
        }
        public ZernikeMomentRecognizerUpdateable()
        {
            List <string> features = new List <string>(new string[] {
                "A00", "A11", "A20", "A22", "A31", "A33", "A40",
                "A42", "A44", "A51", "A53", "A55", "A60", "A62",
                "A64", "A66", "A71", "A73", "A75", "A77", "A80",
                "A82", "A84", "A86", "A88", "A91", "A93", "A95",
                "A97", "A99", "A100", "A102", "A104", "A106",
                "A108", "A1010"
            });

            NeuralNetworkInfo info = new NeuralNetworkInfo("Digital Circuits", "All");

            info.Layers            = new int[] { 20, 7 };
            info.NumInputs         = features.Count;
            info.LearningRate      = 0.3;
            info.Momentum          = 0.3;
            info.NumTrainingEpochs = 2000;
            info.Teacher           = "Back Propagation";
            info.FeatureNames      = features;

            info.OutputTypeNames = new List <string>();
            foreach (ShapeType gate in LogicDomain.Gates)
            {
                info.OutputTypeNames.Add(gate.Name);
            }

            _network         = new NeuralNetwork(info);
            _momentInstances = new Dictionary <string, List <ZernikeMoment> >();
        }
예제 #3
0
        /// <summary>
        /// Train a new neural image recognizer. The trained recognizer
        /// will use the given image recognizer and will be trained on
        /// the given list of good gates (shapes that are actually
        /// gates) and bad gates (shapes that aren't gates).
        /// </summary>
        /// <param name="recognizer"></param>
        /// <param name="goodGates"></param>
        /// <param name="badGates"></param>
        /// <param name="info">The network information. NOTE: You do not need to set info.NumInputs; that will be taken care of for you.</param>
        public NeuralImageRecognizer(ImageRecognizer recognizer, IEnumerable <Shape> goodGates, IEnumerable <Shape> badGates, NeuralNetworkInfo info)
        {
            _imageRecognizer = recognizer;

            info.NumInputs = 4;

            // Assemble training data
            List <FeatureSet> trainingData = ConstructTrainingSet(_imageRecognizer, goodGates, badGates);

            // Create network
            _neuralNetwork = new NeuralNetwork(info);
            _neuralNetwork.Train(trainingData.ToArray());
        }
예제 #4
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (m_Worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            if (radioButtonSingleNetwork.Checked)
            {
                FeatureSet[] sets = (FeatureSet[])e.Argument;

                if (sets.Length == 0)
                {
                    return;
                }

                m_Info.NumInputs = sets[0].Features.Length;
                m_Info.Type      = "All";

                m_Network = new NeuralNetwork(m_Info);

                m_Network.TrainBackground(sets, m_Worker);
            }
            else
            {
                string userKey = (string)e.Argument;

                List <FeatureSet> sets = new List <FeatureSet>(10000);
                foreach (KeyValuePair <string, FeatureSet[]> set in m_AllFeaturesForLOOCV)
                {
                    if (set.Key != userKey)
                    {
                        foreach (FeatureSet values in set.Value)
                        {
                            sets.Add(values);
                        }
                    }
                }

                if (sets.Count == 0)
                {
                    return;
                }

                m_Info.NumInputs = sets[0].Features.Length;
                m_Info.Type      = "UserHoldout";
                int    n       = 0;
                string userNum = userKey.Substring(n, 2);

                int num;
                while (!int.TryParse(userNum, out num) && n < userKey.Length - 2)
                {
                    n++;
                    userNum = userKey.Substring(n, 2);
                }
                string platform = "";
                string x        = userKey.Substring(n + 2, 2);
                if (x == "_P" || x == "_T")
                {
                    platform = x;
                }
                m_CurrentUserForLOOCV = userNum + platform;
                m_Info.UserName       = userNum;
                m_Info.UserID         = Guid.NewGuid();
                NeuralNetworkInfo info = new NeuralNetworkInfo(m_Info);

                m_Network = new NeuralNetwork(m_Info);

                m_Network.TrainBackground(sets.ToArray(), m_Worker);
            }
        }
예제 #5
0
        /// <summary>
        /// Arguments
        ///    0: the directory to find files
        ///    1: directory to find real-world data (recursive)
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                return;
            }

            // Get the list of files
            Console.WriteLine("Finding sketch files...");
            List <string> allSketches = new List <string>(System.IO.Directory.GetFiles(args[0], "*.xml"));

            Console.WriteLine("    found " + allSketches.Count + " sketches");


            // Load all the shapes in all the sketches
            Console.WriteLine("Loading full data set...");
            List <Shape> shapeData = GetShapeData(allSketches);

            Console.WriteLine("    found " + shapeData.Count + " gates");


            // Print classes found
            HashSet <ShapeType> types = new HashSet <ShapeType>();

            foreach (Shape shape in shapeData)
            {
                types.Add(shape.Type);
            }
            Console.WriteLine("Found " + types.Count + " types:");
            foreach (ShapeType type in types)
            {
                Console.WriteLine("    " + type);
            }

            // Save all the shapes to images in the "sketches" folder
            string outputPath = @"shapes\";

            Console.WriteLine("Saving gates to '" + outputPath + "'...");
            if (!System.IO.Directory.Exists(outputPath))
            {
                System.IO.Directory.CreateDirectory(outputPath);
            }
            foreach (Shape shape in shapeData)
            {
                System.Drawing.Bitmap b = shape.createBitmap(100, 100, true);
                shape.TemplateDrawing = b;
                string filename = String.Format(outputPath + shape.Type + "-{0:x}.png", shape.GetHashCode());
                b.Save(filename);
            }
            Console.WriteLine("    finished saving gates");


            // Train the base recognizers on all the data
            Console.WriteLine("Training recognizers on all data...");

#if false
            Console.WriteLine("    rubine");
            RubineRecognizerUpdateable rubine = new RubineRecognizerUpdateable(shapeData);
            rubine.Save("Rubine.rru");
            rubine.LiteRecognizer.Save("RubineLite.rr");

            Console.WriteLine("    dollar");
            DollarRecognizer dollar = new DollarRecognizer(shapeData);
            dollar.Save("Dollar.dr");

            RubineRecognizerUpdateable rubine = new RubineRecognizerUpdateable();
            rubine.Save("Rubine.rru");
            rubine.LiteRecognizer.Save("RubineLite.rr");

            DollarRecognizer dollar = new DollarRecognizer();
            dollar.Save("Dollar.dr");

            Console.WriteLine("    zernike");
            ZernikeMomentRecognizerUpdateable zernike = new ZernikeMomentRecognizerUpdateable(shapeData);
            zernike.Save("Zernike.zru");
            zernike.LiteRecognizer.Save("ZernikeLite.zr");
#endif

            Console.WriteLine("    adaptive image");
            AdaptiveImageRecognizer adaptiveimage = new AdaptiveImageRecognizer(shapeData);
            adaptiveimage.Save("AdaptiveImage.air");

            Console.WriteLine("    image");
            ImageRecognizer image = new ImageRecognizer(shapeData);
            image.Save("Image.ir");

            Console.WriteLine("    finished training recognizers");

#if false
            RubineRecognizer        fullRubine  = rubine.LiteRecognizer;
            DollarRecognizer        fullDollar  = dollar;
            ZernikeMomentRecognizer fullZernike = zernike.LiteRecognizer;

            ImageRecognizer fullImage = image;

            // Split the data up per-user
            Console.WriteLine("Loading per-user data...");
            Dictionary <string, List <Shape>[]> user2data = GetSketchesPerUser(allSketches);
            Console.WriteLine("    found " + user2data.Count + " users");


            // Foreach user: train each of the recognizers and accumulate training data
            // for the combo recognizer
            List <KeyValuePair <ShapeType, Dictionary <string, object> > > data = new List <KeyValuePair <ShapeType, Dictionary <string, object> > >();
            foreach (KeyValuePair <string, List <Shape>[]> pair in user2data)
            {
                string user = pair.Key;

                ////////////////////////////////////////
                ////////////   Train   /////////////////
                ////////////////////////////////////////

                Console.WriteLine("User: "******"    rubine");
                rubine = new RubineRecognizerUpdateable(trainingSet);
                rubine.Save("Rubine" + user + ".rru");
                rubine.LiteRecognizer.Save("RubineLite" + user + ".rr");

                Console.WriteLine("    dollar");
                dollar = new DollarRecognizer(trainingSet);
                dollar.Save("Dollar" + user + ".dr");
#else
                rubine = new RubineRecognizerUpdateable();
                rubine.Save("Rubine" + user + ".rru");
                rubine.LiteRecognizer.Save("RubineLite" + user + ".rr");

                dollar = new DollarRecognizer();
                dollar.Save("Dollar" + user + ".dr");
#endif

                Console.WriteLine("    zernike");
                zernike = new ZernikeMomentRecognizerUpdateable(trainingSet);
                zernike.Save("Zernike" + user + ".zru");
                zernike.LiteRecognizer.Save("ZernikeLite" + user + ".zr");

                Console.WriteLine("    image");
                image = new ImageRecognizer(trainingSet);
                image.Save("Image" + user + ".ir");
                fullImage = image;

                ////////////////////////////////////////
                //////////// Evaluate //////////////////
                ////////////////////////////////////////


                List <Shape> testingSet = pair.Value[1];

                // Create the training data for the combo recognizer
                List <KeyValuePair <ShapeType, Dictionary <string, object> > > comboTrainingData = TrainingDataCombo(testingSet, rubine, dollar, zernike, image);
                foreach (KeyValuePair <ShapeType, Dictionary <string, object> > pair2 in comboTrainingData)
                {
                    data.Add(pair2);
                }
            }

            if (data.Count == 0)
            {
                throw new Exception("no data!");
            }

            List <string> features = new List <string>();
            foreach (KeyValuePair <ShapeType, Dictionary <string, object> > instance in data)
            {
                foreach (string feature in instance.Value.Keys)
                {
                    if (!features.Contains(feature))
                    {
                        features.Add(feature);
                    }
                }
            }

            Console.WriteLine("Found " + data.Count + " data points and " + features.Count + " features.");

            ComboRecognizer combo = new ComboRecognizer(fullRubine, fullDollar, fullZernike, fullImage);
            combo.TrainCombo(features, data);
            combo.Save("Combo.cru");

            Console.WriteLine("Naive bayes updatable has " + combo.ComboClassifier.Examples.Count + " examples.");
            Console.WriteLine("Naive bayes updatable has " + combo.ComboClassifier.Classifier.Classes.Count + " classes:");
            foreach (ShapeType cls in combo.ComboClassifier.Classifier.Classes)
            {
                Console.WriteLine("    " + cls);
            }
#endif

            Console.WriteLine("Training neural image recognizer on real-world data...");
            List <Shape> goodGates;                  // list of correctly-identified gates
            List <Shape> badGates;                   // list of shapes grouped as gates that aren't
            Dictionary <Shape, string> shapeSources; // map of shapes to source filename

            string cacheFile = outputPath + "goodAndBadGates.data";
            if (!System.IO.File.Exists(cacheFile))
            {
                goodGates    = new List <Shape>();
                badGates     = new List <Shape>();
                shapeSources = new Dictionary <Shape, string>();

                Grouper             grouper    = RecognitionPipeline.createDefaultGrouper();
                Classifier          classifier = RecognitionPipeline.createDefaultClassifier();
                RecognitionPipeline pipeline   = new RecognitionPipeline(classifier, grouper);
                var files = Files.FUtil.AllFiles(args[1], Files.Filetype.XML, true);
                Console.WriteLine("    Found " + files.Count() + " real-world sketches");
                int i = 1;
                foreach (string file in files)
                {
                    Console.WriteLine("    Sketch " + i + " / " + files.Count());
                    i++;

                    Sketch.Sketch sketch   = new ReadXML(file).Sketch;
                    Sketch.Sketch original = sketch.Clone();

                    sketch.RemoveLabels();
                    sketch.resetShapes();

                    pipeline.process(sketch);

                    foreach (Sketch.Shape shape in sketch.Shapes)
                    {
                        if (shape.Classification != LogicDomain.GATE_CLASS)
                        {
                            continue;
                        }

                        Shape originalGate = original.ShapesL.Find(delegate(Shape s) { return(s.GeometricEquals(shape)); });

                        if (originalGate != null && originalGate.Classification == LogicDomain.GATE_CLASS)
                        {
                            goodGates.Add(shape);
                        }
                        else
                        {
                            // We can't just say "this is a bad gate." If it wasn't found,
                            // the shape might be an XOR gate missing the back, or a NAND
                            // gate missing a bubble. We will apply the following heuristic:
                            //    if all the strokes in the shape are part of the same
                            //    shape in the original sketch and that shape in the
                            //    original sketch is a gate, this is not a bad gate.

                            // a shape consists of one or more substrokes from shapes in the
                            // original, correct sketch
                            HashSet <Shape> originalShapes = new HashSet <Shape>();
                            foreach (Substroke substroke in shape.Substrokes)
                            {
                                Substroke originalSubstroke = original.SubstrokesL.Find(delegate(Substroke s) { return(s.GeometricEquals(substroke)); });
                                if (originalSubstroke == null)
                                {
                                    throw new Exception("A substroke is missing in the original sketch???");
                                }
                                if (originalSubstroke.ParentShape != null)
                                {
                                    originalShapes.Add(originalSubstroke.ParentShape);
                                }
                            }

                            List <Shape> originalShapesL = originalShapes.ToList();
                            if (originalShapesL.Count != 1 || originalShapesL[0].Classification != LogicDomain.GATE_CLASS)
                            {
                                badGates.Add(shape);
                            }
                        }
                        shapeSources.Add(shape, file);
                    }
                }

                Console.WriteLine("Saving found gates to " + cacheFile);
                var stream     = System.IO.File.Open(cacheFile, System.IO.FileMode.Create);
                var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                bformatter.Serialize(stream, Tuple.Create(goodGates, badGates, shapeSources));
                stream.Close();
            }
            else
            {
                Console.WriteLine("Loading good and bad gates from " + cacheFile);
                var stream     = System.IO.File.Open(cacheFile, System.IO.FileMode.Open);
                var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                var data       = (Tuple <List <Shape>, List <Shape>, Dictionary <Shape, string> >)bformatter.Deserialize(stream);
                stream.Close();

                goodGates    = data.Item1;
                badGates     = data.Item2;
                shapeSources = data.Item3;
            }
            Console.WriteLine("    Found " + goodGates.Count + " good gates, " + badGates.Count + " bad gates");

            ImageRecognizer innerNeuralRecgonizer = image;
            string          neuralPath            = @"neuralResults\";
            string          arffFilename          = "data.arff";
            if (!System.IO.Directory.Exists(neuralPath))
            {
                System.IO.Directory.CreateDirectory(neuralPath);
            }

            Console.WriteLine("    Writing ARFF file '" + neuralPath + arffFilename + "'...");
            TextWriter arffWriter = new StreamWriter(neuralPath + arffFilename);
            NeuralImageRecognizer.WriteARFF(arffWriter, innerNeuralRecgonizer, goodGates, badGates);
            arffWriter.Close();

            Console.WriteLine("    Training the network...");

            // Network settings -- determined empircally
            NeuralNetworkInfo info = new NeuralNetworkInfo();
            info.Layers            = new int[] { 8, 1 };
            info.NumTrainingEpochs = 1000;
            info.LearningRate      = 0.05;
            info.Momentum          = 0.2;
            NeuralImageRecognizer neuralImage = new NeuralImageRecognizer(innerNeuralRecgonizer, goodGates, badGates, info);

            neuralImage.Save("NeuralImage.nir");


            Console.WriteLine("    Testing the network (results in " + neuralPath + ")...");
            neuralImage = NeuralImageRecognizer.Load("NeuralImage.nir");

            TextWriter writer = new StreamWriter(neuralPath + "info.csv");

            writer.WriteLine("Sketch File, Shape Bitmap, Good?, Tanimoto, Yule, Partial Hausdorff, Modified Hausdorff, Output Confidence");

            int falseNegatives = 0;
            foreach (Shape gate in goodGates)
            {
                ImageRecognitionResult result = (ImageRecognitionResult)neuralImage.recognize(gate, null);
                if (result.Confidence < 0.5)
                {
                    falseNegatives++;
                }

                System.Drawing.Bitmap b = gate.createBitmap(100, 100, true);
                string filename         = String.Format("good-" + "-{0:x}.png", gate.GetHashCode());
                b.Save(neuralPath + filename);

                writer.WriteLine(shapeSources[gate] + "," + filename +
                                 ", 1, " +
                                 result.Tanimoto + ", " +
                                 result.Yule + ", " +
                                 result.PartialHausdorff + ", " +
                                 result.ModifiedHausdorff + ", " +
                                 result.Confidence);
            }
            Console.WriteLine("    Good gates with low confidence: " + falseNegatives + "/" + (goodGates.Count));

            int falsePositives = 0;
            foreach (Shape gate in badGates)
            {
                ImageRecognitionResult result = (ImageRecognitionResult)neuralImage.recognize(gate, null);
                if (result.Confidence > 0.5)
                {
                    falsePositives++;
                }

                System.Drawing.Bitmap b = gate.createBitmap(100, 100, true);
                string filename         = String.Format("bad-" + "-{0:x}.png", gate.GetHashCode());
                b.Save(neuralPath + filename);

                writer.WriteLine(shapeSources[gate] + "," + filename +
                                 ", 0, " +
                                 result.Tanimoto + ", " +
                                 result.Yule + ", " +
                                 result.PartialHausdorff + ", " +
                                 result.ModifiedHausdorff + ", " +
                                 result.Confidence);
            }
            Console.WriteLine("    Bad gates with high confidence: " + falsePositives + "/" + (badGates.Count));
            writer.Close();

            Console.WriteLine("Press ENTER to continue...");
            Console.ReadLine();
        }