예제 #1
0
        public void Binarize(Bytearray outarray, Floatarray inarray)
        {
            Bytearray image = new Bytearray();

            image.Copy(inarray);
            Binarize(outarray, image);
        }
예제 #2
0
        public int Classify(Floatarray v)
        {
            OutputVector p = new OutputVector();

            XOutputs(p, v);
            return(p.ArgMax());
        }
예제 #3
0
파일: ImgIo.cs 프로젝트: liaoheping/OCRonet
        public static void write_image_gray(string path, Floatarray image)
        {
            Bitmap bitmap = ImgRoutine.NarrayToRgbBitmap(image);

            bitmap.Save(path);
            bitmap.Dispose();
        }
예제 #4
0
        public override void FindBestCuts()
        {
            unchecked
            {
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(cutcosts[i] + 10), 0xff0000);
                }
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(min_thresh + 10), 0x800000);
                }
            }
            Floatarray temp = new Floatarray();

            Gauss.Gauss1d(temp, cutcosts, 3.0f);
            cutcosts.Move(temp);
            SegmRoutine.local_minima(ref bestcuts, cutcosts, min_range, min_thresh);
            for (int i = 0; i < bestcuts.Length(); i++)
            {
                Narray <Point> cut = cuts[bestcuts[i]];
                for (int j = 0; j < cut.Length(); j++)
                {
                    Point p = cut[j];
                    NarrayUtil.ExtPut(dimage, p.X, p.Y, 0x00ff00);
                }
            }
            ///-if(debug.Length > 0) write_image_packed(debug, dimage);
            // dshow1d(cutcosts,"Y");
            //dshow(dimage,"Y");
        }
예제 #5
0
 public override void Binarize(Bytearray outa, Bytearray ina_)
 {
     fraction = (float)PGetf("f");
     Floatarray ina = new Floatarray();
     ina.Copy(ina_);
     binarize_by_range(outa, ina, fraction);
 }
예제 #6
0
        public override bool GetCosts(Floatarray costs, int page, int line, string variant = null)
        {
            costs.Clear();
            costs.Resize(10000);
            costs.Fill(1e38f);

            if (!File.Exists(PathFile(page, line, variant, "costs")))
                return false;
            FileStream fs = null;
            try
            {
                fs = Open(FileMode.Open, page, line, variant, "costs");
                StreamReader reader = new StreamReader(fs);
                int index;
                float cost;
                while (!reader.EndOfStream)
                {
                    string sline = reader.ReadLine();
                    string[] parts = sline.Split(new char[] { ' ' }, 2);
                    if (parts.Length == 2 && int.TryParse(parts[0], out index) && float.TryParse(parts[1], out cost))
                        costs[index] = cost;
                }
                reader.Close();
                fs.Close();
            }
            catch (FileNotFoundException e) { return false; }
            catch (Exception e)
            {
                if (fs != null) fs.Close();
                return false;
            }
            return true;
        }
예제 #7
0
파일: Heap.cs 프로젝트: nickun/OCRonet
        Intarray heapback; // heap[heapback[node]] == node; -1 if not in the heap

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Constructor.
        /// Create a heap storing node indices from 0 to n - 1.
        /// </summary>
        public Heap(int n)
        {
            heap = new Intarray();
            heapback = new Intarray(n);
            heapback.Fill(-1);
            costs = new Floatarray();
        }
예제 #8
0
 /// <summary>
 /// If it's initialized with an array, the result vector
 /// is copied into that array when the vector gets destroyed.
 /// This allows calls like classifier.Outputs(v,x); with
 /// floatarray v.
 /// </summary>
 public OutputVector(Floatarray v)
     : this()
 {
     _result = new Floatarray();
     _result.Copy(v);
     v.Clear();
 }
예제 #9
0
 public override void Extract(Narray<Floatarray> outarrays, Floatarray inarray)
 {
     outarrays.Clear();
     Floatarray image = outarrays.Push();
     rescale(image, inarray);
     //image /= Math.Max(1.0f, NarrayUtil.Max(image));
 }
예제 #10
0
        /// <summary>
        /// Randomly sample an FST, assuming any input.
        /// </summary>
        /// <param name="result">The array of output symbols, excluding epsilons.</param>
        /// <param name="fst">The FST.</param>
        /// <param name="max">The maximum length of the result.</param>
        /// <returns>total cost</returns>
        public static double fst_sample(Intarray result, IGenericFst fst, int max = 1000)
        {
            double total_cost = 0;
            int    current    = fst.GetStart();

            for (int counter = 0; counter < max; counter++)
            {
                Intarray   inputs  = new Intarray();
                Intarray   outputs = new Intarray();
                Intarray   targets = new Intarray();
                Floatarray costs   = new Floatarray();

                fst.Arcs(inputs, targets, outputs, costs, current);

                // now we need to deal with the costs uniformly, so:
                costs.Push(fst.GetAcceptCost(current));
                int choice = sample_by_costs(costs);
                if (choice == costs.Length() - 1)
                {
                    break;
                }
                result.Push(outputs[choice]);
                total_cost += costs[choice];
                current     = targets[choice];
            }
            return(total_cost + fst.GetAcceptCost(current));
        }
예제 #11
0
 public OutputVector()
 {
     _len = 0;
     _keys = new Intarray();
     _values = new Floatarray();
     _result = null;
 }
