예제 #1
0
 public void Clear()
 {
     _keys.Fill(0);
     _keys.Clear();
     _values.Fill(0f);
     _values.Clear();
     _len = 0;
 }
예제 #2
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));*/
 }
예제 #3
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;
        }
예제 #4
0
 public override void ClearLattice()
 {
     class_costs.Dealloc();
     class_costs.Resize(boxes.Length());
     class_outputs.Dealloc();
     class_outputs.Resize(boxes.Length());
     spaces.Resize(boxes.Length(), 2);
     spaces.Fill(float.PositiveInfinity);
 }
예제 #5
0
        public void SetImage(Bytearray image_)
        {
            Bytearray image = new Bytearray();

            //image = image_;
            image.Copy(image_);
            dimage.Copy(image);
            if (PGeti("fill_holes") > 0)
            {
                Bytearray holes = new Bytearray();
                SegmRoutine.extract_holes(ref holes, image);
                for (int i = 0; i < image.Length(); i++)
                {
                    if (holes.At1d(i) > 0)
                    {
                        image.Put1d(i, 255);
                    }
                }
            }
            int w = image.Dim(0), h = image.Dim(1);

            wimage.Resize(w, h);
            wimage.Fill(0);
            float s1 = 0.0f, sy = 0.0f;

            for (int i = 1; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    if (image[i, j] > 0)
                    {
                        s1++; sy += j;
                    }
                    if (image[i, j] > 0)
                    {
                        wimage[i, j] = inside_weight;
                    }
                    else
                    {
                        wimage[i, j] = outside_weight;
                    }
                }
            }
            if (s1 == 0)
            {
                where = image.Dim(1) / 2;
            }
            else
            {
                where = (int)(sy / s1);
            }
            for (int i = 0; i < dimage.Dim(0); i++)
            {
                dimage[i, where] = 0x008000;
            }
        }
예제 #6
0
        public Floatarray AsArray()
        {
            Floatarray result = new Floatarray();

            result.Resize(Length());
            result.Fill(0f);
            for (int i = 0; i < _keys.Length(); i++)
            {
                result.UnsafePut1d(_keys.UnsafeAt1d(i), _values.UnsafeAt1d(i));
            }
            return(result);
        }
예제 #7
0
        public static void fst_line(IGenericFst fst, string s)
        {
            int      n      = s.Length;
            Intarray inputs = new Intarray(n);

            for (int j = 0; j < n; j++)
            {
                inputs[j] = (int)s[j];
            }
            Floatarray costs = new Floatarray(n);

            costs.Fill(0f);
            fst.SetString(s, costs, inputs);
        }
예제 #8
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));*/
        }
예제 #9
0
        public static void scale_to(Floatarray v, Floatarray sub, int csize, float noupscale = 1.0f, float aa = 1.0f)
        {
            // compute the scale factor
            float s = Math.Max(sub.Dim(0), sub.Dim(1)) / (float)csize;

            // don't upscale if that's prohibited
            if (s < noupscale)
            {
                s = 1.0f;
            }

            // compute the offset to keep the input centered in the output
            float dx = (csize * s - sub.Dim(0)) / 2;
            float dy = (csize * s - sub.Dim(1)) / 2;

            // antialiasing via Gaussian convolution
            float sig = s * aa;

            if (sig > 1e-3f)
            {
                Gauss.Gauss2d(sub, sig, sig);
            }

            // now compute the output image via bilinear interpolation
            v.Resize(csize, csize);
            v.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);
                    v[i, j] = value;
                }
            }
        }
예제 #10
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);
        }
예제 #11
0
        protected static void vmmul0(Floatarray result, Floatarray v, Floatarray a)
        {
            int n = a.Dim(0);
            int m = a.Dim(1);

            CHECK_ARG(n == v.Length(), "n == v.Length()");
            result.Resize(m);
            result.Fill(0f);
            for (int i = 0; i < n; i++)
            {
                float value = v.UnsafeAt(i);//v[i];
                if (value == 0f)
                {
                    continue;
                }
                for (int j = 0; j < m; j++)
                {
                    result.UnsafePut(j, result.UnsafeAt(j) + (a.UnsafeAt(i, j) * value));
                }
            }
        }
