Exemple #1
0
        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)));
        }
Exemple #2
0
        public override void TrainDense(IDataset ds)
        {
            int   nclasses = ds.nClasses();
            float miters   = PGetf("miters");
            int   niters   = (int)(ds.nSamples() * miters);

            niters = Math.Max(1000, Math.Min(10000000, niters));
            double     err    = 0.0;
            Floatarray x      = new Floatarray();
            Floatarray z      = new Floatarray();
            Floatarray target = new Floatarray(nclasses);
            int        count  = 0;

            for (int i = 0; i < niters; i++)
            {
                int row = i % ds.nSamples();
                ds.Output(target, row);
                ds.Input1d(x, row);
                TrainOne(z, target, x, PGetf("eta"));
                err += NarrayUtil.Dist2Squared(z, target);
                count++;
            }
            err /= count;
            Global.Debugf("info", "   {4} n {0} niters={1} eta={2:0.#####} errors={3:0.########}",
                          ds.nSamples(), niters, PGetf("eta"), err, FullName);
        }
Exemple #3
0
        /// <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);
        }
Exemple #4
0
        public static void remove_small_components(Intarray segmentation, int w = 5, int h = 4)
        {
            if (NarrayUtil.Max(segmentation) > 100000)
            {
                throw new Exception("remove_small_components: to many segments");
            }
            Narray <Rect> bboxes = new Narray <Rect>();

            ImgLabels.bounding_boxes(ref bboxes, segmentation);
            for (int i = 1; i < bboxes.Length(); i++)
            {
                Rect b = bboxes[i];
                if (b.Width() < w && b.Height() < h)
                {
                    for (int x = b.x0; x < b.x1; x++)
                    {
                        for (int y = b.y0; y < b.y1; y++)
                        {
                            if (segmentation[x, y] == i)
                            {
                                segmentation[x, y] = 0;
                            }
                        }
                    }
                }
            }
        }
Exemple #5
0
        public override void FindBestCuts()
        {
            unchecked
            {
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(cutcosts[i] + 10), 0xff0000);
                }
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(min_thresh + 10), 0x800000);
                }
            }
            Floatarray temp = new Floatarray();

            Gauss.Gauss1d(temp, cutcosts, 3.0f);
            cutcosts.Move(temp);
            SegmRoutine.local_minima(ref bestcuts, cutcosts, min_range, min_thresh);
            for (int i = 0; i < bestcuts.Length(); i++)
            {
                Narray <Point> cut = cuts[bestcuts[i]];
                for (int j = 0; j < cut.Length(); j++)
                {
                    Point p = cut[j];
                    NarrayUtil.ExtPut(dimage, p.X, p.Y, 0x00ff00);
                }
            }
            ///-if(debug.Length > 0) write_image_packed(debug, dimage);
            // dshow1d(cutcosts,"Y");
            //dshow(dimage,"Y");
        }
Exemple #6
0
        public override void SetSegmentationAndGt(Intarray segmentation, Intarray cseg, ref string text)
        {
            // first, set the segmentation as usual
            SetSegmentation(segmentation);

            // Maybe fix up the transcript (remove spaces).
            gttranscript = text;
            string s = text;

            fixup_transcript(ref s, false);
            int  max_cseg  = NarrayUtil.Max(cseg);
            bool old_csegs = (s.Length != max_cseg);

            fixup_transcript(ref gttranscript, old_csegs);

            // Complain if it doesn't match.
            if (gttranscript.Length != max_cseg)
            {
                Logger.Default.Format("transcript = '{0}'\n", gttranscript);
                throw new Exception(String.Format("transcript doesn't agree with cseg (transcript {0}, cseg {1})",
                                                  gttranscript.Length, max_cseg));
            }

            // Now compute the correspondences between the character segmentation
            // and the raw segmentation.
            GrouperRoutine.segmentation_correspondences(gtsegments, segmentation, cseg);
        }
Exemple #7
0
        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);
        }