예제 #12
0
        /// <summary>
        /// Copy one FST to another.
        /// </summary>
        /// <param name="dst">The destination. Will be cleared before copying.</param>
        /// <param name="src">The FST to copy.</param>
        public static void fst_copy(IGenericFst dst, IGenericFst src)
        {
            dst.Clear();
            int n = src.nStates();

            for (int i = 0; i < n; i++)
            {
                dst.NewState();
            }
            dst.SetStart(src.GetStart());
            for (int i = 0; i < n; i++)
            {
                dst.SetAccept(i, src.GetAcceptCost(i));
                Intarray   targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray();
                Floatarray costs = new Floatarray();
                src.Arcs(inputs, targets, outputs, costs, i);
                int inlen = inputs.Length();
                if (inlen != targets.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == targets.length()");
                }
                if (inlen != outputs.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == outputs.length()");
                }
                if (inlen != costs.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == costs.length()");
                }
                for (int j = 0; j < inputs.Length(); j++)
                {
                    dst.AddTransition(i, targets.At1d(j), outputs.At1d(j), costs.At1d(j), inputs.At1d(j));
                }
            }
        }
예제 #13
0
        /// <summary>
        /// Pick an array element with probability proportional to exp(-cost).
        /// </summary>
        public static int sample_by_costs(Floatarray costs)
        {
            Doublearray p = new Doublearray();

            p.Copy(costs);
            double mincost = NarrayUtil.Min(costs);

            p -= mincost;

            for (int i = 0; i < p.Length(); i++)
            {
                p.UnsafePut1d(i, Math.Exp(-p.UnsafeAt1d(i)));
            }
            double sump = NarrayUtil.Sum(p);

            p /= sump;

            double choice = rnd.NextDouble();
            double s      = 0;

            for (int i = 0; i < p.Length(); i++)
            {
                s += p[i];
                if (choice < s)
                {
                    return(i);
                }
            }

            // shouldn't happen...
            return(costs.Length() - 1);
        }
예제 #14
0
 public OutputVector()
 {
     _len    = 0;
     _keys   = new Intarray();
     _values = new Floatarray();
     _result = null;
 }
예제 #15
0
        public override void Input(Floatarray v, int i)
        {
            Floatarray temp = new Floatarray();

            _ds.Input(temp, i);
            _ex.Extract(v, temp);
        }
예제 #16
0
        public void BestPath(Intarray v1, Intarray v2, Intarray inputs,
                             Intarray outputs, Floatarray costs)
        {
            stree.Clear();

            beam.Resize(1);
            beamcost.Resize(1);
            beam[0]     = stree.Add(-1, fst1.GetStart(), fst2.GetStart(), 0, 0, 0);
            beamcost[0] = 0;

            best_so_far      = 0;
            best_cost_so_far = fst1.GetAcceptCost(fst1.GetStart()) +
                               fst2.GetAcceptCost(fst1.GetStart());

            while (beam.Length() > 0)
            {
                Radiate();
            }

            stree.Get(v1, v2, inputs, outputs, costs, best_so_far);
            costs.Push(fst1.GetAcceptCost(stree.v1[best_so_far]) +
                       fst2.GetAcceptCost(stree.v2[best_so_far]));

            //logger("costs", costs);
        }
예제 #17
0
 public static void fill_random(Floatarray v, float lo, float hi)
 {
     for (int i = 0; i < v.Length1d(); i++)
     {
         v.Put1d(i, (float)((hi - lo) * DRandomizer.Default.drand() + lo));
     }
 }
예제 #18
0
        public override float OutputsDense(Floatarray result, Floatarray x_raw)
        {
            CHECK_ARG(x_raw.Length() == w1.Dim(1), "x_raw.Length() == w1.Dim(1)");
            Floatarray z      = new Floatarray();
            int        sparse = PGeti("sparse");
            Floatarray y      = new Floatarray();
            Floatarray x      = new Floatarray();

            x.Copy(x_raw);
            mvmul0(y, w1, x);
            y += b1;
            for (int i = 0; i < y.Length(); i++)
            {
                y[i] = sigmoid(y[i]);
            }
            if (sparse > 0)
            {
                ClassifierUtil.Sparsify(y, sparse);
            }
            mvmul0(z, w2, y);
            z += b2;
            for (int i = 0; i < z.Length(); i++)
            {
                z[i] = sigmoid(z[i]);
            }
            result.Copy(z);
            //int idx = NarrayUtil.ArgMax(result);
            //float val = NarrayUtil.Max(result);
            return(Convert.ToSingle(Math.Abs(NarrayUtil.Sum(z) - 1.0)));
        }
예제 #19
0
 protected void CorrectCostsNull(int vertex)
 {
     if (m_costs[vertex] == null)
     {
         m_costs[vertex] = new Floatarray();
     }
 }
예제 #20
0
        public static void local_min(ref Floatarray result, Floatarray data, int r)
        {
            int n = data.Length();

            result.Resize(n);
            for (int i = 0; i < n; i++)
            {
                float lmin = data[i];
                for (int j = -r; j <= r; j++)
                {
                    int k = i + j;
                    unchecked
                    {
                        if ((uint)(k) >= (uint)(n))
                        {
                            continue;
                        }
                    }
                    if (data[k] >= lmin)
                    {
                        continue;
                    }
                    lmin = data[k];
                }
                result[i] = lmin;
            }
        }