예제 #12
0
        public void EstimateSpaceSize()
        {
            Intarray labels = new Intarray();

            labels.Copy(segmentation);
            ImgLabels.label_components(ref labels);
            Narray <Rect> boxes = new Narray <Rect>();

            ImgLabels.bounding_boxes(ref boxes, labels);
            Floatarray distances = new Floatarray();

            distances.Resize(boxes.Length());
            distances.Fill(99999f);
            for (int i = 1; i < boxes.Length(); i++)
            {
                Rect b = boxes[i];
                for (int j = 1; j < boxes.Length(); j++)
                {
                    Rect n     = boxes[j];
                    int  delta = n.x0 - b.x1;
                    if (delta < 0)
                    {
                        continue;
                    }
                    if (delta >= distances[i])
                    {
                        continue;
                    }
                    distances[i] = delta;
                }
            }
            float interchar = NarrayUtil.Fractile(distances, PGetf("space_fractile"));

            space_threshold = interchar * PGetf("space_multiplier");
            // impose some reasonable upper and lower bounds
            float xheight = 10.0f; // FIXME

            space_threshold = Math.Max(space_threshold, PGetf("space_min") * xheight);
            space_threshold = Math.Min(space_threshold, PGetf("space_max") * xheight);
        }
예제 #13
0
        public virtual void TrainBatch(IDataset ds, IDataset ts)
        {
            Stopwatch sw = Stopwatch.StartNew();
            bool parallel = PGetb("parallel");
            float eta_init = PGetf("eta_init"); // 0.5
            float eta_varlog = PGetf("eta_varlog"); // 1.5
            float hidden_varlog = PGetf("hidden_varlog"); // 1.2
            int hidden_lo = PGeti("hidden_lo");
            int hidden_hi = PGeti("hidden_hi");
            int rounds = PGeti("rounds");
            int mlp_noopt = PGeti("noopt");
            int hidden_min = PGeti("hidden_min");
            int hidden_max = PGeti("hidden_max");
            CHECK_ARG(hidden_min > 1 && hidden_max < 1000000, "hidden_min > 1 && hidden_max < 1000000");
            CHECK_ARG(hidden_hi >= hidden_lo, "hidden_hi >= hidden_lo");
            CHECK_ARG(hidden_max >= hidden_min, "hidden_max >= hidden_min");
            CHECK_ARG(hidden_lo >= hidden_min && hidden_hi <= hidden_max, "hidden_lo >= hidden_min && hidden_hi <= hidden_max");
            int nn = PGeti("nensemble");
            ObjList<MlpClassifier> nets = new ObjList<MlpClassifier>();
            nets.Resize(nn);
            for (int i = 0; i < nn; i++)
                nets[i] = new MlpClassifier(i);
            Floatarray errs = new Floatarray(nn);
            Floatarray etas = new Floatarray(nn);
            Intarray index = new Intarray();
            float best = 1e30f;
            if (PExists("%error"))
                best = PGetf("%error");
            int nclasses = ds.nClasses();

            /*Floatarray v = new Floatarray();
            for (int i = 0; i < ds.nSamples(); i++)
            {
                ds.Input1d(v, i);
                CHECK_ARG(NarrayUtil.Min(v) > -100 && NarrayUtil.Max(v) < 100, "min(v)>-100 && max(v)<100");
            }*/
            CHECK_ARG(ds.nSamples() >= 10 && ds.nSamples() < 100000000, "ds.nSamples() >= 10 && ds.nSamples() < 100000000");

            for (int i = 0; i < nn; i++)
            {
                // nets(i).init(data.dim(1),logspace(i,nn,hidden_lo,hidden_hi),nclasses);
                if (w1.Length() > 0)
                {
                    nets[i].Copy(this);
                    etas[i] = ClassifierUtil.rLogNormal(eta_init, eta_varlog);
                }
                else
                {
                    nets[i].InitData(ds, (int)(logspace(i, nn, hidden_lo, hidden_hi)), c2i, i2c);
                    etas[i] = PGetf("eta");
                }
            }
            etas[0] = PGetf("eta");     // zero position is identical to itself

            Global.Debugf("info", "mlp training n {0} nc {1}", ds.nSamples(), nclasses);
            for (int round = 0; round < rounds; round++)
            {
                Stopwatch swRound = Stopwatch.StartNew();
                errs.Fill(-1);
                if (parallel)
                {
                    // For each network i
                    Parallel.For(0, nn, i =>
                    {
                        nets[i].PSet("eta", etas[i]);
                        nets[i].TrainDense(ds);     // было XTrain
                        errs[i] = ClassifierUtil.estimate_errors(nets[i], ts);
                    });
                }
                else
                {
                    for (int i = 0; i < nn; i++)
                    {
                        nets[i].PSet("eta", etas[i]);
                        nets[i].TrainDense(ds);     // было XTrain
                        errs[i] = ClassifierUtil.estimate_errors(nets[i], ts);
                        //Global.Debugf("detail", "net({0}) {1} {2} {3}", i,
                        //       errs[i], nets[i].Complexity(), etas[i]);
                    }
                }
                NarrayUtil.Quicksort(index, errs);
                if (errs[index[0]] < best)
                {
                    best = errs[index[0]];
                    cv_error = best;
                    this.Copy(nets[index[0]]);
                    this.PSet("eta", etas[index[0]]);
                    Global.Debugf("info", "  best mlp[{0}] update errors={1} {2}", index[0], best, crossvalidate ? "cv" : "");
                }
                if (mlp_noopt == 0)
                {
                    for (int i = 0; i < nn / 2; i++)
                    {
                        int j = i + nn / 2;
                        nets[index[j]].Copy(nets[index[i]]);
                        int n = nets[index[j]].nHidden();
                        int nm = Math.Min(Math.Max(hidden_min, (int)(ClassifierUtil.rLogNormal(n, hidden_varlog))), hidden_max);
                        nets[index[j]].ChangeHidden(nm);
                        etas[index[j]] = ClassifierUtil.rLogNormal(etas[index[i]], eta_varlog);
                    }
                }
                Global.Debugf("info", " end mlp round {0} err {1} nHidden {2}", round, best, nHidden());
                swRound.Stop();
                int totalTest= ts.nSamples();
                int errCnt = Convert.ToInt32(best * totalTest);
                OnTrainRound(this, new TrainEventArgs(
                    round, best, totalTest - errCnt, totalTest, best, swRound.Elapsed, TimeSpan.Zero
                    ));
            }

            sw.Stop();
            Global.Debugf("info", String.Format("          training time: {0} minutes, {1} seconds",
                (int)sw.Elapsed.TotalMinutes, sw.Elapsed.Seconds));
            PSet("%error", best);
            int nsamples = ds.nSamples() * rounds;
            if (PExists("%nsamples"))
                nsamples += PGeti("%nsamples");
            PSet("%nsamples", nsamples);
        }
