예제 #1
0
        /// <summary>
        /// Trains the adaptive image recognizer on one of the incorrectly recognized gates
        /// in the training set.
        /// </summary>
        private void learnOne()
        {
            // Make a list of the incorrectly recognized gates from the training set
            List <Sketch.Shape> wrong = new List <Sketch.Shape>();

            foreach (Sketch.Shape train in _trainSet.Keys)
            {
                if (!_trainSet[train])
                {
                    wrong.Add(train);
                }
            }

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

            // chose a random gate from your list
            int index = (new Random()).Next(wrong.Count);

            Sketch.Shape learn = wrong[index];

            // learnFromExample requires a shape to have all it's attributes updated by the recognizer
            // (ie. - template used and recognition results)
            // However, we don't want to change the shapes in _gates, so clone it first.
            Sketch.Shape clone = learn.Clone();
            _recognizer.processShape(clone, null);
            clone.Type = learn.Type;

            // Teach the recognizer the correct recognition for the shape, then
            // update dictionaries, etc. accordingly.
            _recognizer.learnFromExample(clone);
            _numTemplatesAdded++;
            _trainSet.Remove(learn);
            _alreadySet.Add(learn, false);
        }
예제 #2
0
        public override void finalize(System.IO.TextWriter handle, string path)
        {
            foreach (Sketch.Shape shape in _gates)
            {
                if (recognizeShape(shape))
                {
                    _right.Add(shape);
                }
                else
                {
                    _wrong.Add(shape);
                }
            }

            while (_numTemplates < _maxTemplates)
            {
                //We need to add some wront templates to the train set, and others to the
                //test set.  This is the percentage that is added to the testing set.
                _tempPercent = 0.5;

                //The loop only stops when there are no more wrongly recognized shapes anywhere.
                if (Math.Ceiling(_wrong.Count * _tempPercent) > _wrong.Count)
                {
                    break;
                }

                _train.RemoveRange(0, _train.Count);
                _test.RemoveRange(0, _test.Count);
                shuffleList(ref _right);
                shuffleList(ref _wrong);
                //add all of the "right" list to the test set
                //this is because having a "right" template in the
                //train set does us no good.
                for (int i = 0; i < _right.Count; i++)
                {
                    _test.Add(_right[i]);
                }
                //add a percentage of the "wrong" list to the test set
                for (int i = 0; i < _wrong.Count * _tempPercent; i++)
                {
                    _test.Add(_wrong[i]);
                }

                //add a percentage of the "wrong" list to the train set
                for (int i = (int)Math.Ceiling(_wrong.Count * _tempPercent); i < _wrong.Count; i++)
                {
                    _train.Add(_wrong[i]);
                }

                if (_train.Count == 0)
                {
                    break;
                }

                //just for good measure, to make sure they're random
                shuffleList(ref _test);
                shuffleList(ref _train);

                #region Learn One Gate
                // chose a random gate from your list

                Sketch.Shape learn = new Sketch.Shape();
                int          index = (new Random()).Next(_train.Count - 1);
                learn = _train[index];

                //We don't want to test on what we train on, and
                //we don't want to train on what we already have
                //trained on.
                _gates.Remove(learn);

                // learnFromExample requires a shape to have all it's attributes updated by the recognizer
                // (ie. - template used and recognition results)
                // However, we don't want to change the shapes in _gates, so clone it first.
                Sketch.Shape clone = learn.Clone();
                _recognizer.processShape(clone, null);
                clone.Type = learn.Type;

                // Teach the recognizer the correct recognition for the shape, then
                // update dictionaries, etc. accordingly.
                _recognizer.learnFromExample(clone);
                _numTemplates++;
                #endregion



                // We empty the right and wrong lists so that we can refill them with
                // new recognition results based on the adaptation
                _right.RemoveRange(0, _right.Count);
                _wrong.RemoveRange(0, _wrong.Count);

                foreach (Sketch.Shape shape in _gates)
                {
                    if (recognizeShape(shape))
                    {
                        _right.Add(shape);
                    }
                    else
                    {
                        _wrong.Add(shape);
                    }
                }

                if (_right.Count > _test.Count)
                {
                    break;
                }

                _results.Add(_numTemplates, (double)_right.Count / (double)_test.Count);

                if (_right.Count == _test.Count)
                {
                    break;
                }
            }

            #region Write Results
            // general information about the tests we ran
            handle.WriteLine("Total number of gates: " + _gates.Count);
            handle.WriteLine("Max templates per gate: " + _maxTemplates);
            handle.WriteLine("First file: " + _filename);

            handle.WriteLine();

            // headers
            handle.Write("Num templates added,");
            handle.Write("Percent accuracy,");

            handle.WriteLine();

            // our results
            foreach (var result in _results)
            {
                handle.WriteLine(result.Key + "," + result.Value);
            }
            #endregion
        }