/// <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)); } } }
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); } }
/// <summary> /// Brushfire transformation using 2-norm. /// </summary> public static void brushfire_2(ref Floatarray distance, ref Narray <Point> source, float maxdist) { BrushFire.Go(Metric2.Default, ref distance, ref source, maxdist); for (int i = 0; i < distance.Length1d(); i++) { distance.Put1d(i, (float)Math.Sqrt(distance.At1d(i))); } }
public static void Sparsify(Floatarray a, int n) { NBest nbest = new NBest(n); for (int i = 0; i < a.Length1d(); i++) { nbest.Add(i, Math.Abs(a.At1d(i))); } double threshold = nbest.Value(n - 1); for (int i = 0; i < a.Length1d(); i++) { if (Math.Abs(a.At1d(i)) < threshold) { a.Put1d(i, 0f); } } }
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); } }
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); } } }
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); } } }
public void Copy(Floatarray v, float eps = 1e-11f) { Clear(); int n = v.Length(); for (int i = 0; i < n; i++) { float value = v.At1d(i); if (Math.Abs(value) >= eps) { _keys.Push(i); _values.Push(value); } } _len = v.Length(); _keys.Resize(_len); for (int i = 0; i < _len; i++) { _keys.Put1d(i, i); } _values.Copy(v); }
/// <summary> /// Reverse the FST's arcs, adding a new start vertex (former accept). /// </summary> public static void fst_copy_reverse(IGenericFst dst, IGenericFst src, bool no_accept = false) { dst.Clear(); int n = src.nStates(); for (int i = 0; i <= n; i++) { dst.NewState(); } if (!no_accept) { dst.SetAccept(src.GetStart()); } dst.SetStart(n); for (int i = 0; i < n; i++) { dst.AddTransition(n, i, 0, src.GetAcceptCost(i), 0); Intarray targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray(); Floatarray costs = new Floatarray(); src.Arcs(inputs, targets, outputs, costs, i); if (inputs.Length() != targets.Length()) { throw new Exception("ASSERT: inputs.length() == targets.length()"); } if (inputs.Length() != outputs.Length()) { throw new Exception("ASSERT: inputs.length() == outputs.length()"); } if (inputs.Length() != costs.Length()) { throw new Exception("ASSERT: inputs.length() == costs.length()"); } for (int j = 0; j < inputs.Length(); j++) { dst.AddTransition(targets.At1d(j), i, outputs.At1d(j), costs.At1d(j), inputs.At1d(j)); } } }
/// <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)); } }
public override void Arcs(Intarray ids, Intarray targets, Intarray outputs, Floatarray costs, int node) { int n1 = node / l2.nStates(); int n2 = node % l2.nStates(); Intarray ids1 = new Intarray(); Intarray ids2 = new Intarray(); Intarray t1 = new Intarray(); Intarray t2 = new Intarray(); Intarray o1 = new Intarray(); Intarray o2 = new Intarray(); Floatarray c1 = new Floatarray(); Floatarray c2 = new Floatarray(); l1.Arcs(ids1, t1, o1, c1, n1); l2.Arcs(ids2, t2, o2, c2, n2); // sort & permute Intarray p1 = new Intarray(); Intarray p2 = new Intarray(); NarrayUtil.Quicksort(p1, o1); NarrayUtil.Permute(ids1, p1); NarrayUtil.Permute(t1, p1); NarrayUtil.Permute(o1, p1); NarrayUtil.Permute(c1, p1); NarrayUtil.Quicksort(p2, ids2); NarrayUtil.Permute(ids2, p2); NarrayUtil.Permute(t2, p2); NarrayUtil.Permute(o2, p2); NarrayUtil.Permute(c2, p2); int k1, k2; // l1 epsilon moves for (k1 = 0; k1 < o1.Length() && o1.At1d(k1) == 0; k1++) { ids.Push(ids1.At1d(k1)); targets.Push(Combine(t1.At1d(k1), n2)); outputs.Push(0); costs.Push(c1.At1d(k1)); } // l2 epsilon moves for (k2 = 0; k2 < o2.Length() && ids2.At1d(k2) == 0; k2++) { ids.Push(0); targets.Push(Combine(n1, t2.At1d(k2))); outputs.Push(o2.At1d(k2)); costs.Push(c2.At1d(k2)); } // non-epsilon moves while (k1 < o1.Length() && k2 < ids2.Length()) { while (k1 < o1.Length() && o1.At1d(k1) < ids2.At1d(k2)) k1++; if (k1 >= o1.Length()) break; while (k2 < ids2.Length() && o1.At1d(k1) > ids2.At1d(k2)) k2++; while (k1 < o1.Length() && k2 < ids2.Length() && o1.At1d(k1) == ids2.At1d(k2)) { for (int j = k2; j < ids2.Length() && o1.At1d(k1) == ids2.At1d(j); j++) { ids.Push(ids1.At1d(k1)); targets.Push(Combine(t1.At1d(k1), t2.At1d(j))); outputs.Push(o2.At1d(j)); costs.Push(c1.At1d(k1) + c2.At1d(j)); } k1++; } } }
public float Value(int i) { return(_values.At1d(i)); }
/// <summary> /// Brushfire transformation using 2-norm. /// </summary> public static void brushfire_2(ref Floatarray distance, ref Narray<Point> source, float maxdist) { BrushFire.Go(Metric2.Default, ref distance, ref source, maxdist); for (int i = 0; i < distance.Length1d(); i++) distance.Put1d(i, (float)Math.Sqrt(distance.At1d(i))); }
/// <summary> /// Reverse the FST's arcs, adding a new start vertex (former accept). /// </summary> public static void fst_copy_reverse(IGenericFst dst, IGenericFst src, bool no_accept = false) { dst.Clear(); int n = src.nStates(); for (int i = 0; i <= n; i++) dst.NewState(); if (!no_accept) dst.SetAccept(src.GetStart()); dst.SetStart(n); for (int i = 0; i < n; i++) { dst.AddTransition(n, i, 0, src.GetAcceptCost(i), 0); Intarray targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray(); Floatarray costs = new Floatarray(); src.Arcs(inputs, targets, outputs, costs, i); if (inputs.Length() != targets.Length()) throw new Exception("ASSERT: inputs.length() == targets.length()"); if (inputs.Length() != outputs.Length()) throw new Exception("ASSERT: inputs.length() == outputs.length()"); if (inputs.Length() != costs.Length()) throw new Exception("ASSERT: inputs.length() == costs.length()"); for (int j = 0; j < inputs.Length(); j++) dst.AddTransition(targets.At1d(j), i, outputs.At1d(j), costs.At1d(j), inputs.At1d(j)); } }
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); }
public void Copy(Floatarray v, float eps = 1e-11f) { Clear(); int n = v.Length(); for (int i = 0; i < n; i++) { float value = v.At1d(i); if (Math.Abs(value) >= eps) { _keys.Push(i); _values.Push(value); } } _len = v.Length(); _keys.Resize(_len); for (int i = 0; i < _len; i++) _keys.Put1d(i, i); _values.Copy(v); }
public override void Arcs(Intarray ids, Intarray targets, Intarray outputs, Floatarray costs, int node) { int n1 = node / l2.nStates(); int n2 = node % l2.nStates(); Intarray ids1 = new Intarray(); Intarray ids2 = new Intarray(); Intarray t1 = new Intarray(); Intarray t2 = new Intarray(); Intarray o1 = new Intarray(); Intarray o2 = new Intarray(); Floatarray c1 = new Floatarray(); Floatarray c2 = new Floatarray(); l1.Arcs(ids1, t1, o1, c1, n1); l2.Arcs(ids2, t2, o2, c2, n2); // sort & permute Intarray p1 = new Intarray(); Intarray p2 = new Intarray(); NarrayUtil.Quicksort(p1, o1); NarrayUtil.Permute(ids1, p1); NarrayUtil.Permute(t1, p1); NarrayUtil.Permute(o1, p1); NarrayUtil.Permute(c1, p1); NarrayUtil.Quicksort(p2, ids2); NarrayUtil.Permute(ids2, p2); NarrayUtil.Permute(t2, p2); NarrayUtil.Permute(o2, p2); NarrayUtil.Permute(c2, p2); int k1, k2; // l1 epsilon moves for (k1 = 0; k1 < o1.Length() && o1.At1d(k1) == 0; k1++) { ids.Push(ids1.At1d(k1)); targets.Push(Combine(t1.At1d(k1), n2)); outputs.Push(0); costs.Push(c1.At1d(k1)); } // l2 epsilon moves for (k2 = 0; k2 < o2.Length() && ids2.At1d(k2) == 0; k2++) { ids.Push(0); targets.Push(Combine(n1, t2.At1d(k2))); outputs.Push(o2.At1d(k2)); costs.Push(c2.At1d(k2)); } // non-epsilon moves while (k1 < o1.Length() && k2 < ids2.Length()) { while (k1 < o1.Length() && o1.At1d(k1) < ids2.At1d(k2)) { k1++; } if (k1 >= o1.Length()) { break; } while (k2 < ids2.Length() && o1.At1d(k1) > ids2.At1d(k2)) { k2++; } while (k1 < o1.Length() && k2 < ids2.Length() && o1.At1d(k1) == ids2.At1d(k2)) { for (int j = k2; j < ids2.Length() && o1.At1d(k1) == ids2.At1d(j); j++) { ids.Push(ids1.At1d(k1)); targets.Push(Combine(t1.At1d(k1), t2.At1d(j))); outputs.Push(o2.At1d(j)); costs.Push(c1.At1d(k1) + c2.At1d(j)); } k1++; } } }