예제 #14
0
        protected void rescale(Floatarray v, Floatarray input)
        {
            if (input.Rank() != 2)
            {
                throw new Exception("CHECK_ARG: sub.Rank()==2");
            }

            Floatarray sub = new Floatarray();
            // find the largest connected component
            // and crop to its bounding box
            // (use a binary version of the character
            // to compute the bounding box)
            Intarray components = new Intarray();
            float    threshold  = PGetf("threshold") * NarrayUtil.Max(input);

            Global.Debugf("biggestcc", "threshold {0}", threshold);
            components.MakeLike(input);
            components.Fill(0);
            for (int i = 0; i < components.Length(); i++)
            {
                components[i] = (input[i] > threshold ? 1 : 0);
            }
            int      n      = ImgLabels.label_components(ref components);
            Intarray totals = new Intarray(n + 1);

            totals.Fill(0);
            for (int i = 0; i < components.Length(); i++)
            {
                totals[components[i]]++;
            }
            totals[0] = 0;
            Narray <Rect> boxes = new Narray <Rect>();

            ImgLabels.bounding_boxes(ref boxes, components);
            int  biggest = NarrayUtil.ArgMax(totals);
            Rect r       = boxes[biggest];
            int  pad     = (int)(PGetf("pad") + 0.5f);

            r.PadBy(pad, pad);
            Global.Debugf("biggestcc", "({0}) {1}[{2}] :: {3} {4} {5} {6}",
                          n, biggest, totals[biggest],
                          r.x0, r.y0, r.x1, r.y1);

            // now perform normal feature extraction
            // (use the original grayscale input)
            sub = input;
            ImgMisc.Crop(sub, r);
            int   csize = PGeti("csize");
            float s     = Math.Max(sub.Dim(0), sub.Dim(1)) / (float)csize;

            if (PGetf("noupscale") > 0 && s < 1.0f)
            {
                s = 1.0f;
            }
            float sig = s * PGetf("aa");
            float dx  = (csize * s - sub.Dim(0)) / 2f;
            float dy  = (csize * s - sub.Dim(1)) / 2f;

            if (sig > 1e-3f)
            {
                Gauss.Gauss2d(sub, sig, sig);
            }
            v.Resize(csize, csize);
            v.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);
                    v[i, j] = value;
                }
            }

            /*Global.Debugf("biggestcc", "{0} {1} ({2}) -> {3} {4} ({5})",
             *     sub.Dim(0), sub.Dim(1), NarrayUtil.Max(sub),
             *     v.Dim(0), v.Dim(1), NarrayUtil.Max(v));*/
        }
