Example #1
0
        /// <summary>
        /// Recognizes the character in the file of the given path.
        /// </summary>
        /// <param name="p">Path to a file with a character image.</param>
        /// <returns>The character in this image.</returns>
        public string recognise(string p)
        {
            // file path 2 array
            ArrayInputData aid = new ArrayInputData(DataManipulation.Bmp2Pattern(p), null);

            // use the network
            Matrix <float> result = mln.use(aid);

            return(Mat2String(result));
        }
Example #2
0
        /// <summary>
        /// Recognizes the character in the file of the given path.
        /// </summary>
        /// <param name="p">Path to a file with a character image.</param>
        /// <returns>The character in this image.</returns>
        public string recognise(string p)
        {
            // file path 2 array
            ArrayInputData aid = new ArrayInputData(DataManipulation.Bmp2Pattern(p), null);

            // use the network
            Matrix<float> result = mln.use(aid);

            return Mat2String(result);
        }
Example #3
0
 /// <summary>
 /// Loads images into an input pattern structure.
 /// </summary>
 /// <param name="p">list of StringInputData, containing the image path as Key and the represented character as Value.</param>
 public void loadTrainImages(List<StringInputData> p)
 {
     trainImages = p;
     trainData = new ArrayInputData(new DenseMatrix(patternSize, p.Count), new DenseMatrix(26, p.Count, 0));
     Matrix<float> inputPattern;
     for (int img = 0; img < p.Count; img++)
     {
         inputPattern = DataManipulation.Bmp2Pattern(p[img].Key);
         trainData.Key.SetColumn(img, inputPattern.Column(0));
         // Careful: only upper letters are covered so far!!
         trainData.Value.At(p[img].Value.ToUpper()[0] - 'A', img, 1.0f);
     }
 }
Example #4
0
        /// <summary>
        /// Loads images into an input pattern structure.
        /// </summary>
        /// <param name="p">list of StringInputData, containing the image path as Key and the represented character as Value.</param>
        public void loadTrainImages(List <StringInputData> p)
        {
            trainImages = p;
            trainData   = new ArrayInputData(new DenseMatrix(patternSize, p.Count), new DenseMatrix(26, p.Count, 0));
            Matrix <float> inputPattern;

            for (int img = 0; img < p.Count; img++)
            {
                inputPattern = DataManipulation.Bmp2Pattern(p[img].Key);
                trainData.Key.SetColumn(img, inputPattern.Column(0));
                // Careful: only upper letters are covered so far!!
                trainData.Value.At(p[img].Value.ToUpper()[0] - 'A', img, 1.0f);
            }
        }
Example #5
0
 /// <summary>
 /// Clears the training data.
 /// </summary>
 public void clearTrainImages()
 {
     trainImages = new List <StringInputData>();
     trainData   = null;
 }
        /// <summary>
        /// Uses the network with one input pattern to genereate output.
        /// </summary>
        /// <param name="pat">input pattern. Size (PatternSize X 1)</param>
        /// <returns>the output matrix of the network</returns>
        internal Matrix<float> use(ArrayInputData pat)
        {
            // convert
            pat.Key = pat.Key.InsertRow(pat.Key.RowCount, new DenseVector(1, 1.0f));

            // pass
            forwardPass(pat.Key);

            // return result
            return net_out;
        }
        /// <summary>
        /// Trains the network using common backprop.
        /// </summary>
        /// <param name="traindata">all training data used in this training session. Matrix must have the size (PatternSize X PatternCount)</param>
        /// <param name="epochs">Number of epochs to train the network.</param>
        internal void backpropTraining(ArrayInputData traindata, int epochs)
        {
            // training pattern init
            Matrix<float> pat = new DenseMatrix(traindata.Key.RowCount + 1, traindata.Key.ColumnCount, 1.0f);
            pat.SetSubMatrix(0, traindata.Key.RowCount, 0, traindata.Key.ColumnCount, traindata.Key);

            // target values init
            Matrix<float> targets = traindata.Value;

            // dw, dv
            Matrix<float> dw = new DenseMatrix(HiddenNodes, PatternSize + 1);
            Matrix<float> dv = new DenseMatrix(TargetSize, HiddenNodes + 1);

            // training
            for (int i = 0; i < epochs; i++)
            {
                // backprop
                forwardPass(pat);
                backwardPass(pat, targets);
                weightUpdate(pat, dw, dv, targets);
            }
        }
Example #8
0
 /// <summary>
 /// Clears the training data.
 /// </summary>
 public void clearTrainImages()
 {
     trainImages = new List<StringInputData>();
     trainData = null;
 }