예제 #21
0
        public override void Extract(Narray <Floatarray> outarrays, Floatarray inarray)
        {
            outarrays.Clear();
            Floatarray image = outarrays.Push(new Floatarray());

            image.Copy(inarray);
        }
예제 #22
0
        public static bool a_star_in_composition(Intarray inputs,
                                                 Intarray vertices1,
                                                 Intarray vertices2,
                                                 Intarray outputs,
                                                 Floatarray costs,
                                                 OcroFST fst1,
                                                 Floatarray g1,
                                                 OcroFST fst2,
                                                 Floatarray g2)
        {
            CompositionFst composition = FstFactory.MakeCompositionFst(fst1, fst2);
            bool           result;

            try
            {
                result = a_star2_internal(inputs, vertices1, vertices2, outputs,
                                          costs, fst1, fst2,
                                          g1,
                                          g2, composition);
            }
            catch (Exception ex)
            {
                composition.Move1();
                composition.Move2();
                throw ex;
            }
            composition.Move1();
            composition.Move2();
            return(result);
        }
예제 #23
0
        public void TestRowDataset()
        {
            DRandomizer.Default.init_drand(DateTime.Now.Millisecond);

            // load Mnist datasource
            MnistDatasource mds = new MnistDatasource();
            mds.LoadFromFile(mnistFileNamePrefix);

            // convert mnist to RowDataset8
            RowDataset8 ds8 = MnistDatasetConvert.GetRowDataset8(mds, classes);

            // show random sample to console
            Floatarray fa = new Floatarray();
            int isample = (int)DRandomizer.Default.drand(ds8.nSamples(), 0);
            ds8.Input(fa, isample);
            Console.WriteLine("Char is '{0}'", (char)ds8.Cls(isample));
            NarrayShow.ShowConsole(fa);

            // compare random float sample and original mnist
            StdInput inp1 = new StdInput(mds.ImagesData[isample], mds.ImgHeight, mds.ImgWidth);
            StdInput inp2 = new StdInput(fa);
            Console.WriteLine("Arrays is identical? {0}", Equals(inp1.GetDataBuffer(), inp2.GetDataBuffer()));

            // save RowDataset8 to file
            Console.WriteLine("Saving {0} samples..", ds8.nSamples());
            ds8.Save(mnistFileNamePrefix + dsExt);

            // load RowDataset8 from file
            RowDataset8 ds = new RowDataset8();
            ds.Load(mnistFileNamePrefix + dsExt);
            Console.WriteLine("Loaded {0} samples", ds.nSamples());
        }
예제 #24
0
        public static void threshold_frac(Bytearray thresholded, Floatarray input, float frac)
        {
            float minofinput = NarrayUtil.Min(input);
            float theta      = frac * (NarrayUtil.Max(input) - minofinput) + minofinput;

            binarize_with_threshold(thresholded, input, theta);
        }
예제 #25
0
        public void Get(Intarray r_vertices1,
                 Intarray r_vertices2,
                 Intarray r_inputs,
                 Intarray r_outputs,
                 Floatarray r_costs,
                 int id)
        {
            Intarray t_v1 = new Intarray(); // vertices
            Intarray t_v2 = new Intarray(); // vertices
            Intarray t_i = new Intarray(); // inputs
            Intarray t_o = new Intarray(); // outputs
            Floatarray t_c = new Floatarray(); // costs
            int current = id;
            while (current != -1)
            {
                t_v1.Push(v1[current]);
                t_v2.Push(v2[current]);
                t_i.Push(inputs[current]);
                t_o.Push(outputs[current]);
                t_c.Push(costs[current]);
                current = parents[current];
            }

            NarrayUtil.Reverse(r_vertices1, t_v1);
            NarrayUtil.Reverse(r_vertices2, t_v2);
            NarrayUtil.Reverse(r_inputs, t_i);
            NarrayUtil.Reverse(r_outputs, t_o);
            NarrayUtil.Reverse(r_costs, t_c);
        }
예제 #26
0
        public static bool a_star_in_composition(Intarray inputs,
                                                 Intarray vertices1,
                                                 Intarray vertices2,
                                                 Intarray outputs,
                                                 Floatarray costs,
                                                 OcroFST fst1,
                                                 OcroFST fst2)
        {
            CompositionFst composition = FstFactory.MakeCompositionFst(fst1, fst2);
            bool           result;

            try
            {
                //Floatarray g1 = new Floatarray();
                //Floatarray g2 = new Floatarray();
                fst1.CalculateHeuristics();
                fst2.CalculateHeuristics();
                result = a_star2_internal(inputs, vertices1, vertices2, outputs,
                                          costs, fst1, fst2,
                                          fst1.Heuristics(),
                                          fst2.Heuristics(), composition);
            }
            catch (Exception ex)
            {
                composition.Move1();
                composition.Move2();
                throw ex;
            }
            composition.Move1();
            composition.Move2();
            return(result);
        }
예제 #27
0
        protected override float Outputs(OutputVector result, Floatarray v)
        {
            result.Clear();
            charclass.Object.XOutputs(result, v);
            CHECK_ARG(result.nKeys() > 0, "result.nKeys() > 0");

            if (PGetb("junk") && !DisableJunk && !junkclass.IsEmpty)
            {
                result.Normalize();
                OutputVector jv = new OutputVector();
                junkclass.Object.XOutputs(jv, v);
                for (int i = 0; i < result.nKeys(); i++)
                {
                    result.Values[i] *= jv.Value(0);
                }
                result[jc()] = jv.Value(1);
            }

            if (PGeti("ul") > 0 && !ulclass.IsEmpty)
            {
                throw new Exception("ulclass not implemented");
            }

            return(0.0f);
        }