예제 #15
0
 public Floatarray AsArray()
 {
     Floatarray result = new Floatarray();
     result.Resize(Length());
     result.Fill(0f);
     for (int i = 0; i < _keys.Length(); i++)
         result.UnsafePut1d(_keys.UnsafeAt1d(i), _values.UnsafeAt1d(i));
     return result;
 }
예제 #16
0
        protected void rescale(Floatarray v, Floatarray input)
        {
            if (input.Rank() != 2)
                throw new Exception("CHECK_ARG: sub.Rank()==2");

            Floatarray sub = new Floatarray();
            // find the largest connected component
            // and crop to its bounding box
            // (use a binary version of the character
            // to compute the bounding box)
            Intarray components = new Intarray();
            float threshold = PGetf("threshold") * NarrayUtil.Max(input);
            Global.Debugf("biggestcc", "threshold {0}", threshold);
            components.MakeLike(input);
            components.Fill(0);
            for (int i = 0; i < components.Length(); i++)
                components[i] = (input[i] > threshold ? 1 : 0);
            int n = ImgLabels.label_components(ref components);
            Intarray totals = new Intarray(n + 1);
            totals.Fill(0);
            for (int i = 0; i < components.Length(); i++)
                totals[components[i]]++;
            totals[0] = 0;
            Narray<Rect> boxes = new Narray<Rect>();
            ImgLabels.bounding_boxes(ref boxes, components);
            int biggest = NarrayUtil.ArgMax(totals);
            Rect r = boxes[biggest];
            int pad = (int)(PGetf("pad") + 0.5f);
            r.PadBy(pad, pad);
            Global.Debugf("biggestcc", "({0}) {1}[{2}] :: {3} {4} {5} {6}",
                   n, biggest, totals[biggest],
                   r.x0, r.y0, r.x1, r.y1);

            // now perform normal feature extraction
            // (use the original grayscale input)
            sub = input;
            ImgMisc.Crop(sub, r);
            int csize = PGeti("csize");
            float s = Math.Max(sub.Dim(0), sub.Dim(1))/(float)csize;
            if(PGetf("noupscale") > 0 && s < 1.0f)
                s = 1.0f;
            float sig = s * PGetf("aa");
            float dx = (csize*s-sub.Dim(0))/2f;
            float dy = (csize*s-sub.Dim(1))/2f;
            if(sig > 1e-3f)
                Gauss.Gauss2d(sub, sig, sig);
            v.Resize(csize, csize);
            v.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);
                    v[i, j] = value;
                }
            }
            /*Global.Debugf("biggestcc", "{0} {1} ({2}) -> {3} {4} ({5})",
                   sub.Dim(0), sub.Dim(1), NarrayUtil.Max(sub),
                   v.Dim(0), v.Dim(1), NarrayUtil.Max(v));*/
        }
예제 #17
0
        public static void scale_to(Floatarray v, Floatarray sub, int csize, float noupscale=1.0f, float aa=1.0f)
        {
            // compute the scale factor
            float s = Math.Max(sub.Dim(0), sub.Dim(1))/(float)csize;

            // don't upscale if that's prohibited
            if(s < noupscale)
                s = 1.0f;

            // compute the offset to keep the input centered in the output
            float dx = (csize*s-sub.Dim(0))/2;
            float dy = (csize*s-sub.Dim(1))/2;

            // antialiasing via Gaussian convolution
            float sig = s * aa;
            if(sig > 1e-3f)
                Gauss.Gauss2d(sub, sig, sig);

            // now compute the output image via bilinear interpolation
            v.Resize(csize, csize);
            v.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);
                    v[i, j] = value;
                }
            }
        }
