/// <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); }
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))); }
public static double beam_search(out string result, Intarray inputs, Floatarray costs, OcroFST fst1, OcroFST fst2, int beam_width) { Intarray v1 = new Intarray(); Intarray v2 = new Intarray(); Intarray o = new Intarray(); //fprintf(stderr,"starting beam search\n"); beam_search(v1, v2, inputs, o, costs, fst1, fst2, beam_width); //fprintf(stderr,"finished beam search\n"); FstUtil.remove_epsilons(out result, o); return(NarrayUtil.Sum(costs)); }
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)); }
public static double Perplexity(Floatarray weights) { Floatarray w = new Floatarray(); w.Copy(weights); w /= NarrayUtil.Sum(w); double total = 0.0; for (int i = 0; i < w.Length(); i++) { float value = w[i]; total += value * Math.Log(value); } return(Math.Exp(-total)); }
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)); }
public static double Entropy(Floatarray a) { double z = NarrayUtil.Sum(a); double total = 0.0; for (int i = 0; i < a.Length(); i++) { double p = a[i] / z; if (p < 1e-8) { continue; } total += p * Math.Log(p); } return(-total); }
/// <summary> /// This is a weird, optional method that exposes character segmentation /// for those line recognizers that have it segmentation contains colored pixels, /// and a transition in the transducer of the form * --- 1/eps --> * --- 2/a --> * /// means that pixels with color 1 and 2 together form the letter "a" /// </summary> public override double RecognizeLine(Intarray segmentation_, IGenericFst result, Bytearray image_) { double rate = 0.0; CHECK_ARG(image_.Dim(1) < PGeti("maxheight"), String.Format("input line too high ({0} x {1})", image_.Dim(0), image_.Dim(1))); CHECK_ARG(image_.Dim(1) * 1.0 / image_.Dim(0) < PGetf("maxaspect"), String.Format("input line has bad aspect ratio ({0} x {1})", image_.Dim(0), image_.Dim(1))); bool use_reject = PGetb("use_reject") && !DisableJunk; //Console.WriteLine("IMG: imin:{0} imax:{1}", NarrayUtil.ArgMin(image_), NarrayUtil.ArgMax(image_)); Bytearray image = new Bytearray(); image.Copy(image_); SetLine(image_); if (PGeti("invert") > 0) { NarrayUtil.Sub(NarrayUtil.Max(image), image); } segmentation_.Copy(segmentation); Bytearray available = new Bytearray(); Floatarray cp = new Floatarray(); Floatarray ccosts = new Floatarray(); Floatarray props = new Floatarray(); OutputVector p = new OutputVector(); int ncomponents = grouper.Object.Length(); int minclass = PGeti("minclass"); float minprob = PGetf("minprob"); float space_yes = PGetf("space_yes"); float space_no = PGetf("space_no"); float maxcost = PGetf("maxcost"); // compute priors if possible; fall back on // using no priors if no counts are available Floatarray priors = new Floatarray(); bool use_priors = PGeti("use_priors") > 0; if (use_priors) { if (counts.Length() > 0) { priors.Copy(counts); priors /= NarrayUtil.Sum(priors); } else { if (!counts_warned) { Global.Debugf("warn", "use_priors specified but priors unavailable (old model)"); } use_priors = false; counts_warned = true; } } EstimateSpaceSize(); for (int i = 0; i < ncomponents; i++) { Rect b; Bytearray mask = new Bytearray(); grouper.Object.GetMask(out b, ref mask, i, 0); Bytearray cv = new Bytearray(); grouper.Object.ExtractWithMask(cv, mask, image, i, 0); //ImgIo.write_image_gray("extrmask_image.png", cv); Floatarray v = new Floatarray(); v.Copy(cv); v /= 255.0f; float ccost = classifier.Object.XOutputs(p, v); if (use_reject && classifier.Object.HigherOutputIsBetter) { ccost = 0; float total = p.Sum(); if (total > 1e-11f) { //p /= total; } else { p.Values.Fill(0.0f); } } int count = 0; Global.Debugf("dcost", "output {0}", p.Keys.Length()); for (int index = 0; index < p.Keys.Length(); index++) { int j = p.Keys[index]; if (j < minclass) { continue; } if (j == reject_class) { continue; } float value = p.Values[index]; if (value <= 0.0f) { continue; } if (value < minprob) { continue; } float pcost = classifier.Object.HigherOutputIsBetter ? (float)-Math.Log(value) : value; Global.Debugf("dcost", "{0} {1} {2}", j, pcost + ccost, (j > 32 ? (char)j : '_')); float total_cost = pcost + ccost; if (total_cost < maxcost) { if (use_priors) { total_cost -= (float)-Math.Log(priors[j]); } grouper.Object.SetClass(i, j, total_cost); count++; } } Global.Debugf("dcost", ""); if (count == 0) { float xheight = 10.0f; if (b.Height() < xheight / 2 && b.Width() < xheight / 2) { grouper.Object.SetClass(i, (int)'~', high_cost / 2); } else { grouper.Object.SetClass(i, (int)'#', (b.Width() / xheight) * high_cost); } } if (grouper.Object.PixelSpace(i) > space_threshold) { Global.Debugf("spaces", "space {0}", grouper.Object.PixelSpace(i)); grouper.Object.SetSpaceCost(i, space_yes, space_no); } } grouper.Object.GetLattice(result); return(rate); }
public override void Info() { bool bak = Logger.Default.verbose; Logger.Default.verbose = true; Logger.Default.WriteLine("Linerec"); PPrint(); Logger.Default.WriteLine(String.Format("segmenter: {0}", segmenter.IsEmpty ? "null" : segmenter.Object.Description)); Logger.Default.WriteLine(String.Format("grouper: {0}", grouper.IsEmpty ? "null" : grouper.Object.Description)); Logger.Default.WriteLine(String.Format("counts: {0} {1}", counts.Length(), NarrayUtil.Sum(counts))); //classifier.Object.Info(); Logger.Default.verbose = bak; }