예제 #28
0
        Floatarray g1, g2; // well, that's against our convention,

        #endregion Fields

        #region Constructors

        public AStarCompositionSearch(Floatarray g1, Floatarray g2, CompositionFst c)
            : base(c)
        {
            this.g1 = g1;
            this.g2 = g2;
            this.c = c;
        }
예제 #29
0
        protected int personal_number = -1;  // personal number

        public MlpClassifier()
        {
            w1 = new Floatarray();
            b1 = new Floatarray();
            w2 = new Floatarray();
            b2 = new Floatarray();
            PDef("eta", 0.5, "default learning rate");
            PDef("eta_init", 0.5, "initial eta");
            PDef("eta_varlog", 1.5, "eta variance in lognormal");
            PDef("hidden_varlog", 1.2, "nhidden variance in lognormal");
            PDef("rounds", 8, "number of training rounds");
            PDef("miters", 8, "number of presentations in multiple of training set");
            PDef("nensemble", 4, "number of mlps in ensemble");
            PDef("hidden_min", 5, "minimum number of hidden units");
            PDef("hidden_lo", 20, "minimum number of hidden units at start");
            PDef("hidden_hi", 80, "maximum number of hidden units at start");
            PDef("hidden_max", 300, "maximum number of hidden units");
            PDef("sparse", -1, "sparsify the hidden layer");
            PDef("cv_split", 0.8, "cross validation split");
            PDef("cv_max", 5000, "max # samples to use for cross validation");
            PDef("normalization", -1, "kind of normalization of the input");
            PDef("noopt", 0, "disable optimization search");
            PDef("crossvalidate", 1, "perform crossvalidation");
            //eta = PGetf("eta");
            cv_error      = 1e30f;
            nn_error      = 1e30f;
            crossvalidate = PGetb("crossvalidate");
            Persist(w1, "w1");
            Persist(b1, "b1");
            Persist(w2, "w2");
            Persist(b2, "b2");
        }
예제 #30
0
 protected void rescale(Floatarray outv, Floatarray sub)
 {
     if (sub.Rank() != 2)
         throw new Exception("CHECK_ARG: sub.Rank()==2");
     int csize = PGeti("csize");
     int indent = PGeti("indent");
     float s = Math.Max(sub.Dim(0), sub.Dim(1)) / (float)(csize - indent - indent);
     if (PGeti("noupscale") > 0 && s < 1.0f)
         s = 1.0f;
     float sig = s * PGetf("aa");
     float dx = (csize * s - sub.Dim(0)) / 2;
     float dy = (csize * s - sub.Dim(1)) / 2;
     if (sig > 1e-3f)
         Gauss.Gauss2d(sub, sig, sig);
     outv.Resize(csize, csize);
     outv.Fill(0f);
     for (int i = 0; i < csize; i++)
     {
         for (int j = 0; j < csize; j++)
         {
             float x = i * s - dx;
             float y = j * s - dy;
             if (x < 0 || x >= sub.Dim(0)) continue;
             if (y < 0 || y >= sub.Dim(1)) continue;
             float value = ImgOps.bilin(sub, x, y);
             outv[i, j] = value;
         }
     }
     /*Global.Debugf("fe", "{0} {1} ({2}) -> {3} {4} ({5})\n",
            sub.Dim(0), sub.Dim(1), NarrayUtil.Max(sub),
            outv.Dim(0), outv.Dim(1), NarrayUtil.Max(outv));*/
 }
예제 #31
0
파일: Heap.cs 프로젝트: liaoheping/OCRonet
        Floatarray costs;  // the cost of the node on the heap

        /// <summary>
        /// Constructor.
        /// Create a heap storing node indices from 0 to n - 1.
        /// </summary>
        public Heap(int n)
        {
            heap     = new Intarray();
            heapback = new Intarray(n);
            heapback.Fill(-1);
            costs = new Floatarray();
        }
예제 #32
0
 public MlpClassifier()
 {
     w1 = new Floatarray();
     b1 = new Floatarray();
     w2 = new Floatarray();
     b2 = new Floatarray();
     PDef("eta", 0.5, "default learning rate");
     PDef("eta_init", 0.5, "initial eta");
     PDef("eta_varlog", 1.5, "eta variance in lognormal");
     PDef("hidden_varlog", 1.2, "nhidden variance in lognormal");
     PDef("rounds", 8, "number of training rounds");
     PDef("miters", 8, "number of presentations in multiple of training set");
     PDef("nensemble", 4, "number of mlps in ensemble");
     PDef("hidden_min", 5, "minimum number of hidden units");
     PDef("hidden_lo", 20, "minimum number of hidden units at start");
     PDef("hidden_hi", 80, "maximum number of hidden units at start");
     PDef("hidden_max", 300, "maximum number of hidden units");
     PDef("sparse", -1, "sparsify the hidden layer");
     PDef("cv_split", 0.8, "cross validation split");
     PDef("cv_max", 5000, "max # samples to use for cross validation");
     PDef("normalization", -1, "kind of normalization of the input");
     PDef("noopt", 0, "disable optimization search");
     PDef("crossvalidate", 1, "perform crossvalidation");
     //eta = PGetf("eta");
     cv_error = 1e30f;
     nn_error = 1e30f;
     crossvalidate = PGetb("crossvalidate");
     Persist(w1, "w1");
     Persist(b1, "b1");
     Persist(w2, "w2");
     Persist(b2, "b2");
 }