예제 #18
0
파일: IDataset.cs 프로젝트: nickun/OCRonet
 public virtual void Output(Floatarray outv, int i)
 {
     outv.Resize(nClasses());
     outv.Fill(limit);
     outv.Put1d(Cls(i), 1 - limit);
 }
예제 #19
0
 public virtual void Output(Floatarray outv, int i)
 {
     outv.Resize(nClasses());
     outv.Fill(limit);
     outv.Put1d(Cls(i), 1 - limit);
 }
예제 #20
0
 protected static void vmmul0(Floatarray result, Floatarray v, Floatarray a)
 {
     int n = a.Dim(0);
     int m = a.Dim(1);
     CHECK_ARG(n == v.Length(), "n == v.Length()");
     result.Resize(m);
     result.Fill(0f);
     for (int i = 0; i < n; i++)
     {
         float value = v.UnsafeAt(i);//v[i];
         if (value == 0f)
             continue;
         for (int j = 0; j < m; j++)
             result.UnsafePut(j, result.UnsafeAt(j) + (a.UnsafeAt(i, j) * value));
     }
 }
예제 #21
0
파일: Linerec.cs 프로젝트: nickun/OCRonet
 public void EstimateSpaceSize()
 {
     Intarray labels = new Intarray();
     labels.Copy(segmentation);
     ImgLabels.label_components(ref labels);
     Narray<Rect> boxes = new Narray<Rect>();
     ImgLabels.bounding_boxes(ref boxes, labels);
     Floatarray distances = new Floatarray();
     distances.Resize(boxes.Length());
     distances.Fill(99999f);
     for (int i = 1; i < boxes.Length(); i++)
     {
         Rect b = boxes[i];
         for (int j = 1; j < boxes.Length(); j++)
         {
             Rect n = boxes[j];
             int delta = n.x0 - b.x1;
             if (delta < 0) continue;
             if (delta >= distances[i]) continue;
             distances[i] = delta;
         }
     }
     float interchar = NarrayUtil.Fractile(distances, PGetf("space_fractile"));
     space_threshold = interchar * PGetf("space_multiplier");
     // impose some reasonable upper and lower bounds
     float xheight = 10.0f; // FIXME
     space_threshold = Math.Max(space_threshold, PGetf("space_min") * xheight);
     space_threshold = Math.Min(space_threshold, PGetf("space_max") * xheight);
 }
예제 #22
0
파일: FstUtil.cs 프로젝트: nickun/OCRonet
 public static void fst_line(IGenericFst fst, string s)
 {
     int n = s.Length;
     Intarray inputs = new Intarray(n);
     for(int j = 0; j < n; j++)
         inputs[j] = (int)s[j];
     Floatarray costs = new Floatarray(n);
     costs.Fill(0f);
     fst.SetString(s, costs, inputs);
 }