Exemple #8
0
        public void Get(Intarray r_vertices1,
                        Intarray r_vertices2,
                        Intarray r_inputs,
                        Intarray r_outputs,
                        Floatarray r_costs,
                        int id)
        {
            Intarray   t_v1    = new Intarray();   // vertices
            Intarray   t_v2    = new Intarray();   // vertices
            Intarray   t_i     = new Intarray();   // inputs
            Intarray   t_o     = new Intarray();   // outputs
            Floatarray t_c     = new Floatarray(); // costs
            int        current = id;

            while (current != -1)
            {
                t_v1.Push(v1[current]);
                t_v2.Push(v2[current]);
                t_i.Push(inputs[current]);
                t_o.Push(outputs[current]);
                t_c.Push(costs[current]);
                current = parents[current];
            }

            NarrayUtil.Reverse(r_vertices1, t_v1);
            NarrayUtil.Reverse(r_vertices2, t_v2);
            NarrayUtil.Reverse(r_inputs, t_i);
            NarrayUtil.Reverse(r_outputs, t_o);
            NarrayUtil.Reverse(r_costs, t_c);
        }
Exemple #9
0
        private void ProcessSegmentationMethod(object sender, RoutedEventArgs e)
        {
            string       strSermenterName = (sender as Control).Tag.ToString();
            ISegmentLine segmenter        = ComponentCreator.MakeComponent <ISegmentLine>(strSermenterName);

            if (segmenter == null || currBookLine == null)
            {
                return;
            }

            // приведем к чернобелому
            Bytearray image = currBookLine.ImageBytearray;

            //IBinarize binarizer = new BinarizeByOtsu();
            //binarizer.Binarize(image, image);
            OcrRoutine.binarize_simple(image, image);
            // сегментация
            Intarray charseg = new Intarray();

            segmenter.Charseg(ref charseg, image);
            // фон равен 0
            SegmRoutine.make_line_segmentation_black(charseg);
            // удалим маленькие сегменты
            SegmRoutine.remove_small_components(charseg, 3, 3);
            ImgLabels.renumber_labels(charseg, 1);

            currBookLine.CharsegIntarray = charseg;
            CurrSegmentsCount            = NarrayUtil.Max(charseg);
            // Show segmented image
            ShowCharsegImage(charseg,
                             (currBookLine.HaveTranscript && CurrSegmentsCount == currBookLine.Transcript.Length) ?
                             currBookLine.Transcript : "");
            // to enable save button
            EnableCharsegCmdButtons();
        }
Exemple #10
0
        public static void rseg_to_cseg(Intarray cseg, Intarray rseg, Intarray ids)
        {
            Intarray map = new Intarray(NarrayUtil.Max(rseg) + 1);

            map.Fill(0);
            int color = 0;

            for (int i = 0; i < ids.Length(); i++)
            {
                if (ids[i] == 0)
                {
                    continue;
                }
                color++;
                int start = ids[i] >> 16;
                int end   = ids[i] & 0xFFFF;
                if (start > end)
                {
                    throw new Exception("segmentation encoded in IDs looks seriously broken!");
                }
                if (start >= map.Length() || end >= map.Length())
                {
                    throw new Exception("segmentation encoded in IDs doesn't fit!");
                }
                for (int j = start; j <= end; j++)
                {
                    map[j] = color;
                }
            }
            cseg.MakeLike(rseg);
            for (int i = 0; i < cseg.Length1d(); i++)
            {
                cseg.Put1d(i, map[rseg.At1d(i)]);
            }
        }
Exemple #11
0
        /// <summary>
        /// Merge segments from start to end.
        /// </summary>
        /// <param name="cseg">Output</param>
        /// <param name="rseg">Input</param>
        /// <param name="start">start merge position</param>
        /// <param name="end">end merge position</param>
        public static void rseg_to_cseg(Intarray cseg, Intarray rseg, int start, int end)
        {
            int maxSegNum = NarrayUtil.Max(rseg);

            if (start > end)
            {
                throw new Exception("segmentation encoded in IDs looks seriously broken!");
            }
            if (start > maxSegNum || end > maxSegNum)
            {
                throw new Exception("segmentation encoded in IDs doesn't fit!");
            }
            Intarray map = new Intarray(maxSegNum + 1);

            map.Fill(0);

            int color = 1;

            for (int i = 1; i <= maxSegNum; i++)
            {
                map[i] = color;
                if (i < start || i >= end)
                {
                    color++;
                }
            }
            cseg.MakeLike(rseg);
            for (int i = 0; i < cseg.Length1d(); i++)
            {
                cseg.Put1d(i, map[rseg.At1d(i)]);
            }
        }
