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));*/ }
public TrainInfo TestDense(IDataset ts) { TrainInfo trInfo = new TrainInfo(); bool first = true; Floatarray v = new Floatarray(); // Send Test Dataset to Lenet for (int i = 0; i < ts.nSamples(); i++) { ts.Input(v, i); if (v.Rank() == 1) { v.Reshape(csize, csize, 0, 0); } StdInput linput = new StdInput(v); if (first) { first = false; BeginTestEpoch(HrLenet, linput.Height, linput.Width, ts.nSamples()); // init test } try { if (C2i.ContainsKey(ts.Cls(i))) { AddSampleToTestOfEpoch(HrLenet, linput.GetDataBuffer(), linput.Length, C2i[ts.Cls(i)]); } } catch (Exception e) { GetStdout(sbout); // get train messages if (sbout.Length > 0) { Global.Debugf("error", sbout.ToString() + "\r\nException in AddSampleToTestOfEpoch"); } throw new Exception("Exception in AddSampleToTestOfEpoch\r\n" + e.Message); } } // do test one epoch try { EndAndRunTestEpoch(HrLenet, ref trInfo); } catch (Exception e) { GetStdout(sbout); // get test messages if (sbout.Length > 0) { Global.Debugf("error", sbout.ToString() + "\r\nException in EndAndRunTestEpoch"); } throw new Exception("Exception in EndAndRunTestEpoch\r\n" + e.Message); } return(trInfo); }
public static void putd1 <T, S>(Floatarray image, Floatarray slice, int index) { if (!(slice.Rank() == 1 && slice.Dim(0) == image.Dim(0))) { throw new Exception("ASSERT: slice.Rank()==1 && slice.Dim(0)==image.Dim(1)"); } for (int i = 0; i < image.Dim(0); i++) { image.UnsafePut(i, index, slice.UnsafeAt(i)); } }
protected override float Outputs(OutputVector result, Floatarray v) { result.Clear(); if (v.Rank() == 1) { v.Reshape(csize, csize, 0, 0); } // byte array input StdInput vinput = new StdInput(v); byte[] buffer = vinput.GetDataBuffer(); // char classifier compute outputs if (CharClass.AsciiTarget) { // net output 0..~ (lower - winner) CharClass.ComputeOutputs(buffer, vinput.Length, vinput.Height, vinput.Width, result); } else { // net output 0..1; (higher - winner) CharClass.ComputeOutputsRaw(buffer, vinput.Length, vinput.Height, vinput.Width, result); } // junk classifier if (PGetb("junk") && !DisableJunk && !JunkClass.IsEmpty) { OutputVector jv = new OutputVector(); if (JunkClass.AsciiTarget) { JunkClass.ComputeOutputs(buffer, vinput.Length, vinput.Height, vinput.Width, jv); result[jc()] = jv.Value(1); } else { //result.Normalize(); result.Normalize2(); JunkClass.ComputeOutputsRaw(buffer, vinput.Length, vinput.Height, vinput.Width, jv); jv.Normalize2(); for (int i = 0; i < result.nKeys(); i++) { result.Values[i] *= jv.Value(0); } result[jc()] = jv.Value(1); } } return(0.0f); }
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));*/ }
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));*/ }
public virtual TrainInfo TrainBatch(IDataset ds, IDataset ts, int epochs) { TrainInfo trInfo = new TrainInfo(); bool first = true; Floatarray v = new Floatarray(); // Send Train Dataset to Lenet for (int i = 0; i < ds.nSamples(); i++) { ds.Input(v, i); if (v.Rank() == 1) { v.Reshape(csize, csize, 0, 0); } StdInput linput = new StdInput(v); try { if (first) { first = false; //StartRedirectStdout(); // start redirect cout to string buffer BeginTrainEpoch(HrLenet, linput.Height, linput.Width, ds.nSamples(), ts.nSamples()); // init train } if (C2i.ContainsKey(ds.Cls(i))) { AddSampleToTrainOfEpoch(HrLenet, linput.GetDataBuffer(), linput.Length, C2i[ds.Cls(i)]); } else { Global.Debugf("error", "class '{0}' is not contained in the char list", (char)ds.Cls(i)); } } catch (Exception e) { GetStdout(sbout); // get train messages if (sbout.Length > 0) { Global.Debugf("error", sbout.ToString() + "\r\nException in AddSampleToTrainOfEpoch"); } throw new Exception("Exception in AddSampleToTrainOfEpoch\r\n" + e.Message); } } // Send Test Dataset to Lenet for (int i = 0; i < ts.nSamples(); i++) { ts.Input(v, i); if (v.Rank() == 1) { v.Reshape(csize, csize, 0, 0); } StdInput linput = new StdInput(v); try { if (C2i.ContainsKey(ts.Cls(i))) { AddSampleToTestOfEpoch(HrLenet, linput.GetDataBuffer(), linput.Length, C2i[ts.Cls(i)]); } } catch (Exception e) { GetStdout(sbout); // get train messages if (sbout.Length > 0) { Global.Debugf("error", sbout.ToString() + "\r\nException in AddSampleToTestOfEpoch"); } throw new Exception("Exception in AddSampleToTestOfEpoch\r\n" + e.Message); } } // debug save mnist //SaveTrainMnist(HrLenet, "debug-images-idx3-ubyte", "debug-labels-idx1-ubyte"); // do train epochs for (int epoch = 0; epoch < epochs; epoch++) { trInfo = new TrainInfo(); try { DateTime startDate = DateTime.Now; EndAndRunTrainEpoch(HrLenet, ref trInfo); // do train one epoch // show train info Global.Debugf("info", String.Format("|{0,7}| Energy:{1:0.#####} Correct:{2:0.00#%} Errors:{3:0.00#%} Count:{4} ", trInfo.age, trInfo.energy, (trInfo.correct / (float)trInfo.size), (trInfo.error / (float)trInfo.size), trInfo.size)); Global.Debugf("info", String.Format(" TEST Energy={0:0.#####} Correct={1:0.00#%} Errors={2:0.00#%} Count={3} ", trInfo.tenergy, (trInfo.tcorrect / (float)trInfo.tsize), (trInfo.terror / (float)trInfo.tsize), trInfo.tsize)); TimeSpan spanTrain = DateTime.Now - startDate; Global.Debugf("info", String.Format(" training time: {0} minutes, {1} seconds", (int)spanTrain.TotalMinutes, spanTrain.Seconds)); // get dll stdout messages GetStdout(sbout); if (sbout.Length > 0) { Console.Write(sbout.ToString()); } } catch (Exception e) { GetStdout(sbout); // get train messages if (sbout.Length > 0) { Global.Debugf("error", sbout.ToString() + "\r\nException in EndAndRunTrainEpoch"); } throw new Exception("Exception in EndAndRunTrainEpoch\r\n" + e.Message); } } return(trInfo); }
protected override float Outputs(OutputVector result, Floatarray v) { result.Clear(); if (v.Rank() == 1) v.Reshape(csize, csize, 0, 0); // byte array input StdInput vinput = new StdInput(v); byte[] buffer = vinput.GetDataBuffer(); // char classifier compute outputs if (CharClass.AsciiTarget) // net output 0..~ (lower - winner) CharClass.ComputeOutputs(buffer, vinput.Length, vinput.Height, vinput.Width, result); else // net output 0..1; (higher - winner) CharClass.ComputeOutputsRaw(buffer, vinput.Length, vinput.Height, vinput.Width, result); // junk classifier if (PGetb("junk") && !DisableJunk && !JunkClass.IsEmpty) { OutputVector jv = new OutputVector(); if (JunkClass.AsciiTarget) { JunkClass.ComputeOutputs(buffer, vinput.Length, vinput.Height, vinput.Width, jv); result[jc()] = jv.Value(1); } else { //result.Normalize(); result.Normalize2(); JunkClass.ComputeOutputsRaw(buffer, vinput.Length, vinput.Height, vinput.Width, jv); jv.Normalize2(); for (int i = 0; i < result.nKeys(); i++) result.Values[i] *= jv.Value(0); result[jc()] = jv.Value(1); } } return 0.0f; }
public virtual TrainInfo TrainBatch(IDataset ds, IDataset ts, int epochs) { TrainInfo trInfo = new TrainInfo(); bool first = true; Floatarray v = new Floatarray(); // Send Train Dataset to Lenet for (int i = 0; i < ds.nSamples(); i++) { ds.Input(v, i); if (v.Rank() == 1) v.Reshape(csize, csize, 0, 0); StdInput linput = new StdInput(v); try { if (first) { first = false; //StartRedirectStdout(); // start redirect cout to string buffer BeginTrainEpoch(HrLenet, linput.Height, linput.Width, ds.nSamples(), ts.nSamples()); // init train } if (C2i.ContainsKey(ds.Cls(i))) AddSampleToTrainOfEpoch(HrLenet, linput.GetDataBuffer(), linput.Length, C2i[ds.Cls(i)]); else Global.Debugf("error", "class '{0}' is not contained in the char list", (char)ds.Cls(i)); } catch (Exception e) { GetStdout(sbout); // get train messages if (sbout.Length > 0) Global.Debugf("error", sbout.ToString() + "\r\nException in AddSampleToTrainOfEpoch"); throw new Exception("Exception in AddSampleToTrainOfEpoch\r\n" + e.Message); } } // Send Test Dataset to Lenet for (int i = 0; i < ts.nSamples(); i++) { ts.Input(v, i); if (v.Rank() == 1) v.Reshape(csize, csize, 0, 0); StdInput linput = new StdInput(v); try { if (C2i.ContainsKey(ts.Cls(i))) AddSampleToTestOfEpoch(HrLenet, linput.GetDataBuffer(), linput.Length, C2i[ts.Cls(i)]); } catch (Exception e) { GetStdout(sbout); // get train messages if (sbout.Length > 0) Global.Debugf("error", sbout.ToString() + "\r\nException in AddSampleToTestOfEpoch"); throw new Exception("Exception in AddSampleToTestOfEpoch\r\n" + e.Message); } } // debug save mnist //SaveTrainMnist(HrLenet, "debug-images-idx3-ubyte", "debug-labels-idx1-ubyte"); // do train epochs for (int epoch = 0; epoch < epochs; epoch++) { trInfo = new TrainInfo(); try { DateTime startDate = DateTime.Now; EndAndRunTrainEpoch(HrLenet, ref trInfo); // do train one epoch // show train info Global.Debugf("info", String.Format("|{0,7}| Energy:{1:0.#####} Correct:{2:0.00#%} Errors:{3:0.00#%} Count:{4} ", trInfo.age, trInfo.energy, (trInfo.correct / (float)trInfo.size), (trInfo.error / (float)trInfo.size), trInfo.size) ); Global.Debugf("info", String.Format(" TEST Energy={0:0.#####} Correct={1:0.00#%} Errors={2:0.00#%} Count={3} ", trInfo.tenergy, (trInfo.tcorrect / (float)trInfo.tsize), (trInfo.terror / (float)trInfo.tsize), trInfo.tsize)); TimeSpan spanTrain = DateTime.Now - startDate; Global.Debugf("info", String.Format(" training time: {0} minutes, {1} seconds", (int)spanTrain.TotalMinutes, spanTrain.Seconds)); // get dll stdout messages GetStdout(sbout); if (sbout.Length > 0) Console.Write(sbout.ToString()); } catch (Exception e) { GetStdout(sbout); // get train messages if (sbout.Length > 0) Global.Debugf("error", sbout.ToString() + "\r\nException in EndAndRunTrainEpoch"); throw new Exception("Exception in EndAndRunTrainEpoch\r\n" + e.Message); } } return trInfo; }
public TrainInfo TestDense(IDataset ts) { TrainInfo trInfo = new TrainInfo(); bool first = true; Floatarray v = new Floatarray(); // Send Test Dataset to Lenet for (int i = 0; i < ts.nSamples(); i++) { ts.Input(v, i); if (v.Rank() == 1) v.Reshape(csize, csize, 0, 0); StdInput linput = new StdInput(v); if (first) { first = false; BeginTestEpoch(HrLenet, linput.Height, linput.Width, ts.nSamples()); // init test } try { if (C2i.ContainsKey(ts.Cls(i))) AddSampleToTestOfEpoch(HrLenet, linput.GetDataBuffer(), linput.Length, C2i[ts.Cls(i)]); } catch (Exception e) { GetStdout(sbout); // get train messages if (sbout.Length > 0) Global.Debugf("error", sbout.ToString() + "\r\nException in AddSampleToTestOfEpoch"); throw new Exception("Exception in AddSampleToTestOfEpoch\r\n" + e.Message); } } // do test one epoch try { EndAndRunTestEpoch(HrLenet, ref trInfo); } catch (Exception e) { GetStdout(sbout); // get test messages if (sbout.Length > 0) Global.Debugf("error", sbout.ToString() + "\r\nException in EndAndRunTestEpoch"); throw new Exception("Exception in EndAndRunTestEpoch\r\n" + e.Message); } return trInfo; }
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));*/ }