Example #1
0
        public int recallNetworkGuess(OCRCharacter character)
        {
            // Insert the 64 bit OCR data for the image as inputs.
            double[] imgData = character.getPoints();
            for (int i = 0; i < imgData.Length; i++)
            {
                inputNeuronsArr[i] = (float)imgData[i];
            }

            // Calculate the network
            calculateNetwork();

            float winner = -1;
            int   index  = 0;

            // Find the best fitting output
            for (int i = 0; i < outputs; i++)
            {
                if (outputNeuronsArr[i] > winner)
                {
                    winner = outputNeuronsArr[i];
                    index  = i;
                }
            }

            return(index);
        }
Example #2
0
        public double getDistance(IDistance distCalculator, OCRCharacter otherCharacter)
        {
            double dist = distCalculator.getDistance(this.pointsArr, otherCharacter.getPoints());

            distance = dist;
            return(dist);
        }
Example #3
0
        private static List <OCRCharacter> findKNearestNeighbors(
            List <OCRCharacter> trainListCharacters,
            IDistance calculator, OCRCharacter currentCharacter, int kSize)
        {
            trainListCharacters.Sort(delegate(OCRCharacter itemA, OCRCharacter itemB)
            {
                return(itemA.getDistance(calculator, currentCharacter).CompareTo(itemB.getDistance(calculator, currentCharacter)));
            });

            List <OCRCharacter> listRet = trainListCharacters.Take <OCRCharacter>(kSize).ToList();

            return(listRet);
        }
Example #4
0
        /**
         * Given the 64 bit OCR data for binarised image and the
         * expected outcome value, tests the network for a correct output.
         *
         * Runs the 64 bit inputs through the network and checks
         * which output it gets.
         *
         * @param character: OCRCharacter obj with 64 bit numeric inputs
         *                    representing the OCR data & identifier.
         * @return good or bad (true or false).
         */
        public bool recallNetwork(OCRCharacter character)
        {
            // Populate the input neurons. //
            int answer = character.getIdentifier();

            int index = recallNetworkGuess(character);

            bool correct = index == answer ? true : false;

            // Output feedback
            Console.WriteLine("Input data for: " + answer + " / output: "
                              + index + " (" + answer + ")" + " correct: " + correct);

            return(correct);
        }
Example #5
0
        public static int processNNAndPredict(List <OCRCharacter> charactersTrain,
                                              OCRCharacter characterTest, IDistance distCalc)
        {
            try
            {
                int answer = int.MinValue;

                answer = processAndPredict(characterTest, charactersTrain, distCalc);

                return(answer);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #6
0
 /**
  * Runs the KNN classifier on the given a character and a training set.
  *
  * @param charactersTrain: Training set array.
  * @param charactersTest: Test set array.
  * @param distCalc: Distance metric to use ex: Euclidean, Manhattan etc.
  * @param kSize : The number of neoighbours to choose for classification vote.
  * @param useDistanceScore: True use scoring based on euclidean distance for
  *          classification, False uses majority votes.
  *
  */
 public static int processKNNAndPredict(List <OCRCharacter> charactersTrain,
                                        OCRCharacter characterTest, IDistance distCalc, int kSize,
                                        bool useDistanceScore)
 {
     try
     {
         List <OCRCharacter> nearestNeighbours =
             findKNearestNeighbors(charactersTrain,
                                   distCalc, characterTest, kSize);
         int classLabel = classify(nearestNeighbours, useDistanceScore);
         return(classLabel);
     }
     catch (Exception e)
     {
         Console.WriteLine("ERROR " + e.Message);
         return(-1);
     }
 }
Example #7
0
        private static int processAndPredict(OCRCharacter testChar,
                                             List <OCRCharacter> charactersTrainList, IDistance distCalc)
        {
            double minDistance       = 0;
            int    minCharIdentifier = Int32.MinValue;

            for (int i = 0; i < charactersTrainList.Count; i++)
            {
                // Compare distance between vectors array using a distance metric (ex: EuclideanDistance distance).
                double distance = charactersTrainList[i].getDistance(distCalc, testChar);
                // If got a shorter distance than before, make this the current nearest distance.
                if (minDistance == 0 || distance < minDistance)
                {
                    minDistance       = distance;
                    minCharIdentifier = charactersTrainList[i].getIdentifier();
                }
            }
            return(minCharIdentifier);
        }
Example #8
0
        /**
         * Given a a list of neighbouring characters, checks which character kind has
         * the most frequent occurrence (majority)
         * @param neighbors
         * @param useDistanceScore: True use scoring based on euclidean distance for
         *          classification, False uses majority votes.
         * @return charIdentifier: character which has highest majority.
         */
        private static int classify(List <OCRCharacter> neighbors, bool useDistanceScore)
        {
            Dictionary <int, double> charOccurenceList = new Dictionary <int, double>();

            int num = neighbors.Count;

            for (int index = 0; index < num; index++)
            {
                OCRCharacter temp = neighbors[index];
                int          key  = temp.getIdentifier();

                // If character kind has not been added yet.
                if (!charOccurenceList.ContainsKey(key))
                {
                    // Best practise is to add the total distance of the neighbour char type as score value.

                    if (useDistanceScore)
                    {
                        charOccurenceList.Add(key, 1 / temp.distance);
                    }
                    else
                    {
                        charOccurenceList.Add(key, 1.0);
                    }
                }
                // If already added, add occurence majority vote
                else
                {
                    double value = charOccurenceList[key];
                    // Best practise is to add the total distance of the neighbour char type as score value.

                    if (useDistanceScore)
                    {
                        value += 1 / temp.distance;
                    }
                    else
                    {
                        value += 1.0;
                    }
                    charOccurenceList[key] = value; // update existing value + 1
                }
            }

            // Check which kind of identifiers have the most of the same type
            double majorityScore  = 0;
            int    charIdentifier = -1;

            // Loop and determine which char identifier scored the highest

            foreach (KeyValuePair <int, double> entry in charOccurenceList)
            {
                int    identifier = entry.Key;
                double value      = entry.Value;

                if (value > majorityScore)
                {
                    majorityScore  = value;
                    charIdentifier = identifier;
                }
            }

            return(charIdentifier);
        }