예제 #33
0
        public void TestArrays()
        {
            string imgfn = "test-c3.png";

            // load Bytearray
            Bytearray ba = new Bytearray(1, 1);

            ImgIo.read_image_gray(ba, imgfn);
            OcrRoutine.Invert(ba);
            //NarrayUtil.Sub((byte)255, image);
            byte[] bytes1 = ba.To1DArray();
            NarrayShow.ShowConsole(ba);
            StdInput linput1 = new StdInput(ba);

            Console.WriteLine();

            // load StdInput
            Bitmap   bitmap  = ImgIo.LoadBitmapFromFile(imgfn);
            StdInput linput2 = StdInput.FromBitmap(bitmap);
            //NarrayShow.ShowConsole(linput2);

            // test convert
            Floatarray fa      = linput2.ToFloatarray();
            StdInput   linput3 = new StdInput(fa);

            Console.WriteLine("Arrays is identical? {0}", Equals(linput1.GetDataBuffer(), linput2.GetDataBuffer()));
            Console.WriteLine("Arrays is identical? {0}", Equals(linput2.GetDataBuffer(), linput3.GetDataBuffer()));
        }
예제 #34
0
 public override void Arcs(Intarray out_inputs, Intarray out_targets, Intarray out_outputs, Floatarray out_costs, int from)
 {
     out_inputs.Copy(m_inputs[from]);
     out_targets.Copy(m_targets[from]);
     out_outputs.Copy(m_outputs[from]);
     out_costs.Copy(m_costs[from]);
 }
예제 #35
0
        internal static bool a_star2_internal(Intarray inputs,
                                              Intarray vertices1,
                                              Intarray vertices2,
                                              Intarray outputs,
                                              Floatarray costs,
                                              IGenericFst fst1,
                                              IGenericFst fst2,
                                              Floatarray g1,
                                              Floatarray g2,
                                              CompositionFst composition)
        {
            Intarray vertices        = new Intarray();
            AStarCompositionSearch a = new AStarCompositionSearch(g1, g2, composition);

            if (!a.Loop())
            {
                return(false);
            }
            if (!a.reconstruct_vertices(vertices))
            {
                return(false);
            }
            a.reconstruct_edges(inputs, outputs, costs, vertices);
            composition.SplitIndices(vertices1, vertices2, vertices);
            return(true);
        }
예제 #36
0
        /// <summary>
        /// convenience methods
        /// </summary>
        public virtual void Extract(Floatarray v)
        {
            Floatarray temp = new Floatarray();

            Extract(temp, v);
            v.Move(temp);
        }
예제 #37
0
파일: AStarUtil.cs 프로젝트: nickun/OCRonet
 public static bool a_star_in_composition(Intarray inputs,
                        Intarray vertices1,
                        Intarray vertices2,
                        Intarray outputs,
                        Floatarray costs,
                        OcroFST fst1,
                        OcroFST fst2)
 {
     CompositionFst composition = FstFactory.MakeCompositionFst(fst1, fst2);
     bool result;
     try
     {
         //Floatarray g1 = new Floatarray();
         //Floatarray g2 = new Floatarray();
         fst1.CalculateHeuristics();
         fst2.CalculateHeuristics();
         result = a_star2_internal(inputs, vertices1, vertices2, outputs,
                                   costs, fst1, fst2,
                                   fst1.Heuristics(),
                                   fst2.Heuristics(), composition);
     }
     catch (Exception ex)
     {
         composition.Move1();
         composition.Move2();
         throw ex;
     }
     composition.Move1();
     composition.Move2();
     return result;
 }
예제 #38
0
        public void Get(Intarray r_vertices1,
                        Intarray r_vertices2,
                        Intarray r_inputs,
                        Intarray r_outputs,
                        Floatarray r_costs,
                        int id)
        {
            Intarray   t_v1    = new Intarray();   // vertices
            Intarray   t_v2    = new Intarray();   // vertices
            Intarray   t_i     = new Intarray();   // inputs
            Intarray   t_o     = new Intarray();   // outputs
            Floatarray t_c     = new Floatarray(); // costs
            int        current = id;

            while (current != -1)
            {
                t_v1.Push(v1[current]);
                t_v2.Push(v2[current]);
                t_i.Push(inputs[current]);
                t_o.Push(outputs[current]);
                t_c.Push(costs[current]);
                current = parents[current];
            }

            NarrayUtil.Reverse(r_vertices1, t_v1);
            NarrayUtil.Reverse(r_vertices2, t_v2);
            NarrayUtil.Reverse(r_inputs, t_i);
            NarrayUtil.Reverse(r_outputs, t_o);
            NarrayUtil.Reverse(r_costs, t_c);
        }