예제 #23
0
        public virtual void TrainBatch(IDataset ds, IDataset ts)
        {
            Stopwatch sw            = Stopwatch.StartNew();
            bool      parallel      = PGetb("parallel");
            float     eta_init      = PGetf("eta_init");      // 0.5
            float     eta_varlog    = PGetf("eta_varlog");    // 1.5
            float     hidden_varlog = PGetf("hidden_varlog"); // 1.2
            int       hidden_lo     = PGeti("hidden_lo");
            int       hidden_hi     = PGeti("hidden_hi");
            int       rounds        = PGeti("rounds");
            int       mlp_noopt     = PGeti("noopt");
            int       hidden_min    = PGeti("hidden_min");
            int       hidden_max    = PGeti("hidden_max");

            CHECK_ARG(hidden_min > 1 && hidden_max < 1000000, "hidden_min > 1 && hidden_max < 1000000");
            CHECK_ARG(hidden_hi >= hidden_lo, "hidden_hi >= hidden_lo");
            CHECK_ARG(hidden_max >= hidden_min, "hidden_max >= hidden_min");
            CHECK_ARG(hidden_lo >= hidden_min && hidden_hi <= hidden_max, "hidden_lo >= hidden_min && hidden_hi <= hidden_max");
            int nn = PGeti("nensemble");
            ObjList <MlpClassifier> nets = new ObjList <MlpClassifier>();

            nets.Resize(nn);
            for (int i = 0; i < nn; i++)
            {
                nets[i] = new MlpClassifier(i);
            }
            Floatarray errs  = new Floatarray(nn);
            Floatarray etas  = new Floatarray(nn);
            Intarray   index = new Intarray();
            float      best  = 1e30f;

            if (PExists("%error"))
            {
                best = PGetf("%error");
            }
            int nclasses = ds.nClasses();

            /*Floatarray v = new Floatarray();
             * for (int i = 0; i < ds.nSamples(); i++)
             * {
             *  ds.Input1d(v, i);
             *  CHECK_ARG(NarrayUtil.Min(v) > -100 && NarrayUtil.Max(v) < 100, "min(v)>-100 && max(v)<100");
             * }*/
            CHECK_ARG(ds.nSamples() >= 10 && ds.nSamples() < 100000000, "ds.nSamples() >= 10 && ds.nSamples() < 100000000");

            for (int i = 0; i < nn; i++)
            {
                // nets(i).init(data.dim(1),logspace(i,nn,hidden_lo,hidden_hi),nclasses);
                if (w1.Length() > 0)
                {
                    nets[i].Copy(this);
                    etas[i] = ClassifierUtil.rLogNormal(eta_init, eta_varlog);
                }
                else
                {
                    nets[i].InitData(ds, (int)(logspace(i, nn, hidden_lo, hidden_hi)), c2i, i2c);
                    etas[i] = PGetf("eta");
                }
            }
            etas[0] = PGetf("eta");     // zero position is identical to itself

            Global.Debugf("info", "mlp training n {0} nc {1}", ds.nSamples(), nclasses);
            for (int round = 0; round < rounds; round++)
            {
                Stopwatch swRound = Stopwatch.StartNew();
                errs.Fill(-1);
                if (parallel)
                {
                    // For each network i
                    Parallel.For(0, nn, i =>
                    {
                        nets[i].PSet("eta", etas[i]);
                        nets[i].TrainDense(ds);     // было XTrain
                        errs[i] = ClassifierUtil.estimate_errors(nets[i], ts);
                    });
                }
                else
                {
                    for (int i = 0; i < nn; i++)
                    {
                        nets[i].PSet("eta", etas[i]);
                        nets[i].TrainDense(ds);     // было XTrain
                        errs[i] = ClassifierUtil.estimate_errors(nets[i], ts);
                        //Global.Debugf("detail", "net({0}) {1} {2} {3}", i,
                        //       errs[i], nets[i].Complexity(), etas[i]);
                    }
                }
                NarrayUtil.Quicksort(index, errs);
                if (errs[index[0]] < best)
                {
                    best     = errs[index[0]];
                    cv_error = best;
                    this.Copy(nets[index[0]]);
                    this.PSet("eta", etas[index[0]]);
                    Global.Debugf("info", "  best mlp[{0}] update errors={1} {2}", index[0], best, crossvalidate ? "cv" : "");
                }
                if (mlp_noopt == 0)
                {
                    for (int i = 0; i < nn / 2; i++)
                    {
                        int j = i + nn / 2;
                        nets[index[j]].Copy(nets[index[i]]);
                        int n  = nets[index[j]].nHidden();
                        int nm = Math.Min(Math.Max(hidden_min, (int)(ClassifierUtil.rLogNormal(n, hidden_varlog))), hidden_max);
                        nets[index[j]].ChangeHidden(nm);
                        etas[index[j]] = ClassifierUtil.rLogNormal(etas[index[i]], eta_varlog);
                    }
                }
                Global.Debugf("info", " end mlp round {0} err {1} nHidden {2}", round, best, nHidden());
                swRound.Stop();
                int totalTest = ts.nSamples();
                int errCnt    = Convert.ToInt32(best * totalTest);
                OnTrainRound(this, new TrainEventArgs(
                                 round, best, totalTest - errCnt, totalTest, best, swRound.Elapsed, TimeSpan.Zero
                                 ));
            }

            sw.Stop();
            Global.Debugf("info", String.Format("          training time: {0} minutes, {1} seconds",
                                                (int)sw.Elapsed.TotalMinutes, sw.Elapsed.Seconds));
            PSet("%error", best);
            int nsamples = ds.nSamples() * rounds;

            if (PExists("%nsamples"))
            {
                nsamples += PGeti("%nsamples");
            }
            PSet("%nsamples", nsamples);
        }