Exemple #12
0
        /// <summary>
        /// Return the segmentation-derived mask for the character.
        /// This may optionally be grown by some pixels.
        /// </summary>
        public override void GetMask(out Rect r, ref Bytearray outmask, int index, int grow)
        {
            r = boxes.At1d(index).Grow(grow);
            r.Intersect(new Rect(0, 0, labels.Dim(0), labels.Dim(1)));
            if (fullheight)
            {
                r.y0 = 0;
                r.y1 = labels.Dim(1);
            }
            int      x = r.x0, y = r.y0, w = r.Width(), h = r.Height();
            Intarray segs = segments.At1d(index);

            outmask.Resize(w, h);
            outmask.Fill(0);
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    int label = labels[x + i, y + j];
                    if (NarrayUtil.first_index_of(segs, label) >= 0)
                    {
                        outmask[i, j] = (byte)255;
                    }
                }
            }
            if (grow > 0)
            {
                Morph.binary_dilate_circle(outmask, grow);
            }
        }
Exemple #13
0
        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);
            }
        }
Exemple #14
0
 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");
 }
Exemple #15
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 override void TrainDense(IDataset ds)
        {
            //PSet("%nsamples", ds.nSamples());
            float split      = PGetf("cv_split");
            int   mlp_cv_max = PGeti("cv_max");

            if (crossvalidate)
            {
                // perform a split for cross-validation, making sure
                // that we don't have the same sample in both the
                // test and the training set (even if the data set
                // is the result of resampling)
                Intarray test_ids = new Intarray();
                Intarray ids      = new Intarray();
                for (int i = 0; i < ds.nSamples(); i++)
                {
                    ids.Push(ds.Id(i));
                }
                NarrayUtil.Uniq(ids);
                Global.Debugf("cvdetail", "reduced {0} ids to {1} ids", ds.nSamples(), ids.Length());
                NarrayUtil.Shuffle(ids);
                int nids = (int)((1.0 - split) * ids.Length());
                nids = Math.Min(nids, mlp_cv_max);
                for (int i = 0; i < nids; i++)
                {
                    test_ids.Push(ids[i]);
                }
                NarrayUtil.Quicksort(test_ids);
                Intarray training = new Intarray();
                Intarray testing  = new Intarray();
                for (int i = 0; i < ds.nSamples(); i++)
                {
                    int id = ds.Id(i);
                    if (ClassifierUtil.Bincontains(test_ids, id))
                    {
                        testing.Push(i);
                    }
                    else
                    {
                        training.Push(i);
                    }
                }
                Global.Debugf("cvdetail", "#training {0} #testing {1}",
                              training.Length(), testing.Length());
                PSet("%ntraining", training.Length());
                PSet("%ntesting", testing.Length());
                Datasubset trs = new Datasubset(ds, training);
                Datasubset tss = new Datasubset(ds, testing);
                TrainBatch(trs, tss);
            }
            else
            {
                TrainBatch(ds, ds);
            }
        }
Exemple #17
0
        public static int binarize_simple(Bytearray result, Bytearray image)
        {
            int threshold = (NarrayUtil.Max(image) /* + NarrayUtil.Min(image)*/) / 2;

            result.MakeLike(image);
            for (int i = 0; i < image.Length1d(); i++)
            {
                result.Put1d(i, image.At1d(i) < threshold ? (byte)0 : (byte)255);
            }
            return(threshold);
        }