예제 #39
0
        public void InitData(IDataset ds, int nhidden, Intarray newc2i = null, Intarray newi2c = null)
        {
            CHECK_ARG(nhidden > 1 && nhidden < 1000000, "nhidden > 1 && nhidden < 1000000");
            int ninput  = ds.nFeatures();
            int noutput = ds.nClasses();

            w1.Resize(nhidden, ninput);
            b1.Resize(nhidden);
            w2.Resize(noutput, nhidden);
            b2.Resize(noutput);
            Intarray indexes = new Intarray();

            NarrayUtil.RPermutation(indexes, ds.nSamples());
            Floatarray v = new Floatarray();

            for (int i = 0; i < w1.Dim(0); i++)
            {
                int row = indexes[i];
                ds.Input1d(v, row);
                float normv = (float)NarrayUtil.Norm2(v);
                v /= normv * normv;
                NarrayRowUtil.RowPut(w1, i, v);
            }
            ClassifierUtil.fill_random(b1, -1e-6f, 1e-6f);
            ClassifierUtil.fill_random(w2, -1.0f / nhidden, 1.0f / nhidden);
            ClassifierUtil.fill_random(b2, -1e-6f, 1e-6f);
            if (newc2i != null)
            {
                c2i.Copy(newc2i);
            }
            if (newi2c != null)
            {
                i2c.Copy(newi2c);
            }
        }
예제 #40
0
        public override void TrainDense(IDataset ds)
        {
            int   nclasses = ds.nClasses();
            float miters   = PGetf("miters");
            int   niters   = (int)(ds.nSamples() * miters);

            niters = Math.Max(1000, Math.Min(10000000, niters));
            double     err    = 0.0;
            Floatarray x      = new Floatarray();
            Floatarray z      = new Floatarray();
            Floatarray target = new Floatarray(nclasses);
            int        count  = 0;

            for (int i = 0; i < niters; i++)
            {
                int row = i % ds.nSamples();
                ds.Output(target, row);
                ds.Input1d(x, row);
                TrainOne(z, target, x, PGetf("eta"));
                err += NarrayUtil.Dist2Squared(z, target);
                count++;
            }
            err /= count;
            Global.Debugf("info", "   {4} n {0} niters={1} eta={2:0.#####} errors={3:0.########}",
                          ds.nSamples(), niters, PGetf("eta"), err, FullName);
        }
예제 #41
0
        public void TestSimple()
        {
            Global.SetEnv("debug", Global.GetEnv("debug") + "");
            // image file name to recognize
            string imgFileName           = "line.png";
            string imgCsegFileName       = "line.cseg.png";
            string imgTranscriptFileName = "line.txt";

            // line recognizer
            Linerec lrec = (Linerec)Linerec.LoadLinerec("default.model");

            //Linerec lrec = (Linerec)Linerec.LoadLinerec("2m2-reject.cmodel");
            //Linerec lrec = (Linerec)Linerec.LoadLinerec("multi3.cmodel");
            //Linerec lrec = (Linerec)Linerec.LoadLinerec("latin-ascii.model");
            lrec.Info();

            // language model
            OcroFST default_lmodel = OcroFST.MakeOcroFst();

            default_lmodel.Load("default.fst");
            OcroFST lmodel = default_lmodel;

            // read image
            Bytearray image = new Bytearray(1, 1);

            ImgIo.read_image_gray(image, imgFileName);

            // recognize!
            OcroFST  fst  = OcroFST.MakeOcroFst();
            Intarray rseg = new Intarray();

            lrec.RecognizeLine(rseg, fst, image);

            // show result 1
            string resStr;

            fst.BestPath(out resStr);
            Console.WriteLine("Fst BestPath:   {0}", resStr);

            // find result 2
            Intarray   v1   = new Intarray();
            Intarray   v2   = new Intarray();
            Intarray   inp  = new Intarray();
            Intarray   outp = new Intarray();
            Floatarray c    = new Floatarray();

            BeamSearch.beam_search(v1, v2, inp, outp, c, fst, lmodel, 100);

            FstUtil.remove_epsilons(out resStr, outp);
            Console.WriteLine("Fst BeamSearch: {0}", resStr);

            Intarray cseg = new Intarray();

            SegmRoutine.rseg_to_cseg(cseg, rseg, inp);
            SegmRoutine.make_line_segmentation_white(cseg);
            ImgLabels.simple_recolor(cseg); // for human readable
            ImgIo.write_image_packed(imgCsegFileName, cseg);
            File.WriteAllText(imgTranscriptFileName, resStr.Replace(" ", ""));
        }
예제 #42
0
 public static void binarize_with_threshold(Bytearray result, Floatarray image, float threshold)
 {
     result.MakeLike(image);
     for (int i = 0; i < image.Length1d(); i++)
     {
         result.Put1d(i, image.At1d(i) < threshold ? (byte)0 : (byte)255);
     }
 }
예제 #43
0
 /// <summary>
 /// Return an array of arcs leading from the given node.
 /// WARN_DEPRECATED
 /// </summary>
 public virtual void Arcs(Intarray ids,
                          Intarray targets,
                          Intarray outputs,
                          Floatarray costs,
                          int from)
 {
     throw new NotImplementedException("IGenericFst:Arcs: unimplemented");
 }
예제 #44
0
 public virtual void Extract(Bytearray outa, Bytearray ina)
 {
     Floatarray fina = new Floatarray();
     Floatarray fouta = new Floatarray();
     fina.Copy(ina);
     Extract(fouta, fina);
     outa.Copy(fouta);
 }
예제 #45
0
        public override void Extract(Narray <Floatarray> outarrays, Floatarray inarray)
        {
            outarrays.Clear();
            Floatarray image = outarrays.Push();

            rescale(image, inarray);
            //image /= Math.Max(1.0f, NarrayUtil.Max(image));
        }
