public void InitData(IDataset ds, int nhidden, Intarray newc2i = null, Intarray newi2c = null) { CHECK_ARG(nhidden > 1 && nhidden < 1000000, "nhidden > 1 && nhidden < 1000000"); int ninput = ds.nFeatures(); int noutput = ds.nClasses(); w1.Resize(nhidden, ninput); b1.Resize(nhidden); w2.Resize(noutput, nhidden); b2.Resize(noutput); Intarray indexes = new Intarray(); NarrayUtil.RPermutation(indexes, ds.nSamples()); Floatarray v = new Floatarray(); for (int i = 0; i < w1.Dim(0); i++) { int row = indexes[i]; ds.Input1d(v, row); float normv = (float)NarrayUtil.Norm2(v); v /= normv * normv; NarrayRowUtil.RowPut(w1, i, v); } ClassifierUtil.fill_random(b1, -1e-6f, 1e-6f); ClassifierUtil.fill_random(w2, -1.0f / nhidden, 1.0f / nhidden); ClassifierUtil.fill_random(b2, -1e-6f, 1e-6f); if (newc2i != null) { c2i.Copy(newc2i); } if (newi2c != null) { i2c.Copy(newi2c); } }
public void Init(params string[] books) { bool retrain = PGetb("retrain"); bool randomize = PGetb("randomize"); string cbookstore = PGet("cbookstore"); bookstores.Clear(); all_lines.Clear(); cseg_variant = "cseg.gt"; text_variant = "gt"; if (retrain) { cseg_variant = "cseg"; text_variant = ""; } int nbooks = books.Length; bookstores.Resize(nbooks); int totalNumberOfPages = 0; for (int i = 0; i < nbooks; i++) { bookstores[i] = ComponentCreator.MakeComponent <IBookStore>(cbookstore); bookstores[i].SetPrefix(books[i].Trim()); Global.Debugf("info", "{0}: {1} pages", books[i], bookstores[i].NumberOfPages()); totalNumberOfPages += bookstores[i].NumberOfPages(); } //CHECK_ARG(totalNumberOfPages > 0, "totalNumberOfPages > 0"); // compute a list of all lines Intarray triple = new Intarray(3); for (int i = 0; i < nbooks; i++) { for (int j = 0; j < bookstores[i].NumberOfPages(); j++) { for (int k = 0; k < bookstores[i].LinesOnPage(j); k++) { triple[0] = i; triple[1] = j; triple[2] = k; NarrayRowUtil.RowPush(all_lines, triple); } } } Global.Debugf("info", "got {0} lines", all_lines.Dim(0)); // randomly permute it so that we train in random order if (randomize) { Shuffle(); } index = 0; }
/// <summary> /// Randomly permute it so that we train in random order /// </summary> public void Shuffle() { bool randomize = PGetb("randomize"); if (randomize) { Intarray permutation = new Intarray(all_lines.Dim(0)); for (int i = 0; i < all_lines.Dim(0); i++) { permutation[i] = i; } NarrayUtil.RandomlyPermute(permutation); NarrayRowUtil.RowPermute(all_lines, permutation); } }
public static void segmentation_correspondences(Narray <Intarray> outsegments, Intarray seg, Intarray cseg) { if (NarrayUtil.Max(seg) >= 10000) { throw new Exception("CHECK_ARG: (max(seg)<10000)"); } if (NarrayUtil.Max(cseg) >= 10000) { throw new Exception("CHECK_ARG: (max(cseg)<10000)"); } int nseg = NarrayUtil.Max(seg) + 1; int ncseg = NarrayUtil.Max(cseg) + 1; Intarray overlaps = new Intarray(nseg, ncseg); overlaps.Fill(0); if (seg.Length() != cseg.Length()) { throw new Exception("CHECK_ARG: (seg.Length()==cseg.Length())"); } for (int i = 0; i < seg.Length(); i++) { overlaps[seg.At1d(i), cseg.At1d(i)]++; } outsegments.Clear(); outsegments.Resize(ncseg); for (int i = 0; i < nseg; i++) { int j = NarrayRowUtil.RowArgMax(overlaps, i); if (!(j >= 0 && j < ncseg)) { throw new Exception("ASSERT: (j>=0 && j<ncseg)"); } if (outsegments[j] == null) { outsegments[j] = new Intarray(); } outsegments[j].Push(i); } }
/// <summary> /// Put image to last book and last page. /// Return new line number. /// </summary> public int PutNewImage(Bitmap bitmap) { int ibook = bookstores.Length() - 1; bool emptyBook = bookstores[ibook].NumberOfPages() <= 0; int ipage = emptyBook ? 0 : bookstores[ibook].NumberOfPages() - 1; int iline = emptyBook ? 0 : bookstores[ibook].LinesOnPage(ipage); // next line pos int lineid = iline == 0 ? 0 : bookstores[ibook].GetLineId(ipage, iline - 1) + 1; // next line id bookstores[ibook].AddLineId(ipage, lineid); // reserve position for line id bookstores[ibook].PutLine(bitmap, ipage, lineid); // put image to reserved position // update collection Intarray triple = new Intarray(3); triple[0] = ibook; triple[1] = ipage; triple[2] = iline; NarrayRowUtil.RowPush(all_lines, triple); // set current position bookno = ibook; pageno = ipage; lineno = lineid; return(lineid); }