Exemplo n.º 1
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());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads a previously saved ImageRecognizer from the given filename,
        /// using the deserialization constructor
        /// </summary>
        /// <param name="filename">Filename which is the saved ImageRecognizer</param>
        /// <returns>Re-instantiated ImageRecognzier</returns>
        public static ImageRecognizer Load(string filename)
        {
            System.IO.Stream stream = System.IO.File.Open(filename, System.IO.FileMode.Open);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            ImageRecognizer image = (ImageRecognizer)bformatter.Deserialize(stream);

            stream.Close();

             #if DEBUG
            Console.WriteLine("Image recognizer loaded.");
             #endif

            return(image);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Construct the training data set that this recognizer will use to train the neural network.
        /// </summary>
        /// <param name="recognizer"></param>
        /// <param name="goodGates"></param>
        /// <param name="badGates"></param>
        /// <returns></returns>
        public static List <FeatureSet> ConstructTrainingSet(ImageRecognizer recognizer, IEnumerable <Shape> goodGates, IEnumerable <Shape> badGates)
        {
            List <FeatureSet> trainingData = new List <FeatureSet>();

            foreach (Shape goodGate in goodGates)
            {
                ImageRecognitionResult result = (ImageRecognitionResult)recognizer.recognize(goodGate, null);
                trainingData.Add(new FeatureSet(inputVectorForResult(result), new double[] { 1 }));
            }
            foreach (Shape badGate in badGates)
            {
                ImageRecognitionResult result = (ImageRecognitionResult)recognizer.recognize(badGate, null);
                trainingData.Add(new FeatureSet(inputVectorForResult(result), new double[] { 0 }));
            }
            return(trainingData);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructs a universal recognizer that using the specified
        /// recognizers and caching enabled or disabled.
        /// </summary>
        /// <param name="wireRecognizer">the recognizer to use on wires</param>
        /// <param name="textRecognizer">the recognizer to use on text</param>
        /// <param name="gateRecognizer">the recognizer to use on gates</param>
        /// <param name="cacheInnerRecognizers">True if the individual
        /// sub-recognizers should cache their results. (NOTE: you typically
        /// would not want to wrap a UniversalRecognizer in a CachedRecognizer
        /// since the CachedRecognizer ignores the shape classification when
        /// retrieving results. So if you did that and changed the shape
        /// classification, the resulting recognizer may often ignore it.</param>
        /// <exception cref="ArgumentException.ArgumentException">Thrown if
        /// the provided recognizers cannot recognize what they are intended
        /// to (e.g., if the wireRecognizer reports that it cannot recognize
        /// wires).</exception>
        public UniversalRecognizer(
            Recognizer wireRecognizer,
            Recognizer textRecognizer,
            Recognizer gateRecognizer,
            bool cacheInnerRecognizers)
        {
            if (wireRecognizer == null)
            {
                wireRecognizer = new WireRecognizer();
            }
            if (textRecognizer == null)
            {
                textRecognizer = new TextRecognizer();
            }
            if (gateRecognizer == null)
            {
                gateRecognizer = ImageRecognizer.Load(AppDomain.CurrentDomain.BaseDirectory + @"SubRecognizers\ImageRecognizer\Image.ir");
            }

            if (!wireRecognizer.canRecognize("Wire"))
            {
                throw new ArgumentException("The provided wire recognizer '" + wireRecognizer + "' cannot recognize wires!");
            }
            if (!textRecognizer.canRecognize("Text"))
            {
                throw new ArgumentException("The provided text recognizer '" + textRecognizer + "' cannot recognize text!");
            }
            if (!gateRecognizer.canRecognize("Gate"))
            {
                throw new ArgumentException("The provided gate recognizer '" + gateRecognizer + "' cannot recognize gates!");
            }

            if (cacheInnerRecognizers)
            {
                wireRecognizer = new CachedRecognizer(wireRecognizer);
                textRecognizer = new CachedRecognizer(textRecognizer);
                gateRecognizer = new CachedRecognizer(gateRecognizer);
            }

            _wireRecognizer = wireRecognizer;
            _textRecognizer = textRecognizer;
            _gateRecognizer = gateRecognizer;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Constructs a universal recognizer that using the specified
        /// recognizers and caching enabled or disabled.
        /// </summary>
        /// <param name="wireRecognizer">the recognizer to use on wires</param>
        /// <param name="textRecognizer">the recognizer to use on text</param>
        /// <param name="gateRecognizer">the recognizer to use on gates</param>
        /// <exception cref="ArgumentException.ArgumentException">Thrown if
        /// the provided recognizers cannot recognize what they are intended
        /// to (e.g., if the wireRecognizer reports that it cannot recognize
        /// wires).</exception>
        public UniversalRecognizer(
            Recognizer wireRecognizer = null,
            Recognizer textRecognizer = null,
            Recognizer gateRecognizer = null)
        {
            if (wireRecognizer == null)
            {
                wireRecognizer = new WireRecognizer();
            }
            if (textRecognizer == null)
            {
                textRecognizer = new TextRecognizer();
            }
            if (gateRecognizer == null)
            {
#if AIR_OFF
                gateRecognizer = new ImageRecognizer();
#else
                gateRecognizer = AdaptiveImageRecognizer.LoadDefault();
#endif
            }

            if (!wireRecognizer.canRecognize("Wire"))
            {
                throw new ArgumentException("The provided wire recognizer '" + wireRecognizer + "' cannot recognize wires!");
            }
            if (!textRecognizer.canRecognize("Text"))
            {
                throw new ArgumentException("The provided text recognizer '" + textRecognizer + "' cannot recognize text!");
            }
            if (!gateRecognizer.canRecognize("Gate"))
            {
                throw new ArgumentException("The provided gate recognizer '" + gateRecognizer + "' cannot recognize gates!");
            }

            _wireRecognizer = wireRecognizer;
            _textRecognizer = textRecognizer;
            _gateRecognizer = gateRecognizer;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Write a WEKA ARFF file to the given text writer for the given data.
        /// Neither the ARFF file nor Weka is used by this class, but the format
        /// is useful since Weka provides a very nice data exploration tool
        /// (see http://www.cs.waikato.ac.nz/ml/weka/).
        /// NOTE: This function does not close the given writer.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="recognizer"></param>
        /// <param name="goodGates"></param>
        /// <param name="badGates"></param>
        public static void WriteARFF(System.IO.TextWriter writer, ImageRecognizer recognizer, IEnumerable <Shape> goodGates, IEnumerable <Shape> badGates)
        {
            writer.WriteLine("@RELATION neuralImageRecognizerConfidence");
            writer.WriteLine("@ATTRIBUTE partialHausdorff  NUMERIC");
            writer.WriteLine("@ATTRIBUTE modifiedHausdorff NUMERIC");
            writer.WriteLine("@ATTRIBUTE yule              NUMERIC");
            writer.WriteLine("@ATTRIBUTE tanimoto          NUMERIC");
            writer.WriteLine("@ATTRIBUTE imageConfidence   NUMERIC");
            writer.WriteLine("@ATTRIBUTE imageType         {AND,OR,XOR,XNOR,NOR,NOT,NAND,NotBubble}");
            writer.WriteLine("@ATTRIBUTE idealConfidence   NUMERIC");

            writer.WriteLine("@DATA");

            foreach (Shape gate in goodGates)
            {
                ImageRecognitionResult result = (ImageRecognitionResult)recognizer.recognize(gate, null);
                writer.WriteLine(
                    result.PartialHausdorff + "," +
                    result.ModifiedHausdorff + "," +
                    result.Yule + "," +
                    result.Tanimoto + "," +
                    result.Confidence + "," +
                    result.Type.Name + ",1");
            }

            foreach (Shape gate in badGates)
            {
                ImageRecognitionResult result = (ImageRecognitionResult)recognizer.recognize(gate, null);
                writer.WriteLine(
                    result.PartialHausdorff + "," +
                    result.ModifiedHausdorff + "," +
                    result.Yule + "," +
                    result.Tanimoto + "," +
                    result.Confidence + "," +
                    result.Type.Name + ",0");
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Construct a neural image recognizer from the given recognizer and pre-trained network
 /// </summary>
 /// <param name="recognizer"></param>
 /// <param name="network"></param>
 public NeuralImageRecognizer(ImageRecognizer recognizer, NeuralNetwork network)
 {
     _imageRecognizer = recognizer;
     _neuralNetwork   = network;
 }