예제 #46
0
        public List<List<float>> SpaceCosts(List<Candidate> candidates, Bytearray image)
        {
            /*
                Given a list of character recognition candidates and their
                classifications, and an image of the corresponding text line,
                compute a list of pairs of costs for putting/not putting a space
                after each of the candidate characters.

                The basic idea behind this simple algorithm is to try larger
                and larger horizontal closing operations until most of the components
                start having a "wide" aspect ratio; that's when characters have merged
                into words.  The remaining whitespace should be spaces.

                This is just a simple stopgap measure; it will be replaced with
                trainable space modeling.
             */
            int w = image.Dim(0);
            int h = image.Dim(1);

            Bytearray closed = new Bytearray();
            int r;
            for (r = 0; r < maxrange; r++)
            {
                if (r > 0)
                {
                    closed.Copy(image);
                    Morph.binary_close_circle(closed, r);
                }
                else
                    closed.Copy(image);
                Intarray labeled = new Intarray();
                labeled.Copy(closed);
                ImgLabels.label_components(ref labeled);
                Narray<Rect> rects = new Narray<Rect>();
                ImgLabels.bounding_boxes(ref rects, labeled);
                Floatarray aspects = new Floatarray();
                for (int i = 0; i < rects.Length(); i++)
                {
                    Rect rect = rects[i];
                    float aspect = rect.Aspect();
                    aspects.Push(aspect);
                }
                float maspect = NarrayUtil.Median(aspects);
                if (maspect >= this.aspect_threshold)
                    break;
            }

            // close with a little bit of extra space
            closed.Copy(image);
            Morph.binary_close_circle(closed, r+1);

            // compute the remaining aps
            //Morph.binary_dilate_circle();

            // every character box that ends near a cap gets a space appended

            return null;
        }
예제 #47
0
 protected override float Outputs(OutputVector result, Floatarray v)
 {
     Floatarray outar = new Floatarray();
     float cost = OutputsDense(outar, v);
     result.Clear();
     for (int i = 0; i < outar.Length(); i++)
         result[i2c[i]] = outar[i];
     return cost;
 }
예제 #48
0
        public Intarray v2; // vertices from FST 2

        #endregion Fields

        #region Constructors

        public SearchTree()
        {
            parents = new Intarray();
            inputs = new Intarray();
            outputs = new Intarray();
            v1 = new Intarray();
            v2 = new Intarray();
            costs = new Floatarray();
        }
예제 #49
0
파일: IBatch.cs 프로젝트: nickun/OCRonet
 protected override void Add(Floatarray v, int c)
 {
     if (_ds == null)
     {
         Global.Debugf("info", "allocating {0} buffer for classifier", PGet("cds"));
         _ds = ComponentCreator.MakeComponent<IExtDataset>(PGet("cds"));
     }
     _ds.Add(v, c);
 }
예제 #50
0
파일: AStarUtil.cs 프로젝트: nickun/OCRonet
 public static void a_star_backwards(Floatarray costs_for_all_nodes, IGenericFst fst)
 {
     IGenericFst reverse = FstFactory.MakeOcroFST();
     FstUtil.fst_copy_reverse(reverse, fst, true); // creates an extra vertex
     AStarSearch a = new AStarSearch(reverse);
     a.Loop();
     costs_for_all_nodes.Copy(a.g);
     costs_for_all_nodes.Pop(); // remove the extra vertex
 }
예제 #51
0
 /// <summary>
 /// constructor for a NBest data structure of size n
 /// </summary>
 public PriorityQueue(int n)
 {
     this.n = n;
     ids = new Intarray();
     tags = new Intarray();
     values = new Floatarray();
     ids.Resize(n + 1);
     tags.Resize(n + 1);
     values.Resize(n + 1);
     Clear();
 }
예제 #52
0
파일: AStarUtil.cs 프로젝트: nickun/OCRonet
 public static double a_star(out string result, OcroFST fst)
 {
     result = "";
     Intarray inputs = new Intarray();
     Intarray vertices = new Intarray();
     Intarray outputs = new Intarray();
     Floatarray costs = new Floatarray();
     if (!a_star(inputs, vertices, outputs, costs, fst))
         return 1e38;
     FstUtil.remove_epsilons(out result, outputs);
     return NarrayUtil.Sum(costs);
 }
예제 #53
0
 public static void binarize_by_range(Bytearray outa, Floatarray ina, float fraction)
 {
     float imin = NarrayUtil.Min(ina);
     float imax = NarrayUtil.Max(ina);
     float thresh = (int)(imin + (imax - imin) * fraction);
     outa.MakeLike(ina);
     for (int i = 0; i < ina.Length1d(); i++)
     {
         if (ina.At1d(i) > thresh) outa.Put1d(i, 255);
         else outa.Put1d(i, 0);
     }
 }
예제 #54
0
 public CurvedCutSegmenterImpl()
 {
     wimage = new Intarray();
     costs = new Intarray();
     sources = new Intarray();
     bestcuts = new Intarray();
     dimage = new Intarray();
     cuts = new Narray<Narray<Point>>();
     cutcosts = new Floatarray();
     //params_for_chars();
     params_for_lines();
     //params_from_hwrec_c();
 }