Exemple #18
0
        public static void combine_segmentations(ref Intarray dst, Intarray src)
        {
            dst.SameDims(src);
            int n = NarrayUtil.Max(dst) + 1;

            for (int i = 0; i < dst.Length1d(); i++)
            {
                dst.Put1d(i, (dst.At1d(i) + src.At1d(i) * n));
            }
            ImgLabels.renumber_labels(dst, 1);
        }
Exemple #19
0
        /// <summary>
        /// Изменился текущий BookLine
        /// </summary>
        private void BookLine_CurrentChanged(object sender, EventArgs e)
        {
            if (listingDataView.View.CurrentItem == null)
            {
                return;
            }

            // сбросим кэш предыдущего елемента
            if (currBookLine != null)
            {
                currBookLine.Image           = null;
                currBookLine.ImageBytearray  = null;
                currBookLine.CharsegIntarray = null;
            }
            // текущий елемент
            BookLine bline = listingDataView.View.CurrentItem as BookLine;

            currBookLine = bline;
            // сбросим некоторые признаки
            isCurrDeletedSegment = false;

            // показать Charseg
            if (bline != null && bline.HaveCharseg)
            {
                // загрузим с файла
                Intarray charseg = bline.CharsegIntarray;
                // занесем в кэш
                bline.CharsegIntarray = charseg;
                // номер максимального сегмента
                CurrSegmentsCount = NarrayUtil.Max(charseg);
                // отобразим картинку
                ShowCharsegImage(charseg, bline.Transcript);
                // активируем кнопки действий
                EnableCharsegCmdButtons();
            }
            else
            {
                imgCharSeg.Source = null;
                DisableCharsegCmdButtons();
            }

            // показать Transcript
            if (bline != null && bline.HaveTranscript)
            {
                ShowTranscript(bline.Transcript);
            }
            else
            {
                ShowTranscript("");
            }
            // сбросим к началу номер начальног осегмента
            numUpDnStart.Value = 1;
            numUpDnEnd.Value   = 2;
        }
Exemple #20
0
        private void DoTestRecognize(LenetClassifier classifier)
        {
            OutputVector ov = new OutputVector();
            Floatarray   v  = new Floatarray();
            Bytearray    ba = new Bytearray(1, 1);

            ImgIo.read_image_gray(ba, testPngFileName);
            NarrayUtil.Sub(255, ba);
            v.Copy(ba);
            v /= 255.0;
            classifier.XOutputs(ov, v);
            Console.WriteLine("Featured output class '{0}', score '{1}'", (char)ov.Key(ov.BestIndex), ov.Value(ov.BestIndex));
        }
Exemple #21
0
        public static void binary_or(Bytearray image, Bytearray image2, int dx, int dy)
        {
            int w = image.Dim(0);
            int h = image.Dim(1);

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    image[i, j] = Math.Max(image[i, j], NarrayUtil.Ext(image2, i - dx, j - dy));
                }
            }
        }
Exemple #22
0
        public static void skeletal_features(Bytearray endpoints, Bytearray junctions,
                                             Bytearray image, float presmooth, float skelsmooth)
        {
            Bytearray temp = new Bytearray();

            temp.Copy(image);
            NarrayUtil.Greater(temp, (byte)128, (byte)0, (byte)255);
            if (presmooth > 0f)
            {
                Gauss.Gauss2d(temp, presmooth, presmooth);
                NarrayUtil.Greater(temp, (byte)128, (byte)0, (byte)255);
            }
        }
Exemple #23
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));
        }
Exemple #24
0
        /// <summary>
        /// Get spacing to the next component.
        /// </summary>
        public override int PixelSpace(int i)
        {
            int end = NarrayUtil.Max(segments.At1d(i));

            if (end >= rboxes.Length() - 1)
            {
                return(-1);
            }
            int x0 = rboxes.At1d(end).x1;
            int x1 = rboxes.At1d(end + 1).x0;

            return(x1 - x0);
        }
Exemple #25
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);
        }
Exemple #26
0
        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);
        }
Exemple #27
0
 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);
 }
Exemple #28
0
        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));
        }
Exemple #29
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);
            }
        }
Exemple #30
0
        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));
        }