/// <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 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); }
protected void Recompute() { nc = NarrayUtil.Max(classes) + 1; nf = data[0].Dim(0); CHECK_ARG(DataMin(data) >= 0 && DataMax(data) < 256, "min(data) >= 0 && max(data) < 256"); CHECK_ARG(NarrayUtil.Min(classes) >= -1 && NarrayUtil.Max(classes) < 10000, "min(classes)>=-1 && max(classes)<10000"); CHECK_ARG(nc > 0, "nc > 0"); CHECK_ARG(nf > 0, "nf > 0"); }
protected void Recompute() { nc = NarrayUtil.Max(classes) + 1; nf = data.Dim(1); CHECK_ARG(Min(data) > -100 && Max(data) < 100, "min(data) > -100 && max(data) < 100"); CHECK_ARG(NarrayUtil.Min(classes) >= -1 && NarrayUtil.Max(classes) < 10000, "min(classes)>=-1 && max(classes)<10000"); CHECK_ARG(nc > 0, "nc > 0"); CHECK_ARG(nf > 0, "nf > 0"); }
public static void binsmooth(Bytearray binary, Floatarray input, float sigma) { Floatarray smoothed = new Floatarray(); smoothed.Copy(input); smoothed -= NarrayUtil.Min(smoothed); smoothed /= NarrayUtil.Max(smoothed); if (sigma > 0) { Gauss.Gauss2d(smoothed, sigma, sigma); } binarize_with_threshold(binary, smoothed, 0.5f); }
public static Bitmap read_image_binary(Bytearray image, string path) { Bitmap bitmap = LoadBitmapFromFile(path); image.Resize(bitmap.Width, bitmap.Height); ImgRoutine.NarrayFromBitmap(image, bitmap); double threshold = (NarrayUtil.Min(image) + NarrayUtil.Max(image)) / 2.0; for (int i = 0; i < image.Length1d(); i++) { image.Put1d(i, (byte)((image.At1d(i) < threshold) ? 0 : 255)); } return(bitmap); }
public override void Add(Floatarray v, int c) { CHECK_ARG(NarrayUtil.Min(v) > -1.2f && NarrayUtil.Max(v) < 1.2f, "float8: value out of range (-1.2..1.2)"); CHECK_ARG(c >= -1, "c>=-1"); if (c >= nc) { nc = c + 1; } if (nf < 0) { nf = v.Length(); } RowPush(data, v); classes.Push(c); }
public override void Info() { bool bak = Logger.Default.verbose; Logger.Default.verbose = true; Logger.Default.WriteLine("MLP"); PPrint(); Logger.Default.WriteLine(String.Format("nInput {0} nHidden {1} nOutput {2}", w1.Dim(1), w1.Dim(0), w2.Dim(0))); if (w1.Length() > 0 && w2.Length() > 0) { Logger.Default.WriteLine(String.Format("w1 [{0},{1}] b1 [{2},{3}]", NarrayUtil.Min(w1), NarrayUtil.Max(w1), NarrayUtil.Min(b1), NarrayUtil.Max(b1))); Logger.Default.WriteLine(String.Format("w2 [{0},{1}] b2 [{2},{3}]", NarrayUtil.Min(w2), NarrayUtil.Max(w2), NarrayUtil.Min(b2), NarrayUtil.Max(b2))); } Logger.Default.verbose = bak; }
public Dataset8(Narray <sbyte> data, Intarray classes) : this() { data.Copy(data); classes.Copy(classes); if (classes.Length() > 0) { nc = NarrayUtil.Max(classes) + 1; nf = data.Dim(1); //CHECK_ARG(NarrayUtil.Min(data) > -100 && NarrayUtil.Max(data) < 100, "min(data)>-100 && max(data)<100"); CHECK_ARG(NarrayUtil.Min(classes) >= -1 && NarrayUtil.Max(classes) < 10000, "min(classes)>=-1 && max(classes)<10000"); } else { nc = 0; nf = -1; } }
public override void Add(Floatarray v, int c) { CHECK_ARG(NarrayUtil.Min(v) >= 0.0f && NarrayUtil.Max(v) <= 1.0f, "float8: value out of range (0..1)"); CHECK_ARG(c >= -1, "c>=-1"); if (c >= nc) { nc = c + 1; } if (nf < 0) { nf = v.Length(); } Narray <byte> newDataItem = data.Push(new Narray <byte>()); Copy(newDataItem, v); classes.Push(c); CHECK_ARG(nc > 0, "nc>0"); CHECK_ARG(nf > 0, "nf>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); } } }
public static void erase_small_components(Floatarray input, float mins = 0.2f, float thresh = 0.25f) { // compute a thresholded image for component labeling float threshold = thresh * NarrayUtil.Max(input); Intarray components = new Intarray(); components.MakeLike(input); components.Fill(0); for (int i = 0; i < components.Length(); i++) { components[i] = (input[i] > threshold ? 1 : 0); } // compute the number of pixels in each component 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[components[i]] + 1; } totals[0] = 0; int biggest = NarrayUtil.ArgMax(totals); // erase small components float minsize = mins * totals[biggest]; Bytearray keep = new Bytearray(n + 1); float background = NarrayUtil.Min(input); for (int i = 0; i < keep.Length(); i++) { keep[i] = (byte)(totals[i] > minsize ? 1 : 0); } for (int i = 0; i < input.Length(); i++) { if (keep[components[i]] == 0) { input[i] = background; } } }
/// <summary> /// Output the segmentation into a segmentation graph. /// Construct a state for each of the segments, then /// add transitions between states (segments) /// from min(segments[i]) to max(segments[i])+1. /// </summary> public override void GetLattice(IGenericFst fst) { fst.Clear(); int final = NarrayUtil.Max(labels) + 1; Intarray states = new Intarray(final + 1); states.Fill(-1); for (int i = 1; i < states.Length(); i++) { states[i] = fst.NewState(); } fst.SetStart(states[1]); fst.SetAccept(states[final]); for (int i = 0; i < boxes.Length(); i++) { int start = NarrayUtil.Min(segments.At1d(i)); int end = NarrayUtil.Max(segments.At1d(i)); int id = (start << 16) + end; if (segments.At1d(i).Length() == 0) { id = 0; } float yes = spaces[i, 0]; float no = spaces[i, 1]; // if no space is set, assume no space is present if (yes == float.PositiveInfinity && no == float.PositiveInfinity) { no = 0.0f; } for (int j = 0; j < class_costs[i].Length(); j++) { float cost = class_costs[i][j]; string str = class_outputs[i][j]; int n = str.Length; int last = start; for (int k = 0; k < n; k++) { int c = (int)str[k]; if (k < n - 1) { // add intermediate states/transitions for all but the last character states.Push(fst.NewState()); fst.AddTransition(states[last], states.Last(), c, 0.0f, 0); last = states.Length() - 1; } else { // for the last character, handle the spaces as well if (no < 1000.0f) { // add the last character as a direct transition with no space fst.AddTransition(states[last], states[end + 1], c, cost + no, id); } if (yes < 1000.0f) { // insert another state to handle spaces states.Push(fst.NewState()); int space_state = states.Last(); fst.AddTransition(states[start], space_state, c, cost, id); fst.AddTransition(space_state, states[end + 1], (int)' ', yes, 0); } } } // for k } // for j } // for i }
/// <summary> /// Return the starting segment /// </summary> public override int Start(int index) { return(NarrayUtil.Min(segments[index])); }
public static bool background_seems_white(Bytearray a) { return(average_on_border(a) >= (NarrayUtil.Min(a) + NarrayUtil.Max(a) / 2)); }
public float Min() { return(NarrayUtil.Min(_values)); }