예제 #55
0
파일: AStarUtil.cs 프로젝트: nickun/OCRonet
 public static double a_star(out string result, OcroFST fst1, OcroFST fst2)
 {
     result = "";
     Intarray inputs = new Intarray();
     Intarray v1 = new Intarray();
     Intarray v2 = new Intarray();
     Intarray outputs = new Intarray();
     Floatarray costs = new Floatarray();
     if (!a_star_in_composition(inputs, v1, v2, outputs, costs, fst1, fst2))
         return 1e38;
     FstUtil.remove_epsilons(out result, outputs);
     return NarrayUtil.Sum(costs);
 }
예제 #56
0
파일: BrushFire.cs 프로젝트: nickun/OCRonet
        /// <summary>
        /// SGI compiler bug: can't make this a template function with
        /// an unused last argument for the template parameter
        /// </summary>
        public static void Go(Metric m, ref Floatarray distance, ref Narray<Point> source, float maxdist)
        {
            const float BIG = 1e38f;

            int w = distance.Dim(0);
            int h = distance.Dim(1);
            distance.Resize(w,h);
            source.Resize(w,h);

            Queue<Point> queue = new Queue<Point>(w*h);

            int i, j;
            for(i = 0; i < w; i++) for(j = 0; j < h; j++) {
                if(distance.At(i, j) > 0) {
                    queue.Enqueue(new Point(i, j));
                    distance[i, j] = 0;
                    source[i, j] = new Point(i, j);
                } else {
                    distance[i, j] = BIG;
                    source[i, j] = new Point(-1, -1);
                }
            }

            while(queue.Count != 0) {
                Point q = queue.Dequeue();
                float d = m.metric(new Point(q.X - 1, q.Y), source.At(q.X, q.Y));
                if(d <= maxdist && q.X > 0 && d < distance.At(q.X - 1, q.Y)) {
                    queue.Enqueue(new Point(q.X - 1, q.Y));
                    source[q.X - 1, q.Y] = source.At(q.X, q.Y);
                    distance[q.X - 1, q.Y] = d;
                }
                d = m.metric(new Point(q.X, q.Y - 1), source.At(q.X, q.Y));
                if(d <= maxdist && q.Y > 0 && d < distance.At(q.X, q.Y - 1)) {
                    queue.Enqueue(new Point(q.X, q.Y - 1));
                    source[q.X, q.Y - 1] = source.At(q.X, q.Y);
                    distance[q.X, q.Y - 1] = d;
                }
                d = m.metric(new Point(q.X + 1, q.Y), source.At(q.X, q.Y));
                if(d <= maxdist && q.X < w - 1 && d < distance.At(q.X + 1, q.Y)) {
                    queue.Enqueue(new Point(q.X + 1, q.Y));
                    source[q.X + 1, q.Y] = source.At(q.X, q.Y);
                    distance[q.X + 1, q.Y] = d;
                }
                d = m.metric(new Point(q.X, q.Y + 1), source.At(q.X, q.Y));
                if(d <= maxdist && q.Y < h - 1 && d < distance.At(q.X, q.Y + 1)) {
                    queue.Enqueue(new Point(q.X, q.Y + 1));
                    source[q.X, q.Y + 1] = source.At(q.X, q.Y);
                    distance[q.X, q.Y + 1] = d;
                }
            }
        }
예제 #57
0
 public static double beam_search(out string result, OcroFST fst1, OcroFST fst2,
                                  int beam_width)
 {
     Intarray v1 = new Intarray();
     Intarray v2 = new Intarray();
     Intarray i = new Intarray();
     Intarray o = new Intarray();
     Floatarray c = new Floatarray();
     //fprintf(stderr,"starting beam search\n");
     beam_search(v1, v2, i, o, c, fst1, fst2, beam_width);
     //fprintf(stderr,"finished beam search\n");
     FstUtil.remove_epsilons(out result, o);
     return NarrayUtil.Sum(c);
 }
예제 #58
0
파일: AStarUtil.cs 프로젝트: nickun/OCRonet
 public static bool a_star(Intarray inputs,
         Intarray vertices,
         Intarray outputs,
         Floatarray costs,
         OcroFST fst)
 {
     AStarSearch a = new AStarSearch(fst);
     if (!a.Loop())
         return false;
     if (!a.reconstruct_vertices(vertices))
         return false;
     a.reconstruct_edges(inputs, outputs, costs, vertices);
     return true;
 }
예제 #59
0
        int n; // the number of nodes; also the virtual accept index

        #endregion Fields

        #region Constructors

        public AStarSearch(IGenericFst fst)
        {
            this.fst = fst;
            this.accepted_from = -1;
            this.heap = new Heap(fst.nStates() + 1);
            this.n = fst.nStates();
            this.came_from = new Intarray(n);
            this.came_from.Fill(-1);
            this.g = new Floatarray(n);
            // insert the start node
            int s = fst.GetStart();
            g[s] = 0;
            came_from[s] = s;
            heap.Push(s, Convert.ToSingle(Heuristic(s)));
        }
예제 #60
0
 public virtual void Extract(Floatarray outa, Floatarray ina)
 {
     outa.Clear();
     Narray<Floatarray> items = new Narray<Floatarray>();
     Extract(items, ina);
     //int num = 0;
     for (int i = 0; i < items.Length(); i++)
     {
         Floatarray a = items[i];
         outa.ReserveTo(outa.Length() + a.Length());    // optimization
         for (int j = 0; j < a.Length(); j++)
         {
             outa.Push(a.At1d(j));
             //outa[num++] = a.At1d(j);
         }
     }
 }